inspirum/phpunit-extension 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

inspirum/phpunit-extension

最新稳定版本:v1.1.0

Composer 安装命令:

composer require --dev inspirum/phpunit-extension

包简介

PHPUnit extension with additional assertions

README 文档

README

Latest Stable Version Build Status Coverage Status Quality Score PHPStan Total Downloads Software License

PHPUnit extension with additional assertions and other helpers.

  • Support some deprecated functionality
  • Easy to implement

Features

Supports deprecated assertion method withConsecutive

Before PHPUnit 10.0

use PHPUnit\Framework\TestCase;

final class CalculatorTest extends TestCase
{
    public function testMultiply(): void
    {
        $mock = $this->createMock(Calculator::class);
        
        $arguments = [[1,2,3], [4,5,6]]
        $responses = [6, 120]
        
        $mock->expects($this->exactly(count($arguments)))->method('multiply')
            ->withConsecutive(...$arguments)
            ->willReturnOnConsecutiveCalls(...$responses);
            
        // ... test
    }
}

After PHPUnit 10.0

use Inspirum\PHPUnit\Extension;
use PHPUnit\Framework\TestCase;

final class CalculatorTest extends TestCase
{
    use Extension;
    
    public function testMultiply(): void
    {
        $mock = $this->createMock(Calculator::class);
        
        $arguments = [[1,2,3], [4,5,6]]
        $responses = [6, 120]
        
        $mock->expects($this->exactly(count($arguments)))->method('multiply')
            ->will(self::withConsecutive($arguments, $responses));
            
        // ... test
    }
}

System requirements

Installation

Run composer require command

$ composer require inspirum/phpunit-extension

or add requirement to your composer.json

"inspirum/phpunit-extension": "^1.0"

Usage

Validate arguments and responses:

$mock->expects($this->exactly(2))->method('example')
    ->will(self::withConsecutive(
        arguments: [
            [1, 2, 0.1],
            [2, 3, 0.01],
        ], 
        responses: [
            true,
            false,
        ],
    ));

self::assertTrue($mock->example(1, 2, 0.1));
self::assertFalse($mock->example(2, 3, 0.01));

Optional responses:

$mock->expects($this->exactly(2))->method('example')
    ->will(self::withConsecutive(
        arguments: [
            [1, 2, 0.1],
            [2, 3, 0.01],
        ], 
    ));

$mock->example(1, 2, 0.1);
$mock->example(2, 3, 0.01);

Simplification for same response for each call

$mock->expects($this->exactly(2))->method('example')
    ->will(self::withConsecutive(
        arguments: [
            [1, 2, 0.1],
            [2, 3, 0.01],
        ], 
        responses: true,
    ));

self::assertTrue($mock->example(1, 2, 0.1));
self::assertTrue($mock->example(2, 3, 0.01));

Supports throwing exceptions:

$mock->expects($this->exactly(2))->method('example')
    ->will(self::withConsecutive(
        arguments: [
            [1, 2, 0.1],
            [2, 3, 0.01],
        ], 
        responses: [
           true,
           new RuntimeException('Custom error'),
        ],
    ));

self::assertTrue($mock->example(1, 2, 0.1));

try {
    $mock->example(2, 3, 0.01);
} catch (RuntimeException $exception) {
    self::assertSame('Custom error', $exception->getMessage());
}

Testing

To run unit tests, run:

$ composer test:test

To show coverage, run:

$ composer test:coverage

Contributing

Please see CONTRIBUTING and CODE_OF_CONDUCT for details.

Security

If you discover any security related issues, please email tomas.novotny@inspirum.cz instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

统计信息

  • 总下载量: 401
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 1
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-05-17

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固