2017 © Pedro Peláez
 

library doctrine-orm-test-infrastructure

Provides utils to create a test infrastructure for Doctrine 2 entities.

image

webfactory/doctrine-orm-test-infrastructure

Provides utils to create a test infrastructure for Doctrine 2 entities.

  • Tuesday, October 17, 2017
  • by webfactory
  • Repository
  • 11 Watchers
  • 12 Stars
  • 6,596 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 1 Forks
  • 3 Open issues
  • 28 Versions
  • 14 % Grown

The README.md

doctrine-orm-test-infrastructure

Tests, (*1)

This library provides some infrastructure for tests of Doctrine ORM entities, featuring:, (*2)

  • configuration of a SQLite in memory database, compromising well between speed and a database environment being both realistic and isolated
  • a mechanism for importing fixtures into your database that circumvents Doctrine's caching. This results in a more realistic test environment when loading entities from a repository.

We use it to test Doctrine repositories and entities in Symfony applications. It's a lightweight alternative to the heavyweight functional tests suggested in the Symfony documentation (we don't suggest you should skip those - we just want to open another path)., (*3)

In non-application bundles, where functional tests are not possible, it is our only way to test repositories and entities., (*4)

Installation

Install via composer (see http://getcomposer.org/):, (*5)

composer require --dev webfactory/doctrine-orm-test-infrastructure

Usage

<?php

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();
        // ...
    }
}

Migrating to attribute-based mapping configuration (with version 1.x)

In versions 1.x of this library, the ORMInfrastructure::createWithDependenciesFor() and ORMInfrastructure::createOnlyFor() methods by default assume that the Doctrine ORM mapping is provided through annotations. Annotations-based configuration is no supported anymore in ORM 3.0., (*6)

To allow for a seamless transition towards attribute-based or other types of mapping, a mapping driver can be passed when creating instances of the ORMInfrastructure., (*7)

If you wish to switch to attribute-based mappings, pass a new \Doctrine\ORM\Mapping\Driver\AttributeDriver($paths), where $paths is an array of directory paths where your entity classes are stored., (*8)

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., (*9)

Starting in version 2.0.0, attributes-based mapping will be the default., (*10)

Testing the library itself

After installing the dependencies managed via composer, just run, (*11)

vendor/bin/phpunit

from the library's root folder. This uses the shipped phpunit.xml.dist - feel free to create your own phpunit.xml if you need local changes., (*12)

Happy testing!, (*13)

Known Issues

Please note that apart from any open issues in this library, you may stumble upon any Doctrine issues. Especially take care of it's known sqlite issues., (*14)

This package was first written by webfactory GmbH (Bonn, Germany) and received contributions from other people since then., (*15)

webfactory is a software development agency with a focus on PHP (mostly Symfony). If you're a developer looking for new challenges, we'd like to hear from you!, (*16)

Copyright 2012 – 2024 webfactory GmbH, Bonn. Code released under the MIT license., (*17)

The Versions

17/10 2017
23/06 2017
14/06 2017

dev-bugfix/23_separate_entity_manager

dev-bugfix/23_separate_entity_manager

Provides utils to create a test infrastructure for Doctrine 2 entities.

  Sources   Download

MIT

The Requires

 

The Development Requires

orm testing doctrine

15/05 2017
15/05 2017

dev-feature/file-databases

dev-feature/file-databases

Provides utils to create a test infrastructure for Doctrine 2 entities.

  Sources   Download

MIT

The Requires

 

The Development Requires

orm testing doctrine

12/05 2017
12/05 2017

dev-bugfix/interface_associations

dev-bugfix/interface_associations

Provides utils to create a test infrastructure for Doctrine 2 entities.

  Sources   Download

MIT

The Requires

 

The Development Requires

orm testing doctrine

28/04 2017
28/04 2017

dev-bugfix/20_entity_imported_twice_on_cascade

dev-bugfix/20_entity_imported_twice_on_cascade

Provides utils to create a test infrastructure for Doctrine 2 entities.

  Sources   Download

MIT

The Requires

 

The Development Requires

orm testing doctrine

21/03 2017
21/03 2017

dev-bugfix/entities_with_custom_annotations

dev-bugfix/entities_with_custom_annotations

Provides utils to create a test infrastructure for Doctrine 2 entities.

  Sources   Download

MIT

The Requires

 

The Development Requires

orm testing doctrine

21/03 2017

dev-feature/check_entity_class

dev-feature/check_entity_class

Provides utils to create a test infrastructure for Doctrine 2 entities.

  Sources   Download

MIT

The Requires

 

The Development Requires

orm testing doctrine

21/03 2017

dev-feature/php_version_cleanup

dev-feature/php_version_cleanup

Provides utils to create a test infrastructure for Doctrine 2 entities.

  Sources   Download

MIT

The Requires

 

The Development Requires

orm testing doctrine

21/03 2017

dev-bugfix/avoid_circular_reference

dev-bugfix/avoid_circular_reference

Provides utils to create a test infrastructure for Doctrine 2 entities.

  Sources   Download

MIT

The Requires

 

The Development Requires

orm testing doctrine

07/09 2015

dev-bugfix/11_expose_only_simulated_entity_classes

dev-bugfix/11_expose_only_simulated_entity_classes

Provides utils to create a test infrastructure for Doctrine 2 entities.

  Sources   Download

MIT

The Requires

 

The Development Requires

orm testing doctrine

04/09 2015

dev-bugfix/9_detect_inheritance_dependencies

dev-bugfix/9_detect_inheritance_dependencies

Provides utils to create a test infrastructure for Doctrine 2 entities.

  Sources   Download

MIT

The Requires

 

The Development Requires

orm testing doctrine

19/08 2015

dev-feature/annotation_reader_performance

dev-feature/annotation_reader_performance

Provides utils to create a test infrastructure for Doctrine 2 entities.

  Sources   Download

MIT

The Requires

 

The Development Requires

orm testing doctrine

01/08 2015

dev-feature/benchmarks

dev-feature/benchmarks

Provides utils to create a test infrastructure for Doctrine 2 entities.

  Sources   Download

MIT

The Requires

 

The Development Requires

orm testing doctrine

05/06 2015

1.2.0

1.2.0.0

Provides utils to create a test infrastructure for Doctrine 2 entities.

  Sources   Download

MIT

The Requires

 

The Development Requires

orm testing doctrine

05/06 2015

dev-feature/4-automatically-determine-and-setup-dependencies

dev-feature/4-automatically-determine-and-setup-dependencies

Provides utils to create a test infrastructure for Doctrine 2 entities.

  Sources   Download

MIT

The Requires

 

The Development Requires

orm testing doctrine

20/02 2015

1.1.0

1.1.0.0

Provides utils to create a test infrastructure for Doctrine 2 entities.

  Sources   Download

MIT

The Requires

 

The Development Requires

orm testing doctrine

26/09 2014

1.0.1

1.0.1.0

Provides utils to create a test infrastructure for Doctrine 2 entities.

  Sources   Download

MIT

The Requires

 

The Development Requires

orm testing doctrine

09/09 2014

1.0.0

1.0.0.0

Provides utils to create a test infrastructure for Doctrine 2 entities.

  Sources   Download

MIT

The Requires

 

The Development Requires

orm testing doctrine