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