|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * CROSS PHPunit Utils |
| 4 | + * |
| 5 | + * @filesource |
| 6 | + * @copyright 2019 Cross Solution <https://www.cross-solution.de> |
| 7 | + * @license MIT |
| 8 | + */ |
| 9 | + |
| 10 | +declare(strict_types=1); |
| 11 | + |
| 12 | +namespace Cross\TestUtilsTest\Utils; |
| 13 | + |
| 14 | +use Cross\TestUtils\Exception\InvalidUsageException; |
| 15 | + |
| 16 | +use Cross\TestUtils\Utils\Instance; |
| 17 | + |
| 18 | +/** |
| 19 | + * Tests for \Cross\TestUtils\Utils\Instance |
| 20 | + * |
| 21 | + * @covers \Cross\TestUtils\Utils\Instance |
| 22 | + * @author Mathias Gelhausen <gelhausen@cross-solution.de> |
| 23 | + * |
| 24 | + * @group Cross.TestUtils |
| 25 | + * @group Cross.TestUtils.Utils |
| 26 | + * @group Cross.TestUtils.Utils.Instance |
| 27 | + */ |
| 28 | +class InstanceTest extends \PHPUnit_Framework_TestCase |
| 29 | +{ |
| 30 | + |
| 31 | + public function createsReflectionData() |
| 32 | + { |
| 33 | + return [ |
| 34 | + 'fromString' => [\stdClass::class], |
| 35 | + 'fromObject' => [new \stdClass()], |
| 36 | + 'fromArrayNumeric' => [[\stdClass::class, 'elem1', 'elem2']], |
| 37 | + 'fromArray' => [['elem', 'class' => \stdClass::class]], |
| 38 | + ]; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @dataProvider createsReflectionData |
| 43 | + * |
| 44 | + * @param mixed $spec |
| 45 | + * @return void |
| 46 | + */ |
| 47 | + public function testCreatesReflection($spec) |
| 48 | + { |
| 49 | + $actual = Instance::reflection($spec); |
| 50 | + |
| 51 | + static::assertInstanceOf(\ReflectionClass::class, $actual); |
| 52 | + } |
| 53 | + |
| 54 | + public function createsObjectsData() |
| 55 | + { |
| 56 | + $object = new class |
| 57 | + { |
| 58 | + public $args; |
| 59 | + |
| 60 | + public function __construct(...$args) |
| 61 | + { |
| 62 | + $this->args = $args; |
| 63 | + } |
| 64 | + }; |
| 65 | + $fqcn = get_class($object); |
| 66 | + |
| 67 | + return [ |
| 68 | + [$fqcn, [], $fqcn], |
| 69 | + [$fqcn, ['arg1'], $fqcn, 'arg1'], |
| 70 | + [$fqcn, ['arg1'], [$fqcn, 'arg1'], 'arg2'], |
| 71 | + [\ReflectionClass::class, false, "!$fqcn"] |
| 72 | + ]; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @dataProvider createsObjectsData |
| 77 | + * @param string $fqcn |
| 78 | + * @param array|false $expect |
| 79 | + * @param array ...$args |
| 80 | + * @return void |
| 81 | + */ |
| 82 | + public function testCreatesObjects($fqcn, $expect, ...$args) |
| 83 | + { |
| 84 | + $actual = Instance::create(...$args); |
| 85 | + |
| 86 | + static::assertInstanceOf($fqcn, $actual); |
| 87 | + if (false !== $expect) { |
| 88 | + static::assertEquals($expect, $actual->args); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + public function testCreateThrowsException() |
| 93 | + { |
| 94 | + $this->expectException(InvalidUsageException::class); |
| 95 | + $this->expectExceptionMessage('string as FQCN'); |
| 96 | + |
| 97 | + Instance::create(1); |
| 98 | + } |
| 99 | + |
| 100 | + public function mappedArgumentsData() |
| 101 | + { |
| 102 | + $object = new class |
| 103 | + { |
| 104 | + public $called; |
| 105 | + public $args; |
| 106 | + |
| 107 | + public function __construct(...$args) |
| 108 | + { |
| 109 | + $this->args = $args; |
| 110 | + } |
| 111 | + }; |
| 112 | + $context = new class |
| 113 | + { |
| 114 | + public $called; |
| 115 | + |
| 116 | + public function __call($method, $args) |
| 117 | + { |
| 118 | + $this->called[] = $method; |
| 119 | + return '__mapped__'; |
| 120 | + } |
| 121 | + }; |
| 122 | + $context2 = new class |
| 123 | + { |
| 124 | + private function privateCallback() |
| 125 | + { |
| 126 | + return '__mapped__'; |
| 127 | + } |
| 128 | + }; |
| 129 | + |
| 130 | + $fqcn = get_class($object); |
| 131 | + |
| 132 | + return [ |
| 133 | + // expectFqcn, expectArgs, expectCalled, fqcn, arguments, context |
| 134 | + [$fqcn, [], null, $fqcn, [], null], |
| 135 | + [$fqcn, ['arg1', 'arg2'], false, $fqcn, ['some' => 'arg1', 'arg2'], null], |
| 136 | + [$fqcn, ['@'], null, $fqcn, ['arg' => '@'], $context], |
| 137 | + [$fqcn, ['__mapped__', phpversion()], ['arg'], $fqcn, ['arg' => $context, 'rev' => 'phpversion'], $context], |
| 138 | + [$fqcn, [phpversion(), '__mapped__'], ['arg'], $fqcn, ['@phpversion', '@arg'], $context], |
| 139 | + [ |
| 140 | + $fqcn, |
| 141 | + [phpversion(), '__mapped__', '__mapped__'], |
| 142 | + ['arg', 'arg2'], |
| 143 | + $fqcn, |
| 144 | + [['@' => 'phpversion'], ['@' => 'arg'], ['@' => [$context, 'arg2']]], |
| 145 | + $context |
| 146 | + ], |
| 147 | + [$fqcn, ['@nonExistentMethod'], false, $fqcn, ['@nonExistentMethod'], $context2], |
| 148 | + [$fqcn, ['__mapped__'], false, $fqcn, ['@privateCallback'], $context2], |
| 149 | + [$fqcn, ['__mapped__'], ['arg'], [$fqcn, '@arg'], ['ignored'], $context], |
| 150 | + [$fqcn, ['__mapped__'], ['arg'], [$fqcn, '@arg'], $context, null], |
| 151 | + ]; |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * @dataProvider mappedArgumentsData |
| 156 | + * @param string $expectFqcn |
| 157 | + * @param array|false|null $expectArgs |
| 158 | + * @param array|false|null $expectCalled |
| 159 | + * @param string|array $fqcn |
| 160 | + * @param array|object $arguments |
| 161 | + * @param object|null $context |
| 162 | + * @return void |
| 163 | + */ |
| 164 | + public function testCreatesObjectsWithMappedArguments( |
| 165 | + $expectFqcn, |
| 166 | + $expectArgs, |
| 167 | + $expectCalled, |
| 168 | + $fqcn, |
| 169 | + $arguments, |
| 170 | + $context |
| 171 | + ) { |
| 172 | + if ($context) { |
| 173 | + $context->called = null; |
| 174 | + } |
| 175 | + |
| 176 | + $actual = Instance::withMappedArguments($fqcn, $arguments, $context); |
| 177 | + |
| 178 | + static::assertInstanceOf($expectFqcn, $actual); |
| 179 | + if (false !== $expectArgs) { |
| 180 | + static::assertEquals($expectArgs, $actual->args); |
| 181 | + } |
| 182 | + if (false !== $expectCalled && $context) { |
| 183 | + static::assertEquals($expectCalled, $context->called); |
| 184 | + } |
| 185 | + } |
| 186 | +} |
0 commit comments