PostCheepController test

This commit is contained in:
theUniC
2022-08-05 22:04:30 +02:00
parent be01918ee7
commit d801b987b0
7 changed files with 78 additions and 7 deletions

View File

@@ -6,6 +6,7 @@
bootstrap="tests/bootstrap.php"
executionOrder="random"
defaultTestSuite="UnitTests"
verbose="true"
>
<coverage processUncoveredFiles="true">
<include>

View File

@@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
namespace App\Controller;
use App\Dto\CheepDto;
use Cheeper\Application\CheepApplicationService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
final class GetCheepController extends AbstractController
{
public function __construct(
private readonly CheepApplicationService $cheepApplicationService
) {
}
#[Route("/cheeps/{id}", methods: [Request::METHOD_GET])]
public function __invoke(string $id): Response
{
return $this->json(
CheepDto::assembleFrom(
$this->cheepApplicationService->getCheep($id)
)
);
}
}

View File

@@ -51,4 +51,9 @@ final class CheepApplicationService
return $this->cheepRepository->ofFollowingPeopleOf($author, $offset, $size);
}
public function getCheep(string $id): Cheep
{
return $this->cheepRepository->ofId(CheepId::fromString($id));
}
}

View File

@@ -8,7 +8,6 @@ use Cheeper\DomainModel\Author\Author;
use Cheeper\DomainModel\Cheep\Cheep;
use Cheeper\DomainModel\Cheep\CheepId;
use Cheeper\DomainModel\Cheep\CheepRepository;
use DateTimeInterface;
use Doctrine\ORM\EntityManagerInterface;
use Ramsey\Uuid\Uuid;
@@ -42,8 +41,8 @@ SELECT c
FROM Cheeper\DomainModel\Cheep\Cheep c
JOIN Cheeper\DomainModel\Follow\Follow f WITH f.toAuthorId = c.authorId
WHERE f.fromAuthorId = :fromAuthorId
OR c.authorId = :fromAuthorId
ORDER BY c.cheepDate.date DESC
DQL;
$query = $this->em->createQuery($dql);

View File

@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace App\Tests\Controller;
use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\ApiTestCase;
use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\Client;
use Faker\Factory as FakerFactory;
use Symfony\Component\HttpFoundation\Request;
use function Functional\pluck;
@@ -48,11 +49,7 @@ final class GetTimelineControllerTest extends ApiTestCase
$this->makeRandomizedCheep($client, $fifthAuthorData['userName']);
// Get timeline
$client->request(Request::METHOD_GET, "/authors/${firstAuthorData['id']}/timeline");
$this->assertResponseIsSuccessful();
$timeline = $client->getResponse()->toArray();
$timeline = $this->getAuthorTimeline($client, $firstAuthorData['id']);
$this->assertIsArray($timeline);

View File

@@ -71,4 +71,13 @@ trait HelperFunctions
return $client->getResponse()->toArray();
}
private function getAuthorTimeline(Client $client, string $authorId): array
{
$client->request(Request::METHOD_GET, "/authors/${authorId}/timeline");
$this->assertResponseIsSuccessful();
return $client->getResponse()->toArray();
}
}

View File

@@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
namespace App\Tests\Controller;
use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\ApiTestCase;
use Symfony\Component\HttpFoundation\Request;
final class PostCheepControllerTest extends ApiTestCase
{
use HelperFunctions;
/** @test */
public function itPostsACheep(): void
{
$client = self::createClient();
$autorData = $this->createAuthorWithRandomizedData($client);
$cheepData = $this->makeRandomizedCheep($client, $autorData['userName']);
$client->request(Request::METHOD_GET, "/cheeps/${cheepData['id']}");
$this->assertResponseIsSuccessful();
$data = $client->getResponse()->toArray();
$this->assertEquals($cheepData, $data);
}
}