| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- <?php
- namespace App\Tests\Application;
- use Zenstruck\Foundry\Proxy;
- use App\Entity\Access\Access;
- use Doctrine\ORM\EntityManagerInterface;
- use ApiPlatform\Symfony\Bundle\Test\Client;
- use Symfony\Component\HttpFoundation\Request;
- use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
- use Symfony\Contracts\HttpClient\ResponseInterface;
- class SubdomainTest extends ApiTestCase
- {
- protected Access | Proxy | null $user = null;
- private $securityToken;
- protected Client $client;
- private EntityManagerInterface $em;
- protected function setUp(): void
- {
- self::bootKernel();
- $this->em = self::getContainer()->get(EntityManagerInterface::class);
- $this->client = static::createClient();
- }
- // public function testGetCollectionSubdomain(): void
- // {
- // $this->loginAs($this->user);
-
- // $this->assertResponseIsSuccessful();
- // $this->get('/api/subdomains');
- // $this->assertResponseStatusCodeSame(200);
- // $this->assertJsonContains([
- // '@context' => '/api/contexts/Subdomain',
- // '@id' => '/api/subdomains',
- // '@type' => 'hydra:Collection',
- // 'hydra:totalItems' => 1,
- // 'hydra:member' => [
- // ["subdomain" => "subdomain"]
- // ],
- // ]);
- // }
- // public function testPutSubdomain(): void
- // {
- // $this->loginAs($this->user);
- // $this->assertResponseIsSuccessful();
- // $this->put('/api/subdomains/6622', [
- // 'json' => [
- // 'organization' => '/api/organizations/642459',
- // 'subdomain' => 'toto',
- // 'active' => true,
- // ],
- // ]);
- // $this->assertResponseStatusCodeSame(200);
- // $this->assertJsonContains([
- // '@context' => '/api/contexts/Subdomain',
- // '@id' => '/api/subdomains/6622',
- // '@type' => 'Subdomain',
- // 'subdomain' => 'subdomain',
- // 'active' => true,
- // 'organization' => '/api/organizations/616013',
- // ]);
- // }
- public function testDeleteSubdomain(): void
- {
- $this->loginAs($this->user);
- $this->assertResponseIsSuccessful();
- $this->delete('/api/subdomains/6622');
- // méthode non supporté
- $this->assertResponseStatusCodeSame(405);
- }
- // tester en ayant perdu le role admin admin_core
- public function loginAs()
- {
- // on récupère l'access qui a l'id 641003 dans l'entity manager
- $access = $this->em->getRepository(Access::class)->find(642459);
- $person = $access->getPerson();
- $response = $this->post(
- '/login_check',
- ['username' => $person->getUsername(), 'password' => $person->getPassword()]
- );
- $content = $response->getContent();
- $this->securityToken = json_decode($content)->token;
- $this->user = $access;
- }
- /**
- * Send a GET request and return the response parsed content
- *
- * @param string $route
- * @param array<mixed> $headers
- * @return ResponseInterface
- */
- protected function get(string $route, array $headers = []): ResponseInterface
- {
- return $this->request(
- Request::METHOD_GET,
- $route,
- null,
- $headers
- );
- }
- /**
- * Send a POST request and return the response parsed content
- *
- * @param string $route
- * @param array<mixed> $data
- * @param array<mixed> $headers
- * @return ResponseInterface
- */
- protected function post(string $route, array $data, array $headers = []): ResponseInterface
- {
- return $this->request(
- Request::METHOD_POST,
- $route,
- $data,
- $headers
- );
- }
- /**
- * Send a requests, parse the hydra response and return an object or a Collection
- *
- * @param string $method
- * @param string $route
- * @param array<mixed> $data
- * @param array<mixed> $headers
- * @return ResponseInterface
- */
- protected function request(string $method, string $route, array | null $data = null, array $headers = []): ResponseInterface
- {
- if ($this->user) {
- $headers = array_merge(
- ['x-accessid' => $this->user->getId(), 'authorization' => 'BEARER ' . $this->securityToken],
- $headers
- );
- }
- $parameters = ['headers' => $headers];
- if ($data) {
- $parameters['json'] = $data;
- }
- return $this->client->request(
- $method,
- $route,
- $parameters
- );
- }
- /**
- * Send a PUT request and return the response parsed content
- *
- * @param string $route
- * @param array<mixed> $data
- * @param array<mixed> $headers
- * @return ResponseInterface
- */
- protected function put(string $route, array $data, array $headers = []): ResponseInterface
- {
- return $this->request(
- Request::METHOD_PUT,
- $route,
- $data,
- $headers
- );
- }
- /**
- * Send a DELETE request and return the response parsed content
- *
- * @param string $route
- * @param array<mixed> $headers
- * @return ResponseInterface
- */
- protected function delete(string $route, array $headers = []): ResponseInterface
- {
- return $this->request(
- Request::METHOD_DELETE,
- $route,
- null,
- $headers
- );
- }
- /**
- * Assert that the response has the expected status code and is well formated
- *
- * @param string $resourceClass
- * @param int $expectedStatus
- * @return void
- */
- protected function validateCollectionSchema(string $resourceClass, int $expectedStatus = 200): void
- {
- $this->assertResponseStatusCodeSame($expectedStatus);
- if ($expectedStatus == 200) {
- $this->assertResponseIsSuccessful();
- }
- // Asserts that the returned content type is JSON-LD (the default)
- $this->assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
- // Asserts that the returned JSON is validated by the JSON Schema generated for this resource by API Platform
- // >>> Issue with the json typed PublicStructure::addresses properties
- // $this->assertMatchesResourceCollectionJsonSchema($resourceClass);
- }
- }
|