PageRenderTime 55ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/phpspec/prophecy/spec/Prophecy/Argument/Token/ArrayEntryTokenSpec.php

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