aeris/spy

1.1.0 2015-08-13 17:14 UTC

This package is not auto-updated.

Last update: 2024-09-18 09:29:24 UTC


README

PHP的测试间谍

安装

composer require aeris/spy

概述

一个 Aeris\Spy 允许你在PHP中模拟 callable。一个 Spy 包围了一个 Mockery\ 对象,这意味着你可以使用Mockery期望与你的Aeris Spies一起使用。

例如

$spy = new Spy();

$spy(5);
$spy(6);
$spy(7);

$spy->shouldHaveBeenCalled()
  ->twice()
  ->with(\Mockery::on(function($arg) {
    return $arg > 5;
  )))

API

shouldHaveBeenCalled() / shouldNotHaveBeenCalled()

$spy = new Spy();

$spy();

$spy->shouldHaveBeenCalled();  // Passes (no exception)
$spy->shouldNotHaveBeenCalled(); // Failed (throws \Mockery\Exception\InvalidCountException)

andReturn($val)

$spy = new Spy();
$spy->andReturn('foo');

$spy();  // 'foo'

andReturnUsing

$spy = new Spy()
$spy->andReturnUsing(function($str) {
  strtoupper($str);
});

$spy('foo');  // 'FOO'

Spy::returns($val);

创建一个返回值的间谍。创建间谍后调用 andReturn 的简写。

$spy = Spy::returns('foo');

$spy(); 	// 'foo'

Spy::returnsUsing($callable);

创建一个通过可调用返回值的间谍。创建间谍后调用 andReturnUsing 的简写。

$spy = Spy::returnsUsing(function($str) {
  strtoupper($str);
});

$spy('foo'); 	// 'FOO'