Skip to content

Commit e61d5b1

Browse files
committed
Merge pull request #2
* pr-2: TestSetterAndGetter: Allow specifiying an expected value Update TOC
2 parents 67c9a7b + 9059f8f commit e61d5b1

File tree

3 files changed

+62
-17
lines changed

3 files changed

+62
-17
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

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

6-
- [Installation](#installation)
7-
- [Usage](#usage)
8-
- [License](#license)
6+
- [Installation](#Installation)
7+
- [Usage](#Usage)
8+
- [License](#License)
99

1010
<!-- /TOC -->
1111

src/TestCase/TestSetterAndGetterTrait.php

+26-12
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@
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+
* * 'expect': If the setter modifies the value, you can specify the expected value
52+
* for the getter here.
53+
* * 'expect_object': see 'value_object'
54+
* * 'expect_callback': see 'value_callback'
55+
*
5156
* * 'getter': - Name of getter method (a '*' gets replaced by the property name)
5257
* - [GetterName, [arg, arg, ...]]
5358
* Use this format to provide arguments to the getter method.
@@ -146,17 +151,22 @@ public function testSetterAndGetter($name, $spec = null): void
146151

147152
// Test setter
148153
if (false !== $spec['setter'][0]) {
149-
$setter = str_replace('*', $name, $spec['setter'][0]);
150-
$setterValue = $target->$setter($value, ...$spec['setter'][1]);
154+
[$setter, $setterArgs] = $spec['setter'];
155+
$setterValue = $target->$setter($value, ...$setterArgs);
151156

152157
if ('__SETTER_AND_GETTER__' != $spec['setter_value']) {
153158
$spec['setter_assert']($spec['setter_value'], $setterValue);
154159
}
155160
}
156161

157162
if (false !== $spec['getter'][0]) {
158-
$getter = str_replace('*', $name, $spec['getter'][0]);
159-
$getterValue = $target->$getter(...$spec['getter'][1]);
163+
[$getter, $getterArgs] = $spec['getter'];
164+
$getterValue = $target->$getter(...$getterArgs);
165+
166+
if ($spec['expect'] != '__SETTER_AND_GETTER__') {
167+
$value = $spec['expect'];
168+
}
169+
160170
$spec['assert']($value, $getterValue);
161171
}
162172
}
@@ -173,12 +183,13 @@ public function testSetterAndGetter($name, $spec = null): void
173183
private function setterAndGetterNormalizeSpec($spec, string $name, object $target): array
174184
{
175185
$normalized = [
176-
'getter' => ["get*", []],
186+
'getter' => ["get$name", []],
177187
'assert' => [static::class, 'assertEquals'],
178-
'setter' => ["set*", []],
188+
'setter' => ["set$name", []],
179189
'setter_assert' => [static::class, 'assertEquals'],
180190
'setter_value' => '__SETTER_AND_GETTER__',
181191
'value' => null,
192+
'expect' => '__SETTER_AND_GETTER__',
182193
'exception' => null,
183194
];
184195

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

227+
if (is_string($value[0])) {
228+
$value[0] = str_replace('*', $name, $value[0]);
229+
}
230+
216231
break;
217232

218233
case 'value_object':
219234
case 'setter_value_object':
235+
case 'expect_object':
220236
if (!is_array($value)) {
221237
$value = [$value];
222238
}
@@ -228,23 +244,21 @@ private function setterAndGetterNormalizeSpec($spec, string $name, object $targe
228244

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

231-
if (!isset($spec[$assertKey])) {
247+
if (!isset($spec[$assertKey]) && array_key_exists($assertKey, $normalized)) {
232248
$normalized[$assertKey] = [static::class, 'assertSame'];
233249
}
234-
if ('value' == $key && !isset($spec['property_assert'])) {
235-
$normalized['property_assert'] = [static::class, 'assertAttributeSame'];
236-
}
237250

238251
break;
239252

240253
case 'value_callback':
241254
case 'setter_value_callback':
255+
case 'expect_callback':
242256
if (is_string($value)) {
243257
$value = [$this, $value];
244258
}
245259

246260
if (!is_callable($value)) {
247-
throw new \PHPUnit\Framework\Exception($err . 'Invalid value callback.');
261+
throw new \PHPUnit\Framework\Exception($err . 'Invalid callback for "' . $key . '".');
248262
}
249263

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

265279
if (!is_callable($value)) {
266-
throw new \PHPUnit\Framework\Exception($err . 'Invalid assert callback.');
280+
throw new \PHPUnit\Framework\Exception($err . 'Invalid callback for "' . $key . '".');
267281
}
268282

269283
break;

test/TestUtilsTest/TestCase/TestSetterAndGetterTraitTest.php

+33-2
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ public function normalizationData() : array
113113
['setter_value' => '__SELF__'],
114114
['setter_value' => '__TARGET__']
115115
],
116+
[
117+
['value' => 'value', 'expect' => 'expect'],
118+
['value' => 'value', 'expect' => 'expect'],
119+
],
116120

117121
[
118122
[
@@ -134,10 +138,12 @@ public function normalizationData() : array
134138
[
135139
'value_object' => \stdClass::class,
136140
'setter_value_object' => [\stdClass::class, ['arg']],
141+
'expect_object' => \stdClass::class,
137142
],
138143
[
139144
'value' => new \stdClass,
140145
'setter_value' => new \stdClass,
146+
'expect' => new \stdClass,
141147
]
142148
],
143149

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

154160
[
155161
['value_callback' => 'unallable'],
156-
'Invalid value callback',
162+
'Invalid callback',
157163
],
158164

159165
[
160166
['value_callback' => function() { return 'calledValue'; }],
161167
['value' => 'calledValue'],
162168
],
163169

170+
[
171+
['expect_callback' => function() { return 'calledback'; }],
172+
['expect' => 'calledback']
173+
],
174+
164175
[
165176
['assert' => [$this, 'uncallable']],
166-
'Invalid assert callback'
177+
'Invalid callback'
167178
],
168179
[
169180
['nonexistent' => 'papp'],
@@ -351,4 +362,24 @@ public function testGetterAssertion()
351362
static::assertEquals(['value'], $trait->target->called['setprop'][0]);
352363
static::assertEquals(['value', 'value'], $trait->target->called['assert'][0]);
353364
}
365+
366+
public function testExpectedValueWillBePassedToGetterAssertion()
367+
{
368+
$trait = $this->getConcreteTrait();
369+
370+
$trait->target->return['getprop'][] = 'modifiedValueFromGetter';
371+
372+
$assertVars = [];
373+
$spec = [
374+
'value' => 'value',
375+
'expect' => 'modifiedValue',
376+
'assert' => function($expect, $actual) use (&$assertVars) {
377+
$assertVars = [$expect, $actual];
378+
}
379+
];
380+
381+
$trait->testSetterAndGetter('prop', $spec);
382+
383+
static::assertEquals(['modifiedValue', 'modifiedValueFromGetter'], $assertVars);
384+
}
354385
}

0 commit comments

Comments
 (0)