Skip to content
This repository was archived by the owner on Oct 26, 2022. It is now read-only.

Commit 862a20f

Browse files
committed
Cover unsuccessful response case and update meta files
1 parent 9769566 commit 862a20f

10 files changed

+219
-180
lines changed

.github/FUNDING.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
github: [jamesrweb]
1+
github: jamesrweb

composer.lock

Lines changed: 141 additions & 139 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/infection/config.json

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,20 @@
1515
},
1616
"mutators": {
1717
"@default": true,
18-
"Coalesce": false,
18+
"ArrayItem": false,
19+
"ArrayItemRemoval": false,
20+
"ArrayOneItem": false,
1921
"DecrementInteger": false,
22+
"Foreach_": false,
23+
"GreaterThanOrEqualTo": false,
24+
"GreaterThanOrEqualToNegotiation": false,
2025
"IncrementInteger": false,
2126
"Minus": false,
22-
"ArrayItemRemoval": false,
23-
"LessThanNegotiation": false,
24-
"LessThan": false,
25-
"Foreach_": false,
26-
"UnwrapArrayMerge": false,
27-
"ArrayOneItem": false,
28-
"Increment": false
27+
"NotIdentical": false,
28+
"Plus": false,
29+
"Ternary": false,
30+
"UnwrapArrayMap": false,
31+
"UnwrapArrayMerge": false
2932
},
3033
"tmpDir": "../../var/tmp",
3134
"phpUnit": {

src/Client.php

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace CodewarsApiClient;
66

77
use CodewarsApiClient\Interfaces\ClientInterface as CodewarsApiClientInterface;
8-
use JsonException;
98
use Nyholm\Psr7\Factory\Psr17Factory;
109
use Psr\Http\Client\ClientExceptionInterface;
1110
use Psr\Http\Client\ClientInterface;
@@ -21,14 +20,11 @@ final class Client implements CodewarsApiClientInterface
2120
private ClientInterface $psr18Client;
2221
private RequestFactoryInterface $psr17Factory;
2322

24-
public function __construct(
25-
string $api_key,
26-
?ClientInterface $http_client = null,
27-
?RequestFactoryInterface $http_factory = null
28-
) {
23+
public function __construct(string $api_key)
24+
{
2925
$this->api_key = $api_key;
30-
$this->psr18Client = $http_client ?? new Psr18Client();
31-
$this->psr17Factory = $http_factory ?? new Psr17Factory();
26+
$this->psr18Client = new Psr18Client();
27+
$this->psr17Factory = new Psr17Factory();
3228
}
3329

3430
/**
@@ -38,7 +34,7 @@ public function user(string $username): array
3834
{
3935
$uri = "{$this->base_uri}/users/{$username}";
4036

41-
return $this->request('GET', $uri);
37+
return $this->request('GET', $uri, []);
4238
}
4339

4440
/**
@@ -47,7 +43,7 @@ public function user(string $username): array
4743
public function authored(string $username): array
4844
{
4945
$uri = "{$this->base_uri}/users/{$username}/code-challenges/authored";
50-
$data = $this->request('GET', $uri);
46+
$data = $this->request('GET', $uri, []);
5147

5248
return $data['data'];
5349
}
@@ -57,7 +53,7 @@ public function authored(string $username): array
5753
*/
5854
public function completed(string $username): array
5955
{
60-
return $this->completedPaginationHelper($username);
56+
return $this->completedPaginationHelper($username, 1, []);
6157
}
6258

6359
/**
@@ -67,7 +63,7 @@ public function challenge(string $id): array
6763
{
6864
$uri = "{$this->base_uri}/code-challenges/{$id}";
6965

70-
return $this->request('GET', $uri);
66+
return $this->request('GET', $uri, []);
7167
}
7268

7369
/**
@@ -88,17 +84,17 @@ public function challenges(array $challenges): array
8884
*
8985
* @return array<mixed>
9086
*/
91-
private function completedPaginationHelper(string $username, int $page = 1, array $output = []): array
87+
private function completedPaginationHelper(string $username, int $page, array $output): array
9288
{
9389
$uri = "{$this->base_uri}/users/{$username}/code-challenges/completed";
9490
$data = $this->request('GET', $uri, ['page' => $page - 1]);
9591
$output = array_merge($output, $data['data']);
9692

97-
if ($page < $data['totalPages']) {
98-
return $this->completedPaginationHelper($username, ++$page, $output);
93+
if ($page >= $data['totalPages']) {
94+
return $output;
9995
}
10096

101-
return $output;
97+
return $this->completedPaginationHelper($username, $page + 1, $output);
10298
}
10399

104100
/**
@@ -110,7 +106,7 @@ private function completedPaginationHelper(string $username, int $page = 1, arra
110106
*
111107
* @return array<string, mixed>
112108
*/
113-
private function request(string $method, string $uri, array $query_params = []): array
109+
private function request(string $method, string $uri, array $query_params): array
114110
{
115111
$query_string = http_build_query(data: $query_params, encoding_type: PHP_QUERY_RFC3986);
116112
$request_uri = "{$uri}?{$query_string}";
@@ -122,7 +118,7 @@ private function request(string $method, string $uri, array $query_params = []):
122118

123119
$response = $this->psr18Client->sendRequest($request);
124120

125-
return $response->getStatusCode() !== 404 ? $this->parse($response) : [];
121+
return $this->parseJSONFromResponse($response);
126122
}
127123

128124
/**
@@ -143,12 +139,18 @@ private function headers(): array
143139
*
144140
* @return array<string, mixed>
145141
*/
146-
private function parse(ResponseInterface $response): array
142+
private function parseJSONFromResponse(ResponseInterface $response): array
147143
{
148-
try {
149-
return json_decode($response->getBody()->getContents(), true);
150-
} catch (JsonException $e) {
144+
if ($response->getStatusCode() === 404) {
145+
return [];
146+
}
147+
148+
$response_json = json_decode($response->getBody()->getContents(), true);
149+
150+
if (key_exists('success', $response_json) && $response_json['success'] === false) {
151151
return [];
152152
}
153+
154+
return $response_json;
153155
}
154156
}

tests/AuthoredChallengesSchemaTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@ protected function setUp(): void
2929
public function testValidateReturnsFalseWithMissingFields(): void
3030
{
3131
$authored = $this->responses->partial_authored_challenge();
32+
3233
$this->assertFalse($this->schema->validate($authored));
3334
}
3435

3536
public function testValidateReturnsTrueWithAllFieldsGiven(): void
3637
{
3738
$authored = $this->responses->valid_authored_challenge();
39+
3840
$this->assertTrue($this->schema->validate($authored));
3941
}
4042
}

tests/ChallengeSchemaTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,21 @@ protected function setUp(): void
2929
public function testValidateReturnsFalseWithEmptyArrayGiven(): void
3030
{
3131
$challenge = $this->responses->empty();
32+
3233
$this->assertFalse($this->schema->validate($challenge));
3334
}
3435

3536
public function testValidateReturnsFalseWithMissingFields(): void
3637
{
3738
$challenge = $this->responses->partial_challenge();
39+
3840
$this->assertFalse($this->schema->validate($challenge));
3941
}
4042

4143
public function testValidateReturnsTrueWithAllFieldsGiven(): void
4244
{
4345
$challenge = $this->responses->valid_challenge();
46+
4447
$this->assertTrue($this->schema->validate($challenge));
4548
}
4649
}

tests/ClientTest.php

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,45 +21,66 @@ final class ClientTest extends TestCase
2121
{
2222
private ClientInterface $client;
2323

24-
protected function setUp(): void
25-
{
24+
protected function setUp(): void {
2625
$this->client = new Client($_ENV['CODEWARS_DUMMY_API_KEY']);
2726
}
2827

2928
public function testUser(): void
3029
{
30+
$schema = new UserSchema();
31+
3132
$response = $this->client->user($_ENV['CODEWARS_VALID_USERNAME']);
32-
$this->assertTrue((new UserSchema())->validate($response));
33+
34+
$this->assertTrue($schema->validate($response));
3335
}
3436

3537
public function testCompletedChallenges(): void
3638
{
39+
$schema = new CompletedChallengesSchema();
40+
3741
$response = $this->client->completed($_ENV['CODEWARS_VALID_USERNAME']);
38-
$this->assertTrue((new CompletedChallengesSchema())->validate($response));
42+
43+
$this->assertTrue($schema->validate($response));
3944
}
4045

4146
public function testAuthoredChallenges(): void
4247
{
48+
$schema = new AuthoredChallengesSchema();
49+
4350
$response = $this->client->authored($_ENV['CODEWARS_VALID_USERNAME']);
44-
$this->assertTrue((new AuthoredChallengesSchema())->validate($response));
51+
52+
$this->assertTrue($schema->validate($response));
4553
}
4654

4755
public function testChallenge(): void
4856
{
57+
$schema = new ChallengeSchema();
58+
4959
$response = $this->client->challenge($_ENV['CODEWARS_VALID_CHALLENGE_ID']);
50-
$this->assertTrue((new ChallengeSchema())->validate($response));
60+
61+
$this->assertTrue($schema->validate($response));
5162
}
5263

5364
public function testChallenges(): void
5465
{
66+
$schema = new ChallengeSchema();
67+
5568
$response = $this->client->challenges([$_ENV['CODEWARS_VALID_CHALLENGE_ID']]);
56-
$candidate = array_shift($response);
57-
$this->assertTrue((new ChallengeSchema())->validate($candidate));
69+
70+
$this->assertTrue($schema->validate(array_shift($response)));
5871
}
5972

6073
public function testClientReturnsEmptyArrayWhen404(): void
61-
{
74+
{
6275
$response = $this->client->user($_ENV['CODEWARS_INVALID_USERNAME']);
76+
77+
$this->assertSame([], $response);
78+
}
79+
80+
public function testClientReturnsEmptyArrayWhenResponseIsUnsuccessful(): void
81+
{
82+
$response = $this->client->user("");
83+
6384
$this->assertSame([], $response);
6485
}
6586
}

tests/CompletedChallengesSchemaTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@ protected function setUp(): void
2929
public function testValidateReturnsFalseWithMissingFields(): void
3030
{
3131
$completed = $this->responses->partial_completed_challenge();
32+
3233
$this->assertFalse($this->schema->validate($completed));
3334
}
3435

3536
public function testValidateReturnsTrueWithAllFieldsGiven(): void
3637
{
3738
$completed = $this->responses->valid_completed_challenge();
39+
3840
$this->assertTrue($this->schema->validate($completed));
3941
}
4042
}

tests/Schemas/AbstractSchema.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ abstract class AbstractSchema implements SchemaInterface
2626
final public function validate(array $data): bool
2727
{
2828
try {
29-
(new Processor())->process($this->schema(), $data);
29+
$processor = new Processor();
30+
$processor->process($this->schema(), $data);
3031

3132
return true;
3233
} catch (ValidationException $e) {

tests/UserSchemaTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,21 @@ protected function setUp(): void
2929
public function testValidateReturnsFalseWithEmptyArrayGiven(): void
3030
{
3131
$user = $this->responses->empty();
32+
3233
$this->assertFalse($this->schema->validate($user));
3334
}
3435

3536
public function testValidateReturnsFalseWithMissingFields(): void
3637
{
3738
$user = $this->responses->partial_user();
39+
3840
$this->assertFalse($this->schema->validate($user));
3941
}
4042

4143
public function testValidateReturnsTrueWithAllFieldsGiven(): void
4244
{
4345
$user = $this->responses->valid_user();
46+
4447
$this->assertTrue($this->schema->validate($user));
4548
}
4649
}

0 commit comments

Comments
 (0)