OtWebTestCase.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. namespace App\Tests\Application;
  3. use App\Entity\Access\Access;
  4. use App\Entity\Person\Person;
  5. use App\Entity\Public\FederationStructure;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Doctrine\Common\DataFixtures\Purger\ORMPurger;
  8. use Symfony\Bundle\SecurityBundle\Security;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\DomCrawler\Crawler;
  11. use ApiPlatform\Symfony\Bundle\Test\Client;
  12. use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
  13. use Symfony\Contracts\HttpClient\ResponseInterface;
  14. use Zenstruck\Foundry\Proxy;
  15. /**
  16. * Base class for applicative tests
  17. */
  18. abstract class OtWebTestCase extends ApiTestCase
  19. {
  20. protected EntityManagerInterface $em;
  21. protected Client $client;
  22. protected Access | Proxy | null $user = null;
  23. protected ?string $securityToken = null;
  24. /**
  25. * Executed before each test
  26. *
  27. * @return void
  28. * @throws \Exception
  29. */
  30. public function setup(): void {
  31. // Boot le kernel symfony et récupère l'entity manager
  32. // @see https://symfony.com/doc/current/testing.html#retrieving-services-in-the-test
  33. self::bootKernel();
  34. $this->em = static::getContainer()->get(EntityManagerInterface::class);
  35. // Purge DB before populating new fixtures
  36. $this->purgeDb();
  37. // Définit les fixtures et flush
  38. $this->setFixtures();
  39. $this->em->flush();
  40. // Instancie le client qui exécutera les requêtes à l'api
  41. // @see https://symfony.com/doc/current/testing.html#making-requests
  42. $this->client = static::createClient();
  43. }
  44. /**
  45. * Delete all DB records before populating fixtures.
  46. *
  47. * @return void
  48. * @throws \Doctrine\DBAL\Exception
  49. */
  50. private function purgeDb() {
  51. if (!preg_match('/.*test.*/', $this->em->getConnection()->getDatabase())) {
  52. throw new \RuntimeException("The DB name shall contain 'test' in its name to be purge");
  53. }
  54. $this->em->getConnection()->exec('SET FOREIGN_KEY_CHECKS = 0;');
  55. $purger = new ORMPurger($this->em);
  56. $purger->setPurgeMode(ORMPurger::PURGE_MODE_DELETE);
  57. $purger->purge();
  58. $this->em->getConnection()->exec('SET FOREIGN_KEY_CHECKS = 1;');
  59. }
  60. /**
  61. * Create and persist the fixtures (no need to flush, the setup will perform it later)
  62. *
  63. * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#same-entities-used-in-these-docs
  64. *
  65. * @return void
  66. */
  67. protected function setFixtures(): void {
  68. // TODO: préparer un jeu de fixtures par défaut
  69. }
  70. /**
  71. * Send a requests, parse the hydra response and return an object or a Collection
  72. *
  73. * @param string $method
  74. * @param string $route
  75. * @param array<mixed> $data
  76. * @param array<mixed> $headers
  77. * @return ResponseInterface
  78. */
  79. protected function request(string $method, string $route, array | null $data = null, array $headers = []): ResponseInterface {
  80. if ($this->user) {
  81. $headers = array_merge(
  82. ['x-accessid' => $this->user->getId(), 'authorization' => 'BEARER ' . $this->securityToken],
  83. $headers
  84. );
  85. }
  86. $parameters = ['headers' => $headers];
  87. if ($data) {
  88. $parameters['json'] = $data;
  89. }
  90. return $this->client->request(
  91. $method,
  92. $route,
  93. $parameters
  94. );
  95. }
  96. /**
  97. * Send a GET request and return the response parsed content
  98. *
  99. * @param string $route
  100. * @param array<mixed> $headers
  101. * @return ResponseInterface
  102. */
  103. protected function get(string $route, array $headers = []): ResponseInterface {
  104. return $this->request(
  105. Request::METHOD_GET,
  106. $route,
  107. null,
  108. $headers
  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. return $this->request(
  121. Request::METHOD_PUT,
  122. $route,
  123. $data,
  124. $headers
  125. );
  126. }
  127. /**
  128. * Send a POST request and return the response parsed content
  129. *
  130. * @param string $route
  131. * @param array<mixed> $data
  132. * @param array<mixed> $headers
  133. * @return ResponseInterface
  134. */
  135. protected function post(string $route, array $data, array $headers = []): ResponseInterface {
  136. return $this->request(
  137. Request::METHOD_POST,
  138. $route,
  139. $data,
  140. $headers
  141. );
  142. }
  143. /**
  144. * Send a DELETE request and return the response parsed content
  145. *
  146. * @param string $route
  147. * @param array<mixed> $headers
  148. * @return ResponseInterface
  149. */
  150. protected function delete(string $route, array $headers = []): ResponseInterface {
  151. return $this->request(
  152. Request::METHOD_DELETE,
  153. $route,
  154. null,
  155. $headers
  156. );
  157. }
  158. protected function validateCollectionSchema(string $resourceClass, int $expectedStatus = 200): void {
  159. $this->assertResponseStatusCodeSame($expectedStatus);
  160. if ($expectedStatus == 200) {
  161. $this->assertResponseIsSuccessful();
  162. }
  163. // Asserts that the returned content type is JSON-LD (the default)
  164. $this->assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
  165. // Asserts that the returned JSON is validated by the JSON Schema generated for this resource by API Platform
  166. // >>> Issue with the json typed PublicStructure::addresses properties
  167. // $this->assertMatchesResourceCollectionJsonSchema($resourceClass);
  168. }
  169. protected function loginAs(Proxy | Access $access): void {
  170. $person = $access->getPerson();
  171. $response = $this->post(
  172. '/login_check',
  173. ['username' => $person->getUsername(), 'password' => $person->getPassword()]
  174. );
  175. $content = $response->getContent();
  176. $this->securityToken = json_decode($content)->token;
  177. $this->user = $access;
  178. }
  179. }