承接 kenjis/phpunit-helper 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

kenjis/phpunit-helper

Composer 安装命令:

composer require kenjis/phpunit-helper

包简介

Provides helper traits for PHPUnit

关键字:

README 文档

README

Provides helper traits for PHPUnit.

Table of Contents

Requirements

  • PHP 7.3 or later

Installation

Run:

$ composer require --dev kenjis/phpunit-helper

Usage

TestDouble

This trait provides helper methods to create mock objects and to verify invocations.

Import the Kenjis\PhpUnitHelper\TestDouble trait into your test class:

<?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()

param type description
$classname string class name
$params array [method_name => return_value]
[[method_name => [return_value1, return_value2 [, ...]]]
$constructor_params false/array false: disable constructor / array: constructor params

returns (object) PHPUnit mock object.

Gets PHPUnit mock object.

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

You could write code above like below:

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

You can set Closure as the return value of a mocked method.

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

You can also set the mock itself as the return value of a mocked method with using $this->returnSelf().

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

You can create mocks with consecutive calls.

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

You could write code above like below:

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

$this->verifyInvoked()

param type description
$mock object PHPUnit mock object
$method string method name
$params array arguments

Verifies a method was invoked at least once.

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

You could write code above like below:

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

$this->verifyInvokedOnce()

param type description
$mock object PHPUnit mock object
$method string method name
$params array arguments

Verifies that method was invoked only once.

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

You could write code above like below:

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

$this->verifyInvokedMultipleTimes()

param type description
$mock object PHPUnit mock object
$method string method name
$times int times
$params array arguments

Verifies that method was called exactly $times times.

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

You could write code above like below:

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

$this->verifyNeverInvoked()

param type description
$mock object PHPUnit mock object
$method string method name
$params array arguments

Verifies that method was not called.

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

You could write code above like below:

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

ReflectionHelper

This trait provides helper methods to access private or protected properties and methods.

But generally it is not recommended testing non-public properties or methods, so think twice before you use the methods in this trait.

Import the Kenjis\PhpUnitHelper\ReflectionHelper trait into your test class:

<?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()

param type description
$obj object/string object / class name
$property string property name

returns (mixed) property value.

Gets private or protected property value.

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

$this->setPrivateProperty()

param type description
$obj object/string object / class name
$property string property name
$value mixed value

Sets private or protected property value.

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

$this->getPrivateMethodInvoker()

param type description
$obj object/string object / class name
$method string method name

returns (closure) method invoker.

Gets private or protected method invoker.

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

DebugHelper

This trait provides helper functions, dd() and d() to dump variables.

Import the Kenjis\PhpUnitHelper\DebugHelper trait into your test class:

<?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;
}

License

This package is licensed using the MIT License.

Please have a look at LICENSE.

kenjis/phpunit-helper 适用场景与选型建议

kenjis/phpunit-helper 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 13.81k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2021 年 02 月 01 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「phpunit」 「mock」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 kenjis/phpunit-helper 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 kenjis/phpunit-helper 我们能提供哪些服务?
定制开发 / 二次开发

基于 kenjis/phpunit-helper 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 13.81k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 4
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-02-01