ArrayHasSameValuesConstraint.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. /**
  11. * Constraint that accepts arrays with the same elements but different order.
  12. */
  13. class ArrayHasSameValuesConstraint extends PHPUnit_Framework_Constraint
  14. {
  15. protected $array;
  16. /**
  17. * @param array $array
  18. */
  19. public function __construct($array)
  20. {
  21. $this->array = $array;
  22. }
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public function matches($other)
  27. {
  28. if (count($this->array) !== count($other)) {
  29. return false;
  30. }
  31. if (array_diff($this->array, $other)) {
  32. return false;
  33. }
  34. return true;
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function toString()
  40. {
  41. return 'two arrays contain the same elements.';
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. protected function failureDescription($other)
  47. {
  48. return $this->toString();
  49. }
  50. }