boukeversteegh/phpunit-bliss

此包已被弃用,并且不再维护。没有建议的替代包。

PhpUnit 断言集合

dev-master 2015-11-25 18:41 UTC

This package is not auto-updated.

Last update: 2022-03-30 12:18:09 UTC


README

安装

composer require boukeversteegh/phpunit-bliss dev-master

使用

use \PhpUnitBliss\Assertions; 添加到测试类中

assertArrayMatches

参见 tests/AssertionsTest.php 以获取多个示例。

向您的 TestCase 添加有用的断言:assertArrayMatches,允许您测试给定的数组是否与某个子集匹配。您可以按值或按其他断言进行匹配。

class AssertionsTest extends \PHPUnit_Framework_TestCase
{
    use \PhpUnitBliss\Assertions;

    public function testSimpleExample()
    {
        $array = [
            'id' => 1,
            'name' => 'John',
            'preferences' => [
                'vegetables' => true,
                'beer' => false,
            ],
        ];

        $pattern = [
            'name' => 'John',
            'preferences' => [
                'vegetables' => true,
            ],
        ];

        $this->assertArrayMatches($array, $pattern);

        $notPattern = [
            'name' => 'John',
            'preferences' => [
                'beer' => true,
            ],
        ];

        $this->assertArrayNotMatches($array, $notPattern);
    }

    /**
     * You can use constraints to match values inside arrays
     */
    public function testComplexExample()
    {
        $array = [
            'id' => 1,
            'age' => 25,
            'friends' => [
                [
                    'name' => 'Sally',
                ]
            ],
            'other' => 'some field that is ignored',
            'tree' => [
                'subtree' => ['foo', 'bar', 'baz']
            ],
        ];

        $pattern = [
            'id' => self::anything(),
            'age' => self::greaterThan(18),
            'friends' => self::contains(['name' => 'Sally']),
            'tree' => [
                'subtree' => self::logicalAnd(
                    self::countOf(3),
                    self::arrayMatches([1 => 'bar'])
                )
            ],
        ];

        $this->assertArrayMatches($array, $pattern);
    }
}