Skip to content

TestSetterAndGetter: Allow specifying an expected value #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

<!-- TOC depthFrom:2 depthTo:6 withLinks:1 updateOnSave:1 orderedList:0 -->

- [Installation](#installation)
- [Usage](#usage)
- [License](#license)
- [Installation](#Installation)
- [Usage](#Usage)
- [License](#License)

<!-- /TOC -->

Expand Down
38 changes: 26 additions & 12 deletions src/TestCase/TestSetterAndGetterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@
* * 'value_callback': callable || method name (in the TestCase class)
* The return value of that callback is taken as value for the test.
*
* * 'expect': If the setter modifies the value, you can specify the expected value
* for the getter here.
* * 'expect_object': see 'value_object'
* * 'expect_callback': see 'value_callback'
*
* * 'getter': - Name of getter method (a '*' gets replaced by the property name)
* - [GetterName, [arg, arg, ...]]
* Use this format to provide arguments to the getter method.
Expand Down Expand Up @@ -146,17 +151,22 @@ public function testSetterAndGetter($name, $spec = null): void

// Test setter
if (false !== $spec['setter'][0]) {
$setter = str_replace('*', $name, $spec['setter'][0]);
$setterValue = $target->$setter($value, ...$spec['setter'][1]);
[$setter, $setterArgs] = $spec['setter'];
$setterValue = $target->$setter($value, ...$setterArgs);

if ('__SETTER_AND_GETTER__' != $spec['setter_value']) {
$spec['setter_assert']($spec['setter_value'], $setterValue);
}
}

if (false !== $spec['getter'][0]) {
$getter = str_replace('*', $name, $spec['getter'][0]);
$getterValue = $target->$getter(...$spec['getter'][1]);
[$getter, $getterArgs] = $spec['getter'];
$getterValue = $target->$getter(...$getterArgs);

if ($spec['expect'] != '__SETTER_AND_GETTER__') {
$value = $spec['expect'];
}

$spec['assert']($value, $getterValue);
}
}
Expand All @@ -173,12 +183,13 @@ public function testSetterAndGetter($name, $spec = null): void
private function setterAndGetterNormalizeSpec($spec, string $name, object $target): array
{
$normalized = [
'getter' => ["get*", []],
'getter' => ["get$name", []],
'assert' => [static::class, 'assertEquals'],
'setter' => ["set*", []],
'setter' => ["set$name", []],
'setter_assert' => [static::class, 'assertEquals'],
'setter_value' => '__SETTER_AND_GETTER__',
'value' => null,
'expect' => '__SETTER_AND_GETTER__',
'exception' => null,
];

Expand Down Expand Up @@ -213,10 +224,15 @@ private function setterAndGetterNormalizeSpec($spec, string $name, object $targe
$value = [$value, []];
}

if (is_string($value[0])) {
$value[0] = str_replace('*', $name, $value[0]);
}

break;

case 'value_object':
case 'setter_value_object':
case 'expect_object':
if (!is_array($value)) {
$value = [$value];
}
Expand All @@ -228,23 +244,21 @@ private function setterAndGetterNormalizeSpec($spec, string $name, object $targe

$assertKey = str_replace('value', 'assert', $key);

if (!isset($spec[$assertKey])) {
if (!isset($spec[$assertKey]) && array_key_exists($assertKey, $normalized)) {
$normalized[$assertKey] = [static::class, 'assertSame'];
}
if ('value' == $key && !isset($spec['property_assert'])) {
$normalized['property_assert'] = [static::class, 'assertAttributeSame'];
}

break;

case 'value_callback':
case 'setter_value_callback':
case 'expect_callback':
if (is_string($value)) {
$value = [$this, $value];
}

if (!is_callable($value)) {
throw new \PHPUnit\Framework\Exception($err . 'Invalid value callback.');
throw new \PHPUnit\Framework\Exception($err . 'Invalid callback for "' . $key . '".');
}

$key = substr($key, 0, -9);
Expand All @@ -263,7 +277,7 @@ private function setterAndGetterNormalizeSpec($spec, string $name, object $targe
}

if (!is_callable($value)) {
throw new \PHPUnit\Framework\Exception($err . 'Invalid assert callback.');
throw new \PHPUnit\Framework\Exception($err . 'Invalid callback for "' . $key . '".');
}

break;
Expand Down
35 changes: 33 additions & 2 deletions test/TestUtilsTest/TestCase/TestSetterAndGetterTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ public function normalizationData() : array
['setter_value' => '__SELF__'],
['setter_value' => '__TARGET__']
],
[
['value' => 'value', 'expect' => 'expect'],
['value' => 'value', 'expect' => 'expect'],
],

[
[
Expand All @@ -134,10 +138,12 @@ public function normalizationData() : array
[
'value_object' => \stdClass::class,
'setter_value_object' => [\stdClass::class, ['arg']],
'expect_object' => \stdClass::class,
],
[
'value' => new \stdClass,
'setter_value' => new \stdClass,
'expect' => new \stdClass,
]
],

Expand All @@ -153,17 +159,22 @@ public function normalizationData() : array

[
['value_callback' => 'unallable'],
'Invalid value callback',
'Invalid callback',
],

[
['value_callback' => function() { return 'calledValue'; }],
['value' => 'calledValue'],
],

[
['expect_callback' => function() { return 'calledback'; }],
['expect' => 'calledback']
],

[
['assert' => [$this, 'uncallable']],
'Invalid assert callback'
'Invalid callback'
],
[
['nonexistent' => 'papp'],
Expand Down Expand Up @@ -351,4 +362,24 @@ public function testGetterAssertion()
static::assertEquals(['value'], $trait->target->called['setprop'][0]);
static::assertEquals(['value', 'value'], $trait->target->called['assert'][0]);
}

public function testExpectedValueWillBePassedToGetterAssertion()
{
$trait = $this->getConcreteTrait();

$trait->target->return['getprop'][] = 'modifiedValueFromGetter';

$assertVars = [];
$spec = [
'value' => 'value',
'expect' => 'modifiedValue',
'assert' => function($expect, $actual) use (&$assertVars) {
$assertVars = [$expect, $actual];
}
];

$trait->testSetterAndGetter('prop', $spec);

static::assertEquals(['modifiedValue', 'modifiedValueFromGetter'], $assertVars);
}
}