EducationTimingsTest.php 5.3 KB

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