Skip to content

Commit

Permalink
Merge pull request #6: Release 2.0.1
Browse files Browse the repository at this point in the history
* pr-6:
  Fix #5: Setting expected values does not work properly
  Create TestCase that reproduce unwanted behaviour
  • Loading branch information
TiSiE committed Feb 19, 2020
2 parents 0eec4a7 + d02137d commit 7b061f9
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/TestCase/TestSetterAndGetterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand All @@ -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'];
}

Expand Down Expand Up @@ -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'];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

/**
* phpunit-utils
*
* @filesource
* @copyright 2020 CROSS Solution <https://www.cross-solution.de>
* @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 <[email protected]>
* @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);
}
}

0 comments on commit 7b061f9

Please sign in to comment.