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