LimeTesterArray.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. /*
  3. * This file is part of the Lime framework.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  6. * (c) Bernhard Schussek <bernhard.schussek@symfony-project.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. class LimeTesterArray extends LimeTester implements ArrayAccess, Iterator
  12. {
  13. protected
  14. $type = 'array';
  15. public function is(LimeTesterInterface $expected)
  16. {
  17. if (!$expected instanceof LimeTesterArray || $this->getType() !== $expected->getType())
  18. {
  19. throw new LimeAssertionFailedException($this, $expected);
  20. }
  21. $remaining = $this->value;
  22. foreach ($expected as $key => $value)
  23. {
  24. if (!isset($this[$key]))
  25. {
  26. throw new LimeAssertionFailedException($this, $expected->dumpExcerpt($key, $value));
  27. }
  28. try
  29. {
  30. $this[$key]->is($value);
  31. }
  32. catch (LimeAssertionFailedException $e)
  33. {
  34. throw new LimeAssertionFailedException($this->dumpExcerpt($key, $e->getActual()), $expected->dumpExcerpt($key, $e->getExpected()));
  35. }
  36. unset($remaining[$key]);
  37. }
  38. foreach ($remaining as $key => $value)
  39. {
  40. throw new LimeAssertionFailedException($this->dumpExcerpt($key, $value), $expected);
  41. }
  42. }
  43. public function isnt(LimeTesterInterface $expected)
  44. {
  45. if (!$expected instanceof LimeTesterArray || $this->getType() !== $expected->getType())
  46. {
  47. return;
  48. }
  49. foreach ($expected as $key => $value)
  50. {
  51. if (!isset($this[$key]))
  52. {
  53. return;
  54. }
  55. try
  56. {
  57. $this[$key]->isnt($value);
  58. return;
  59. }
  60. catch (LimeAssertionFailedException $e)
  61. {
  62. }
  63. }
  64. throw new LimeAssertionFailedException($this, $expected);
  65. }
  66. public function same(LimeTesterInterface $expected)
  67. {
  68. if (!$expected instanceof LimeTesterArray || $this->getType() !== $expected->getType())
  69. {
  70. throw new LimeAssertionFailedException($this, $expected);
  71. }
  72. for ($expected->rewind(), $this->rewind(); $expected->valid(); $expected->next(), $this->next())
  73. {
  74. if (!$this->valid())
  75. {
  76. throw new LimeAssertionFailedException($this, $expected->dumpExcerpt($expected->key(), $expected->current()));
  77. }
  78. if ($this->key() != $expected->key())
  79. {
  80. throw new LimeAssertionFailedException($this->dumpExcerpt(key($this->value), current($this->value)), $expected->dumpExcerpt($expected->key(), $expected->current()));
  81. }
  82. try
  83. {
  84. $this->current()->same($expected->current());
  85. }
  86. catch (LimeAssertionFailedException $e)
  87. {
  88. throw new LimeAssertionFailedException($this->dumpExcerpt($this->key(), $e->getActual()), $expected->dumpExcerpt($expected->key(), $e->getExpected()));
  89. }
  90. }
  91. if ($this->valid())
  92. {
  93. throw new LimeAssertionFailedException($this->dumpExcerpt($this->key(), $this->current()), $expected);
  94. }
  95. }
  96. public function isntSame(LimeTesterInterface $expected)
  97. {
  98. if (!$expected instanceof LimeTesterArray || $this->getType() !== $expected->getType())
  99. {
  100. return;
  101. }
  102. for ($expected->rewind(), $this->rewind(); $expected->valid(); $expected->next(), $this->next())
  103. {
  104. if (!$this->valid() || $this->key() !== $expected->key())
  105. {
  106. return;
  107. }
  108. try
  109. {
  110. $this->current()->isntSame($expected->current());
  111. }
  112. catch (LimeAssertionFailedException $e)
  113. {
  114. throw new LimeAssertionFailedException($this->dumpExcerpt($this->key(), $e->getActual()), $expected->dumpExcerpt($expected->key(), $e->getExpected()));
  115. }
  116. }
  117. }
  118. public function contains(LimeTesterInterface $expected)
  119. {
  120. foreach ($this as $key => $value)
  121. {
  122. try
  123. {
  124. $value->is($expected);
  125. return;
  126. }
  127. catch (LimeAssertionFailedException $e)
  128. {
  129. }
  130. }
  131. throw new LimeAssertionFailedException($this->dumpAll(), $expected);
  132. }
  133. public function containsNot(LimeTesterInterface $expected)
  134. {
  135. foreach ($this as $key => $value)
  136. {
  137. $equal = true;
  138. try
  139. {
  140. $value->is($expected);
  141. }
  142. catch (LimeAssertionFailedException $e)
  143. {
  144. $equal = false;
  145. }
  146. if ($equal)
  147. {
  148. throw new LimeAssertionFailedException($this->dumpAll(), $expected);
  149. }
  150. }
  151. }
  152. public function __toString()
  153. {
  154. return $this->dumpExcerpt();
  155. }
  156. protected function getType()
  157. {
  158. return 'array';
  159. }
  160. protected function dumpAll()
  161. {
  162. $result = $this->getType().' (';
  163. if (!empty($this->value))
  164. {
  165. $result .= "\n";
  166. foreach ($this->value as $k => $v)
  167. {
  168. $result .= sprintf(" %s => %s,\n", var_export($k, true), $this->indent($v));
  169. }
  170. }
  171. $result .= ')';
  172. return $result;
  173. }
  174. protected function dumpExcerpt($key = null, $value = null)
  175. {
  176. $result = $this->getType().' (';
  177. if (!empty($this->value))
  178. {
  179. $truncated = false;
  180. $result .= "\n";
  181. foreach ($this->value as $k => $v)
  182. {
  183. if ((is_null($key) || $key != $k) && !$truncated)
  184. {
  185. $result .= " ...\n";
  186. $truncated = true;
  187. }
  188. else if ($k == $key)
  189. {
  190. $value = is_null($value) ? $v : $value;
  191. $result .= sprintf(" %s => %s,\n", var_export($k, true), $this->indent($value));
  192. $truncated = false;
  193. }
  194. }
  195. }
  196. $result .= ')';
  197. return $result;
  198. }
  199. protected function indent($lines)
  200. {
  201. $lines = explode("\n", $lines);
  202. foreach ($lines as $key => $line)
  203. {
  204. $lines[$key] = ' '.$line;
  205. }
  206. return trim(implode("\n", $lines));
  207. }
  208. public function offsetGet($key)
  209. {
  210. return LimeTester::create($this->value[$key]);
  211. }
  212. public function offsetExists($key)
  213. {
  214. return array_key_exists($key, $this->value);
  215. }
  216. public function offsetSet($key, $value)
  217. {
  218. throw new BadMethodCallException('This method is not supported');
  219. }
  220. public function offsetUnset($key)
  221. {
  222. throw new BadMethodCallException('This method is not supported');
  223. }
  224. public function current()
  225. {
  226. return $this[$this->key()];
  227. }
  228. public function key()
  229. {
  230. return key($this->value);
  231. }
  232. public function next()
  233. {
  234. next($this->value);
  235. }
  236. public function valid()
  237. {
  238. return $this->key() !== null;
  239. }
  240. public function rewind()
  241. {
  242. reset($this->value);
  243. }
  244. }