Php 带有回调参数的Mocking方法

Php 带有回调参数的Mocking方法,php,unit-testing,mocking,phpunit,mockery,Php,Unit Testing,Mocking,Phpunit,Mockery,我试图模仿这种方法: $transformer = $this->transformerFactory->createProductTransformer($product, function (ProductInterface $product) use ($discount) { $product->setDiscount($discount); }); 它接受回调参数作为第二个参数,我不知道如何模拟它 我使用的是嘲弄,看起来是这样的: $this->tra

我试图模仿这种方法:

$transformer = $this->transformerFactory->createProductTransformer($product, function (ProductInterface $product) use ($discount) {
    $product->setDiscount($discount);
});
它接受回调参数作为第二个参数,我不知道如何模拟它

我使用的是嘲弄,看起来是这样的:

$this->transformerFactoryMock
    ->shouldReceive('createProductTransformer')
    ->with(???) // here!
如果我将相同的回调传递给with()方法,则实例不匹配。 如果mockry不支持,我不介意使用PHPUnit mocking。

如果“相同的回调”意味着相同的代码,那么它就不是PHP的相同回调,因此mockry不会接受它

var_dump(function () {} === function () {}); // false
$func = function () {};
var_dump($func === $func); // true

要检查回调类型,可以使用mockry::type方法(带有参数“closure”),为了更精确地检查,可以使用mockry::on

很难从粘贴的代码片段中准确地说出您要在测试文件中执行的操作,但是您可以模拟闭包及其参数。下面是一个快速/肮脏/未经测试的示例,它是您试图实现的最佳猜测:

class Transformer {

    protected transformerFactory;

    public function __construct($factory) {
        $this->transformerFactory = $factor;
    }

    public function doSomething(Discount $discount, ProductInterface $product) {

        return $this->transformerFactory->createProductTransformer($product, function($product) use ($discount) {
            $product->setDiscount($discount);
        });
    }

}


class TransformerTest {

    protected function makeTransformerWithFakeFactory()
    {
        $fakeFactory = \Mockery::mock('TransformerFactory');

        $transformer = new Transformer($fakeFactory);

        return array($tranformer, $fakeFactory);
    }

    protected function fakeDiscount()
    {
        return \Mockery::mock('Discount');
    }

    protected function fakeProduct()
    {
        return \Mocker::mock('ProductInterface');
    }

            // first let's test to make sure that the factory's correct method is called with correct parameters
    function test_doSomething_WhenPassedProduct_CallsCreateProductTransformerOnFactoryWithProductAndClosure()
    {
        list($transformer, $mockFactory) = $this->makeTransformerWithFakeFactory();
        $fakeDiscount = $this->fakeDiscount();
        $fakeProduct = $this->fakeProduct();

        $mockFactory->shouldReceive('createProductTransformer')->once()->with($fakeProduct, \Mockery::type('Closure'));

        $transfomer->doSomething($fakeDiscount, $fakeProduct);
    }

            // now, let's test to make sure that the $discount within the closure is called with the right method and params
        function test_doSomething_createProductTransformerCalledWithProductAndClosure_CallsSetDiscountOnProductWithDiscount()
    {
        list($transformer, $stubFactory) = $this->makeTransfomerWithFakeFactory();
        $fakeDiscount = $this->fakeDiscount();
        $mockProduct = $this->fakeProduct();
        $stubFactory->shouldReceive('createProductTransfomer')->passthru();

        $mockProduct->shouldReceive('setDiscount')->once()->with($fakeDiscount);

        $transfomer->doSomething($fakeDiscount, $mockProduct);
    }

            // now lets make sure that doSomething returns what the call to factory's createProductTransformer method returns
    function test_doSomething_createProductTransformerCalledWithProductAndClosureReturnsValue_ReturnsSameValue()
    {
        list($transformer, $stubFactory) = $this->makeTransfomerWithFakeFactory();
        $fakeDiscount = $this->fakeDiscount();
        $fakeProduct = $this->fakeProduct();
        $stubFactory->shouldReceive('createProductTransfomer')->andReturn('transformed result');

        $result = $transfomer->doSomething($fakeDiscount, $fakeProduct);

        $this->assertEquals('transformed result', $result);
    }

}

如果您需要准确地检查是否调用了回调,那么这是不可能的,因为每个
\Closure
实例都是唯一的,并且在您测试的方法中创建了一个新实例

但下一步你可以做:

1) 验证类型的动态参数

// Assume in test case you have these mock/stub
$discount;
$product;

$transformerFactoryMock = \Mockery::mock('TransformerFactory')
    ->shouldReceive('createProductTransformer')
    ->with($product, \Mockery::type(\Closure::class))
2) 检查所有隐藏的参数(在测试方法中调用模拟方法之前创建)

3) 最后,使用传递给mock方法的回调来伪造结果

// then mock will be
$transformerFactoryMock = \Mockery::mock('TransformerFactory')
    ->shouldReceive('createProductTransformer')
    ->andReturnUsing(function($closure) {
        // execute closure
        $closure();

        // any logic to fake return
        return;
    })
检查和的文件

// then mock will be
$transformerFactoryMock = \Mockery::mock('TransformerFactory')
    ->shouldReceive('createProductTransformer')
    ->andReturnUsing(function($closure) {
        // execute closure
        $closure();

        // any logic to fake return
        return;
    })