Passed
Push — master ( 57b861...ca1e70 )
by Alexander
01:33
created

ArgumentExceptionTest::testRichClosureReflection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 26
nc 1
nop 0
dl 0
loc 30
rs 9.504
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Injector\Tests;
6
7
use PHPUnit\Framework\TestCase;
8
use stdClass;
9
use Yiisoft\Injector\ArgumentException;
10
use Yiisoft\Injector\Tests\Support\ColorInterface;
11
use Yiisoft\Injector\Tests\Support\EngineVAZ2101;
12
use Yiisoft\Injector\Tests\Support\MakeEngineMatherWithParam;
13
14
function testFunction(): void
15
{
16
    return;
17
}
18
19
abstract class ArgumentExceptionTest extends TestCase
20
{
21
    protected const EXCEPTION_CLASS_NAME = '';
22
23
    public function testConstructorReflection(): void
24
    {
25
        $class = MakeEngineMatherWithParam::class;
26
        $reflection = (new \ReflectionClass($class))->getConstructor();
27
        $exception = $this->createException($reflection, 'parameter');
28
29
        $this->assertStringContainsString("{$class}::__construct", $exception->getMessage());
30
    }
31
    public function testMethodReflection(): void
32
    {
33
        $class = EngineVAZ2101::class;
34
        $method = 'rust';
35
        $classReflection = new \ReflectionClass($class);
36
        $reflection = $classReflection->getMethod($method);
37
        $exception = $this->createException($reflection, 'index');
38
39
        $this->assertStringContainsString("{$class}::{$method}", $exception->getMessage());
40
        $this->assertStringContainsString('index', $exception->getMessage());
41
    }
42
    public function testSimpleClosureReflection(): void
43
    {
44
        $functionLine = __LINE__ + 1;
45
        $reflection = new \ReflectionFunction(fn (bool $toInverse) => !$toInverse);
46
        $exception = $this->createException($reflection, 'toInverse');
47
48
        $this->assertStringContainsString(__FILE__, $exception->getMessage());
49
        $this->assertStringContainsString(' at line ' . $functionLine, $exception->getMessage());
50
        $this->assertStringContainsString('toInverse', $exception->getMessage());
51
    }
52
    public function testRichClosureReflection(): void
53
    {
54
        $reflection = new \ReflectionFunction(static function (
55
            callable $callable,
56
            object $object,
57
            ColorInterface $class,
58
            bool $boolean = false,
59
            int $int = 10,
60
            float $float = 0.0,
61
            array $array = [0],
62
            ?string $string = '',
63
            ?stdClass $nullable = null,
64
            &...$variadic
65
        ): void {
66
            array_map(null, func_get_args());
67
        });
68
        $exception = $this->createException($reflection, 'toInverse');
69
70
        $this->assertStringContainsString('function ('
71
            . 'callable $callable,'
72
            . ' object $object,'
73
            . ' Yiisoft\Injector\Tests\Support\ColorInterface $class,'
74
            . ' bool $boolean = false,'
75
            . ' int $int = 10,'
76
            . ' float $float = 0.0,'
77
            . " array \$array = array (\n  0 => 0,\n),"
78
            . ' ?string $string = \'\','
79
            . ' ?stdClass $nullable = NULL,'
80
            . ' &...$variadic'
81
        . ')', $exception->getMessage());
82
    }
83
    public function testInternalStaticCallableReflection(): void
84
    {
85
        $callable = \Closure::fromCallable('\DateTimeImmutable::createFromMutable');
86
        $reflection = new \ReflectionFunction($callable);
87
        $exception = $this->createException($reflection, 'anyParameter');
88
89
        $this->assertStringContainsString('createFromMutable', $exception->getMessage());
90
        $this->assertStringContainsString('anyParameter', $exception->getMessage());
91
    }
92
    public function testInternalFunctionReflection(): void
93
    {
94
        $reflection = new \ReflectionFunction('\\array_map');
95
        $exception = $this->createException($reflection, 'anyParameter');
96
97
        $this->assertStringContainsString('array_map', $exception->getMessage());
98
        $this->assertStringNotContainsString('\\array_map', $exception->getMessage());
99
        $this->assertStringContainsString('anyParameter', $exception->getMessage());
100
    }
101
    public function testUserFunctionInNameSpaceReflection(): void
102
    {
103
        $reflection = new \ReflectionFunction(__NAMESPACE__ . '\\testFunction');
104
        $exception = $this->createException($reflection, 'anyParameter');
105
106
        $this->assertStringContainsString(__NAMESPACE__ . '\\testFunction', $exception->getMessage());
107
        $this->assertStringContainsString('anyParameter', $exception->getMessage());
108
    }
109
110
    protected function createException(\ReflectionFunctionAbstract $reflection, string $parameter): ArgumentException
111
    {
112
        $class = static::EXCEPTION_CLASS_NAME;
113
        /** @var ArgumentException $exception */
114
        $exception = new $class($reflection, $parameter);
115
        return $exception;
116
    }
117
}
118