OtWebTestCase.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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> $parameters
  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 POST request and return the response parsed content
  113. *
  114. * @param string $route
  115. * @param array<mixed> $parameters
  116. * @return ResponseInterface
  117. */
  118. protected function post(string $route, array $data, array $headers = []): ResponseInterface {
  119. return $this->request(
  120. Request::METHOD_POST,
  121. $route,
  122. $data,
  123. $headers
  124. );
  125. }
  126. protected function validateCollectionSchema(string $resourceClass): void {
  127. $this->assertResponseIsSuccessful();
  128. // Asserts that the returned content type is JSON-LD (the default)
  129. $this->assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
  130. // Asserts that the returned JSON is validated by the JSON Schema generated for this resource by API Platform
  131. // >>> Issue with the json typed PublicStructure::addresses properties
  132. // $this->assertMatchesResourceCollectionJsonSchema($resourceClass);
  133. }
  134. protected function loginAs(Proxy | Access $access): void {
  135. $person = $access->getPerson();
  136. $response = $this->post(
  137. '/login_check',
  138. ['username' => $person->getUsername(), 'password' => $person->getPassword()]
  139. );
  140. $content = $response->getContent();
  141. $this->securityToken = json_decode($content)->token;
  142. $this->user = $access;
  143. }
  144. }