OtWebTestCase.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace App\Tests\Application;
  3. use App\Entity\Public\FederationStructure;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\DomCrawler\Crawler;
  6. use ApiPlatform\Symfony\Bundle\Test\Client;
  7. use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
  8. use Symfony\Contracts\HttpClient\ResponseInterface;
  9. /**
  10. * Base class for applicative tests
  11. */
  12. abstract class OtWebTestCase extends ApiTestCase
  13. {
  14. protected Client $client;
  15. public function setup(): void {
  16. $this->client = static::createClient();
  17. }
  18. /**
  19. * Send a requests, parse the hydra response and return an object or a Collection
  20. *
  21. * @param string $method
  22. * @param string $route
  23. * @param array<mixed> $parameters
  24. * @return ResponseInterface
  25. */
  26. protected function request(string $method, string $route, array $parameters = []): ResponseInterface {
  27. return $this->client->request(
  28. $method,
  29. $route,
  30. $parameters
  31. );
  32. }
  33. /**
  34. * Send a GET request and return the response parsed content
  35. *
  36. * @param string $route
  37. * @param array<mixed> $parameters
  38. * @return ResponseInterface
  39. */
  40. protected function get(string $route, array $parameters = []): ResponseInterface {
  41. return $this->request(
  42. Request::METHOD_GET,
  43. $route,
  44. $parameters
  45. );
  46. }
  47. protected function validateCollectionSchema(string $resourceClass): void {
  48. $this->assertResponseIsSuccessful();
  49. // Asserts that the returned content type is JSON-LD (the default)
  50. $this->assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
  51. // Asserts that the returned JSON is validated by the JSON Schema generated for this resource by API Platform
  52. // >>> Issue with the json typed PublicStructure::addresses properties
  53. // $this->assertMatchesResourceCollectionJsonSchema($resourceClass);
  54. }
  55. }