em = static::getContainer()->get(EntityManagerInterface::class); // Purge DB before populating new fixtures $this->purgeDb(); // Définit les fixtures et flush $this->setFixtures(); $this->em->flush(); // Instancie le client qui exécutera les requêtes à l'api // @see https://symfony.com/doc/current/testing.html#making-requests $this->client = static::createClient(); } /** * Delete all DB records before populating fixtures. * * @return void * @throws \Doctrine\DBAL\Exception */ private function purgeDb() { if (!preg_match('/.*test.*/', $this->em->getConnection()->getDatabase())) { throw new \RuntimeException("The DB name shall contain 'test' in its name to be purge"); } $this->em->getConnection()->exec('SET FOREIGN_KEY_CHECKS = 0;'); $purger = new ORMPurger($this->em); $purger->setPurgeMode(ORMPurger::PURGE_MODE_DELETE); $purger->purge(); $this->em->getConnection()->exec('SET FOREIGN_KEY_CHECKS = 1;'); } /** * Create and persist the fixtures (no need to flush, the setup will perform it later) * * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#same-entities-used-in-these-docs * * @return void */ protected function setFixtures(): void { // TODO: préparer un jeu de fixtures par défaut } /** * Send a requests, parse the hydra response and return an object or a Collection * * @param string $method * @param string $route * @param array $data * @param array $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 GET request and return the response parsed content * * @param string $route * @param array $headers * @return ResponseInterface */ protected function get(string $route, array $headers = []): ResponseInterface { return $this->request( Request::METHOD_GET, $route, null, $headers ); } /** * Send a PUT request and return the response parsed content * * @param string $route * @param array $data * @param array $headers * @return ResponseInterface */ protected function put(string $route, array $data, array $headers = []): ResponseInterface { return $this->request( Request::METHOD_PUT, $route, $data, $headers ); } /** * Send a POST request and return the response parsed content * * @param string $route * @param array $data * @param array $headers * @return ResponseInterface */ protected function post(string $route, array $data, array $headers = []): ResponseInterface { return $this->request( Request::METHOD_POST, $route, $data, $headers ); } /** * Send a DELETE request and return the response parsed content * * @param string $route * @param array $headers * @return ResponseInterface */ protected function delete(string $route, array $headers = []): ResponseInterface { return $this->request( Request::METHOD_DELETE, $route, null, $headers ); } 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); } protected function loginAs(Proxy | Access $access): void { $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; } }