EzDevInfo.com

prophecy

Highly opinionated mocking framework for PHP 5.3+

Issue with generating a mock for an interface

I'm currently trying to build mocks for an interface (defined here as the Policy class) which only has one method, check; as seen below I'm basically just replacing it with a stub method which always returns a known value:

$mockBuilder = $this->getMockBuilder(Policy::class);
$allowMock = $mockBuilder->getMock();
$allowMock->method('check')->willReturn(Vote::ALLOW);

It registers as an object implementing Policy, as it should, but whenever the check method is called it only ever returns null. What am I doing wrong in my construction here?


Source: (StackOverflow)

Trouble with prophecy and PHP DOM

I've been moving my PHPUnit tests to use PHPSpec's Prophecy library - but I'm getting an odd error when using dummies of PHP DOMDocument:

class MyTest extends PHPUnit_Framework_TestCase {
    function testExample() {
        $inputDocument  = $this->prophesize("DOMDocument")->reveal();
        $outputDocument = $this->prophesize("DOMDocument")->reveal();

        $xsltProcessor = $this->prophesize("XSLTProcessor");
        $xsltProcessor->transformToDoc($inputDocument)->willReturn($outputDocument)->shouldBeCalled();
        $xsltProcessor = $xsltProcessor->reveal();

        $xsltProcessor->transformToDoc($inputDocument);
    }
}

I get an error ErrorException: DOMDocument::loadXML(): Empty string supplied as input

I'm not sure why loadXML is being called - these are supposed to be dummies...


Source: (StackOverflow)

Advertisements

How to mock the same method in Prophecy so it retruns different response in each of its calls

In pure PHPUnit mocking I can do something like this:

$mock->expects($this->at(0))
    ->method('isReady')
    ->will($this->returnValue(false));

$mock->expects($this->at(1))
    ->method('isReady')
    ->will($this->returnValue(true));

I was not able to do the same thing using Prophecy. Is it possible?


Source: (StackOverflow)