ResidenceAreaTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 testGetResidenceArea(): 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 testPostResidenceArea()
  39. // {
  40. // $this->loginAs($this->user);
  41. // $this->post('/api/residence_areas', [
  42. // 'label' => 'toto',
  43. // 'billingSetting' => '/api/billing_settings/6070'
  44. // ]);
  45. // $this->assertResponseStatusCodeSame(201);
  46. // $this->assertJsonContains([
  47. // '@context' => '/api/contexts/ResidenceArea',
  48. // '@type' => 'ResidenceArea',
  49. // 'label' => 'toto',
  50. // 'billingSetting' => '/api/billing_settings/6070'
  51. // ]);
  52. // }
  53. // public function testPutResidenceArea()
  54. // {
  55. // $this->loginAs($this->user);
  56. // $this->put('/api/residence_areas/752', [
  57. // 'label' => 'tata',
  58. // 'billingSetting' => '/api/billing_settings/6070'
  59. // ]);
  60. // $this->assertResponseStatusCodeSame(200);
  61. // $this->assertJsonContains([
  62. // '@context' => '/api/contexts/ResidenceArea',
  63. // '@type' => 'ResidenceArea',
  64. // 'label' => 'tata',
  65. // 'billingSetting' => '/api/billing_settings/6070'
  66. // ]);
  67. // }
  68. public function testDeleteResidenceArea()
  69. {
  70. $this->loginAs($this->user);
  71. $this->delete('/api/residence_areas/752');
  72. $this->assertResponseStatusCodeSame(204);
  73. }
  74. public function loginAs()
  75. {
  76. // on récupère l'access qui a l'id 641003 dans l'entity manager
  77. $access = $this->em->getRepository(Access::class)->find(642459);
  78. $person = $access->getPerson();
  79. $response = $this->post(
  80. '/login_check',
  81. ['username' => $person->getUsername(), 'password' => $person->getPassword()]
  82. );
  83. $content = $response->getContent();
  84. $this->securityToken = json_decode($content)->token;
  85. $this->user = $access;
  86. }
  87. /**
  88. * Send a GET request and return the response parsed content
  89. *
  90. * @param string $route
  91. * @param array<mixed> $headers
  92. * @return ResponseInterface
  93. */
  94. protected function get(string $route, array $headers = []): ResponseInterface
  95. {
  96. return $this->request(
  97. Request::METHOD_GET,
  98. $route,
  99. null,
  100. $headers
  101. );
  102. }
  103. /**
  104. * Send a POST request and return the response parsed content
  105. *
  106. * @param string $route
  107. * @param array<mixed> $data
  108. * @param array<mixed> $headers
  109. * @return ResponseInterface
  110. */
  111. protected function post(string $route, array $data, array $headers = []): ResponseInterface
  112. {
  113. return $this->request(
  114. Request::METHOD_POST,
  115. $route,
  116. $data,
  117. $headers
  118. );
  119. }
  120. /**
  121. * Send a requests, parse the hydra response and return an object or a Collection
  122. *
  123. * @param string $method
  124. * @param string $route
  125. * @param array<mixed> $data
  126. * @param array<mixed> $headers
  127. * @return ResponseInterface
  128. */
  129. protected function request(string $method, string $route, array | null $data = null, array $headers = []): ResponseInterface
  130. {
  131. if ($this->user) {
  132. $headers = array_merge(
  133. ['x-accessid' => $this->user->getId(), 'authorization' => 'BEARER ' . $this->securityToken],
  134. $headers
  135. );
  136. }
  137. $parameters = ['headers' => $headers];
  138. if ($data) {
  139. $parameters['json'] = $data;
  140. }
  141. return $this->client->request(
  142. $method,
  143. $route,
  144. $parameters
  145. );
  146. }
  147. /**
  148. * Send a PUT request and return the response parsed content
  149. *
  150. * @param string $route
  151. * @param array<mixed> $data
  152. * @param array<mixed> $headers
  153. * @return ResponseInterface
  154. */
  155. protected function put(string $route, array $data, array $headers = []): ResponseInterface
  156. {
  157. return $this->request(
  158. Request::METHOD_PUT,
  159. $route,
  160. $data,
  161. $headers
  162. );
  163. }
  164. /**
  165. * Send a DELETE request and return the response parsed content
  166. *
  167. * @param string $route
  168. * @param array<mixed> $headers
  169. * @return ResponseInterface
  170. */
  171. protected function delete(string $route, array $headers = []): ResponseInterface
  172. {
  173. return $this->request(
  174. Request::METHOD_DELETE,
  175. $route,
  176. null,
  177. $headers
  178. );
  179. }
  180. /**
  181. * Assert that the response has the expected status code and is well formated
  182. *
  183. * @param string $resourceClass
  184. * @param int $expectedStatus
  185. * @return void
  186. */
  187. protected function validateCollectionSchema(string $resourceClass, int $expectedStatus = 200): void
  188. {
  189. $this->assertResponseStatusCodeSame($expectedStatus);
  190. if ($expectedStatus == 200) {
  191. $this->assertResponseIsSuccessful();
  192. }
  193. // Asserts that the returned content type is JSON-LD (the default)
  194. $this->assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
  195. // Asserts that the returned JSON is validated by the JSON Schema generated for this resource by API Platform
  196. // >>> Issue with the json typed PublicStructure::addresses properties
  197. // $this->assertMatchesResourceCollectionJsonSchema($resourceClass);
  198. }
  199. }