PersonTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. namespace App\Tests\Application;
  3. use Zenstruck\Foundry\Proxy;
  4. use App\Entity\Access\Access;
  5. use Doctrine\ORM\EntityManager;
  6. use AppBundle\Entity\Person\Person;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use ApiPlatform\Symfony\Bundle\Test\Client;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
  11. use Symfony\Contracts\HttpClient\ResponseInterface;
  12. class PersonTest extends ApiTestCase
  13. {
  14. protected Access | Proxy | null $user = null;
  15. private $securityToken;
  16. protected Client $client;
  17. private EntityManagerInterface $em;
  18. protected function setUp(): void
  19. {
  20. self::bootKernel();
  21. $this->em = self::getContainer()->get(EntityManagerInterface::class);
  22. $this->client = static::createClient();
  23. }
  24. public function testPerson(): void
  25. {
  26. $this->loginAs($this->user);
  27. $this->assertResponseIsSuccessful();
  28. }
  29. public function testPersonGetCollection(): void
  30. {
  31. $this->loginAs($this->user);
  32. $this->get('/api/peoples');
  33. $this->assertResponseStatusCodeSame(404);
  34. }
  35. public function testPersonPut(): void
  36. {
  37. $this->loginAs($this->user);
  38. $this->put('/api/people/' . $this->user->getPerson()->getId(), []);
  39. // Expects : 405 Method Not Allowed
  40. $this->assertResponseStatusCodeSame(405);
  41. }
  42. public function testPersonPost(): void
  43. {
  44. $this->loginAs($this->user);
  45. $this->post('/api/people/' . $this->user->getPerson()->getId(), []);
  46. // Expects : 405 Method Not Allowed
  47. $this->assertResponseStatusCodeSame(405);
  48. }
  49. public function testPersonDelete(): void
  50. {
  51. $this->loginAs($this->user);
  52. $this->delete('/api/people/' . $this->user->getPerson()->getId());
  53. // Expects : 405 Method Not Allowed
  54. $this->assertResponseStatusCodeSame(405);
  55. }
  56. /**
  57. * Assert that the response has the expected status code and is well formated
  58. *
  59. * @param string $resourceClass
  60. * @param int $expectedStatus
  61. * @return void
  62. */
  63. protected function validateCollectionSchema(string $resourceClass, int $expectedStatus = 200): void
  64. {
  65. $this->assertResponseStatusCodeSame($expectedStatus);
  66. if ($expectedStatus == 200) {
  67. $this->assertResponseIsSuccessful();
  68. }
  69. // Asserts that the returned content type is JSON-LD (the default)
  70. $this->assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
  71. // Asserts that the returned JSON is validated by the JSON Schema generated for this resource by API Platform
  72. // >>> Issue with the json typed PublicStructure::addresses properties
  73. // $this->assertMatchesResourceCollectionJsonSchema($resourceClass);
  74. }
  75. public function loginAs()
  76. {
  77. // on récupère l'access qui a l'id 641003 dans l'entity manager
  78. $access = $this->em->getRepository(Access::class)->find(642459);
  79. $person = $access->getPerson();
  80. $response = $this->post(
  81. '/login_check',
  82. ['username' => $person->getUsername(), 'password' => $person->getPassword()]
  83. );
  84. $content = $response->getContent();
  85. $this->securityToken = json_decode($content)->token;
  86. $this->user = $access;
  87. }
  88. /**
  89. * Send a GET request and return the response parsed content
  90. *
  91. * @param string $route
  92. * @param array<mixed> $headers
  93. * @return ResponseInterface
  94. */
  95. protected function get(string $route, array $headers = []): ResponseInterface
  96. {
  97. return $this->request(
  98. Request::METHOD_GET,
  99. $route,
  100. null,
  101. $headers
  102. );
  103. }
  104. /**
  105. * Send a POST request and return the response parsed content
  106. *
  107. * @param string $route
  108. * @param array<mixed> $data
  109. * @param array<mixed> $headers
  110. * @return ResponseInterface
  111. */
  112. protected function post(string $route, array $data, array $headers = []): ResponseInterface
  113. {
  114. return $this->request(
  115. Request::METHOD_POST,
  116. $route,
  117. $data,
  118. $headers
  119. );
  120. }
  121. /**
  122. * Send a requests, parse the hydra response and return an object or a Collection
  123. *
  124. * @param string $method
  125. * @param string $route
  126. * @param array<mixed> $data
  127. * @param array<mixed> $headers
  128. * @return ResponseInterface
  129. */
  130. protected function request(string $method, string $route, array | null $data = null, array $headers = []): ResponseInterface
  131. {
  132. if ($this->user) {
  133. $headers = array_merge(
  134. ['x-accessid' => $this->user->getId(), 'authorization' => 'BEARER ' . $this->securityToken],
  135. $headers
  136. );
  137. }
  138. $parameters = ['headers' => $headers];
  139. if ($data) {
  140. $parameters['json'] = $data;
  141. }
  142. return $this->client->request(
  143. $method,
  144. $route,
  145. $parameters
  146. );
  147. }
  148. /**
  149. * Send a PUT request and return the response parsed content
  150. *
  151. * @param string $route
  152. * @param array<mixed> $data
  153. * @param array<mixed> $headers
  154. * @return ResponseInterface
  155. */
  156. protected function put(string $route, array $data, array $headers = []): ResponseInterface
  157. {
  158. return $this->request(
  159. Request::METHOD_PUT,
  160. $route,
  161. $data,
  162. $headers
  163. );
  164. }
  165. /**
  166. * Send a DELETE request and return the response parsed content
  167. *
  168. * @param string $route
  169. * @param array<mixed> $headers
  170. * @return ResponseInterface
  171. */
  172. protected function delete(string $route, array $headers = []): ResponseInterface
  173. {
  174. return $this->request(
  175. Request::METHOD_DELETE,
  176. $route,
  177. null,
  178. $headers
  179. );
  180. }
  181. }