1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PHP: Nelson Martell Library file |
4
|
|
|
* |
5
|
|
|
* Content: |
6
|
|
|
* - Test case for: [NelsonMartell] Version |
7
|
|
|
* |
8
|
|
|
* Copyright © 2016-2017 Nelson Martell (http://nelson6e65.github.io) |
9
|
|
|
* |
10
|
|
|
* Licensed under The MIT License (MIT) |
11
|
|
|
* For full copyright and license information, please see the LICENSE |
12
|
|
|
* Redistributions of files must retain the above copyright notice. |
13
|
|
|
* |
14
|
|
|
* @copyright 2016-2017 Nelson Martell |
15
|
|
|
* @link http://nelson6e65.github.io/php_nml/ |
16
|
|
|
* @since v0.6.0 |
17
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License (MIT) |
18
|
|
|
* */ |
19
|
|
|
|
20
|
|
|
namespace NelsonMartell\Test\TestCase; |
21
|
|
|
|
22
|
|
|
use NelsonMartell\Extensions\Text; |
23
|
|
|
use NelsonMartell\Test\DataProviders\VersionComponentTestProvider; |
24
|
|
|
use NelsonMartell\VersionComponent; |
25
|
|
|
use PHPUnit\Framework\TestCase; |
26
|
|
|
use \InvalidArgumentException; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* |
30
|
|
|
* @author Nelson Martell <[email protected]> |
31
|
|
|
* @internal |
32
|
|
|
* */ |
33
|
|
|
class VersionComponentTest extends TestCase |
34
|
|
|
{ |
35
|
|
|
use VersionComponentTestProvider; |
36
|
|
|
|
37
|
|
|
public function getTargetClassName() |
38
|
|
|
{ |
39
|
|
|
return VersionComponent::class; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @testdox Performs conversion from compatible objects |
44
|
|
|
* @coverage VersionComponent::parse |
45
|
|
|
* @dataProvider goodVersionComponentParseMethodArgumentsProvider |
46
|
|
|
*/ |
47
|
|
|
public function testParseMethod(VersionComponent $expected, $obj) |
48
|
|
|
{ |
49
|
|
|
$actual = VersionComponent::parse($obj); |
50
|
|
|
|
51
|
|
|
$message = Text::format( |
52
|
|
|
'{class}::{method}({obj}); // {actual}', |
53
|
|
|
[ |
54
|
|
|
'class' => VersionComponent::class, |
55
|
|
|
'method' => 'isValid', |
56
|
|
|
'obj' => static::export($obj), |
57
|
|
|
'actual' => static::export($actual) |
58
|
|
|
] |
59
|
|
|
); |
60
|
|
|
|
61
|
|
|
$this->assertEquals($expected, $actual, $message); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @testdox Informs if error occurs on parsing incompatible objects |
66
|
|
|
* @coverage VersionComponent::parse |
67
|
|
|
* @dataProvider badVersionComponentParseMethodArgumentsProvider |
68
|
|
|
* @expectedException \InvalidArgumentException |
69
|
|
|
*/ |
70
|
|
|
public function testParseMethodWithInvalidArguments($obj) |
71
|
|
|
{ |
72
|
|
|
$actual = VersionComponent::parse($obj); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @coverage VersionComponent::__toString |
77
|
|
|
* @coverage VersionComponent::toString |
78
|
|
|
* @dataProvider versionComponentToStringMethodArgumentsProvider |
79
|
|
|
*/ |
80
|
|
|
public function testPerformsConversionToString($expected, VersionComponent $versionComponent) |
81
|
|
|
{ |
82
|
|
|
$actual = $versionComponent->toString(); |
83
|
|
|
|
84
|
|
|
$message = Text::format( |
85
|
|
|
'$versionComponent->{method}(); // {actual}', |
86
|
|
|
[ |
87
|
|
|
'method' => 'toString', |
88
|
|
|
'actual' => static::export($actual) |
89
|
|
|
] |
90
|
|
|
); |
91
|
|
|
|
92
|
|
|
$this->assertInternalType('string', $actual, $message.' # Should return a string #'); |
93
|
|
|
$this->assertEquals($expected, $actual, $message); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @coverage VersionComponent::__toString |
98
|
|
|
* @coverage VersionComponent::toString |
99
|
|
|
* @depends testPerformsConversionToString |
100
|
|
|
* @dataProvider versionComponentToStringMethodArgumentsProvider |
101
|
|
|
*/ |
102
|
|
|
public function testPerformsImplicitConversionToString($str, VersionComponent $obj) |
103
|
|
|
{ |
104
|
|
|
$expected = "<VersionComponent>$str</VersionComponent>"; |
|
|
|
|
105
|
|
|
$actual = "<VersionComponent>$obj</VersionComponent>"; |
|
|
|
|
106
|
|
|
|
107
|
|
|
$this->assertEquals($expected, $actual); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @coverage VersionComponent::isNull |
112
|
|
|
* @coverage VersionComponent::isNotNull |
113
|
|
|
* @coverage VersionComponent::isDefault |
114
|
|
|
* @coverage VersionComponent::isNotDefault |
115
|
|
|
* @dataProvider nullOrDefaultStatesProvider |
116
|
|
|
*/ |
117
|
|
|
public function testCanCheckIfVersionComponentIsInDefaultOrNullState($expected, VersionComponent $versionComponent) |
|
|
|
|
118
|
|
|
{ |
119
|
|
|
static $format = '$versionComponent->{method}(); // {actual}'; |
120
|
|
|
|
121
|
|
|
$actuals['isDefault'] = $versionComponent->isDefault(); |
|
|
|
|
122
|
|
|
$actuals['isNotDefault'] = $versionComponent->isNotDefault(); |
123
|
|
|
$actuals['isNull'] = $versionComponent->isNull(); |
124
|
|
|
$actuals['isNotNull'] = $versionComponent->isNotNull(); |
125
|
|
|
|
126
|
|
|
$messages = []; |
127
|
|
|
|
128
|
|
|
foreach ($actuals as $method => $actual) { |
129
|
|
|
$messages[$method] = Text::format($format, ['method' => $method, 'actual' => static::export($actual)]); |
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
foreach ($actuals as $method => $actual) { |
133
|
|
|
// Pre-tests for returning type |
134
|
|
|
$this->assertInternalType('boolean', $actual, $messages[$method].' # Should return a boolean #'); |
|
|
|
|
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
// Pre-tests for different values |
138
|
|
|
$this->assertNotEquals( |
139
|
|
|
$actuals['isDefault'], |
140
|
|
|
$actuals['isNotDefault'], |
141
|
|
|
$messages['isDefault'].PHP_EOL.$messages['isNotDefault'] |
142
|
|
|
); |
143
|
|
|
|
144
|
|
|
$this->assertNotEquals( |
145
|
|
|
$actuals['isNull'], |
146
|
|
|
$actuals['isNotNull'], |
147
|
|
|
$messages['isNull'].PHP_EOL.$messages['isNotNull'] |
148
|
|
|
); |
149
|
|
|
|
150
|
|
|
|
151
|
|
|
// Test expected |
152
|
|
|
if ($expected === 'default') { |
153
|
|
|
$this->assertTrue($actuals['isDefault'], $messages['isDefault']); |
154
|
|
|
|
155
|
|
|
// Can't be null AND default |
156
|
|
|
$this->assertNotEquals( |
157
|
|
|
$actuals['isNull'], |
158
|
|
|
$actuals['isDefault'], |
159
|
|
|
'#Can\'t be both, DEFAULT and NULL, at the same time'.PHP_EOL. |
160
|
|
|
$messages['isDefault'].PHP_EOL. |
161
|
|
|
$messages['isNull'] |
162
|
|
|
); |
163
|
|
|
} elseif ($expected === 'null') { |
164
|
|
|
$this->assertTrue($actuals['isNull'], $messages['isNull']); |
165
|
|
|
|
166
|
|
|
// Can't be null AND default |
167
|
|
|
$this->assertNotEquals( |
168
|
|
|
$actuals['isNull'], |
169
|
|
|
$actuals['isDefault'], |
170
|
|
|
'#Can\'t be both, NULL and DEFAULT, at the same time'.PHP_EOL. |
171
|
|
|
$messages['isNull'].PHP_EOL. |
172
|
|
|
$messages['isDefault'] |
173
|
|
|
); |
174
|
|
|
} else { |
175
|
|
|
$this->assertTrue($actuals['isNotDefault'], $messages['isNotDefault']); |
176
|
|
|
$this->assertTrue($actuals['isNotNull'], $messages['isNotNull']); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|