Passed
Pull Request — master (#13)
by Aleksei
01:24
created

testInternalStaticCallableReflection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 10
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
            ?stdClass $nullable = null,
63
            &...$variadic
64
        ): void {
65
            array_map(null, func_get_args());
66
        });
67
        $exception = $this->createException($reflection, 'toInverse');
68
69
        $this->assertStringContainsString('function ('
70
            . 'callable $callable,'
71
            . ' object $object,'
72
            . ' Yiisoft\Injector\Tests\Support\ColorInterface $class,'
73
            . ' bool $boolean = false,'
74
            . ' int $int = 10,'
75
            . ' float $float = 0.0,'
76
            . " array \$array = array (\n  0 => 0,\n),"
77
            . ' ?stdClass $nullable = NULL,'
78
            . ' &...$variadic'
79
        . ')', $exception->getMessage());
80
    }
81
    public function testInternalStaticCallableReflection(): void
82
    {
83
        $callable = \Closure::fromCallable('\DateTimeImmutable::createFromMutable');
84
        $reflection = new \ReflectionFunction($callable);
85
        $exception = $this->createException($reflection, 'anyParameter');
86
87
        $this->assertStringContainsString('createFromMutable', $exception->getMessage());
88
        $this->assertStringContainsString('anyParameter', $exception->getMessage());
89
    }
90
    public function testInternalFunctionReflection(): void
91
    {
92
        $reflection = new \ReflectionFunction('\\array_map');
93
        $exception = $this->createException($reflection, 'anyParameter');
94
95
        $this->assertStringContainsString('array_map', $exception->getMessage());
96
        $this->assertStringNotContainsString('\\array_map', $exception->getMessage());
97
        $this->assertStringContainsString('anyParameter', $exception->getMessage());
98
    }
99
    public function testUserFunctionInNameSpaceReflection(): void
100
    {
101
        $reflection = new \ReflectionFunction(__NAMESPACE__ . '\\testFunction');
102
        $exception = $this->createException($reflection, 'anyParameter');
103
104
        $this->assertStringContainsString(__NAMESPACE__ . '\\testFunction', $exception->getMessage());
105
        $this->assertStringContainsString('anyParameter', $exception->getMessage());
106
    }
107
108
    protected function createException(\ReflectionFunctionAbstract $reflection, string $parameter): ArgumentException
109
    {
110
        $class = static::EXCEPTION_CLASS_NAME;
111
        /** @var ArgumentException $exception */
112
        $exception = new $class($reflection, $parameter);
113
        return $exception;
114
    }
115
}
116