RedisCommandConstraint.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 Predis\Command\CommandInterface;
  11. /**
  12. * Constraint that verifies a redis command.
  13. */
  14. class RedisCommandConstraint extends PHPUnit_Framework_Constraint
  15. {
  16. protected $commandID;
  17. protected $arguments;
  18. /**
  19. * @param array $array
  20. */
  21. public function __construct($command = null, array $arguments = null)
  22. {
  23. if ($command instanceof CommandInterface) {
  24. $this->commandID = strtoupper($command->getId());
  25. $this->arguments = $arguments ?: $commant->getArguments();
  26. } else {
  27. $this->commandID = strtoupper($command);
  28. $this->arguments = $arguments;
  29. }
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function matches($other)
  35. {
  36. if (!$other instanceof CommandInterface) {
  37. return false;
  38. }
  39. if ($this->commandID && strtoupper($other->getId()) !== $this->commandID) {
  40. return false;
  41. }
  42. if ($this->arguments !== null) {
  43. $otherArguments = $other->getArguments();
  44. if (count($this->arguments) !== count($otherArguments)) {
  45. return false;
  46. }
  47. for ($i = 0; $i < count($this->arguments); $i++) {
  48. if (((string) $this->arguments[$i]) !== ((string) $otherArguments[$i])) {
  49. return false;
  50. }
  51. }
  52. }
  53. return true;
  54. }
  55. /**
  56. * {@inheritdoc}
  57. *
  58. * @todo Improve output using diff when expected and actual arguments of a
  59. * command do not match.
  60. */
  61. public function toString()
  62. {
  63. $string = 'is a Redis command';
  64. if ($this->commandID) {
  65. $string .= " with ID '{$this->commandID}'";
  66. }
  67. if ($this->arguments) {
  68. $string .= " and the following arguments:\n\n";
  69. $string .= PHPUnit_Util_Type::export($this->arguments);
  70. }
  71. return $string;
  72. }
  73. /**
  74. * {@inheritdoc}
  75. */
  76. protected function failureDescription($other)
  77. {
  78. $string = is_object($other) ? get_class($other) : $other;
  79. return "$string {$this->toString()}";
  80. }
  81. }