entityManager = static::getContainer()->get(EntityManagerInterface::class); } // Auxiliary Methods /** * Defines the HTTP method that will be used in the requests made by the tests. * * @return string The HTTP method to be used for the request * (e.g., 'GET', 'POST', 'PUT', 'DELETE'). */ abstract protected function getMethod(): string; protected function getUri(int $id = -1): string { $param = $id < 0 ? '' : "/$id"; return self::BASE_URI.$param; } protected function createTestUser( string $handle = 'test_user', string $email = self::USER_EMAIL, ): User { $user = new User(); $user->setHandle($handle); $user->setEmail($email); $passwordHasher = static::getContainer()->get('security.user_password_hasher'); $user->setPassword($passwordHasher->hashPassword($user, self::USER_PASSWORD)); return $user; } protected function prepareTestUser(?User $user = null): void { $user ??= $this->createTestUser(); $this->entityManager->persist($user); $this->entityManager->flush(); } protected function getExampleProjectData(): array { return [ 'id' => 1, 'title' => 'Test Project', 'subtitle' => 'Test Project Subtitle', 'category' => Category::LibreSoftware, 'territory' => ['country' => 'ES'], 'description' => 'Test Project Description', 'deadline' => ProjectDeadline::Minimum, 'status' => ProjectStatus::InEditing, ]; } protected function createTestProject(): Project { $example = $this->getExampleProjectData(); $project = new Project(); $project->setTitle($example['title']); $project->setSubtitle($example['subtitle']); $project->setCategory($example['category']); $project->setTerritory(new ProjectTerritory($example['territory']['country'])); $project->setDescription($example['description']); $project->setDeadline($example['deadline']); $project->setOwner($this->createTestUser()); $project->setStatus($example['status']); return $project; } protected function prepareTestProject(?Project $project = null): void { $project ??= $this->createTestProject(); $this->entityManager->persist($project); $this->entityManager->flush(); } protected function getValidToken(Client $client): string { $client->request( 'POST', '/v4/user_tokens', [ 'json' => [ 'identifier' => self::USER_EMAIL, 'password' => self::USER_PASSWORD, ], ] ); return json_decode($client->getResponse()->getContent(), true)['token']; } protected function getHeaders(Client $client): array { return [ 'Authorization' => 'Bearer '.$this->getValidToken($client), 'Content-Type' => 'application/json', ]; } // Auxiliary Tests protected function testOneNotFound(): void { $this->prepareTestProject(); $client = static::createClient(); $client->request( $this->getMethod(), $this->getUri(999), ['headers' => $this->getHeaders($client)] ); $this->assertResponseStatusCodeSame(Response::HTTP_NOT_FOUND); } protected function testInsert( array $data, string $uri = self::BASE_URI, int $expectedCode = Response::HTTP_OK, ): void { $client = static::createClient(); $client->request( $this->getMethod(), $uri, ['headers' => $this->getHeaders($client), 'json' => $data] ); $this->assertResponseStatusCodeSame($expectedCode); } protected function testInvalidToken( string $uri, string $contentType = 'application/json', int $expectedToken = Response::HTTP_UNAUTHORIZED, ): void { static::createClient()->request( $this->getMethod(), $uri, ['headers' => [ 'Authorization' => 'Bearer invalid_token', 'Content-Type' => $contentType, ]] ); $this->assertResponseStatusCodeSame($expectedToken); } protected function testForbidden(): void { $otherUser = $this->createTestUser('other_user', 'otheruser@example.com'); $otherProject = $this->createTestProject()->setOwner($otherUser); $this->entityManager->persist($otherProject); $this->prepareTestUser(); $client = static::createClient(); $client->request( $this->getMethod(), $this->getUri(1), ['headers' => $this->getHeaders($client)] ); $this->assertResponseStatusCodeSame(Response::HTTP_FORBIDDEN); } }