Skip to content

Commit

Permalink
🏕️ Move persisting of entity to test factory
Browse files Browse the repository at this point in the history
By doing this we need less code to arrange our entities while
writing functional tests.
  • Loading branch information
LVoogd committed Nov 24, 2024
1 parent f43da3a commit 68680d5
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 40 deletions.
18 changes: 14 additions & 4 deletions src-dev/Feed/Factory/ArticleFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Feed\Domain\Article\Url\Url;
use App\Feed\Domain\Source\Source;
use DateTime;
use Doctrine\ORM\EntityManagerInterface;
use Ramsey\Uuid\Uuid;

final class ArticleFactory
Expand All @@ -18,7 +19,7 @@ final class ArticleFactory
private DateTime $updated;
private Source $source;

private function __construct()
private function __construct(private readonly ?EntityManagerInterface $entityManager)
{
$faker = \Faker\Factory::create();

Expand All @@ -30,9 +31,9 @@ private function __construct()
$this->source = SourceFactory::setup()->create();
}

public static function setup(): self
public static function setup(?EntityManagerInterface $entityManager = null): self
{
return new self();
return new self($entityManager);
}

public function withSource(Source $source): self
Expand All @@ -51,13 +52,22 @@ public function withUpdated(DateTime $updated): self

public function create(): Article
{
return new Article(
$article = new Article(
$this->id,
$this->title,
$this->summary,
$this->url,
$this->updated,
$this->source,
);

if ($this->entityManager == null) {
return $article;
}

$this->entityManager->persist($article);
$this->entityManager->flush();

return $article;
}
}
2 changes: 1 addition & 1 deletion src/Feed/Domain/Article/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Article
#[ORM\Column(type: 'datetime')]
private DateTime $updated;

#[ORM\ManyToOne(targetEntity: Source::class)]
#[ORM\ManyToOne(targetEntity: Source::class, cascade: ['persist'])]
private Source $source;

public function __construct(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,24 +65,10 @@ public function it_should_return_an_empty_list_if_there_are_no_articles(): void
public function it_should_return_the_latest_articles_paginated(): void
{
// Arrange
$article = ArticleFactory::setup()->withUpdated(new \DateTime('2023-10-10 8:00:00'))->create();
$article2 = ArticleFactory::setup()->withUpdated(new \DateTime('2012-01-5 15:30:00'))->create();
$article3 = ArticleFactory::setup()->withUpdated(new \DateTime('2012-01-5 15:30:01'))->create();
$article4 = ArticleFactory::setup()->withUpdated(new \DateTime('2012-01-4 15:30:00'))->create();

$this->getDoctrine()->persist($article->getSource());
$this->getDoctrine()->persist($article);

$this->getDoctrine()->persist($article2->getSource());
$this->getDoctrine()->persist($article2);

$this->getDoctrine()->persist($article3->getSource());
$this->getDoctrine()->persist($article3);

$this->getDoctrine()->persist($article4->getSource());
$this->getDoctrine()->persist($article4);

$this->getDoctrine()->flush();
$article = ArticleFactory::setup($this->getDoctrine())->withUpdated(new \DateTime('2023-10-10 8:00:00'))->create();
$article2 = ArticleFactory::setup($this->getDoctrine())->withUpdated(new \DateTime('2012-01-5 15:30:00'))->create();
$article3 = ArticleFactory::setup($this->getDoctrine())->withUpdated(new \DateTime('2012-01-5 15:30:01'))->create();
$article4 = ArticleFactory::setup($this->getDoctrine())->withUpdated(new \DateTime('2012-01-4 15:30:00'))->create();

// Act
$response = $this->operation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,10 @@ protected function setUp(): void
public function it_should_return_the_latest_articles_in_a_sorted_manner(): void
{
// Arrange
$source = SourceFactory::setup()->create();
$this->getDoctrine()->getManager()->persist($source);

$article1 = ArticleFactory::setup()->withSource($source)->withUpdated(new DateTime('2021-03-10 00:10:00'))->create();
$article2 = ArticleFactory::setup()->withSource($source)->withUpdated(new DateTime('2020-03-10 00:00:00'))->create();
$article3 = ArticleFactory::setup()->withSource($source)->withUpdated(new DateTime('2021-03-10 00:00:00'))->create();
$article4 = ArticleFactory::setup()->withSource($source)->withUpdated(new DateTime('2020-03-10 00:00:00'))->create();
$article1 = ArticleFactory::setup()->withUpdated(new DateTime('2021-03-10 00:10:00'))->create();
$article2 = ArticleFactory::setup()->withUpdated(new DateTime('2020-03-10 00:00:00'))->create();
$article3 = ArticleFactory::setup()->withUpdated(new DateTime('2021-03-10 00:00:00'))->create();
$article4 = ArticleFactory::setup()->withUpdated(new DateTime('2020-03-10 00:00:00'))->create();

$this->repository->save($article1, $article2, $article3, $article4);
$this->getDoctrine()->resetManager();
Expand All @@ -54,10 +51,7 @@ public function it_should_return_the_latest_articles_in_a_sorted_manner(): void
public function it_should_find_the_article_by_its_url(): void
{
// Arrange
$source = SourceFactory::setup()->create();
$this->getDoctrine()->getManager()->persist($source);

$article = ArticleFactory::setup()->withSource($source)->create();
$article = ArticleFactory::setup()->create();

$this->repository->save($article);
$this->getDoctrine()->resetManager();
Expand Down Expand Up @@ -88,12 +82,9 @@ public function it_should_return_null_if_there_is_no_article_with_given_url(): v
public function it_should_count_the_articles(): void
{
// Arrange
$source = SourceFactory::setup()->create();
$this->getDoctrine()->getManager()->persist($source);

$article1 = ArticleFactory::setup()->withSource($source)->withUpdated(new DateTime('2021-03-10 00:10:00'))->create();
$article2 = ArticleFactory::setup()->withSource($source)->withUpdated(new DateTime('2020-03-10 00:00:00'))->create();
$article3 = ArticleFactory::setup()->withSource($source)->withUpdated(new DateTime('2021-03-10 00:00:00'))->create();
$article1 = ArticleFactory::setup()->withUpdated(new DateTime('2021-03-10 00:10:00'))->create();
$article2 = ArticleFactory::setup()->withUpdated(new DateTime('2020-03-10 00:00:00'))->create();
$article3 = ArticleFactory::setup()->withUpdated(new DateTime('2021-03-10 00:00:00'))->create();

$this->repository->save($article1, $article2, $article3);
$this->getDoctrine()->resetManager();
Expand Down

0 comments on commit 68680d5

Please sign in to comment.