ArrayHasSameValuesConstraint.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. /*
  3. * This file is part of the Predis package.
  4. *
  5. * (c) Daniele Alessandri <suppakilla@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. use \PHPUnit_Framework_Constraint;
  11. use \PHPUnit_Framework_ExpectationFailedException;
  12. /**
  13. * Constraint that accepts arrays with the same elements but different order.
  14. */
  15. class ArrayHasSameValuesConstraint extends PHPUnit_Framework_Constraint
  16. {
  17. protected $array;
  18. /**
  19. * @param array $array
  20. */
  21. public function __construct($array)
  22. {
  23. $this->array = $array;
  24. }
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public function evaluate($other)
  29. {
  30. if (count($this->array) !== count($other)) {
  31. throw new PHPUnit_Framework_ExpectationFailedException('Failed asserting that two arrays have the same elements.');
  32. }
  33. if (array_diff($this->array, $other)) {
  34. throw new PHPUnit_Framework_ExpectationFailedException('Failed asserting that two arrays have the same elements.');
  35. }
  36. return true;
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function toString()
  42. {
  43. return 'two arrays have the same elements.';
  44. }
  45. }