|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Behatch; |
|
4
|
|
|
|
|
5
|
|
|
use Behat\Mink\Exception\ExpectationException; |
|
6
|
|
|
|
|
7
|
|
|
trait Asserter |
|
8
|
|
|
{ |
|
9
|
|
|
protected function not(callable $callbable, $errorMessage) |
|
10
|
|
|
{ |
|
11
|
|
|
try { |
|
12
|
|
|
$callbable(); |
|
13
|
|
|
} |
|
14
|
|
|
catch (\Exception $e) { |
|
15
|
|
|
return; |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
throw new ExpectationException($errorMessage, $this->getSession()->getDriver()); |
|
|
|
|
|
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
protected function assert($test, $message) |
|
22
|
|
|
{ |
|
23
|
|
|
if ($test === false) { |
|
24
|
|
|
throw new ExpectationException($message, $this->getSession()->getDriver()); |
|
|
|
|
|
|
25
|
|
|
} |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
protected function assertContains($expected, $actual, $message = null) |
|
29
|
|
|
{ |
|
30
|
|
|
$regex = '/' . preg_quote($expected, '/') . '/ui'; |
|
31
|
|
|
|
|
32
|
|
|
$this->assert( |
|
33
|
|
|
preg_match($regex, $actual) > 0, |
|
34
|
|
|
$message ?: "The string '$expected' was not found." |
|
35
|
|
|
); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
protected function assertNotContains($expected, $actual, $message = null) |
|
39
|
|
|
{ |
|
40
|
|
|
$message = $message ?: "The string '$expected' was found."; |
|
41
|
|
|
|
|
42
|
|
|
$this->not(function () use($expected, $actual) { |
|
43
|
|
|
$this->assertContains($expected, $actual); |
|
44
|
|
|
}, $message); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
protected function assertCount($expected, array $elements, $message = null) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->assert( |
|
50
|
|
|
intval($expected) === count($elements), |
|
51
|
|
|
$message ?: sprintf('%d elements found, but should be %d.', count($elements), $expected) |
|
52
|
|
|
); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
protected function assertEquals($expected, $actual, $message = null) |
|
56
|
|
|
{ |
|
57
|
|
|
$this->assert( |
|
58
|
|
|
$expected == $actual, |
|
59
|
|
|
$message ?: "The element '$actual' is not equal to '$expected'" |
|
60
|
|
|
); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
protected function assertLessThan($expected, $actual, $message = null) |
|
64
|
|
|
{ |
|
65
|
|
|
$this->assert( |
|
66
|
|
|
$expected > $actual, |
|
67
|
|
|
$message ?: "The element '$actual' is not smaller than to '$expected'" |
|
68
|
|
|
); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
protected function assertMoreThan($expected, $actual, $message = null) |
|
72
|
|
|
{ |
|
73
|
|
|
$this->assert( |
|
74
|
|
|
$expected < $actual, |
|
75
|
|
|
$message ?: "The element '$actual' is not greater than to '$expected'" |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
protected function assertSame($expected, $actual, $message = null) |
|
80
|
|
|
{ |
|
81
|
|
|
$this->assert( |
|
82
|
|
|
$expected === $actual, |
|
83
|
|
|
$message ?: "The element '$actual' is not equal to '$expected'" |
|
84
|
|
|
); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
protected function assertArrayHasKey($key, $array, $message = null) |
|
88
|
|
|
{ |
|
89
|
|
|
$this->assert( |
|
90
|
|
|
isset($array[$key]), |
|
91
|
|
|
$message ?: "The array has no key '$key'" |
|
92
|
|
|
); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
protected function assertArrayNotHasKey($key, $array, $message = null) |
|
96
|
|
|
{ |
|
97
|
|
|
$message = $message ?: "The array has key '$key'"; |
|
98
|
|
|
|
|
99
|
|
|
$this->not(function () use($key, $array) { |
|
100
|
|
|
$this->assertArrayHasKey($key, $array); |
|
101
|
|
|
}, $message); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
protected function assertTrue($value, $message = 'The value is false') |
|
105
|
|
|
{ |
|
106
|
|
|
$this->assert($value, $message); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
protected function assertFalse($value, $message = 'The value is true') |
|
110
|
|
|
{ |
|
111
|
|
|
$this->not(function () use($value) { |
|
112
|
|
|
$this->assertTrue($value); |
|
113
|
|
|
}, $message); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
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
Idableprovides a methodequalsIdthat 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.