1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the box project. |
7
|
|
|
* |
8
|
|
|
* (c) Kevin Herrera <[email protected]> |
9
|
|
|
* Théo Fidry <[email protected]> |
10
|
|
|
* |
11
|
|
|
* This source file is subject to the MIT license that is bundled |
12
|
|
|
* with this source code in the file LICENSE. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace KevinGH\RequirementChecker; |
16
|
|
|
|
17
|
|
|
use Error; |
18
|
|
|
use PHPUnit\Framework\TestCase; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @covers \KevinGH\RequirementChecker\Requirement |
22
|
|
|
*/ |
23
|
|
|
class RequirementTest extends TestCase |
24
|
|
|
{ |
25
|
|
|
public function test_it_can_be_created(): void |
26
|
|
|
{ |
27
|
|
|
$requirement = new Requirement( |
28
|
|
|
'return true;', |
29
|
|
|
'Test message', |
30
|
|
|
'Help message' |
31
|
|
|
); |
32
|
|
|
|
33
|
|
|
$this->assertSame('return true;', $requirement->getIsFullfilledChecker()); |
34
|
|
|
$this->assertTrue($requirement->isFulfilled()); |
35
|
|
|
$this->assertSame('Test message', $requirement->getTestMessage()); |
36
|
|
|
$this->assertSame('Help message', $requirement->getHelpText()); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function test_it_evaluates_the_check_lazily(): void |
40
|
|
|
{ |
41
|
|
|
$requirement = new Requirement( |
42
|
|
|
'throw new \Error();', |
43
|
|
|
'Test message', |
44
|
|
|
'Help message' |
45
|
|
|
); |
46
|
|
|
|
47
|
|
|
$this->assertSame('throw new \Error();', $requirement->getIsFullfilledChecker()); |
48
|
|
|
|
49
|
|
|
try { |
50
|
|
|
$requirement->isFulfilled(); |
51
|
|
|
|
52
|
|
|
$this->fail('Expected exception to be thrown.'); |
53
|
|
|
} catch (Error $error) { |
54
|
|
|
$this->assertTrue(true); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function test_it_casts_the_fulfilled_result_into_a_boolean(): void |
59
|
|
|
{ |
60
|
|
|
$requirement = new Requirement( |
61
|
|
|
'return 1;', |
62
|
|
|
'', |
63
|
|
|
'' |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
$this->assertTrue($requirement->isFulfilled()); |
67
|
|
|
|
68
|
|
|
$requirement = new Requirement( |
69
|
|
|
'return 0;', |
70
|
|
|
'', |
71
|
|
|
'' |
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
$this->assertFalse($requirement->isFulfilled()); |
75
|
|
|
|
76
|
|
|
$requirement = new Requirement( |
77
|
|
|
'return new \stdClass();', |
78
|
|
|
'', |
79
|
|
|
'' |
80
|
|
|
); |
81
|
|
|
|
82
|
|
|
$this->assertTrue($requirement->isFulfilled()); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function test_it_evaluates_the_check_only_once(): void |
86
|
|
|
{ |
87
|
|
|
$GLOBALS['x'] = -1; |
88
|
|
|
|
89
|
|
|
$requirement = new Requirement( |
90
|
|
|
<<<'PHP' |
91
|
|
|
$GLOBALS['x']++; |
92
|
|
|
|
93
|
|
|
return $GLOBALS['x']; |
94
|
|
|
PHP |
95
|
|
|
, |
96
|
|
|
'Test message', |
97
|
|
|
'Help message' |
98
|
|
|
); |
99
|
|
|
|
100
|
|
|
$this->assertFalse($requirement->isFulfilled()); |
101
|
|
|
$this->assertFalse($requirement->isFulfilled()); // Would have gave `true` if it was evaluated a second time |
102
|
|
|
|
103
|
|
|
unset($GLOBALS['x']); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|