|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Tests\users; |
| 4 | + |
| 5 | +use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\ApiTestCase; |
| 6 | +use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\Client; |
| 7 | +use Symfony\Component\HttpFoundation\Response; |
| 8 | + |
| 9 | +class GetUsers extends ApiTestCase |
| 10 | +{ |
| 11 | + |
| 12 | + private $responseWithNoParameter = array(); |
| 13 | + private $lastPage; |
| 14 | + |
| 15 | + public function test(): void |
| 16 | + { |
| 17 | + $client = static::createClient(); |
| 18 | + $this->testNotConnected($client); |
| 19 | + $client->setDefaultOptions([ 'headers' => [ 'CAS-LOGIN' => 'admin' ]]); |
| 20 | + $this->testNoParameter($client); |
| 21 | + $this->testParameter1($client); |
| 22 | + $this->testAllParameters($client); |
| 23 | + $this->testOutOfRangeParameters($client); |
| 24 | + $this->testWrongTypeParameter($client); |
| 25 | + } |
| 26 | + |
| 27 | + private function testNotConnected(Client $client) : void |
| 28 | + { |
| 29 | + $client->request('GET', 'localhost:8000/users'); |
| 30 | + $this->assertResponseStatusCodeSame(Response::HTTP_UNAUTHORIZED); |
| 31 | + } |
| 32 | + |
| 33 | + private function testNoParameter(Client $client) : void |
| 34 | + { |
| 35 | + $crawler = $client->request('GET', 'localhost:8000/users'); |
| 36 | + $response = json_decode($crawler->getContent()); |
| 37 | + $this->assertResponseStatusCodeSame(Response::HTTP_OK); |
| 38 | + $this->assertIsArray($response->{'hydra:member'}); |
| 39 | + $this->assertNotEmpty($response->{'hydra:member'}); |
| 40 | + $this->responseWithNoParameter['member'] = array(); |
| 41 | + foreach ($response->{'hydra:member'} as $i => $member) { |
| 42 | + $this->assertMatchesRegularExpression("/^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$/", $member->{'id'}); |
| 43 | + $this->assertNotEmpty($member->{'login'}); |
| 44 | + $this->assertNotEmpty($member->{'firstName'}); |
| 45 | + $this->assertNotEmpty($member->{'lastName'}); |
| 46 | + $this->assertMatchesRegularExpression("/^https?:\\/\\/[\w.-]*\\.[\w-].+$/", $member->{'infos'}->{'avatar'}); |
| 47 | + $this->assertIsArray($member->{'mailsPhones'}); |
| 48 | + $returnedMember = array(); |
| 49 | + $returnedMember['login'] = $member->{'login'}; |
| 50 | + $returnedMember['firstName'] = $member->{'firstName'}; |
| 51 | + $returnedMember['lastName'] = $member->{'lastName'}; |
| 52 | + $returnedMember['avatar'] = $member->{'infos'}->{'avatar'}; |
| 53 | + $this->responseWithNoParameter['member'][$i] = $returnedMember; |
| 54 | + } |
| 55 | + $this->assertIsNumeric($response->{'hydra:totalItems'}); |
| 56 | + $this->assertTrue($response->{'hydra:totalItems'} >= 0); |
| 57 | + $this->responseWithNoParameter['totalItems'] = $response->{'hydra:totalItems'}; |
| 58 | + $matches = array(); |
| 59 | + $this->assertEquals(1, preg_match("/^\\/users\\?page=(?<id>\d+)+$/", $response->{'hydra:view'}->{'@id'}, $matches)); |
| 60 | + $this->assertArrayHasKey('id', $matches); |
| 61 | + $this->responseWithNoParameter['view:id'] = $response->{'hydra:view'}->{'@id'}; |
| 62 | + $this->assertEquals(1, preg_match("/^\\/users\\?page=(?<id>\d+)+$/", $response->{'hydra:view'}->{'hydra:next'}, $matches)); |
| 63 | + $this->assertArrayHasKey('id', $matches); |
| 64 | + $this->responseWithNoParameter['view:next'] = $response->{'hydra:view'}->{'hydra:next'}; |
| 65 | + $this->assertEquals(1, preg_match("/^\\/users\\?page=(?<id>\d+)+$/", $response->{'hydra:view'}->{'hydra:last'}, $matches)); |
| 66 | + $this->assertArrayHasKey('id', $matches); |
| 67 | + $this->responseWithNoParameter['view:last'] = $response->{'hydra:view'}->{'hydra:last'}; |
| 68 | + $this->lastPage = $matches['id']; |
| 69 | + } |
| 70 | + |
| 71 | + private function testParameter1(Client $client) : void |
| 72 | + { |
| 73 | + $crawler = $client->request('GET', 'localhost:8000/users?page=1'); |
| 74 | + $response = json_decode($crawler->getContent()); |
| 75 | + $this->assertResponseStatusCodeSame(Response::HTTP_OK); |
| 76 | + foreach ($this->responseWithNoParameter['member'] as $i => $member) { |
| 77 | + $this->assertEquals($member['login'], $response->{'hydra:member'}[$i]->{'login'}); |
| 78 | + $this->assertEquals($member['firstName'], $response->{'hydra:member'}[$i]->{'firstName'}); |
| 79 | + $this->assertEquals($member['lastName'], $response->{'hydra:member'}[$i]->{'lastName'}); |
| 80 | + $this->assertEquals($member['avatar'], $response->{'hydra:member'}[$i]->{'infos'}->{'avatar'}); |
| 81 | + } |
| 82 | + $this->assertEquals($this->responseWithNoParameter['totalItems'], $response->{'hydra:totalItems'}); |
| 83 | + $this->assertEquals($this->responseWithNoParameter['view:id'], $response->{'hydra:view'}->{'@id'}); |
| 84 | + $this->assertEquals($this->responseWithNoParameter['view:next'], $response->{'hydra:view'}->{'hydra:next'}); |
| 85 | + $this->assertEquals($this->responseWithNoParameter['view:last'], $response->{'hydra:view'}->{'hydra:last'}); |
| 86 | + } |
| 87 | + |
| 88 | + private function testAllParameters(Client $client) : void |
| 89 | + { |
| 90 | + for ($i = 1; $i <= $this->lastPage; $i++) { |
| 91 | + $client->request('GET', 'localhost:8000/users?page='.$i); |
| 92 | + $this->assertResponseStatusCodeSame(Response::HTTP_OK); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + private function testOutOfRangeParameters(Client $client) : void |
| 97 | + { |
| 98 | + $client->request('GET', 'localhost:8000/users?page=0'); |
| 99 | + $this->assertResponseStatusCodeSame(Response::HTTP_BAD_REQUEST); |
| 100 | + $crawler = $client->request('GET', 'localhost:8000/users?page='.$this->lastPage + 1); |
| 101 | + $response = json_decode($crawler->getContent()); |
| 102 | + $this->assertResponseStatusCodeSame(Response::HTTP_OK); |
| 103 | + $this->assertEmpty($response->{'hydra:member'}); |
| 104 | + } |
| 105 | + |
| 106 | + private function testWrongTypeParameter(Client $client) : void |
| 107 | + { |
| 108 | + $crawler = $client->request('GET', 'localhost:8000/users?page=1.5'); |
| 109 | + $response = json_decode($crawler->getContent()); |
| 110 | + $this->assertResponseStatusCodeSame(Response::HTTP_OK); |
| 111 | + $this->assertEquals('/users?page=1', $response->{'hydra:view'}->{'@id'}); |
| 112 | + $client->request('GET', 'localhost:8000/users?page=abc'); |
| 113 | + $this->assertResponseStatusCodeSame(Response::HTTP_BAD_REQUEST); |
| 114 | + } |
| 115 | + |
| 116 | +} |
0 commit comments