Is possible to check how many times collaborator's static method was called in PhpSpec?

91 views Asked by At

What I'm trying to achieve is:

  1. to check if factory method was called on ContextItem
  2. to check if provided entity exists in the collection

Questions:

  1. how to provide specific entity to factory method of collaborator?
  2. how to check how many times ContextItem static factory's method was called?

I'm using laravel with phpspec extension: https://github.com/BenConstable/phpspec-laravel

The problem is that ->push method on Collection collaborator is not being executed from which phpspec is returning.

class ContextSpec extends ObjectBehavior
{
    function it_builds_a_collection_of_context_items(ContextItem $contextItem, Collection $collector)
    {
        $item = ContextItem::factory(1,2);
        $items = [1, 2, 3];

        $collector->push($contextItem)->shouldBeCalledTimes(3);
        $this->beConstructedThrough("factory", [$items]);

        // $collector->push($contextItem)->willReturn($item);

        // $contextItem->beConstructedThrough("factory", [0,1])->shouldBeCalled();

        // $contextItem::factory(1,2)->shouldBeCalledTimes(3);
        // $contextItem->factory()->shouldBeCalledTimes(3);

        $this->items()->shouldHaveCount(3);
        $this->items()->shouldContain($item);
    }
}

Context class:

class Context implements ContextInterface
{
    /**
     * @var Collection
     */
    private $_items;

    public static function factory($entries) {

        $items = collect([]);

        collect($entries)->each(function($value, $key) use ($items) {
            $items->push(ContextItem::factory($key, $value));
        });

        return new static($items);
    }
}
0

There are 0 answers