51systems/doctrine-test

此包已被 废弃 且不再维护。未建议替代包。

Doctrine2 测试库

0.1.2 2013-10-11 00:44 UTC

This package is not auto-updated.

Last update: 2023-11-12 05:23:25 UTC


README

这是一个基本的 Doctrine2 测试库,用于与 PHPUnit 一起使用。

#贡献者

特性

  • 用于测试的 SQLite 内存数据库
  • 支持固定文件
  • 支持订阅者

安装

通过 composer 安装。

编写测试

namespace Application\Test\Entity;

class MyModelTest extends EntityTestCase
{
    //If you need to set the application module configuration, because you are
    //not using a custom bootloader for tests, then use the trait
    use ServiceLocatorAwareTestTrait;
    
    public function setUp()
    {
        // Load the database schemas
        $this->loadSchemas(array('Entity\MyEntity')); // Load as many needed for the tests

        // Optionally, you can load fixtures
        $this->loadFixture('DataFixtures\ORM\MyFixture');

        // You can also load subscribers, like registering sluggable, timestampable etc behaviour
        //$myListener = new Listener\MyListener();
        //$this->addLifecycleEventSubscriber($myListener);
        
        //If using the ServiceLocatorAwareTestTrait
        $configOverrides = [];
        $this->setApplicationConfig(ArrayUtils::merge(
            include __DIR__ . '/PATH TO YOUR APPLICATION CONFIG/application.config.php',
            $configOverrides
        ));
    }

    public function testCreate()
    {
        // Get the entity manager for managing persistence etc.
        $em = $this->getEntityManager();

        // Test a create
        $myEntity = new Entity\MyEntity;
        $myEntity->setTitle('Hello World');
        $em->persist();

        // Test we issued SQL to the database
        $this->assertEquals(1, $this->getQueryCount(), 'Should have executed one query to the database');

        // Test the generation of an ID
        $this->assertNotEmpty($myEntity->getId(), 'Should have got an ID for my entity');

    }
    
    protected function getServiceLocator() {
        return \Application\Test\Bootstrap::getServiceManager();
    }
}