|
| 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