PHP code example of webfactory / doctrine-orm-test-infrastructure
1. Go to this page and download the library: Download webfactory/doctrine-orm-test-infrastructure library. Choose the download type require. 2. Extract the ZIP file and open the index.php. 3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
webfactory / doctrine-orm-test-infrastructure example snippets
use Doctrine\ORM\EntityManagerInterface;
use Entity\MyEntity;
use Entity\MyEntityRepository;
use PHPUnit\Framework\TestCase;
use Webfactory\Doctrine\ORMTestInfrastructure\ORMInfrastructure;
class MyEntityRepositoryTest extends TestCase
{
private ORMInfrastructure $infrastructure;
private MyEntityRepository $repository;
protected function setUp(): void
{
/*
This will create an in-memory SQLite database with the necessary schema
for the MyEntity entity class and and everything reachable from it through
associations.
*/
$this->infrastructure = ORMInfrastructure::createWithDependenciesFor(MyEntity::class);
$this->repository = $this->infrastructure->getRepository(MyEntity::class);
}
/**
* Example test: Asserts imported fixtures are retrieved with findAll().
*/
public function testFindAllRetrievesFixtures(): void
{
$myEntityFixture = new MyEntity();
$this->infrastructure->import($myEntityFixture);
$entitiesLoadedFromDatabase = $this->repository->findAll();
/*
import() will use a dedicated entity manager, so imported entities do not
end up in the identity map. But this also means loading entities from the
database will create _different object instances_.
So, this does not hold:
*/
// self::assertContains($myEntityFixture, $entitiesLoadedFromDatabase);
// But you can do things like this (you probably want to extract that in a convenient assertion method):
self::assertCount(1, $entitiesLoadedFromDatabase);
$entityLoadedFromDatabase = $entitiesLoadedFromDatabase[0];
self::assertSame($myEntityFixture->getId(), $entityLoadedFromDatabase->getId());
}
/**
* Example test for retrieving Doctrine's entity manager.
*/
public function testSomeFancyThingWithEntityManager(): void
{
$entityManager = $this->infrastructure->getEntityManager();
// ...
}
}