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

Question

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

Answers 0
Added at 2016-12-30 01:12
Tags
Question

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);
    }
}
Answers to

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

Source Show
◀ Wstecz