chmeldax/phpmocks

增强PHP类测试的模拟功能

v0.1 2017-03-05 21:09 UTC

This package is not auto-updated.

Last update: 2024-09-18 20:50:50 UTC


README

让模拟成为乐趣!

用于测试类的库.

功能

  • 模拟所有公共类方法
  • 支持抽象类和接口
  • 模拟 静态 方法
  • 根据方法调用参数的不同,模拟具有不同的行为
  • 默认为严格模拟(默认情况下禁用非模拟方法的调用)
  • 流畅直观的接口

要求

  • PHP 5.6+
  • Composer
  • 允许 eval 语言构造

安装

composer require chmeldax/phpmocks

使用方法

存根

当你想替换类的默认行为,但不是测试方法调用本身时,请使用 allowMethodCall()

$doubleBuilder = new \Chmeldax\PhpMocks\Double\Builder($className);
$doubleBuilder
    ->allowMethodCall('methodToBeStubbed')
    ->with(new Constraints\Anything, new Constraints\Anything, null) // Specify allowed parameters
    ->andReturn('return_value_1');
$double = $doubleBuilder->build(); // This is the stub

指定返回值

$doubleBuilder
    ->allowMethodCall('methodToBeStubbed')
    ->with(new Constraints\Anything, new Constraints\Anything, null) // Specify allowed parameters
    ->andReturn('return_value_1', 'return_value_2'); // Returns different values on #1 and #2 call
$doubleBuilder
    ->allowMethodCall('methodToBeStubbed')
    ->with(new Constraints\Anything, new Constraints\Anything, null) // Specify allowed parameters
    ->andInvoke(function ($a, $b, $c) { // specify your own callback
        return 'return_value'
      });
$doubleBuilder = new \Chmeldax\PhpMocks\Double\Builder($instance);
$doubleBuilder
    ->allowMethodCall('methodToBeStubbed')
    ->with(new Constraints\Anything, new Constraints\Anything, null) // Specify allowed parameters
    ->andCallOriginal(); // Calls the original method from $instance

多个with块

$doubleBuilder
    ->allowMethodCall('methodToBeStubbed')
    ->with(new Constraints\Anything, new Constraints\Anything, 'first')
    ->andReturn('return_value_1'); // Returns different values on #1 and #2 call

$doubleBuilder
    ->allowMethodCall('methodToBeStubbed')
    ->with(new Constraints\Anything, new Constraints\Anything, 'second') // Different parameter value
    ->andReturn('return_value_2');

$double = $doubleBuilder->build();
$double->methodToBeStubbed(1, 2, 'first');  // returns 'return_value_1'
$double->methodToBeStubbed(1, 2, 'second'); // returns 'return_value_2'

更多示例,请参阅 tests/DoubleTest.php

模拟

当你正在测试与类的交互时,请使用 expectMethodCall。这将允许你指定预期的调用。

$doubleBuilder
    ->expectMethodCall('methodToBeMocked')
    ->with(new Constraints\Anything, new Constraints\Anything, null)
    ->times(10) // Expectation
    ->andReturn('return_value_1');
$doubleBuilder
    ->expectMethodCall('methodToBeMocked')
    ->with(new Constraints\Anything, new Constraints\Anything, 'first')
    ->atCalls(1, 3) // Expectation for 1st and 3rd call (counted globally for all "with" blocks)
    ->andReturn('return_value_1');

$doubleBuilder
    ->expectMethodCall('methodToBeMocked')
    ->with(new Constraints\Anything, new Constraints\Anything, 'second')
    ->atCall(2) // Expectation for 2nd call (counted globally for all "with" blocks)
    ->andReturn('return_value_2');

检查预期

$doubleBuilder->checkExpectations() // Throws exception if any expectation is not met

建议在您的测试框架中使用 tearDown()(或类似方法)。

更多示例,请参阅 tests/ExpectedMethodTest.php

要实现的事项

  • 支持PHP 7(主要返回值,标量类型)
  • 实现更好的约束
  • 清理代码
  • 引入真正的单元测试