SeeInOrder.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\Testing\Constraint;
  12. use PHPUnit\Framework\Constraint\Constraint;
  13. use ReflectionClass;
  14. class SeeInOrder extends Constraint
  15. {
  16. /**
  17. * The string under validation.
  18. *
  19. * @var string
  20. */
  21. protected $content;
  22. /**
  23. * The last value that failed to pass validation.
  24. *
  25. * @var string
  26. */
  27. protected $failedValue;
  28. /**
  29. * Create a new constraint instance.
  30. *
  31. * @param string $content
  32. */
  33. public function __construct($content)
  34. {
  35. $this->content = $content;
  36. }
  37. /**
  38. * Determine if the rule passes validation.
  39. *
  40. * @param array $values
  41. */
  42. public function matches($values): bool
  43. {
  44. $position = 0;
  45. foreach ($values as $value) {
  46. if (empty($value)) {
  47. continue;
  48. }
  49. $valuePosition = mb_strpos($this->content, $value, $position);
  50. if ($valuePosition === false || $valuePosition < $position) {
  51. $this->failedValue = $value;
  52. return false;
  53. }
  54. $position = $valuePosition + mb_strlen($value);
  55. }
  56. return true;
  57. }
  58. /**
  59. * Get the description of the failure.
  60. *
  61. * @param array $values
  62. */
  63. public function failureDescription($values): string
  64. {
  65. return sprintf(
  66. 'Failed asserting that \'%s\' contains "%s" in specified order.',
  67. $this->content,
  68. $this->failedValue
  69. );
  70. }
  71. /**
  72. * Get a string representation of the object.
  73. */
  74. public function toString(): string
  75. {
  76. return (new ReflectionClass($this))->name;
  77. }
  78. }