Skip to content

Commit b6f0960

Browse files
committed
Use new Utils\Target and InvalidUsageException in Traits.
1 parent 99d75ff commit b6f0960

8 files changed

Lines changed: 62 additions & 114 deletions

src/TestCase/TestDefaultAttributesTrait.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
namespace Cross\TestUtils\TestCase;
1313

14+
use Cross\TestUtils\Exception\InvalidUsageException;
15+
use Cross\TestUtils\Utils\Target;
16+
1417
/**
1518
* Tests the target for default attribute values.
1619
*
@@ -27,25 +30,23 @@
2730
trait TestDefaultAttributesTrait
2831
{
2932
use AssertDefaultAttributesValuesTrait;
30-
use GetTargetInstanceTrait;
3133

3234
/**
3335
* @testdox Defines correct default attribute values.
3436
* @coversNothing
3537
*/
3638
public function testDefaultAttributes(): void
3739
{
38-
$errTmpl = __TRAIT__ . ': ' . get_class($this);
39-
40-
if (!property_exists($this, 'defaultAttributes')) {
41-
throw new \PHPUnit_Framework_Exception($errTmpl . ' must define the property "defaultAttributes".');
42-
}
43-
44-
if (!is_array($this->defaultAttributes)) {
45-
throw new \PHPUnit_Framework_Exception($errTmpl . ': Property "defaultAttributes" must be an array');
40+
if (!property_exists($this, 'defaultAttributes') || !is_array($this->defaultAttributes)) {
41+
throw InvalidUsageException::fromTrait(
42+
__TRAIT__,
43+
__CLASS__,
44+
'The property "$defaultAttributes" is not defined or not an array.'
45+
);
4646
}
4747

48-
$target = $this->getTargetInstance(
48+
$target = Target::get(
49+
$this,
4950
['getDefaultAttributesTarget', 'getTarget'],
5051
['defaultAttributesTarget', 'target'],
5152
'defaultAttributes'

src/TestCase/TestInheritanceTrait.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
namespace Cross\TestUtils\TestCase;
1313

14+
use Cross\TestUtils\Exception\InvalidUsageException;
15+
use Cross\TestUtils\Utils\Target;
16+
1417
/**
1518
* Inheritance test.
1619
*
@@ -29,25 +32,24 @@
2932
*/
3033
trait TestInheritanceTrait
3134
{
32-
use AssertInheritanceTrait, GetTargetInstanceTrait;
35+
use AssertInheritanceTrait;
3336

3437
/**
3538
* @testdox Extends correct parent and implements required interfaces.
3639
* @coversNothing
3740
*/
3841
public function testInheritance(): void
3942
{
40-
$errTmpl = __TRAIT__ . ': ' . get_class($this);
41-
42-
if (!property_exists($this, 'inheritance')) {
43-
throw new \PHPUnit_Framework_Exception($errTmpl . ' must define the property "inheritance".');
44-
}
45-
46-
if (!is_array($this->inheritance)) {
47-
throw new \PHPUnit_Framework_Exception($errTmpl . ': Property "inheritance" must be an array.');
43+
if (!property_exists($this, 'inheritance') || !is_array($this->inheritance)) {
44+
throw InvalidUsageException::fromTrait(
45+
__TRAIT__,
46+
__CLASS__,
47+
'Property "$inheritance" is not defined or is not an array.'
48+
);
4849
}
4950

50-
$target = $this->getTargetInstance(
51+
$target = Target::get(
52+
$this,
5153
['getInheritanceTarget', 'getTarget'],
5254
['inheritanceTarget', 'target'],
5355
'inheritance'

src/TestCase/TestSetterAndGetterTrait.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Cross\TestUtils\TestCase;
1313

14+
use Cross\TestUtils\Utils\Target;
15+
1416
/**
1517
* Trait for testing setters and getters of the SUT.
1618
*
@@ -96,8 +98,6 @@
9698
*/
9799
trait TestSetterAndGetterTrait
98100
{
99-
use GetTargetInstanceTrait;
100-
101101
/**
102102
* Data provider for the properties test.
103103
*
@@ -122,7 +122,8 @@ public function testSetterAndGetter($name, $spec = null): void
122122
return;
123123
}
124124

125-
$target = $this->getTargetInstance(
125+
$target = Target::get(
126+
$this,
126127
['getSetterAndGetterTarget', 'getTarget'],
127128
['setterAndGetterTarget', 'target'],
128129
'setterAndGetter',
@@ -159,7 +160,7 @@ public function testSetterAndGetter($name, $spec = null): void
159160

160161
// Test property
161162
if (null !== $spec['property']) {
162-
list ($propertyName, $propertyValue) = $spec['property'];
163+
[$propertyName, $propertyValue] = $spec['property'];
163164
if ('__VALUE__' == $propertyValue) {
164165
$propertyValue = $value;
165166
}

src/TestCase/TestUsesTraitsTrait.php

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
namespace Cross\TestUtils\TestCase;
1313

14+
use Cross\TestUtils\Exception\InvalidUsageException;
15+
use Cross\TestUtils\Utils\Target;
16+
1417
/**
1518
* Uses traits test.
1619
*
@@ -23,34 +26,30 @@
2326
* '$usesTraitsTarget' or '$target'.
2427
*
2528
*
26-
* @property object|string $target
2729
* @property string[] $usesTraits
2830
*
2931
* @author Mathias Gelhausen <gelhausen@cross-solution.de>
3032
*/
3133
trait TestUsesTraitsTrait
3234
{
33-
use AssertUsesTraitsTrait, GetTargetInstanceTrait;
35+
use AssertUsesTraitsTrait;
3436

3537
/**
3638
* @testdox Uses required traits.
3739
* @coversNothing
3840
*/
3941
public function testUsesTraits(): void
4042
{
41-
if (!property_exists($this, 'usesTraits')) {
42-
throw new \PHPUnit_Framework_Exception(__TRAIT__ . ': ' . get_class($this)
43-
. ' must define the property "usesTraits".');
44-
}
45-
46-
if (!is_array($this->usesTraits)) {
47-
throw new \PHPUnit_Framework_Exception(
48-
__TRAIT__ . ': ' . get_class($this)
49-
. ': Property "usesTraits" must be an array.'
43+
if (!property_exists($this, 'usesTraits') || !is_array($this->usesTraits)) {
44+
throw InvalidUsageException::fromTrait(
45+
__TRAIT__,
46+
__CLASS__,
47+
'Property "$usesTraits" is not defined or not an array.'
5048
);
5149
}
5250

53-
$target = $this->getTargetInstance(
51+
$target = Target::get(
52+
$this,
5453
['getUsesTraitsTarget', 'getTarget'],
5554
['usesTraitsTarget', 'target'],
5655
'usesTraits'

test/TestUtilsTest/TestCase/TestDefaultAttributesTraitTest.php

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@
1212
namespace Cross\TestUtilsTest\TestCase;
1313

1414
use Cross\TestUtils\TestCase\AssertDefaultAttributesValuesTrait;
15-
use Cross\TestUtils\TestCase\GetTargetInstanceTrait;
1615
use Cross\TestUtils\TestCase\TestDefaultAttributesTrait;
1716
use Cross\TestUtils\TestCase\TestUsesTraitsTrait;
1817

1918
/**
2019
* Tests for \Cross\TestUtils\TestCase\TestDefaultAttributesTrait
21-
*
20+
*
2221
* @covers \Cross\TestUtils\TestCase\TestDefaultAttributesTrait
2322
* @author Mathias Gelhausen <gelhausen@cross-solution.de>
2423
*
@@ -30,12 +29,12 @@ class TestDefaultAttributesTraitTest extends \PHPUnit_Framework_TestCase
3029
{
3130
use TestUsesTraitsTrait;
3231

33-
private $usesTraits = [ 'target' => TestDefaultAttributesTrait::class, AssertDefaultAttributesValuesTrait::class, GetTargetInstanceTrait::class ];
32+
private $usesTraits = [ 'target' => TestDefaultAttributesTrait::class, AssertDefaultAttributesValuesTrait::class ];
3433

3534
public function testThrowsExceptionIfPropertyDoesNotExist()
3635
{
3736
$this->expectException(\PHPUnit_Framework_Exception::class);
38-
$this->expectExceptionMessage('must define');
37+
$this->expectExceptionMessage('$defaultAttributes" is not defined');
3938

4039
$target = new class { use TestDefaultAttributesTrait; };
4140

@@ -45,7 +44,7 @@ public function testThrowsExceptionIfPropertyDoesNotExist()
4544
public function testThrowsExceptionIfPropertyIsNotAnArray()
4645
{
4746
$this->expectException(\PHPUnit_Framework_Exception::class);
48-
$this->expectExceptionMessage('be an array');
47+
$this->expectExceptionMessage('not an array');
4948

5049
$target = new class { use TestDefaultAttributesTrait; public $defaultAttributes = 'string'; };
5150

@@ -68,16 +67,8 @@ public function __construct($obj) {
6867

6968
public $defaultAttributes = ['one', 'two'];
7069

71-
public $getTargetInstanceArgs;
72-
7370
public static $assertDefaultAttributesValuesArgs;
7471

75-
public function getTargetInstance()
76-
{
77-
$this->getTargetInstanceArgs = func_get_args();
78-
return $this->target;
79-
}
80-
8172
public static function assertDefaultAttributesValues()
8273
{
8374
static::$assertDefaultAttributesValuesArgs = func_get_args();
@@ -87,15 +78,6 @@ public static function assertDefaultAttributesValues()
8778

8879
$target->testDefaultAttributes();
8980

90-
static::assertEquals(
91-
[
92-
['getDefaultAttributesTarget', 'getTarget'],
93-
['defaultAttributesTarget', 'target'],
94-
'defaultAttributes'
95-
],
96-
$target->getTargetInstanceArgs
97-
);
98-
9981
static::assertEquals(
10082
[
10183
$target->defaultAttributes,

test/TestUtilsTest/TestCase/TestInheritanceTraitTest.php

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111

1212
namespace Cross\TestUtilsTest\TestCase;
1313

14+
use Cross\TestUtils\Exception\InvalidUsageException;
15+
1416
use Cross\TestUtils\TestCase\AssertInheritanceTrait;
1517
use Cross\TestUtils\TestCase\TestInheritanceTrait;
1618
use Cross\TestUtils\TestCase\TestUsesTraitsTrait;
17-
use Cross\TestUtils\TestCase\GetTargetInstanceTrait;
1819

1920
/**
2021
* Tests for \Cross\TestUtils\TestCase\TestInheritanceTrait
@@ -33,13 +34,12 @@ class TestInheritanceTraitTest extends \PHPUnit_Framework_TestCase
3334
private $usesTraits = [
3435
'target' => TestInheritanceTrait::class,
3536
AssertInheritanceTrait::class,
36-
GetTargetInstanceTrait::class
3737
];
3838

3939
public function testThrowsExceptionIfPropertyDoesNotExist()
4040
{
41-
$this->expectException(\PHPUnit_Framework_Exception::class);
42-
$this->expectExceptionMessage('must define');
41+
$this->expectException(InvalidUsageException::class);
42+
$this->expectExceptionMessage('is not defined');
4343

4444
$target = new class
4545
{
@@ -51,8 +51,8 @@ public function testThrowsExceptionIfPropertyDoesNotExist()
5151

5252
public function testThrowsExceptionIfPropertyIsNotAnArray()
5353
{
54-
$this->expectException(\PHPUnit_Framework_Exception::class);
55-
$this->expectExceptionMessage('be an array');
54+
$this->expectException(InvalidUsageException::class);
55+
$this->expectExceptionMessage('not an array');
5656

5757
$target = new class
5858
{
@@ -81,16 +81,8 @@ public function __construct($obj)
8181

8282
public $inheritance = ['one', 'two'];
8383

84-
public $getTargetInstanceArgs;
85-
8684
public static $assertInheritanceArgs;
8785

88-
public function getTargetInstance()
89-
{
90-
$this->getTargetInstanceArgs = func_get_args();
91-
return $this->target;
92-
}
93-
9486
public static function assertInheritance()
9587
{
9688
static::$assertInheritanceArgs = func_get_args();
@@ -100,15 +92,6 @@ public static function assertInheritance()
10092

10193
$target->testInheritance();
10294

103-
static::assertEquals(
104-
[
105-
['getInheritanceTarget', 'getTarget'],
106-
['inheritanceTarget', 'target'],
107-
'inheritance'
108-
],
109-
$target->getTargetInstanceArgs
110-
);
111-
11295
static::assertEquals(
11396
[
11497
$target->inheritance,

test/TestUtilsTest/TestCase/TestSetterAndGetterTraitTest.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,12 @@
1111

1212
namespace Cross\TestUtilsTest\TestCase;
1313

14-
use Cross\TestUtils\TestCase\GetTargetInstanceTrait;
15-
use Cross\TestUtils\TestCase\SimpleModel;
1614
use Cross\TestUtils\TestCase\TestSetterAndGetterTrait;
1715
use Cross\TestUtils\TestCase\TestUsesTraitsTrait;
1816

1917
/**
2018
* Tests for \Cross\TestUtils\TestCase\TestSetterAndGetterTrait
21-
*
19+
*
2220
* @covers \Cross\TestUtils\TestCase\TestSetterAndGetterTrait
2321
* @author Mathias Gelhausen <gelhausen@cross-solution.de>
2422
*
@@ -30,7 +28,7 @@ class TestSetterAndGetterTraitTest extends \PHPUnit_Framework_TestCase
3028
{
3129
use TestUsesTraitsTrait;
3230

33-
private $usesTraits = ['target' => TestSetterAndGetterTrait::class, GetTargetInstanceTrait::class];
31+
private $usesTraits = ['target' => TestSetterAndGetterTrait::class];
3432

3533
public function testSetterAndGetterDataReturnsPropertyValue()
3634
{
@@ -48,13 +46,13 @@ public function testSetterAndGetterDataReturnsEmptyArray()
4846

4947
public function testReturnsNullIfSpecIsNotGiven()
5048
{
51-
$target = new class
52-
{
53-
use TestSetterAndGetterTrait;
54-
public $normalizeCalled = false;
55-
public function setterAndGetterNormalizeSpec($spec, $name, $target)
56-
{
57-
$this->normalizeCalled = true;
49+
$target = new class
50+
{
51+
use TestSetterAndGetterTrait;
52+
public $normalizeCalled = false;
53+
public function setterAndGetterNormalizeSpec($spec, $name, $target)
54+
{
55+
$this->normalizeCalled = true;
5856
}
5957
};
6058

0 commit comments

Comments
 (0)