bentools/doctrine-static

Doctrine Static - 在没有数据库的情况下,使用简单数组模拟管理器和存储库。

dev-master / 1.0.x-dev 2018-04-10 14:38 UTC

This package is auto-updated.

Last update: 2024-09-06 09:34:35 UTC


README

这个库旨在用于在单元测试中模拟ObjectManagers和Repositories的行为。

当然,它并不打算用作实际的持久化系统。

主要功能

  • 创建不同的ObjectManager并将它们存储在ManagerRegistry
  • 像往常一样使用$objectManager->persist()$objectManager->remove()$objectManager->flush()(在调用flush()之前,对象存储在临时数组中)
  • 使用存储库方法,如find()findBy()findOneBy()findAll()

不支持的功能

  • 与UnitofWork相关的所有内容
  • 类元数据
  • mergedetachrefresh、...

示例用法

namespace App\Entity;

class Foo
{

    private $id;
    private $name;

    /**
     * Foo constructor.
     * @param $id
     * @param $name
     */
    public function __construct($id, $name)
    {
        $this->id = $id;
        $this->name = $name;
    }

    /**
     * @return mixed
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @return mixed
     */
    public function getName()
    {
        return $this->name;
    }

}
use App\Entity\Foo;
use BenTools\DoctrineStatic\ManagerRegistry;
use BenTools\DoctrineStatic\ObjectManager;
use BenTools\DoctrineStatic\ObjectRepository;

require_once __DIR__ . '/vendor/autoload.php';

$foo = new Foo(2, 'foo');

$doctrine = new ManagerRegistry([
    'default' => new ObjectManager([
            new ObjectRepository(Foo::class)
        ]
    ),
]);

$em = $doctrine->getManager();
$em->persist($foo);
$em->flush();

// Retrieve object by id
$em->find(Foo::class, 2);

// Retrieve objects with name matching "foo"
$em->getRepository(Foo::class)->findBy(['name' => 'foo']);

安装

composer require --dev benools/doctrine-static:1.0.x-dev

测试

./vendor/bin/phpunit

许可证

MIT