From d00512bebdda85f961d257f3dbee174124ed5ef9 Mon Sep 17 00:00:00 2001 From: Mathias Gelhausen Date: Wed, 19 Feb 2020 13:02:03 +0100 Subject: [PATCH 1/2] Create TestCase that reproduce unwanted behaviour (refs #5) --- .../SpecifyExpectedValueTest.php | 106 ++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 test/TestUtilsTest/TestCase/TestSetterAndGetterTrait/SpecifyExpectedValueTest.php diff --git a/test/TestUtilsTest/TestCase/TestSetterAndGetterTrait/SpecifyExpectedValueTest.php b/test/TestUtilsTest/TestCase/TestSetterAndGetterTrait/SpecifyExpectedValueTest.php new file mode 100644 index 0000000..a95577e --- /dev/null +++ b/test/TestUtilsTest/TestCase/TestSetterAndGetterTrait/SpecifyExpectedValueTest.php @@ -0,0 +1,106 @@ + + * @license MIT + */ + +declare(strict_types=1); +namespace Cross\TestUtilsTest\TestCase\TestSetterAndGetterTrait; + +use PHPUnit\Framework\TestCase; +use Cross\TestUtils\TestCase\TestSetterAndGetterTrait; + +/** + * Testcase for \Cross\TestUtils\TestCase\TestSetterAndGetterTrait + * + * @covers \Cross\TestUtils\TestCase\TestSetterAndGetterTrait + * @author Mathias Gelhausen + * @group Cross.TestUtils + * @group Cross.TestUtils.TestCase + * @group Cross.TestUtils.TestCase.TestSetterAndGetterTrait + */ +class SpecifyExpectedValueTest extends TestCase +{ + public function setUp(): void + { + $dummy = new class + { + public $attr; + public $expect = 'expected'; + + public function setAttr($v) + { + $this->attr = $v; + return $this->expect; + } + + public function getAttr() + { + return $this->expect; + } + }; + + $this->target = new class($dummy) + { + use TestSetterAndGetterTrait; + + public $target; + public $testSetterAndGetter; + public static $result; + + public function __construct($dummy) + { + $this->target = $dummy; + static::$result = null; + } + + public static function assertEquals($expect) + { + static::$result = $expect; + } + + public static function assertSame($expect) + { + static::$result = $expect; + } + }; + } + + /** + * @testWith [true, "a string"] + * [true, [1,2,3]] + * [true, "stdClass", "object"] + * + */ + public function testSpecifyingExpectedGetterValues($expect, $value, $type = null) + { + $this->target->target->expect = $expect; + $this->target->testSetterAndGetter( + 'attr', + ['value' . ($type ? "_$type" : '') => $value, 'expect' => $expect] + ); + + static::assertEquals($expect, $this->target::$result); + } + + /** + * @testWith [true] + */ + public function testSpecifiyingExpectedSetterValues($expect, $type = null) + { + if ($type === 'object') { + $expect = new $expect(); + } + $this->target->target->expect = $expect; + $this->target->testSetterAndGetter( + 'attr', + ['getter' => false, 'value' => 'irrelevant', 'setter_value' => $expect] + ); + + static::assertEquals($expect, $this->target::$result); + } +} From d02137d47bd45d970839e3bbcbeeffa5c8ab452a Mon Sep 17 00:00:00 2001 From: Mathias Gelhausen Date: Wed, 19 Feb 2020 13:03:11 +0100 Subject: [PATCH 2/2] Fix #5: Setting expected values does not work properly --- src/TestCase/TestSetterAndGetterTrait.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/TestCase/TestSetterAndGetterTrait.php b/src/TestCase/TestSetterAndGetterTrait.php index 2349d48..ef77f77 100644 --- a/src/TestCase/TestSetterAndGetterTrait.php +++ b/src/TestCase/TestSetterAndGetterTrait.php @@ -154,7 +154,7 @@ public function testSetterAndGetter($name, $spec = null): void [$setter, $setterArgs] = $spec['setter']; $setterValue = $target->$setter($value, ...$setterArgs); - if ('__SETTER_AND_GETTER__' != $spec['setter_value']) { + if ($spec['setter_value'] !== '__SETTER_AND_GETTER__') { $spec['setter_assert']($spec['setter_value'], $setterValue); } } @@ -163,7 +163,7 @@ public function testSetterAndGetter($name, $spec = null): void [$getter, $getterArgs] = $spec['getter']; $getterValue = $target->$getter(...$getterArgs); - if ($spec['expect'] != '__SETTER_AND_GETTER__') { + if ($spec['expect'] !== '__SETTER_AND_GETTER__') { $value = $spec['expect']; } @@ -210,7 +210,7 @@ private function setterAndGetterNormalizeSpec($spec, string $name, object $targe break; case 'setter_value': - if ('__SELF__' == $value) { + if ('__SELF__' === $value) { $value = $target; if (!isset($spec['setter_assert'])) { $normalized['setter_assert'] = [static::class, 'assertSame'];