1<?php
2
3namespace spec\Prophecy\Argument\Token;
4
5use PhpSpec\ObjectBehavior;
6use Prophecy\Argument\Token\TokenInterface;
7
8class ArrayEveryEntryTokenSpec extends ObjectBehavior
9{
10    function let(TokenInterface $value)
11    {
12        $this->beConstructedWith($value);
13    }
14
15    function it_implements_TokenInterface()
16    {
17        $this->shouldBeAnInstanceOf('Prophecy\Argument\Token\TokenInterface');
18    }
19
20    function it_is_not_last()
21    {
22        $this->shouldNotBeLast();
23    }
24
25    function it_holds_value($value)
26    {
27        $this->getValue()->shouldBe($value);
28    }
29
30    function its_string_representation_tells_that_its_an_array_containing_only_value($value)
31    {
32        $value->__toString()->willReturn('value');
33        $this->__toString()->shouldBe('[value, ..., value]');
34    }
35
36    function it_wraps_non_token_value_into_ExactValueToken(\stdClass $stdClass)
37    {
38        $this->beConstructedWith($stdClass);
39        $this->getValue()->shouldHaveType('Prophecy\Argument\Token\ExactValueToken');
40    }
41
42    function it_does_not_score_if_argument_is_neither_array_nor_traversable()
43    {
44        $this->scoreArgument('string')->shouldBe(false);
45        $this->scoreArgument(new \stdClass)->shouldBe(false);
46    }
47
48    function it_does_not_score_empty_array()
49    {
50        $this->scoreArgument(array())->shouldBe(false);
51    }
52
53    function it_does_not_score_traversable_object_without_entries(\Iterator $object)
54    {
55        $object->rewind()->willReturn(null);
56        $object->next()->willReturn(null);
57        $object->valid()->willReturn(false);
58        $this->scoreArgument($object)->shouldBe(false);
59    }
60
61    function it_scores_avg_of_scores_from_value_tokens($value)
62    {
63        $value->scoreArgument('value1')->willReturn(6);
64        $value->scoreArgument('value2')->willReturn(3);
65        $this->scoreArgument(array('value1', 'value2'))->shouldBe(4.5);
66    }
67
68    function it_scores_false_if_entry_scores_false($value)
69    {
70        $value->scoreArgument('value1')->willReturn(6);
71        $value->scoreArgument('value2')->willReturn(false);
72        $this->scoreArgument(array('value1', 'value2'))->shouldBe(false);
73    }
74
75    function it_does_not_score_array_keys($value)
76    {
77        $value->scoreArgument('value')->willReturn(6);
78        $value->scoreArgument('key')->shouldNotBeCalled(0);
79        $this->scoreArgument(array('key' => 'value'))->shouldBe(6);
80    }
81
82    function it_scores_traversable_object_from_value_token(TokenInterface $value, \Iterator $object)
83    {
84        $object->current()->will(function ($args, $object) {
85            $object->valid()->willReturn(false);
86
87            return 'value';
88        });
89        $object->key()->willReturn('key');
90        $object->rewind()->willReturn(null);
91        $object->next()->willReturn(null);
92        $object->valid()->willReturn(true);
93        $value->scoreArgument('value')->willReturn(2);
94        $this->scoreArgument($object)->shouldBe(2);
95    }
96}
97