ArrayEntryTokenSpec.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. namespace spec\Prophecy\Argument\Token;
  3. use PhpSpec\ObjectBehavior;
  4. use Prophecy\Argument;
  5. use Prophecy\Argument\Token\ExactValueToken;
  6. use Prophecy\Argument\Token\TokenInterface;
  7. use Prophecy\Exception\InvalidArgumentException;
  8. class ArrayEntryTokenSpec extends ObjectBehavior
  9. {
  10. function let(TokenInterface $key, TokenInterface $value)
  11. {
  12. $this->beConstructedWith($key, $value);
  13. }
  14. function it_implements_TokenInterface()
  15. {
  16. $this->shouldBeAnInstanceOf('Prophecy\Argument\Token\TokenInterface');
  17. }
  18. function it_is_not_last()
  19. {
  20. $this->shouldNotBeLast();
  21. }
  22. function it_holds_key_and_value($key, $value)
  23. {
  24. $this->getKey()->shouldBe($key);
  25. $this->getValue()->shouldBe($value);
  26. }
  27. function its_string_representation_tells_that_its_an_array_containing_the_key_value_pair($key, $value)
  28. {
  29. $key->__toString()->willReturn('key');
  30. $value->__toString()->willReturn('value');
  31. $this->__toString()->shouldBe('[..., key => value, ...]');
  32. }
  33. function it_wraps_non_token_value_into_ExactValueToken(TokenInterface $key, \stdClass $object)
  34. {
  35. $this->beConstructedWith($key, $object);
  36. $this->getValue()->shouldHaveType('\Prophecy\Argument\Token\ExactValueToken');
  37. }
  38. function it_wraps_non_token_key_into_ExactValueToken(\stdClass $object, TokenInterface $value)
  39. {
  40. $this->beConstructedWith($object, $value);
  41. $this->getKey()->shouldHaveType('\Prophecy\Argument\Token\ExactValueToken');
  42. }
  43. function it_scores_array_half_of_combined_scores_from_key_and_value_tokens($key, $value)
  44. {
  45. $key->scoreArgument('key')->willReturn(4);
  46. $value->scoreArgument('value')->willReturn(6);
  47. $this->scoreArgument(array('key'=>'value'))->shouldBe(5);
  48. }
  49. function it_scores_traversable_object_half_of_combined_scores_from_key_and_value_tokens(
  50. TokenInterface $key,
  51. TokenInterface $value,
  52. \Iterator $object
  53. ) {
  54. $object->current()->will(function () use ($object) {
  55. $object->valid()->willReturn(false);
  56. return 'value';
  57. });
  58. $object->key()->willReturn('key');
  59. $object->rewind()->willReturn(null);
  60. $object->next()->willReturn(null);
  61. $object->valid()->willReturn(true);
  62. $key->scoreArgument('key')->willReturn(6);
  63. $value->scoreArgument('value')->willReturn(2);
  64. $this->scoreArgument($object)->shouldBe(4);
  65. }
  66. function it_throws_exception_during_scoring_of_array_accessible_object_if_key_is_not_ExactValueToken(
  67. TokenInterface $key,
  68. TokenInterface $value,
  69. \ArrayAccess $object
  70. ) {
  71. $key->__toString()->willReturn('any_token');
  72. $this->beConstructedWith($key,$value);
  73. $errorMessage = 'You can only use exact value tokens to match key of ArrayAccess object'.PHP_EOL.
  74. 'But you used `any_token`.';
  75. $this->shouldThrow(new InvalidArgumentException($errorMessage))->duringScoreArgument($object);
  76. }
  77. function it_scores_array_accessible_object_half_of_combined_scores_from_key_and_value_tokens(
  78. ExactValueToken $key,
  79. TokenInterface $value,
  80. \ArrayAccess $object
  81. ) {
  82. $object->offsetExists('key')->willReturn(true);
  83. $object->offsetGet('key')->willReturn('value');
  84. $key->getValue()->willReturn('key');
  85. $key->scoreArgument('key')->willReturn(3);
  86. $value->scoreArgument('value')->willReturn(1);
  87. $this->scoreArgument($object)->shouldBe(2);
  88. }
  89. function it_accepts_any_key_token_type_to_score_object_that_is_both_traversable_and_array_accessible(
  90. TokenInterface $key,
  91. TokenInterface $value,
  92. \ArrayIterator $object
  93. ) {
  94. $this->beConstructedWith($key, $value);
  95. $object->current()->will(function () use ($object) {
  96. $object->valid()->willReturn(false);
  97. return 'value';
  98. });
  99. $object->key()->willReturn('key');
  100. $object->rewind()->willReturn(null);
  101. $object->next()->willReturn(null);
  102. $object->valid()->willReturn(true);
  103. $this->shouldNotThrow(new InvalidArgumentException)->duringScoreArgument($object);
  104. }
  105. function it_does_not_score_if_argument_is_neither_array_nor_traversable_nor_array_accessible()
  106. {
  107. $this->scoreArgument('string')->shouldBe(false);
  108. $this->scoreArgument(new \stdClass)->shouldBe(false);
  109. }
  110. function it_does_not_score_empty_array()
  111. {
  112. $this->scoreArgument(array())->shouldBe(false);
  113. }
  114. function it_does_not_score_array_if_key_and_value_tokens_do_not_score_same_entry($key, $value)
  115. {
  116. $argument = array(1 => 'foo', 2 => 'bar');
  117. $key->scoreArgument(1)->willReturn(true);
  118. $key->scoreArgument(2)->willReturn(false);
  119. $value->scoreArgument('foo')->willReturn(false);
  120. $value->scoreArgument('bar')->willReturn(true);
  121. $this->scoreArgument($argument)->shouldBe(false);
  122. }
  123. function it_does_not_score_traversable_object_without_entries(\Iterator $object)
  124. {
  125. $object->rewind()->willReturn(null);
  126. $object->next()->willReturn(null);
  127. $object->valid()->willReturn(false);
  128. $this->scoreArgument($object)->shouldBe(false);
  129. }
  130. function it_does_not_score_traversable_object_if_key_and_value_tokens_do_not_score_same_entry(
  131. TokenInterface $key,
  132. TokenInterface $value,
  133. \Iterator $object
  134. ) {
  135. $object->current()->willReturn('foo');
  136. $object->current()->will(function () use ($object) {
  137. $object->valid()->willReturn(false);
  138. return 'bar';
  139. });
  140. $object->key()->willReturn(1);
  141. $object->key()->willReturn(2);
  142. $object->rewind()->willReturn(null);
  143. $object->next()->willReturn(null);
  144. $object->valid()->willReturn(true);
  145. $key->scoreArgument(1)->willReturn(true);
  146. $key->scoreArgument(2)->willReturn(false);
  147. $value->scoreArgument('foo')->willReturn(false);
  148. $value->scoreArgument('bar')->willReturn(true);
  149. $this->scoreArgument($object)->shouldBe(false);
  150. }
  151. function it_does_not_score_array_accessible_object_if_it_has_no_offset_with_key_token_value(
  152. ExactValueToken $key,
  153. \ArrayAccess $object
  154. ) {
  155. $object->offsetExists('key')->willReturn(false);
  156. $key->getValue()->willReturn('key');
  157. $this->scoreArgument($object)->shouldBe(false);
  158. }
  159. function it_does_not_score_array_accessible_object_if_key_and_value_tokens_do_not_score_same_entry(
  160. ExactValueToken $key,
  161. TokenInterface $value,
  162. \ArrayAccess $object
  163. ) {
  164. $object->offsetExists('key')->willReturn(true);
  165. $object->offsetGet('key')->willReturn('value');
  166. $key->getValue()->willReturn('key');
  167. $value->scoreArgument('value')->willReturn(false);
  168. $key->scoreArgument('key')->willReturn(true);
  169. $this->scoreArgument($object)->shouldBe(false);
  170. }
  171. function its_score_is_capped_at_8($key, $value)
  172. {
  173. $key->scoreArgument('key')->willReturn(10);
  174. $value->scoreArgument('value')->willReturn(10);
  175. $this->scoreArgument(array('key'=>'value'))->shouldBe(8);
  176. }
  177. }