|
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
|
|
|
public function expectException($exception) |
|
19
|
|
|
{ |
|
20
|
|
|
if (!is_string($exception)) { |
|
21
|
|
|
/** @var \PHPUnit_Util_InvalidArgumentHelper|\PHPUnit\Util\InvalidArgumentHelper $class */ |
|
22
|
|
|
$class = $this->getInvalidArgumentHelperClass(); |
|
23
|
|
|
throw $class::factory(1, 'string'); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
$this->setInaccessibleProperty($this, 'expectedException', $exception); |
|
|
|
|
|
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param int|string $code |
|
31
|
|
|
* @throws \Exception |
|
32
|
|
|
*/ |
|
33
|
|
|
public function expectExceptionCode($code) |
|
34
|
|
|
{ |
|
35
|
|
|
if (!$this->getInaccessibleProperty($this, 'expectedException')) { |
|
|
|
|
|
|
36
|
|
|
$this->setInaccessibleProperty($this, 'expectedException', 'Exception'); |
|
|
|
|
|
|
37
|
|
|
} |
|
38
|
|
|
if (!is_int($code) && !is_string($code)) { |
|
39
|
|
|
/** @var \PHPUnit_Util_InvalidArgumentHelper|\PHPUnit\Util\InvalidArgumentHelper $class */ |
|
40
|
|
|
$class = $this->getInvalidArgumentHelperClass(); |
|
41
|
|
|
throw $class::factory(1, 'integer or string'); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$this->setInaccessibleProperty($this, 'expectedExceptionCode', $code); |
|
|
|
|
|
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param string $message |
|
49
|
|
|
* @throws \Exception |
|
50
|
|
|
*/ |
|
51
|
|
|
public function expectExceptionMessage($message) |
|
52
|
|
|
{ |
|
53
|
|
|
if (!$this->getInaccessibleProperty($this, 'expectedException')) { |
|
|
|
|
|
|
54
|
|
|
$this->setInaccessibleProperty($this, 'expectedException', 'Exception'); |
|
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
if (!is_string($message)) { |
|
57
|
|
|
/** @var \PHPUnit_Util_InvalidArgumentHelper|\PHPUnit\Util\InvalidArgumentHelper $class */ |
|
58
|
|
|
$class = $this->getInvalidArgumentHelperClass(); |
|
59
|
|
|
throw $class::factory(1, 'string'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$this->setInaccessibleProperty($this, 'expectedExceptionMessage', $message); |
|
|
|
|
|
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param string $messageRegExp |
|
67
|
|
|
* @throws \Exception |
|
68
|
|
|
*/ |
|
69
|
|
|
public function expectExceptionMessageRegExp($messageRegExp) |
|
70
|
|
|
{ |
|
71
|
|
|
if (!$this->getInaccessibleProperty($this, 'expectedException')) { |
|
|
|
|
|
|
72
|
|
|
$this->setInaccessibleProperty($this, 'expectedException', 'Exception'); |
|
|
|
|
|
|
73
|
|
|
} |
|
74
|
|
|
if (!is_string($messageRegExp)) { |
|
75
|
|
|
/** @var \PHPUnit_Util_InvalidArgumentHelper|\PHPUnit\Util\InvalidArgumentHelper $class */ |
|
76
|
|
|
$class = $this->getInvalidArgumentHelperClass(); |
|
77
|
|
|
throw $class::factory(1, 'string'); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$this->setInaccessibleProperty($this, 'expectedExceptionMessageRegExp', $messageRegExp); |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return string |
|
85
|
|
|
*/ |
|
86
|
|
|
private function getInvalidArgumentHelperClass() |
|
87
|
|
|
{ |
|
88
|
|
|
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
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.