SubdomainTest.php 6.4 KB

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