danrevah/shortifypunit

v1.1.1 2015-12-24 22:07 UTC

This package is not auto-updated.

Last update: 2024-09-28 15:54:48 UTC


README

Build Status Coverage Status Code Quality Latest Stable Version

PHP 模拟框架,受 Java Mockito 库的启发

目录

安装

需要 PHP 版本 >= 5.4!

以下说明使用 Composer 进行安装。如果您没有 Composer,可以从 https://getcomposer.org.cn/ 下载。

$ composer require "danrevah/shortifypunit":"dev-master" 
$ php composer.phar require "danrevah/shortifypunit":"dev-master"

模拟示例

// Creating a new mock for SimpleClassForMocking
$mock = ShortifyPunit::mock('SimpleClassForMocking');

// Returns NULL, was not stubbed yet
$mock->first_method();

基本模拟示例,如果函数未被存根,返回值将始终为 NULL

存根

// Creating a new mock for SimpleClassForMocking
$mock = ShortifyPunit::mock('SimpleClassForMocking');

// Stubbing first_method() function without arguments
ShortifyPunit::when($mock)->first_method()->returns(1);
echo $mock->first_method(); // prints '1'

// Stubbing first_method() function with arguments
ShortifyPunit::when($mock)->first_method(1,2)->returns(2);
echo $mock->first_method(); // still prints '1'
echo $mock->first_method(1,2); // prints '2'

// Stubbing callback
ShortifyPunit::when($mock)->first_method()->callback(function() { echo 'Foo Bar'; });
echo $mock->first_method(); // prints 'Foo Bar'

// Stubbing throws exception
ShortifyPunit::when($mock)->second_method()->throws(new Exception());
$mock->second_method(); // throws Exception

使用 when 函数来存根具有特定参数的方法,后面跟随一个 throwsreturnscallback 操作。

方法

  • throws($exception) - 抛出异常
  • returns($response) - 返回一个 $response
  • callback(function() { /*...*/ }) - 调用回调

部分模拟

当您需要某些方法正常行为,而除了您需要测试的那个方法之外,可以使用部分模拟来做到这一点,除非您存根了该方法,否则它会保留逻辑。

class Foo {
  function bar() { return 'bar'; }
}

$mock = ShortifyPunit::mock('Foo');
$partialMock = ShortifyPunit::partialMock('Foo');

$mock->bar(); // returns NULL
echo $partialMock->bar(); // prints 'bar'

ShortifyPunit::when($partialMock)->bar()->returns('foo'); // stubbing partialMock
echo $partialMock->bar(); // prints 'foo'

存根方法链

 // Creating a new mock for SimpleClassForMocking
 $mock = ShortifyPunit::mock('SimpleClassForMocking');

  ShortifyPunit::when($mock)->first_method()->second_method(1)->returns(1);
  ShortifyPunit::when($mock)->first_method()->second_method(2)->returns(2);
  ShortifyPunit::when($mock)->first_method(1)->second_method(1)->returns(3);
  ShortifyPunit::when($mock)->first_method(2)->second_method(2)->third_method()->returns(4);
  
  echo $mock->first_method()->second_method(1); // prints '1'
  echo $mock->first_method()->second_method(2); // prints '2'
  echo $mock->first_method(1)->second_method(1); // prints '3'
  echo $mock->first_method(2)->second_method(2)->third_method(); // prints '4'

when 函数也用于存根链式方法,遵循与单个函数存根相同的操作 returnthrowcallback

验证

创建后,模拟将记住所有调用。然后您可以选择性地验证您插入的某些交互。

    $mock = ShortifyPunit::mock('SimpleClassForMocking');

    ShortifyPunit::when($mock)->first_method()->returns(1);
    echo $mock->first_method(); // method called once

    ShortifyPunit::verify($mock)->first_method()->neverCalled(); // returns FALSE
    ShortifyPunit::verify($mock)->first_method()->atLeast(2); // returns FALSE
    ShortifyPunit::verify($mock)->first_method()->calledTimes(1); // returns TRUE

    echo $mock->first_method(); // method has been called twice

    ShortifyPunit::verify($mock)->first_method()->neverCalled(); // returns FALSE
    ShortifyPunit::verify($mock)->first_method()->atLeast(2); // returns TRUE
    ShortifyPunit::verify($mock)->first_method()->calledTimes(2); // returns TRUE

方法

  • atLeast($times) - 验证至少调用 $times 次
  • atLeastOnce() - atLeast(1) 的别名
  • calledTimes($times) - 验证正好调用 $times 次
  • neverCalled() - calledTimes(0) 的别名
  • lessThan($times) - 验证调用少于 $times 次

参数匹配器

ShortifyPunit 允许使用 Hamcrest PHP (https://github.com/hamcrest/hamcrest-php) 匹配器对任何参数进行匹配。Hamcrest 是一个“匹配函数”库,给定一个值,如果该值匹配某些规则,则返回 true。

默认包含 Hamcrest 匹配器。

示例

class Foo
{
  function bar($arg){}
}

$stub = ShortifyPunit::mock('Foo');
ShortifyPunit::when($stub)->bar(anything())->return('FooBar');

一些常见的 Hamcrest 匹配器

  • 核心
    • anything - 总是匹配,如果您不在乎正在测试的对象是什么,则很有用
  • 逻辑
    • allOf - 如果所有匹配器都匹配,则匹配,短路(类似于 PHP &&)
    • anyOf - 如果任何匹配器匹配,则匹配,短路(类似于 PHP ||)
    • not - 如果包装的匹配器不匹配,则匹配,反之亦然
  • 对象
    • equalTo - 使用 == 操作符测试对象相等性
    • anInstanceOf - 测试类型
    • notNullValuenullValue - 测试为 null
  • 数字
    • closeTo - 测试浮点值接近给定的值
    • greaterThangreaterThanOrEqualTolessThanlessThanOrEqualTo - 测试排序
  • 文本
    • equalToIgnoringCase - 测试字符串忽略大小写的相等性
    • equalToIgnoringWhiteSpace - 测试字符串忽略空白字符的差异
    • containsStringendsWithstartsWith - 测试字符串匹配