1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Behatch; |
4
|
|
|
|
5
|
|
|
use Behat\Mink\Exception\ExpectationException; |
6
|
|
|
use Coduo\PHPMatcher\PHPMatcher; |
7
|
|
|
|
8
|
|
|
trait Asserter |
9
|
|
|
{ |
10
|
|
|
protected function not(callable $callbable, $errorMessage) |
11
|
|
|
{ |
12
|
|
|
try { |
13
|
|
|
$callbable(); |
14
|
|
|
} |
15
|
|
|
catch (\Exception $e) { |
16
|
|
|
return; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
throw new ExpectationException($errorMessage, $this->getSession()->getDriver()); |
|
|
|
|
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
protected function assert($test, $message) |
23
|
|
|
{ |
24
|
|
|
if ($test === false) { |
25
|
|
|
throw new ExpectationException($message, $this->getSession()->getDriver()); |
|
|
|
|
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
protected function assertContains($expected, $actual, $message = null) |
30
|
|
|
{ |
31
|
|
|
$regex = '/' . preg_quote($expected, '/') . '/ui'; |
32
|
|
|
|
33
|
|
|
$this->assert( |
34
|
|
|
preg_match($regex, $actual) > 0, |
35
|
|
|
$message ?: "The string '$expected' was not found." |
36
|
|
|
); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
protected function assertNotContains($expected, $actual, $message = null) |
40
|
|
|
{ |
41
|
|
|
$message = $message ?: "The string '$expected' was found."; |
42
|
|
|
|
43
|
|
|
$this->not(function () use($expected, $actual) { |
44
|
|
|
$this->assertContains($expected, $actual); |
45
|
|
|
}, $message); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
protected function assertCount($expected, array $elements, $message = null) |
49
|
|
|
{ |
50
|
|
|
$this->assert( |
51
|
|
|
intval($expected) === count($elements), |
52
|
|
|
$message ?: sprintf('%d elements found, but should be %d.', count($elements), $expected) |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
protected function assertEquals($expected, $actual, $message = null) |
57
|
|
|
{ |
58
|
|
|
$this->assert( |
59
|
|
|
$expected == $actual, |
60
|
|
|
$message ?: "The element '$actual' is not equal to '$expected'" |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
protected function assertSame($expected, $actual, $message = null) |
65
|
|
|
{ |
66
|
|
|
$this->assert( |
67
|
|
|
$expected === $actual, |
68
|
|
|
$message ?: "The element '$actual' is not equal to '$expected'" |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
protected function assertMatch($pattern, $actual, $message = null) |
73
|
|
|
{ |
74
|
|
|
if (version_compare(PHP_VERSION, '7.0.0', '<')) { |
75
|
|
|
throw new PHPVersionNotSupported(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$this->assert( |
79
|
|
|
PHPMatcher::match($actual, $pattern, $error), |
80
|
|
|
$message ?: "The element '$actual' do not match '$pattern'" |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
protected function assertArrayHasKey($key, $array, $message = null) |
85
|
|
|
{ |
86
|
|
|
$this->assert( |
87
|
|
|
isset($array[$key]), |
88
|
|
|
$message ?: "The array has no key '$key'" |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
protected function assertArrayNotHasKey($key, $array, $message = null) |
93
|
|
|
{ |
94
|
|
|
$message = $message ?: "The array has key '$key'"; |
95
|
|
|
|
96
|
|
|
$this->not(function () use($key, $array) { |
97
|
|
|
$this->assertArrayHasKey($key, $array); |
98
|
|
|
}, $message); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
protected function assertTrue($value, $message = 'The value is false') |
102
|
|
|
{ |
103
|
|
|
$this->assert($value, $message); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
protected function assertFalse($value, $message = 'The value is true') |
107
|
|
|
{ |
108
|
|
|
$this->not(function () use($value) { |
109
|
|
|
$this->assertTrue($value); |
110
|
|
|
}, $message); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.