matthiasnoback/doctrine-orm-test-service-provider

需要 Doctrine 实体管理器的测试用例的服务提供程序

v3.0.5 2021-07-08 12:37 UTC

This package is auto-updated.

Last update: 2024-09-08 19:37:03 UTC


README

Build Status

此库包含一个服务提供程序,用于与 PHPUnit 测试服务容器 一起使用。

用法

在您的测试类中使用特质 Noback\PHPUnitTestServiceContainer\PHPUnit\TestCaseWithEntityManager。然后您需要实现 getEntityDirectories(),它应该返回一个包含应加载的实体的目录数组。

对于每个测试方法,将有一个连接到 SQLite 数据库。同时,给定实体的模式也将自动创建。

<?php

use PHPUnit\Framework\TestCase;
use Noback\PHPUnitTestServiceContainer\PHPUnit\TestCaseWithEntityManager;

class StorageTest extends TestCase 
{
    use TestCaseWithEntityManager;
    
    protected function getEntityDirectories(): array
    {
        return array(
            __DIR__ . '/Entity'
        );
    }

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

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

当然,您通常会向受测试对象注入实体管理器。

要注册 Doctrine 事件监听器/订阅者,通过调用 $this->getEventManager() 获取 EventManager 实例。要获取数据库 Connection 对象,调用 $this->getConnection()

阅读更多