em = self::getContainer()->get(EntityManagerInterface::class); $this->client = static::createClient(); } public function testGetCollectionEducationTiming(): void { $this->loginAs($this->user); $this->assertResponseIsSuccessful(); $this->get('/api/residence_areas'); $this->assertResponseStatusCodeSame(200); $this->assertJsonContains([ '@context' => '/api/contexts/ResidenceArea', '@id' => '/api/residence_areas', '@type' => 'hydra:Collection', 'hydra:totalItems' => 5, 'hydra:member' => [ ["label" => "label"] ], ]); } 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 $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 $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 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 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 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 ); } /** * 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); } }