Search code examples
phpunit-testingbddmockeryphpspec

Using PHPSpec with array?


I've been starting to use PHPSpec 2, and am loving it with Mockery, but ran into an issue I can't figure out.

I have an array returned by a static function, and I want to validate this array. Make sure all the keys needed are there, etc.

I tried:

$systems = CacheFactory::getCacheSystems();
$systems->shouldBeArray();

As well as:

$systems = CacheFactory::getCacheSystems();
$this->spec($systems)->shouldBeArray();

But neither worked. The first for obvious reasons, error'd saying $systems wasn't an object. The second error'd saying I couldn't serialize a closure. There are closures in the array (it's a config array), but I even tried filtering those out, to no avail.

Array example:

array(
'someCache' => array(
    'cache' => 'SomeCacheSystem',
    'checks' => function () { return isCacheActivated(); }
),

Is this possible with PHPSpec 2 currently? Am I just missing something stupid, I am pretty new to the framework.


Solution

  • I assume CacheFactory is your subject under specification:

    <?php
    
    namespace spec;
    
    use PhpSpec\ObjectBehavior;
    use Prophecy\Argument;
    
    class CacheFactorySpec extends ObjectBehavior
    {
        function it_has_all_the_keys_needed()
        {
            $this::getCacheSystems()->shouldBeArray();
        }
    }