Skip to content

Commit be2010f

Browse files
committed
Refactor to be compatible with PHPUnit 8
1 parent 1a3a9aa commit be2010f

7 files changed

+16
-60
lines changed

src/Constraint/ExtendsOrImplements.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111

1212
namespace Cross\TestUtils\Constraint;
1313

14+
use PHPUnit\Framework\Constraint\Constraint as BaseConstraint;
15+
1416
/**
1517
* Constraint to assert the extending or implementing of specific classes and interfaces.
1618
*
1719
* @author Mathias Gelhausen <[email protected]>
1820
* @todo write tets.
1921
*/
20-
class ExtendsOrImplements extends \PHPUnit_Framework_Constraint
22+
class ExtendsOrImplements extends BaseConstraint
2123
{
2224
/**
2325
* The FQCN of the classes and interfaces which the tested object
@@ -42,7 +44,6 @@ class ExtendsOrImplements extends \PHPUnit_Framework_Constraint
4244
public function __construct(iterable $parentsAndInterfaces = [])
4345
{
4446
$this->parentsAndInterfaces = (array) $parentsAndInterfaces;
45-
parent::__construct();
4647
}
4748

4849
public function count(): int

src/Constraint/UsesTraits.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@
1111

1212
namespace Cross\TestUtils\Constraint;
1313

14+
use PHPUnit\Framework\Constraint\Constraint as BaseConstraint;
15+
1416
/**
1517
* Constraint to assert that a class uses specific traits.
1618
*
1719
* @author Mathias Gelhausen <[email protected]>
1820
* @since 0.26
1921
* @since 0.29 matches() accepts instances of \ReflectionClass.
2022
*/
21-
class UsesTraits extends \PHPUnit_Framework_Constraint
23+
class UsesTraits extends BaseConstraint
2224
{
2325
/**
2426
* The traits that must be used.
@@ -42,7 +44,6 @@ class UsesTraits extends \PHPUnit_Framework_Constraint
4244
public function __construct(iterable $expectedTraits = [])
4345
{
4446
$this->expectedTraits = (array) $expectedTraits;
45-
parent::__construct();
4647
}
4748

4849
public function count(): int

src/Exception/InvalidUsageException.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* @author Mathias Gelhausen <[email protected]>
1818
* @todo write tests
1919
*/
20-
class InvalidUsageException extends \PHPUnit_Framework_Exception implements ExceptionInterface
20+
class InvalidUsageException extends \PHPUnit\Framework\Exception implements ExceptionInterface
2121
{
2222
use TemplatedMessageExceptionTrait;
2323
}

src/TestCase/AssertInheritanceTrait.php

-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ trait AssertInheritanceTrait
3030
* @param iterable $parentsAndInterfaces
3131
* @param object|\ReflectionClass|string $objectOrClass
3232
* @param string $message
33-
*
34-
* @throws \PHPUnit_Framework_Exception
3533
*/
3634
public static function assertInheritance(
3735
iterable $parentsAndInterfaces,

src/TestCase/SetupTargetTrait.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
trait SetupTargetTrait
2525
{
2626

27-
public function setup()
27+
protected function setUp(): void
2828
{
2929
$this->setupTarget();
3030
}
@@ -107,7 +107,7 @@ public function setup()
107107
*
108108
* @return void
109109
*/
110-
public function setupTarget(): void
110+
private function setupTarget(): void
111111
{
112112
if (!property_exists($this, 'target')) {
113113
return;

src/TestCase/TestSetterAndGetterTrait.php

+5-49
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,6 @@
4848
* * 'value_callback': callable || method name (in the TestCase class)
4949
* The return value of that callback is taken as value for the test.
5050
*
51-
* * 'property': If there is no getter method, you may want to test, wether the setter has set
52-
* a property on the SUT properly. This key may be
53-
* - true: The name equals the property to check
54-
* - string: Name of property to hold the expected value
55-
* - [{PropertyName, }expected value (if differs from 'value']
56-
*
57-
* * 'property_assert': callback for property assertion.
58-
* see 'assert'. But please note:
59-
* strings are 'converted' to 'assertAttribute*' methods.
60-
* The callback gets passed the expected value, the name of the
61-
* property ('property') and the SUT instance.
62-
* DEFAULT: 'equals' ( => static::assertAttributeEquals)
63-
*
6451
* * 'getter': - Name of getter method (a '*' gets replaced by the property name)
6552
* - [GetterName, [arg, arg, ...]]
6653
* Use this format to provide arguments to the getter method.
@@ -167,17 +154,7 @@ public function testSetterAndGetter($name, $spec = null): void
167154
}
168155
}
169156

170-
// Test property
171-
if (null !== $spec['property']) {
172-
[$propertyName, $propertyValue] = $spec['property'];
173-
if ('__VALUE__' == $propertyValue) {
174-
$propertyValue = $value;
175-
}
176-
177-
$spec['property_assert']($propertyValue, $propertyName, $target);
178-
179-
// Test getter
180-
} elseif (false !== $spec['getter'][0]) {
157+
if (false !== $spec['getter'][0]) {
181158
$getter = str_replace('*', $name, $spec['getter'][0]);
182159
$getterValue = $target->$getter(...$spec['getter'][1]);
183160
$spec['assert']($value, $getterValue);
@@ -196,8 +173,6 @@ public function testSetterAndGetter($name, $spec = null): void
196173
private function setterAndGetterNormalizeSpec($spec, string $name, object $target): array
197174
{
198175
$normalized = [
199-
'property' => null,
200-
'property_assert' => [static::class, 'assertAttributeEquals'],
201176
'getter' => ["get*", []],
202177
'assert' => [static::class, 'assertEquals'],
203178
'setter' => ["set*", []],
@@ -215,31 +190,14 @@ private function setterAndGetterNormalizeSpec($spec, string $name, object $targe
215190
$err = __TRAIT__ . ': ' . get_class($this) . ': ';
216191

217192
if (!is_array($spec)) {
218-
throw new \PHPUnit_Framework_Exception($err . 'Invalid specification. Must be array.');
193+
throw new \PHPUnit\Framework\Exception($err . 'Invalid specification. Must be array.');
219194
}
220195

221196
foreach ($spec as $key => $value) {
222197
switch ($key) {
223198
default:
224199
break;
225200

226-
case 'property':
227-
if (true === $value) {
228-
$value = [$name, '__VALUE__'];
229-
break;
230-
}
231-
232-
if (is_array($value)) {
233-
$value = isset($value[1])
234-
? [true === $value[0] ? $name : $value[0], $value[1]]
235-
: [$name, $value[0]]
236-
;
237-
break;
238-
}
239-
240-
$value = [$value, '__VALUE__'];
241-
break;
242-
243201
case 'setter_value':
244202
if ('__SELF__' == $value) {
245203
$value = $target;
@@ -286,7 +244,7 @@ private function setterAndGetterNormalizeSpec($spec, string $name, object $targe
286244
}
287245

288246
if (!is_callable($value)) {
289-
throw new \PHPUnit_Framework_Exception($err . 'Invalid value callback.');
247+
throw new \PHPUnit\Framework\Exception($err . 'Invalid value callback.');
290248
}
291249

292250
$key = substr($key, 0, -9);
@@ -295,19 +253,17 @@ private function setterAndGetterNormalizeSpec($spec, string $name, object $targe
295253

296254
case 'setter_assert':
297255
case 'assert':
298-
case 'property_assert':
299256
if (is_string($value)) {
300257
if (method_exists($this, $value)) {
301258
$value = [$this, $value];
302259
break;
303260
}
304-
$attr = 'property_assert' == $key ? 'Attribute' : '';
305-
$value = [static::class, "assert$attr$value"];
261+
$value = [static::class, "assert$value"];
306262
break;
307263
}
308264

309265
if (!is_callable($value)) {
310-
throw new \PHPUnit_Framework_Exception($err . 'Invalid assert callback.');
266+
throw new \PHPUnit\Framework\Exception($err . 'Invalid assert callback.');
311267
}
312268

313269
break;

src/Utils/Target.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public static function get(
6767
if (is_object($target)) {
6868
return $target;
6969
}
70-
70+
7171
if (!$forceObject && is_string($target) && '!' != $target{0}) {
7272
return $target;
7373
}
@@ -115,6 +115,6 @@ public static function get(
115115
}
116116
}
117117

118-
throw new \PHPUnit_Framework_Exception('Could not find or create a target instance.');
118+
throw new \PHPUnit\Framework\Exception('Could not find or create a target instance.');
119119
}
120120
}

0 commit comments

Comments
 (0)