Search code examples
laravellaravel-4phpspec

PHPSpec how to ask if a method returns either this or that


I have a method which queries the database and either returns an array with the results or false if there are no results.

All I need PHPSpec to do in this case is test whether it returns an array or false, but I can't work out how to do that.

Or do I need to mock the database query and separate it out of my method?


Solution

  • You're not showing any code so we can work with, but if current matchers doesn't work for you, you can create new ones:

    function it_should_return_array_or_false()
    {
        $this->getOptions()->shouldBeArrayOrFalse();
    }
    
    public function getMatchers()
    {
        return [
            'beArrayOrFalse' => function($subject, $value) {
                return is_array($value) || $value === false;
            },
        ];
    }