CycleTest.php 5.7 KB

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