|
3 | 3 | namespace App\Tests;
|
4 | 4 |
|
5 | 5 | use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\ApiTestCase;
|
| 6 | +use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\Client; |
6 | 7 | use App\Repository\UserRepository;
|
| 8 | +use Symfony\Component\HttpFoundation\Response; |
7 | 9 |
|
8 | 10 | class UserTest extends ApiTestCase
|
9 | 11 | {
|
10 | 12 |
|
11 |
| - public function testSomething(): void |
| 13 | + public function test_POST_users(): void |
12 | 14 | {
|
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()); |
23 | 18 | $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; |
39 | 46 | }
|
40 | 47 | }
|
0 commit comments