matthiasnoback/doctrine-orm-test-service-provider

Service provider for tests in need of a Doctrine entity manager

v2.0.0 2015-01-20 18:16 UTC

README

Build Status

This library contains a service provider to be used with a service container for PHPUnit tests.

Usage

Extend your test class from Noback\PHPUnitTestServiceContainer\PHPUnit\AbstractTestCaseWithEntityManager. You then need to implement the getEntityClasses() which should return an array of entity class names.

For each test method a connection to an SQLite database will be available. Also the schema for the given entities will be created automatically.

<?php

use Noback\PHPUnitTestServiceContainer\PHPUnit\AbstractTestCaseWithEntityManager;

class StorageTest extends AbstractTestCaseWithEntityManager
{
    protected function getEntityClasses()
    {
        return array(
            'Noback\PHPUnitTestServiceContainer\Tests\PHPUnit\Entity\User'
        );
    }

    /**
     * @test
     */
    public function it_persists_an_entity()
    {
        $user = new User();
        $user->setName('Matthias');

        $this->getEntityManager()->persist($user);
        $this->getEntityManager()->flush();
    }
}

Of course, you would usually inject the entity manager into some object which is the subject-under-test.

To register Doctrine event listeners/subscribers, get the EventManager instance by calling $this->getEventManager(). To get the database Connection object, call $this->getConnection().

Read more