kenjis/phpunit-helper

为PHPUnit提供辅助特性

v1.1.2 2021-09-30 04:10 UTC

This package is auto-updated.

Last update: 2024-08-29 05:41:39 UTC


README

为PHPUnit提供辅助特性。

目录

要求

  • PHP 7.3或更高版本

安装

运行

$ composer require --dev kenjis/phpunit-helper

使用方法

TestDouble

此特性提供创建mock对象和验证调用的辅助方法。

Kenjis\PhpUnitHelper\TestDouble特性导入您的测试类

<?php

declare(strict_types=1);

namespace Foo\Bar\Test\Unit;

use Kenjis\PhpUnitHelper\TestDouble;
use PHPUnit\Framework;

final class BazTest extends Framework\TestCase
{
    use TestDouble;
}

$this->getDouble()

returns (object) PHPUnit mock对象。

获取PHPUnit mock对象。

$email = $this->getMockBuilder(CI_Email::class)
    ->disableOriginalConstructor()
    ->onlyMethods(['send'])
    ->getMock();
$email->method('send')
    ->willReturn(true);

您可以将上述代码编写如下

$email = $this->getDouble(CI_Email::class, ['send' => true]);

您可以将Closure作为模拟方法的返回值。

$ret = function () {
    throw new RuntimeException('Cannot send email!');
};
$mock = $this->getDouble(CI_Email::class, ['send' => $ret]);

您也可以使用$this->returnSelf()将mock本身作为模拟方法的返回值。

$mock = $this->getDouble(CI_Email::class, [
    'to'      => $this->returnSelf(),
    'subject' => $this->returnSelf(),
    'send'    => true,
]);

您可以使用连续调用创建mock。

$mock = $this->getMockBuilder(CI_Email::class)
    ->disableOriginalConstructor()
    ->onlyMethods(['method'])
    ->getMock();
$mock->expects($this->any())->method('method')
    ->will($this->onConsecutiveCalls('GET', 'POST' ,'DELETE'));

您可以将上述代码编写如下

$mock = $this->getDouble(
    CI_Input::class,
    [
        ['method' => ['GET', 'POST' ,'DELETE']],
    ]
);

$this->verifyInvoked()

验证方法至少被调用一次。

$loader->expects($this->atLeastOnce())
    ->method('view')
    ->with(
        'shopConfirm', $this->anything(), true
    );

您可以将上述代码编写如下

$this->verifyInvoked(
    $loader,
    'view',
    [
        'shopConfirm', $this->anything(), true
    ]
);

$this->verifyInvokedOnce()

验证方法只被调用一次。

$loader->expects($this->once())
    ->method('view')
    ->with(
        'shopConfirm', $this->anything(), true
    );

您可以将上述代码编写如下

$this->verifyInvokedOnce(
    $loader,
    'view',
    [
        'shopConfirm', $this->anything(), true
    ]
);

$this->verifyInvokedMultipleTimes()

验证方法正好被调用$times次。

$loader->expects($this->exactly(2))
    ->method('view')
    ->withConsecutive(
        ['shopConfirm', $this->anything(), true],
        ['shopTmplCheckout', $this->anything()]
    );

您可以将上述代码编写如下

$this->verifyInvokedMultipleTimes(
    $loader,
    'view',
    2,
    [
        ['shopConfirm', $this->anything(), true],
        ['shopTmplCheckout', $this->anything()]
    ]
);

$this->verifyNeverInvoked()

验证方法没有被调用。

$loader->expects($this->never())
    ->method('view')
    ->with(
        'shopConfirm', $this->anything(), true
    );

您可以将上述代码编写如下

$this->verifyNeverInvoked(
    $loader,
    'view',
    [
        'shopConfirm', $this->anything(), true
    ]
);

ReflectionHelper

此特性提供访问私有或受保护的属性和方法的辅助方法。

但通常不推荐测试非公共属性或方法,所以在使用此特性中的方法之前请三思。

Kenjis\PhpUnitHelper\ReflectionHelper特性导入您的测试类

<?php

declare(strict_types=1);

namespace Foo\Bar\Test\Unit;

use Kenjis\PhpUnitHelper\ReflectionHelper;
use PHPUnit\Framework;

final class BazTest extends Framework\TestCase
{
    use ReflectionHelper;
}

$this->getPrivateProperty()

returns (mixed) 属性值。

获取私有或受保护的属性值。

$obj = new SomeClass();
$private_propery = $this->getPrivateProperty(
	$obj,
	'privatePropery'
);

$this->setPrivateProperty()

设置私有或受保护的属性值。

$obj = new SomeClass();
$this->setPrivateProperty(
	$obj,
	'privatePropery',
	'new value'
);

$this->getPrivateMethodInvoker()

returns (closure) 方法调用者。

获取私有或受保护的方法调用者。

$obj = new SomeClass();
$method = $this->getPrivateMethodInvoker(
	$obj, 'privateMethod'
);
$this->assertEquals(
	'return value of the privateMethod() method', $method()
);

DebugHelper

此特性提供辅助函数dd()d()来转储变量。

Kenjis\PhpUnitHelper\DebugHelper特性导入您的测试类

<?php

declare(strict_types=1);

namespace Foo\Bar\Test\Unit;

use Kenjis\PhpUnitHelper\DebugHelper;
use PHPUnit\Framework;

final class BazTest extends Framework\TestCase
{
    use DebugHelper;
}

许可

此软件包使用MIT许可证。

请参阅LICENSE