Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test PostRepository with Testcontainer #387

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
"dama/doctrine-test-bundle": "^8.0",
"doctrine/doctrine-fixtures-bundle": "^3.4",
"phpunit/phpunit": "^11.0",
"shyim/testcontainer": "^0.1.2",
"symfony/browser-kit": "^7.1",
"symfony/css-selector": "^7.1",
"symfony/maker-bundle": "^1.34",
Expand Down
55 changes: 54 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

80 changes: 80 additions & 0 deletions tests/Repository/PostRepositoryTestWithTestcontainers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace App\Tests\Repository;

use App\Entity\PostFactory;
use App\Repository\PostRepository;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Console\Tester\CommandTester;
use Testcontainer\Container\PostgresContainer;

class PostRepositoryTestWithTestcontainers extends KernelTestCase
{

private EntityManagerInterface $entityManager;

private PostRepository $postRepository;

private PostgresContainer $postgresContainer;

protected function setUp(): void
{
// starting Postgres Container
$this->postgresContainer = PostgresContainer::make('14', 'password');
$this->postgresContainer->withPostgresDatabase('blogdb');
$this->postgresContainer->withPostgresUser('user');
$this->postgresContainer->withPort("5432", "5432");

try {
$this->postgresContainer->run();
} catch (Exception $e) {
var_dump($e->getMessage());
}

// boot the Symfony kernel
$kernel = self::bootKernel();
$this->assertSame('test', $kernel->getEnvironment());
$this->entityManager = $kernel->getContainer()
->get('doctrine')
->getManager();

$application = new Application($kernel);
$command = $application->find('doctrine:schema:create');
$commandTester = new CommandTester($command);
$result = $commandTester->execute(['n']);
var_dump("schema:create result:".$result);

// use static::getContainer() to access the service container
$container = static::getContainer();

//get PostRepository from container.
$this->postRepository = $container->get(PostRepository::class);
}

protected function tearDown(): void
{
parent::tearDown();
$this->entityManager->close();

// stop postgres container
$this->postgresContainer->stop();
}

public function testCreatePost(): void
{
// persist entities with EntityManager
$entity = PostFactory::create("test post", "test content");
$this->entityManager->persist($entity);
$this->entityManager->flush();
$this->assertNotNull($entity->getId());

// query this post by PostRepository
$byId = $this->postRepository->findOneBy(["id" => $entity->getId()]);
$this->assertEquals("test post", $byId->getTitle());
$this->assertEquals("test content", $byId->getContent());
}

}
3 changes: 3 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@
if ($_SERVER['APP_DEBUG']) {
umask(0000);
}

// https://github.com/symfony/symfony/issues/53812#issuecomment-1962311843
Symfony\Component\ErrorHandler\ErrorHandler::register(null, false);