1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Common tests stuff |
4
|
|
|
* |
5
|
|
|
* @see https://github.com/sergeymakinen/tests |
6
|
|
|
* @copyright Copyright (c) 2017 Sergey Makinen (https://makinen.ru) |
7
|
|
|
* @license https://github.com/sergeymakinen/tests/blob/master/LICENSE MIT License |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace SergeyMakinen\Tests\Util; |
11
|
|
|
|
12
|
|
|
trait ExpectExceptionTrait |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @param string $exception |
16
|
|
|
* @throws \Exception |
17
|
|
|
*/ |
18
|
6 |
|
public function expectException($exception) |
19
|
|
|
{ |
20
|
6 |
|
if (!is_string($exception)) { |
21
|
|
|
/** @var \PHPUnit_Util_InvalidArgumentHelper|\PHPUnit\Util\InvalidArgumentHelper $class */ |
22
|
3 |
|
$class = $this->getInvalidArgumentHelperClass(); |
23
|
3 |
|
throw $class::factory(1, 'string'); |
24
|
|
|
} |
25
|
|
|
|
26
|
3 |
|
$this->setInaccessibleProperty($this, 'expectedException', $exception); |
|
|
|
|
27
|
3 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param int|string $code |
31
|
|
|
* @throws \Exception |
32
|
|
|
*/ |
33
|
6 |
|
public function expectExceptionCode($code) |
34
|
|
|
{ |
35
|
6 |
|
if (!$this->getInaccessibleProperty($this, 'expectedException')) { |
|
|
|
|
36
|
6 |
|
$this->setInaccessibleProperty($this, 'expectedException', 'Exception'); |
|
|
|
|
37
|
4 |
|
} |
38
|
6 |
|
if (!is_int($code) && !is_string($code)) { |
39
|
|
|
/** @var \PHPUnit_Util_InvalidArgumentHelper|\PHPUnit\Util\InvalidArgumentHelper $class */ |
40
|
3 |
|
$class = $this->getInvalidArgumentHelperClass(); |
41
|
3 |
|
throw $class::factory(1, 'integer or string'); |
42
|
|
|
} |
43
|
|
|
|
44
|
3 |
|
$this->setInaccessibleProperty($this, 'expectedExceptionCode', $code); |
|
|
|
|
45
|
3 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $message |
49
|
|
|
* @throws \Exception |
50
|
|
|
*/ |
51
|
6 |
|
public function expectExceptionMessage($message) |
52
|
|
|
{ |
53
|
6 |
|
if (!$this->getInaccessibleProperty($this, 'expectedException')) { |
|
|
|
|
54
|
6 |
|
$this->setInaccessibleProperty($this, 'expectedException', 'Exception'); |
|
|
|
|
55
|
4 |
|
} |
56
|
6 |
|
if (!is_string($message)) { |
57
|
|
|
/** @var \PHPUnit_Util_InvalidArgumentHelper|\PHPUnit\Util\InvalidArgumentHelper $class */ |
58
|
3 |
|
$class = $this->getInvalidArgumentHelperClass(); |
59
|
3 |
|
throw $class::factory(1, 'string'); |
60
|
|
|
} |
61
|
|
|
|
62
|
3 |
|
$this->setInaccessibleProperty($this, 'expectedExceptionMessage', $message); |
|
|
|
|
63
|
3 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $messageRegExp |
67
|
|
|
* @throws \Exception |
68
|
|
|
*/ |
69
|
6 |
|
public function expectExceptionMessageRegExp($messageRegExp) |
70
|
|
|
{ |
71
|
6 |
|
if (!$this->getInaccessibleProperty($this, 'expectedException')) { |
|
|
|
|
72
|
6 |
|
$this->setInaccessibleProperty($this, 'expectedException', 'Exception'); |
|
|
|
|
73
|
4 |
|
} |
74
|
6 |
|
if (!is_string($messageRegExp)) { |
75
|
|
|
/** @var \PHPUnit_Util_InvalidArgumentHelper|\PHPUnit\Util\InvalidArgumentHelper $class */ |
76
|
3 |
|
$class = $this->getInvalidArgumentHelperClass(); |
77
|
3 |
|
throw $class::factory(1, 'string'); |
78
|
|
|
} |
79
|
|
|
|
80
|
3 |
|
$this->setInaccessibleProperty($this, 'expectedExceptionMessageRegExp', $messageRegExp); |
|
|
|
|
81
|
3 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
12 |
|
private function getInvalidArgumentHelperClass() |
87
|
|
|
{ |
88
|
12 |
|
return class_exists('PHPUnit_Util_InvalidArgumentHelper') ? 'PHPUnit_Util_InvalidArgumentHelper' : 'PHPUnit\Util\InvalidArgumentHelper'; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
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.