Skip to content

Commit 7b061f9

Browse files
committed
Merge pull request #6: Release 2.0.1
* pr-6: Fix #5: Setting expected values does not work properly Create TestCase that reproduce unwanted behaviour
2 parents 0eec4a7 + d02137d commit 7b061f9

File tree

2 files changed

+109
-3
lines changed

2 files changed

+109
-3
lines changed

src/TestCase/TestSetterAndGetterTrait.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public function testSetterAndGetter($name, $spec = null): void
154154
[$setter, $setterArgs] = $spec['setter'];
155155
$setterValue = $target->$setter($value, ...$setterArgs);
156156

157-
if ('__SETTER_AND_GETTER__' != $spec['setter_value']) {
157+
if ($spec['setter_value'] !== '__SETTER_AND_GETTER__') {
158158
$spec['setter_assert']($spec['setter_value'], $setterValue);
159159
}
160160
}
@@ -163,7 +163,7 @@ public function testSetterAndGetter($name, $spec = null): void
163163
[$getter, $getterArgs] = $spec['getter'];
164164
$getterValue = $target->$getter(...$getterArgs);
165165

166-
if ($spec['expect'] != '__SETTER_AND_GETTER__') {
166+
if ($spec['expect'] !== '__SETTER_AND_GETTER__') {
167167
$value = $spec['expect'];
168168
}
169169

@@ -210,7 +210,7 @@ private function setterAndGetterNormalizeSpec($spec, string $name, object $targe
210210
break;
211211

212212
case 'setter_value':
213-
if ('__SELF__' == $value) {
213+
if ('__SELF__' === $value) {
214214
$value = $target;
215215
if (!isset($spec['setter_assert'])) {
216216
$normalized['setter_assert'] = [static::class, 'assertSame'];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
/**
4+
* phpunit-utils
5+
*
6+
* @filesource
7+
* @copyright 2020 CROSS Solution <https://www.cross-solution.de>
8+
* @license MIT
9+
*/
10+
11+
declare(strict_types=1);
12+
namespace Cross\TestUtilsTest\TestCase\TestSetterAndGetterTrait;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Cross\TestUtils\TestCase\TestSetterAndGetterTrait;
16+
17+
/**
18+
* Testcase for \Cross\TestUtils\TestCase\TestSetterAndGetterTrait
19+
*
20+
* @covers \Cross\TestUtils\TestCase\TestSetterAndGetterTrait
21+
* @author Mathias Gelhausen <[email protected]>
22+
* @group Cross.TestUtils
23+
* @group Cross.TestUtils.TestCase
24+
* @group Cross.TestUtils.TestCase.TestSetterAndGetterTrait
25+
*/
26+
class SpecifyExpectedValueTest extends TestCase
27+
{
28+
public function setUp(): void
29+
{
30+
$dummy = new class
31+
{
32+
public $attr;
33+
public $expect = 'expected';
34+
35+
public function setAttr($v)
36+
{
37+
$this->attr = $v;
38+
return $this->expect;
39+
}
40+
41+
public function getAttr()
42+
{
43+
return $this->expect;
44+
}
45+
};
46+
47+
$this->target = new class($dummy)
48+
{
49+
use TestSetterAndGetterTrait;
50+
51+
public $target;
52+
public $testSetterAndGetter;
53+
public static $result;
54+
55+
public function __construct($dummy)
56+
{
57+
$this->target = $dummy;
58+
static::$result = null;
59+
}
60+
61+
public static function assertEquals($expect)
62+
{
63+
static::$result = $expect;
64+
}
65+
66+
public static function assertSame($expect)
67+
{
68+
static::$result = $expect;
69+
}
70+
};
71+
}
72+
73+
/**
74+
* @testWith [true, "a string"]
75+
* [true, [1,2,3]]
76+
* [true, "stdClass", "object"]
77+
*
78+
*/
79+
public function testSpecifyingExpectedGetterValues($expect, $value, $type = null)
80+
{
81+
$this->target->target->expect = $expect;
82+
$this->target->testSetterAndGetter(
83+
'attr',
84+
['value' . ($type ? "_$type" : '') => $value, 'expect' => $expect]
85+
);
86+
87+
static::assertEquals($expect, $this->target::$result);
88+
}
89+
90+
/**
91+
* @testWith [true]
92+
*/
93+
public function testSpecifiyingExpectedSetterValues($expect, $type = null)
94+
{
95+
if ($type === 'object') {
96+
$expect = new $expect();
97+
}
98+
$this->target->target->expect = $expect;
99+
$this->target->testSetterAndGetter(
100+
'attr',
101+
['getter' => false, 'value' => 'irrelevant', 'setter_value' => $expect]
102+
);
103+
104+
static::assertEquals($expect, $this->target::$result);
105+
}
106+
}

0 commit comments

Comments
 (0)