Skip to content

Tests: Run PHPStan Static Analysis #99

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 36 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
9f60dc7
Add `referrer` parameter to `add_subscriber_to_form` and `add_subscri…
n7studios Dec 4, 2024
dcb923e
Improve tests
n7studios Dec 4, 2024
a28254a
Don’t remove subscribers after test
n7studios Dec 4, 2024
a1de775
Debug test
n7studios Dec 4, 2024
26ea154
Remove test debugging
n7studios Dec 4, 2024
f904ac7
Debug tests, again
n7studios Dec 4, 2024
43b14ba
Delay tests
n7studios Dec 4, 2024
85b4988
Debug tests
n7studios Dec 4, 2024
eb494bd
Fix tests
n7studios Dec 4, 2024
0493a54
Add helper method to assert referrer set
n7studios Dec 4, 2024
f0ff1c6
Debug tests
n7studios Dec 4, 2024
1afc900
Debug tests
n7studios Dec 4, 2024
9653080
Revert debugging
n7studios Dec 4, 2024
513e73c
Add a pause between each test to avoid a 429 rate limit
n7studios Dec 4, 2024
5dd3d01
Check result of form subscribe instead of separate API query in tests
n7studios Dec 4, 2024
687d57d
Add `add_subscribers_to_forms` bulk method
n7studios Dec 4, 2024
66c8d5f
Started tests
n7studios Dec 4, 2024
aa90288
Update method and tests
n7studios Dec 4, 2024
5d5715a
Remove code
n7studios Dec 4, 2024
4057fbc
Run PHPStan static analysis on tests
n7studios Dec 4, 2024
e9a0e15
Bump `write-file-action` to latest version in GitHub actions
n7studios Dec 4, 2024
637692e
Coding standards
n7studios Dec 4, 2024
3a2c694
Merge remote-tracking branch 'origin/tests-run-phpstan' into add-form…
n7studios Dec 4, 2024
98895dc
Coding standards
n7studios Dec 4, 2024
710802c
Bump to 2.1.0 for next release
n7studios Dec 4, 2024
16252a8
Merge branch 'tests-run-phpstan' into add-bulk-add-subscribers-to-forms
n7studios Dec 4, 2024
627ccd0
Fix incorrect times in tests
n7studios Dec 4, 2024
907907b
Merge remote-tracking branch 'origin/tests-run-phpstan' into add-form…
n7studios Dec 4, 2024
59a31d8
Merge remote-tracking branch 'origin/tests-run-phpstan' into add-bulk…
n7studios Dec 4, 2024
e513ef8
PHPStan compat.
n7studios Dec 5, 2024
3133f41
Use PHPStan 2.0
n7studios Dec 5, 2024
ee65d37
Run tests at level 10
n7studios Dec 5, 2024
b629894
Add CONVERTKIT_API_FORM_ID_2 to .env file
n7studios Dec 5, 2024
d19ec8d
Merge pull request #100 from Kit/tests-use-phpstan-2.0
n7studios Dec 6, 2024
38cb077
Merge pull request #97 from Kit/add-form-referrer-support
n7studios Dec 6, 2024
e59f342
Merge pull request #98 from Kit/add-bulk-add-subscribers-to-forms
n7studios Dec 6, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.dist.testing
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
CONVERTKIT_API_BROADCAST_ID="8697158"
CONVERTKIT_API_FORM_ID="2765139"
CONVERTKIT_API_FORM_ID_2="2780977"
CONVERTKIT_API_LEGACY_FORM_URL="https://app.convertkit.com/landing_pages/470099"
CONVERTKIT_API_LANDING_PAGE_URL="https://cheerful-architect-3237.ck.page/cc5eb21744"
CONVERTKIT_API_LEGACY_LANDING_PAGE_URL="https://app.convertkit.com/landing_pages/470103"
Expand Down
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ CONVERTKIT_OAUTH_CLIENT_SECRET=
CONVERTKIT_OAUTH_REDIRECT_URI="https://convertkit-github.local/wp-admin/options-general.php?page=_wp_convertkit_settings"
CONVERTKIT_API_BROADCAST_ID="8697158"
CONVERTKIT_API_FORM_ID="2765139"
CONVERTKIT_API_FORM_ID_2="2780977"
CONVERTKIT_API_LEGACY_FORM_URL="https://app.convertkit.com/landing_pages/470099"
CONVERTKIT_API_LANDING_PAGE_URL="https://cheerful-architect-3237.ck.page/cc5eb21744"
CONVERTKIT_API_LEGACY_LANDING_PAGE_URL="https://app.convertkit.com/landing_pages/470103"
Expand Down
7 changes: 6 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
# Make sure your committed .env.dist.testing file ends with a newline.
# The formatting of the contents to include a blank newline is deliberate.
- name: Define GitHub Secrets in .env.dist.testing
uses: DamianReeves/write-file-action@v1.1
uses: DamianReeves/write-file-action@v1.3
with:
path: .env.dist.testing
contents: |
Expand All @@ -68,6 +68,11 @@ jobs:
- name: Build PHP Autoloader
run: composer dump-autoload

# Run PHPStan for static analysis.
- name: Run PHPStan Static Analysis
working-directory: ${{ env.PLUGIN_DIR }}
run: php vendor/bin/phpstan analyse --memory-limit=1250M

# Run Coding Standards.
- name: Run Coding Standards
run: php vendor/bin/phpcs --standard=phpcs.xml
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"vlucas/phpdotenv": "^5.5",
"phpunit/phpunit": "^5.7 || ^9.0",
"squizlabs/php_codesniffer": "^3.3",
"phpstan/phpstan": "^1.2"
"phpstan/phpstan": "^2.0"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ parameters:

# Should not need to edit anything below here
# Rule Level: https://phpstan.org/user-guide/rule-levels
level: 8
level: 10
7 changes: 4 additions & 3 deletions src/ConvertKit_API.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ private function create_log(string $message)
// Mask email addresses that may be contained within the message.
$message = preg_replace_callback(
'^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})^',
// @phpstan-ignore-next-line - see https://github.com/phpstan/phpstan/issues/10396
function ($matches) {
return preg_replace('/\B[^@.]/', '*', $matches[0]);
},
Expand Down Expand Up @@ -176,7 +177,7 @@ public function get_oauth_url(string $redirectURI)
* @param string $authCode Authorization Code, returned from get_oauth_url() flow.
* @param string $redirectURI Redirect URI.
*
* @return array<string, int|string> API response
* @return mixed|array<string, int|string> API response
*/
public function get_access_token(string $authCode, string $redirectURI)
{
Expand Down Expand Up @@ -214,7 +215,7 @@ public function get_access_token(string $authCode, string $redirectURI)
* @param string $refreshToken Refresh Token.
* @param string $redirectURI Redirect URI.
*
* @return array<string, int|string> API response
* @return mixed|array<string, int|string> API response
*/
public function refresh_token(string $refreshToken, string $redirectURI)
{
Expand Down Expand Up @@ -337,7 +338,7 @@ public function get_resource(string $url)
*
* @throws \Exception If JSON encoding arguments failed.
*
* @return false|mixed
* @return mixed|object
*/
public function request(string $endpoint, string $method, array $args = [])
{
Expand Down
114 changes: 89 additions & 25 deletions src/ConvertKit_API_Traits.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public function get_growth_stats(\DateTime $starting = null, \DateTime $ending =
*
* @see https://developers.convertkit.com/v4.html#convertkit-api-forms
*
* @return false|array<int,\stdClass>
* @return mixed|array<int,\stdClass>
*/
public function get_forms(
string $status = 'active',
Expand Down Expand Up @@ -198,7 +198,7 @@ public function get_forms(
*
* @see https://developers.convertkit.com/v4.html#convertkit-api-forms
*
* @return false|array<int,\stdClass>
* @return mixed|array<int,\stdClass>
*/
public function get_landing_pages(
string $status = 'active',
Expand All @@ -222,21 +222,57 @@ public function get_landing_pages(
);
}

/**
* Adds subscribers to forms in bulk.
*
* @param array<array<string,string>> $forms_subscribers_ids Array of arrays comprising of `form_id`, `subscriber_id` and optional `referrer` URL.
* @param string $callback_url URL to notify for large batch size when async processing complete.
*
* @since 2.1.0
*
* @see https://developers.kit.com/v4.html#bulk-add-subscribers-to-forms
*
* @return false|object
*/
public function add_subscribers_to_forms(array $forms_subscribers_ids, string $callback_url = '')
{
// Build parameters.
$options = ['additions' => $forms_subscribers_ids];
if (!empty($callback_url)) {
$options['callback_url'] = $callback_url;
}

// Send request.
return $this->post(
'bulk/forms/subscribers',
$options
);
}

/**
* Adds a subscriber to a form by email address
*
* @param integer $form_id Form ID.
* @param string $email_address Email Address.
* @param string $referrer Referrer.
*
* @see https://developers.convertkit.com/v4.html#add-subscriber-to-form-by-email-address
*
* @return false|mixed
*/
public function add_subscriber_to_form_by_email(int $form_id, string $email_address)
public function add_subscriber_to_form_by_email(int $form_id, string $email_address, string $referrer = '')
{
// Build parameters.
$options = ['email_address' => $email_address];

if (!empty($referrer)) {
$options['referrer'] = $referrer;
}

// Send request.
return $this->post(
sprintf('forms/%s/subscribers', $form_id),
['email_address' => $email_address]
$options
);
}

Expand All @@ -245,16 +281,28 @@ public function add_subscriber_to_form_by_email(int $form_id, string $email_addr
*
* @param integer $form_id Form ID.
* @param integer $subscriber_id Subscriber ID.
* @param string $referrer Referrer URL.
*
* @see https://developers.convertkit.com/v4.html#add-subscriber-to-form
*
* @since 2.0.0
*
* @return false|mixed
*/
public function add_subscriber_to_form(int $form_id, int $subscriber_id)
public function add_subscriber_to_form(int $form_id, int $subscriber_id, string $referrer = '')
{
return $this->post(sprintf('forms/%s/subscribers/%s', $form_id, $subscriber_id));
// Build parameters.
$options = [];

if (!empty($referrer)) {
$options['referrer'] = $referrer;
}

// Send request.
return $this->post(
sprintf('forms/%s/subscribers/%s', $form_id, $subscriber_id),
$options
);
}

/**
Expand Down Expand Up @@ -456,7 +504,7 @@ public function get_sequence_subscriptions(
*
* @see https://developers.convertkit.com/v4.html#list-tags
*
* @return false|array<int,\stdClass>
* @return mixed|array<int,\stdClass>
*/
public function get_tags(
bool $include_total_count = false,
Expand Down Expand Up @@ -863,10 +911,26 @@ public function get_subscriber_id(string $email_address)
['email_address' => $email_address]
);

if (!$subscribers instanceof \stdClass) {
return false;
}

if (!is_array($subscribers->subscribers)) {
return false;
}

if (!count($subscribers->subscribers)) {
return false;
}

if (!$subscribers->subscribers[0] instanceof \stdClass) {
return false;
}

if (!is_int($subscribers->subscribers[0]->id)) {
return false;
}

// Return the subscriber's ID.
return $subscribers->subscribers[0]->id;
}
Expand All @@ -878,7 +942,7 @@ public function get_subscriber_id(string $email_address)
*
* @see https://developers.convertkit.com/v4.html#get-a-subscriber
*
* @return false|integer
* @return mixed|integer
*/
public function get_subscriber(int $subscriber_id)
{
Expand All @@ -895,7 +959,7 @@ public function get_subscriber(int $subscriber_id)
*
* @see https://developers.convertkit.com/v4.html#update-a-subscriber
*
* @return false|mixed
* @return mixed
*/
public function update_subscriber(
int $subscriber_id,
Expand Down Expand Up @@ -930,7 +994,7 @@ public function update_subscriber(
*
* @see https://developers.convertkit.com/v4.html#unsubscribe-subscriber
*
* @return false|object
* @return mixed|object
*/
public function unsubscribe_by_email(string $email_address)
{
Expand All @@ -949,7 +1013,7 @@ public function unsubscribe_by_email(string $email_address)
*
* @see https://developers.convertkit.com/v4.html#unsubscribe-subscriber
*
* @return false|object
* @return mixed|object
*/
public function unsubscribe(int $subscriber_id)
{
Expand All @@ -967,7 +1031,7 @@ public function unsubscribe(int $subscriber_id)
*
* @see https://developers.convertkit.com/v4.html#list-tags-for-a-subscriber
*
* @return false|array<int,\stdClass>
* @return mixed|array<int,\stdClass>
*/
public function get_subscriber_tags(
int $subscriber_id,
Expand Down Expand Up @@ -1044,7 +1108,7 @@ public function get_broadcasts(
*
* @see https://developers.convertkit.com/v4.html#create-a-broadcast
*
* @return false|object
* @return mixed|object
*/
public function create_broadcast(
string $subject = '',
Expand Down Expand Up @@ -1103,7 +1167,7 @@ public function create_broadcast(
*
* @see https://developers.convertkit.com/v4.html#get-a-broadcast
*
* @return false|object
* @return mixed|object
*/
public function get_broadcast(int $id)
{
Expand All @@ -1118,7 +1182,7 @@ public function get_broadcast(int $id)
*
* @see https://developers.convertkit.com/v4.html#get-stats
*
* @return false|object
* @return mixed|object
*/
public function get_broadcast_stats(int $id)
{
Expand Down Expand Up @@ -1151,7 +1215,7 @@ public function get_broadcast_stats(int $id)
*
* @see https://developers.convertkit.com/#create-a-broadcast
*
* @return false|object
* @return mixed|object
*/
public function update_broadcast(
int $id,
Expand Down Expand Up @@ -1213,7 +1277,7 @@ public function update_broadcast(
*
* @see https://developers.convertkit.com/v4.html#delete-a-broadcast
*
* @return false|object
* @return mixed|object
*/
public function delete_broadcast(int $id)
{
Expand Down Expand Up @@ -1266,7 +1330,7 @@ public function get_webhooks(
*
* @throws \InvalidArgumentException If the event is not supported.
*
* @return false|object
* @return mixed|object
*/
public function create_webhook(string $url, string $event, string $parameter = '')
{
Expand Down Expand Up @@ -1340,7 +1404,7 @@ public function create_webhook(string $url, string $event, string $parameter = '
*
* @see https://developers.convertkit.com/v4.html#delete-a-webhook
*
* @return false|object
* @return mixed|object
*/
public function delete_webhook(int $id)
{
Expand Down Expand Up @@ -1389,7 +1453,7 @@ public function get_custom_fields(
*
* @see https://developers.convertkit.com/v4.html#create-a-custom-field
*
* @return false|object
* @return mixed|object
*/
public function create_custom_field(string $label)
{
Expand All @@ -1409,7 +1473,7 @@ public function create_custom_field(string $label)
*
* @see https://developers.convertkit.com/v4.html#bulk-create-custom-fields
*
* @return false|object
* @return mixed|object
*/
public function create_custom_fields(array $labels, string $callback_url = '')
{
Expand Down Expand Up @@ -1444,7 +1508,7 @@ public function create_custom_fields(array $labels, string $callback_url = '')
*
* @see https://developers.convertkit.com/v4.html#update-a-custom-field
*
* @return false|object
* @return mixed|object
*/
public function update_custom_field(int $id, string $label)
{
Expand All @@ -1463,7 +1527,7 @@ public function update_custom_field(int $id, string $label)
*
* @see https://developers.convertkit.com/#destroy-field
*
* @return false|object
* @return mixed|object
*/
public function delete_custom_field(int $id)
{
Expand Down Expand Up @@ -1510,7 +1574,7 @@ public function get_purchases(
*
* @see https://developers.convertkit.com/v4.html#get-a-purchase
*
* @return false|object
* @return mixed|object
*/
public function get_purchase(int $purchase_id)
{
Expand All @@ -1535,7 +1599,7 @@ public function get_purchase(int $purchase_id)
*
* @see https://developers.convertkit.com/v4.html#create-a-purchase
*
* @return false|object
* @return mixed|object
*/
public function create_purchase(
string $email_address,
Expand Down
Loading