Skip to content

Commit c971157

Browse files
authored
Merge pull request #35 from ConvertKit/fix-sort-order-tests
Fix sort order tests
2 parents 78262b4 + 2619b1b commit c971157

File tree

3 files changed

+125
-58
lines changed

3 files changed

+125
-58
lines changed

TESTING.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,26 @@ If a test fails, you can inspect the output.
3636

3737
Any errors should be corrected by making applicable code or test changes.
3838

39+
## Run PHP CodeSniffer
40+
41+
[PHP_CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer) checks that all code meets the [PSR-12 Coding Standards](https://www.php-fig.org/psr/psr-12/).
42+
43+
To run CodeSniffer on tests, enter the following command:
44+
45+
```bash
46+
vendor/bin/phpcs
47+
```
48+
49+
Any errors should be corrected by either:
50+
- making applicable code changes
51+
- (Experimental) running `vendor/bin/phpcbf` to automatically fix coding standards
52+
53+
Need to change the coding standard rules applied? Either:
54+
- ignore a rule in the affected code, by adding `phpcs:ignore {rule}`, where {rule} is the given rule that failed in the above output.
55+
- edit the [phpcs.tests.xml](phpcs.xml) file.
56+
57+
**Rules can be ignored with caution**, but it's essential that rules relating to coding style and inline code commenting / docblocks remain.
58+
3959
## Run PHP CodeSniffer for Tests
4060

4161
[PHP_CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer) checks that all test code meets the [PSR-12 Coding Standards](https://www.php-fig.org/psr/psr-12/).

src/ConvertKit_API.php

Lines changed: 97 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,15 @@ private function create_log(string $message)
128128
*/
129129
public function get_account()
130130
{
131-
$request = $this->api_version . '/account';
131+
$request = 'account';
132132

133133
$options = [
134134
'api_secret' => $this->api_secret,
135135
];
136136

137137
$this->create_log(sprintf('GET account: %s, %s', $request, json_encode($options)));
138138

139-
return $this->make_request($request, 'GET', $options);
139+
return $this->get($request, $options);
140140
}
141141

142142

@@ -147,15 +147,15 @@ public function get_account()
147147
*/
148148
public function get_sequences()
149149
{
150-
$request = $this->api_version . '/sequences';
150+
$request = 'sequences';
151151

152152
$options = [
153153
'api_key' => $this->api_key,
154154
];
155155

156156
$this->create_log(sprintf('GET sequences: %s, %s', $request, json_encode($options)));
157157

158-
return $this->make_request($request, 'GET', $options);
158+
return $this->get($request, $options);
159159
}
160160

161161

@@ -169,7 +169,7 @@ public function get_sequences()
169169
*/
170170
public function get_sequence_subscriptions(int $sequence_id, string $sort_order = 'asc')
171171
{
172-
$request = $this->api_version . sprintf('/sequences/%s/subscriptions', $sequence_id);
172+
$request = sprintf('sequences/%s/subscriptions', $sequence_id);
173173

174174
$options = [
175175
'api_secret' => $this->api_secret,
@@ -185,7 +185,7 @@ public function get_sequence_subscriptions(int $sequence_id, string $sort_order
185185
)
186186
);
187187

188-
return $this->make_request($request, 'GET', $options);
188+
return $this->get($request, $options);
189189
}
190190

191191

@@ -199,7 +199,7 @@ public function get_sequence_subscriptions(int $sequence_id, string $sort_order
199199
*/
200200
public function add_subscriber_to_sequence(int $sequence_id, string $email)
201201
{
202-
$request = $this->api_version . sprintf('/courses/%s/subscribe', $sequence_id);
202+
$request = sprintf('courses/%s/subscribe', $sequence_id);
203203

204204
$options = [
205205
'api_key' => $this->api_key,
@@ -216,7 +216,7 @@ public function add_subscriber_to_sequence(int $sequence_id, string $email)
216216
)
217217
);
218218

219-
return $this->make_request($request, 'POST', $options);
219+
return $this->post($request, $options);
220220
}
221221

222222

@@ -236,13 +236,13 @@ public function add_tag(int $tag, array $options)
236236
throw new \InvalidArgumentException();
237237
}
238238

239-
$request = $this->api_version . sprintf('/tags/%s/subscribe', $tag);
239+
$request = sprintf('tags/%s/subscribe', $tag);
240240

241241
$options['api_key'] = $this->api_key;
242242

243243
$this->create_log(sprintf('POST add tag: %s, %s, %s', $request, json_encode($options), $tag));
244244

245-
return $this->make_request($request, 'POST', $options);
245+
return $this->post($request, $options);
246246
}
247247

248248

@@ -271,11 +271,17 @@ public function get_resources(string $resource)
271271
'Accept-Encoding' => 'gzip',
272272
];
273273

274-
$request = sprintf('/%s/%s', $this->api_version, (($resource === 'landing_pages') ? 'forms' : $resource));
274+
// Assign the resource to the request variable.
275+
$request = $resource;
276+
277+
// Landing pages are included in the /forms endpoint.
278+
if ($resource === 'landing_pages') {
279+
$request = 'forms';
280+
}
275281

276282
$this->create_log(sprintf('GET request %s, %s', $request, json_encode($options)));
277283

278-
$resources = $this->make_request($request, 'GET', $options);
284+
$resources = $this->get($request, $options);
279285

280286
if (!$resources) {
281287
$this->create_log('No resources');
@@ -363,13 +369,13 @@ public function form_subscribe(int $form_id, array $options)
363369
throw new \InvalidArgumentException();
364370
}
365371

366-
$request = $this->api_version . sprintf('/forms/%s/subscribe', $form_id);
372+
$request = sprintf('forms/%s/subscribe', $form_id);
367373

368374
$options['api_key'] = $this->api_key;
369375

370376
$this->create_log(sprintf('POST form subscribe: %s, %s, %s', $request, json_encode($options), $form_id));
371377

372-
return $this->make_request($request, 'POST', $options);
378+
return $this->post($request, $options);
373379
}
374380

375381

@@ -388,13 +394,13 @@ public function form_unsubscribe(array $options)
388394
throw new \InvalidArgumentException();
389395
}
390396

391-
$request = $this->api_version . '/unsubscribe';
397+
$request = 'unsubscribe';
392398

393399
$options['api_secret'] = $this->api_secret;
394400

395401
$this->create_log(sprintf('PUT form unsubscribe: %s, %s', $request, json_encode($options)));
396402

397-
return $this->make_request($request, 'PUT', $options);
403+
return $this->put($request, $options);
398404
}
399405

400406

@@ -414,7 +420,7 @@ public function get_subscriber_id(string $email_address)
414420
throw new \InvalidArgumentException();
415421
}
416422

417-
$request = $this->api_version . '/subscribers';
423+
$request = 'subscribers';
418424

419425
$options = [
420426
'api_secret' => $this->api_secret,
@@ -431,22 +437,20 @@ public function get_subscriber_id(string $email_address)
431437
)
432438
);
433439

434-
$subscribers = $this->make_request($request, 'GET', $options);
440+
$subscribers = $this->get($request, $options);
435441

436442
if (!$subscribers) {
437443
$this->create_log('No subscribers');
438444
return false;
439445
}
440446

441-
$subscriber_id = $this::check_if_subscriber_in_array($email_address, $subscribers->subscribers);
442-
443-
if ($subscriber_id) {
444-
return $subscriber_id;
447+
if ($subscribers->total_subscribers === 0) {
448+
$this->create_log('No subscribers');
449+
return false;
445450
}
446451

447-
$this->create_log('Subscriber not found');
448-
449-
return false;
452+
// Return the subscriber's ID.
453+
return $subscribers->subscribers[0]->id;
450454
}
451455

452456

@@ -465,15 +469,15 @@ public function get_subscriber(int $subscriber_id)
465469
throw new \InvalidArgumentException();
466470
}
467471

468-
$request = $this->api_version . sprintf('/subscribers/%s', $subscriber_id);
472+
$request = sprintf('subscribers/%s', $subscriber_id);
469473

470474
$options = [
471475
'api_secret' => $this->api_secret,
472476
];
473477

474478
$this->create_log(sprintf('GET subscriber tags: %s, %s, %s', $request, json_encode($options), $subscriber_id));
475479

476-
return $this->make_request($request, 'GET', $options);
480+
return $this->get($request, $options);
477481
}
478482

479483

@@ -492,15 +496,15 @@ public function get_subscriber_tags(int $subscriber_id)
492496
throw new \InvalidArgumentException();
493497
}
494498

495-
$request = $this->api_version . sprintf('/subscribers/%s/tags', $subscriber_id);
499+
$request = sprintf('subscribers/%s/tags', $subscriber_id);
496500

497501
$options = [
498502
'api_key' => $this->api_key,
499503
];
500504

501505
$this->create_log(sprintf('GET subscriber tags: %s, %s, %s', $request, json_encode($options), $subscriber_id));
502506

503-
return $this->make_request($request, 'GET', $options);
507+
return $this->get($request, $options);
504508
}
505509

506510

@@ -519,13 +523,13 @@ public function list_purchases(array $options)
519523
throw new \InvalidArgumentException();
520524
}
521525

522-
$request = $this->api_version . '/purchases';
526+
$request = 'purchases';
523527

524528
$options['api_secret'] = $this->api_secret;
525529

526530
$this->create_log(sprintf('GET list purchases: %s, %s', $request, json_encode($options)));
527531

528-
return $this->make_request($request, 'GET', $options);
532+
return $this->get($request, $options);
529533
}
530534

531535

@@ -544,13 +548,13 @@ public function create_purchase(array $options)
544548
throw new \InvalidArgumentException();
545549
}
546550

547-
$request = $this->api_version . '/purchases';
551+
$request = 'purchases';
548552

549553
$options['api_secret'] = $this->api_secret;
550554

551555
$this->create_log(sprintf('POST create purchase: %s, %s', $request, json_encode($options)));
552556

553-
return $this->make_request($request, 'POST', $options);
557+
return $this->post($request, $options);
554558
}
555559

556560

@@ -694,6 +698,65 @@ private function strip_html_head_body_tags(string $markup)
694698
return $markup;
695699
}
696700

701+
/**
702+
* Performs a GET request to the API.
703+
*
704+
* @param string $endpoint API Endpoint.
705+
* @param array $args Request arguments.
706+
*
707+
* @throws \InvalidArgumentException If the provided arguments are not of the expected type.
708+
*
709+
* @return false|mixed
710+
*/
711+
public function get(string $endpoint, array $args = [])
712+
{
713+
return $this->make_request($endpoint, 'GET', $args);
714+
}
715+
716+
/**
717+
* Performs a POST request to the API.
718+
*
719+
* @param string $endpoint API Endpoint.
720+
* @param array $args Request arguments.
721+
*
722+
* @throws \InvalidArgumentException If the provided arguments are not of the expected type.
723+
*
724+
* @return false|mixed
725+
*/
726+
public function post(string $endpoint, array $args = [])
727+
{
728+
return $this->make_request($endpoint, 'POST', $args);
729+
}
730+
731+
/**
732+
* Performs a PUT request to the API.
733+
*
734+
* @param string $endpoint API Endpoint.
735+
* @param array $args Request arguments.
736+
*
737+
* @throws \InvalidArgumentException If the provided arguments are not of the expected type.
738+
*
739+
* @return false|mixed
740+
*/
741+
public function put(string $endpoint, array $args = [])
742+
{
743+
return $this->make_request($endpoint, 'PUT', $args);
744+
}
745+
746+
/**
747+
* Performs a DELETE request to the API.
748+
*
749+
* @param string $endpoint API Endpoint.
750+
* @param array $args Request arguments.
751+
*
752+
* @throws \InvalidArgumentException If the provided arguments are not of the expected type.
753+
*
754+
* @return false|mixed
755+
*/
756+
public function delete(string $endpoint, array $args = [])
757+
{
758+
return $this->make_request($endpoint, 'DELETE', $args);
759+
}
697760

698761
/**
699762
* Performs an API request using Guzzle.
@@ -712,7 +775,7 @@ public function make_request(string $endpoint, string $method, array $args = [])
712775
throw new \InvalidArgumentException();
713776
}
714777

715-
$url = $this->api_url_base . $endpoint;
778+
$url = $this->api_url_base . $this->api_version . '/' . $endpoint;
716779

717780
$this->create_log(sprintf('Making request on %s.', $url));
718781

@@ -761,26 +824,4 @@ public function make_request(string $endpoint, string $method, array $args = [])
761824
$this->create_log('Failed to finish request.');
762825
return false;
763826
}
764-
765-
766-
/**
767-
* Looks for subscriber with email in array
768-
*
769-
* @param string $email_address Email Address.
770-
* @param array $subscribers Subscribers.
771-
*
772-
* @return false|integer false if not found, else subscriber object
773-
*/
774-
private function check_if_subscriber_in_array(string $email_address, array $subscribers)
775-
{
776-
foreach ($subscribers as $subscriber) {
777-
if ($subscriber->email_address === $email_address) {
778-
$this->create_log('Subscriber found!');
779-
return $subscriber->id;
780-
}
781-
}
782-
783-
$this->create_log('Subscriber not found on current page.');
784-
return false;
785-
}
786827
}

tests/ConvertKitAPITest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,10 @@ public function testGetSequenceSubscriptions()
109109
$this->assertIsArray($result['subscriptions']);
110110

111111
// Assert sort order is ascending.
112-
$this->assertGreaterThan($result['subscriptions'][0]->created_at, $result['subscriptions'][1]->created_at);
112+
$this->assertGreaterThanOrEqual(
113+
$result['subscriptions'][0]->created_at,
114+
$result['subscriptions'][1]->created_at
115+
);
113116
}
114117

115118
/**
@@ -134,7 +137,10 @@ public function testGetSequenceSubscriptionsWithDescSortOrder()
134137
$this->assertIsArray($result['subscriptions']);
135138

136139
// Assert sort order.
137-
$this->assertLessThan($result['subscriptions'][0]->created_at, $result['subscriptions'][1]->created_at);
140+
$this->assertLessThanOrEqual(
141+
$result['subscriptions'][0]->created_at,
142+
$result['subscriptions'][1]->created_at
143+
);
138144
}
139145

140146
/**

0 commit comments

Comments
 (0)