承接 cornermonkey/phpunit-conditional-assertions 相关项目开发

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

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

cornermonkey/phpunit-conditional-assertions

最新稳定版本:0.1.0

Composer 安装命令:

composer require cornermonkey/phpunit-conditional-assertions

包简介

PHPUnit test case extension allowing for conditional assertions to be perofmred.

README 文档

README

This library adds the ability conditionally call any exiting assertions for PHPUnit. Inspiration for this library came from Pest's conditional expectations and the desire to have the similar functionality in PHPUnit.

Author and copyright

Tim Lawson tim@lawson.fyi
This library is MIT-licensed.

Installation

$ composer require --dev cornermonkey/phpunit-conditional-assertions

Compatibility

This package is compatible with PHP 8.0 and later, and PHPUnit 8.0 and later.

Usage

Simply use the trait CornerMonkey\ConditionalAssertions\ConditionalAssertionTrait in your test case. This trait adds an when and unless methods to your test case.

Example:

<?php
use CornerMonkey\ConditionalAssertion\ConditionalAssertionTrait;
use PHPUnit\Framework\TestCase;

class MyTestCase extends TestCase
{
  use ConditionalAssertionTrait;

  public function testConditionIsValid()
  {
    
    $this->when(true)->assertThat(true, true);
    $this->when(false)->assertThat(true, true);    // This assertion will not be called
    $this->unless(true)->assertThat(true, true);   // This assertion will not be called
    $this->unless(false)->assertThat(true, true);
    
  }
}

You can also pass a callback to the when and unless methods. The supplied callback will be called when the condition is true.

<?php
use CornerMonkey\ConditionalAssertion\ConditionalAssertionTrait;
use PHPUnit\Framework\TestCase;

class MyTestCase extends TestCase
{
  use ConditionalAssertionTrait;
   
  public function dataProvider()
  {
    return [
      [true],
      [false]
    ];
  }
  /** 
    * @dataProvider dataProvider
    */
  public function testIfExceptionShouldBeThrown($shouldThrow)
  {
    $this->when($shouldThrow, function(TestCase $testCase, $value) {
      $testCase->expectException(Exception::class);
    });
    
    if ($shouldThrow) {
      throw new Exception();
    }
  }
}

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-11-09