Skip to content

Commit f9186b0

Browse files
committed
Enhance TestSetterAndGetterTrait
* Allow scalar values to be passed as spec (not only strings) * Throws exception if neither is defined nor the method setterAndGetterData is overridden.
1 parent d171a2e commit f9186b0

2 files changed

Lines changed: 21 additions & 5 deletions

File tree

src/TestCase/TestSetterAndGetterTrait.php

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

1212
namespace Cross\TestUtils\TestCase;
1313

14+
use Cross\TestUtils\Exception\InvalidUsageException;
1415
use Cross\TestUtils\Utils\Target;
1516

1617
/**
@@ -107,7 +108,15 @@ trait TestSetterAndGetterTrait
107108
*/
108109
public function setterAndGetterData(): array
109110
{
110-
return property_exists($this, 'setterAndGetter') ? $this->setterAndGetter : [];
111+
if (!property_exists($this, 'setterAndGetter')) {
112+
throw InvalidUsageException::fromTrait(
113+
__TRAIT__,
114+
__CLASS__,
115+
'Property $setterAndGetter is not defined and method setterAndGetter is not overridden.'
116+
);
117+
}
118+
119+
return $this->setterAndGetter;
111120
}
112121

113122
/**
@@ -198,7 +207,7 @@ private function setterAndGetterNormalizeSpec($spec, string $name, object $targe
198207
'exception' => null,
199208
];
200209

201-
if (is_string($spec)) {
210+
if (is_scalar($spec)) {
202211
$normalized['value'] = $spec;
203212
return $normalized;
204213
}

test/TestUtilsTest/TestCase/TestSetterAndGetterTraitTest.php

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

1212
namespace Cross\TestUtilsTest\TestCase;
1313

14+
use Cross\TestUtils\Exception\InvalidUsageException;
15+
1416
use Cross\TestUtils\TestCase\TestSetterAndGetterTrait;
1517
use Cross\TestUtils\TestCase\TestUsesTraitsTrait;
1618

@@ -37,11 +39,14 @@ public function testSetterAndGetterDataReturnsPropertyValue()
3739
static::assertEquals([['prop', 'value']], $target->setterAndGetterData());
3840
}
3941

40-
public function testSetterAndGetterDataReturnsEmptyArray()
42+
public function testSetterAndGetterDataThrowsExceptionIfNotOverridden()
4143
{
4244
$target = new class { use TestSetterAndGetterTrait;};
4345

44-
static::assertEquals([], $target->setterAndGetterData());
46+
$this->expectException(InvalidUsageException::class);
47+
$this->expectExceptionMessage('$setterAndGetter is not defined');
48+
49+
$target->setterAndGetterData();
4550
}
4651

4752
public function testReturnsNullIfSpecIsNotGiven()
@@ -105,6 +110,8 @@ public function normalizationData() : array
105110
{
106111
return [
107112
['value', ['value' => 'value']],
113+
[10, ['value' => 10]],
114+
[true, ['value' => true]],
108115
[new \stdClass, 'Must be array'],
109116
[['property' => true], ['property' => ['prop', '__VALUE__']]],
110117
[['property' => ['test']], ['property' => ['prop', 'test']]],
@@ -215,7 +222,7 @@ public function callback() {}
215222

216223
if (is_array($expect)) {
217224
foreach ($expect as $key => $val) {
218-
if ('__TARGET__' == $val) {
225+
if ('__TARGET__' === $val) {
219226
static::assertSame($target->target, $normalized[$key]);
220227
} else {
221228
static::assertEquals($val, $normalized[$key]);

0 commit comments

Comments
 (0)