Skip to content
This repository was archived by the owner on Apr 3, 2023. It is now read-only.

Commit f353a81

Browse files
author
Teddy Roncin
committed
✅ (tests) added tests for route GET /users
testing content of json
1 parent d901d37 commit f353a81

File tree

3 files changed

+34
-47
lines changed

3 files changed

+34
-47
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"symfony/intl": "6.2.0",
2929
"symfony/mailer": "6.2.2",
3030
"symfony/mime": "6.2.2",
31+
"symfony/http-foundation": "5.3.*",
3132
"symfony/monolog-bundle": "^3.1",
3233
"symfony/notifier": "6.2.0",
3334
"symfony/process": "6.2.0",

tests/AssoTest.php

Lines changed: 0 additions & 21 deletions
This file was deleted.

tests/UserTest.php

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,45 @@
33
namespace App\Tests;
44

55
use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\ApiTestCase;
6+
use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\Client;
67
use App\Repository\UserRepository;
8+
use Symfony\Component\HttpFoundation\Response;
79

810
class UserTest extends ApiTestCase
911
{
1012

11-
public function testSomething(): void
13+
public function test_POST_users(): void
1214
{
13-
$client = static::createClient([], [
14-
'CAS-LOGIN' => 'abc'
15-
]);
16-
$userRepository = static::getContainer()->get(UserRepository::class);
17-
$testUser = $userRepository->findAll()[0];
18-
$client->loginUser($testUser);
19-
$response = $client->request('POST', '/users');
20-
21-
//$response = $client->request('GET', '/users/');
22-
15+
$client = static::createConnectedClient();
16+
$crawler = $client->request('GET', 'localhost:8000/users');
17+
$response = json_decode($crawler->getContent());
2318
$this->assertResponseIsSuccessful();
24-
/*$this->assertMatchesJsonSchema([
25-
"type" => "object",
26-
"hydra:member" => [
27-
"type" => "array",
28-
"items" => [
29-
"type" => "object",
30-
"@id" => ["type" => "string"],
31-
"@type" => ["type" => "string"],
32-
"id" => ["type" => "string"],
33-
"login" => ["type" => "string"],
34-
"firstName" => ["type" => "string"],
35-
"lastName" => ["type" => "string"],
36-
]
37-
]
38-
]);*/
19+
$this->assertResponseStatusCodeSame(Response::HTTP_OK);
20+
$this->assertIsArray($response->{"hydra:member"});
21+
$this->assertNotEmpty($response->{"hydra:member"});
22+
foreach ($response->{"hydra:member"} as $member) {
23+
$this->assertMatchesRegularExpression("/^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$/", $member->{"id"});
24+
$this->assertNotEmpty($member->{"login"});
25+
$this->assertNotEmpty($member->{"firstName"});
26+
$this->assertNotEmpty($member->{"lastName"});
27+
$this->assertMatchesRegularExpression("/^https?:\\/\\/[\w.-]*\\.[\w-].+$/", $member->{"infos"}->{"avatar"});
28+
$this->assertIsArray($member->{"mailsPhones"});
29+
}
30+
$this->assertIsNumeric($response->{"hydra:totalItems"});
31+
$this->assertTrue($response->{"hydra:totalItems"} >= 0);
32+
$matches = array();
33+
$this->assertEquals(1, preg_match("/^\\/users\\?page=(?<id>\d+)+$/", $response->{"hydra:view"}->{"@id"}, $matches));
34+
$this->assertArrayHasKey("id", $matches);
35+
$this->assertEquals(1, preg_match("/^\\/users\\?page=(?<id>\d+)+$/", $response->{"hydra:view"}->{"hydra:next"}, $matches));
36+
$this->assertArrayHasKey("id", $matches);
37+
$this->assertEquals(1, preg_match("/^\\/users\\?page=(?<id>\d+)+$/", $response->{"hydra:view"}->{"hydra:last"}, $matches));
38+
$this->assertArrayHasKey("id", $matches);
39+
}
40+
41+
private static function createConnectedClient(): Client
42+
{
43+
$client = static::createClient();
44+
$client->setDefaultOptions([ 'headers' => [ 'CAS-LOGIN' => 'admin' ]]);
45+
return $client;
3946
}
4047
}

0 commit comments

Comments
 (0)