- Notifications
You must be signed in to change notification settings - Fork5
Provides utils to create a test infrastructure for Doctrine ORM entities.
License
webfactory/doctrine-orm-test-infrastructure
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
This library provides some infrastructure for tests of Doctrine ORM entities, featuring:
- configuration of a SQLite in memory database, compromising well between speed and a database environment being bothrealistic and isolated
- a mechanism for importing fixtures into your database that circumvents Doctrine's caching. This results in a morerealistic test environment when loading entities from a repository.
We use it to test Doctrine repositories and entities in Symfony applications. It's alightweight alternative to theheavyweightfunctional tests suggested in the Symfony documentation(we don't suggest you should skip those - we just want to open another path).
In non-application bundles, where functional tests are not possible,it is our only way to test repositories and entities.
Install via composer (seehttp://getcomposer.org/):
composer require --dev webfactory/doctrine-orm-test-infrastructure<?phpuseDoctrine\ORM\EntityManagerInterface;useEntity\MyEntity;useEntity\MyEntityRepository;usePHPUnit\Framework\TestCase;useWebfactory\Doctrine\ORMTestInfrastructure\ORMInfrastructure;class MyEntityRepositoryTestextends TestCase{privateORMInfrastructure$infrastructure;privateMyEntityRepository$repository;protectedfunctionsetUp():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(). */publicfunctiontestFindAllRetrievesFixtures():void {$myEntityFixture =newMyEntity();$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. */publicfunctiontestSomeFancyThingWithEntityManager():void {$entityManager =$this->infrastructure->getEntityManager();// ... }}
In versions 1.x of this library, theORMInfrastructure::createWithDependenciesFor() andORMInfrastructure::createOnlyFor() methodsby default assume that the Doctrine ORM mapping is provided through annotations. Annotations-based configuration is no supported anymore in ORM 3.0.
To allow for a seamless transition towards attribute-based or other types of mapping, a mapping driver can be passedwhen creating instances of theORMInfrastructure.
If you wish to switch to attribute-based mappings, pass anew \Doctrine\ORM\Mapping\Driver\AttributeDriver($paths),where$paths is an array of directory paths where your entity classes are stored.
For hybrid (annotations and attributes) mapping configurations, you can use\Doctrine\Persistence\Mapping\Driver\MappingDriverChain.Multiple mapping drivers can be registered on the driver chain by providing namespace prefixes. For every namespace prefix,only one mapping driver can be used.
Starting in version 2.0.0, attributes-based mapping will be the default.
After installing the dependencies managed via composer, just run
vendor/bin/phpunitfrom the library's root folder. This uses the shipped phpunit.xml.dist - feel free to create your own phpunit.xml if youneed local changes.
Happy testing!
Please note that apart from anyopen issues in this library, youmay stumble upon any Doctrine issues. Especially take care ofit'sknown sqlite issues.
This package was first written by webfactory GmbH (Bonn, Germany) and receivedcontributionsfrom other people since then.
webfactory is a software development agency with a focus on PHP (mostlySymfony).If you're a developer looking for new challenges, we'd like to hear from you!
Copyright 2012 – 2024 webfactory GmbH, Bonn. Code released underthe MIT license.
About
Provides utils to create a test infrastructure for Doctrine ORM entities.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Contributors5
Uh oh!
There was an error while loading.Please reload this page.