Skip to content
Toche Camille edited this page Nov 23, 2017 · 6 revisions

Peridot does not include any mocking tools out of the box. There are some great tools out there already, and Peridot integrates easily with both of them:

Peridot can make good use of Prophecy via the Prophecy plugin

Prophecy plugin

The Prophecy plugin mixes in a getProphet() method into test scopes

describe('Bird', function() {
    it('should fly', function() {
        $mock = $this->getProphet()->prophesize('Bird');
        //do stuff with the mock
    });
});

In addition to adding the getProphet() method to tests, the Prophecy plugin will automatically inject a mock object if a test or suite description is a known class:

describe('Vendor\Namespace\Klass', function() {
    it('should have a subject', function() {
        $instance = $this->subject->reveal();
        assert($instance instanceof Klass, 'should be instance of Klass');
    });
});

Checking post conditions in afterEach()

We can set up expectations for our mocks in test definitions and then verify weather or not those conditions were met in via afterEach():

describe('Bird', function() {
    afterEach(function() {
        $this->getProphet()->checkPredictions();
    });

    it('should fly and then land', function() {
        $this->subject->fly()->shouldBeCalled();
        $this->subject->land()->shouldBeCalled();
    });

    it('should fly', function() {
        $this->subject->fly()->shouldBeCalled();
        $this->subject->reveal()->fly();
    });
});



class Bird
{
    protected $flying = false;

    public function call()
    {
        return 'tweet';
    }

    public function fly()
    {
        $this->flying = true;
    }

    public function land()
    {
        $this->flying = false;
    }
}
Clone this wiki locally