pccomponentes/ddd-testing

向ddd mini框架添加了测试工具

v1.0.2 2021-03-27 11:28 UTC

This package is auto-updated.

Last update: 2024-08-27 18:58:31 UTC


README

帮助测试开发的工具。

PcComponentes\Ddd\Testing\Util\PhpUnit\IterableMockTrait

允许您通过可迭代模拟对象轻松地在测试方法中引入模拟。

使用

示例

use PcComponentes\Ddd\Testing\Util\PhpUnit\IterableMockTrait;
use PHPUnit\Framework\TestCase;

final class Testing extends TestCase
{
    use IterableMockTrait;

    public function testMethod()
    {
        $mockedItem1 = $this->createMock(\stdClass::class);
        // Assertions in mock
        $mockedItem2 = $this->createMock(\stdClass::class);
        // Assertions in mock

        $mockedIterator = $this->addIterableValuesToMock(
            $this->createMock(\Iterator::class), // Your iterable mock
            [
                $mockedItem1,
                $mockedItem2,
            ],
        );
        // ...
    }
}

PcComponentes\Ddd\Testing\Util\PhpUnit\SerializableMockTrait

允许您简化 \JsonSerializable 模拟的可序列化参数化,使代码更具语义性和易于阅读。

使用

示例

use PcComponentes\Ddd\Testing\Util\PhpUnit\SerializableMockTrait;
use PHPUnit\Framework\TestCase;

final class Testing extends TestCase
{
    use SerializableMockTrait;

    public function testMethod()
    {
        $valueToReturnOnSerialize = 'some compatible with serialization method declared';

        $serializableMock = $this->addJsonSerializationToMock( 
            $this->createMock(\JsonSerializable::class), // Your mock which implements jsonSerialize method 
            $this->once(), // Or other InvocationOrder
            $valueToReturnOnSerialize,
        );
        // ...
    }
}