|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Injector\Tests; |
|
6
|
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
use Yiisoft\Injector\ArgumentException; |
|
9
|
|
|
use Yiisoft\Injector\Tests\Support\EngineVAZ2101; |
|
10
|
|
|
use Yiisoft\Injector\Tests\Support\MakeEngineMatherWithParam; |
|
11
|
|
|
|
|
12
|
|
|
function testFunction(): void |
|
13
|
|
|
{ |
|
14
|
|
|
return; |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
abstract class ArgumentExceptionTest extends TestCase |
|
18
|
|
|
{ |
|
19
|
|
|
protected const EXCEPTION_CLASS_NAME = ''; |
|
20
|
|
|
|
|
21
|
|
|
public function testConstructorReflection(): void |
|
22
|
|
|
{ |
|
23
|
|
|
$class = MakeEngineMatherWithParam::class; |
|
24
|
|
|
$reflection = (new \ReflectionClass($class))->getConstructor(); |
|
25
|
|
|
$exception = $this->createException($reflection, 'parameter'); |
|
26
|
|
|
|
|
27
|
|
|
$this->assertStringContainsString("{$class}::__construct", $exception->getMessage()); |
|
28
|
|
|
} |
|
29
|
|
|
public function testMethodReflection(): void |
|
30
|
|
|
{ |
|
31
|
|
|
$class = EngineVAZ2101::class; |
|
32
|
|
|
$method = 'rust'; |
|
33
|
|
|
$classReflection = new \ReflectionClass($class); |
|
34
|
|
|
$reflection = $classReflection->getMethod($method); |
|
35
|
|
|
$exception = $this->createException($reflection, 'index'); |
|
36
|
|
|
|
|
37
|
|
|
$this->assertStringContainsString("{$class}::{$method}", $exception->getMessage()); |
|
38
|
|
|
$this->assertStringContainsString('index', $exception->getMessage()); |
|
39
|
|
|
} |
|
40
|
|
|
public function testClosureReflection(): void |
|
41
|
|
|
{ |
|
42
|
|
|
// $callable = \Closure::fromCallable($callable); |
|
43
|
|
|
$reflection = new \ReflectionFunction(fn (bool $toInverse) => !$toInverse); |
|
44
|
|
|
$exception = $this->createException($reflection, 'toInverse'); |
|
45
|
|
|
|
|
46
|
|
|
$this->assertStringContainsString(__NAMESPACE__ . '\\{closure}', $exception->getMessage()); |
|
47
|
|
|
$this->assertStringContainsString('toInverse', $exception->getMessage()); |
|
48
|
|
|
} |
|
49
|
|
|
public function testInternalStaticCallableReflection(): void |
|
50
|
|
|
{ |
|
51
|
|
|
$callable = \Closure::fromCallable('\DateTimeImmutable::createFromMutable'); |
|
52
|
|
|
$reflection = new \ReflectionFunction($callable); |
|
53
|
|
|
$exception = $this->createException($reflection, 'anyParameter'); |
|
54
|
|
|
|
|
55
|
|
|
$this->assertStringContainsString('createFromMutable', $exception->getMessage()); |
|
56
|
|
|
$this->assertStringContainsString('anyParameter', $exception->getMessage()); |
|
57
|
|
|
} |
|
58
|
|
|
public function testInternalFunctionReflection(): void |
|
59
|
|
|
{ |
|
60
|
|
|
$reflection = new \ReflectionFunction('\\array_map'); |
|
61
|
|
|
$exception = $this->createException($reflection, 'anyParameter'); |
|
62
|
|
|
|
|
63
|
|
|
$this->assertStringContainsString('array_map', $exception->getMessage()); |
|
64
|
|
|
$this->assertStringNotContainsString('\\array_map', $exception->getMessage()); |
|
65
|
|
|
$this->assertStringContainsString('anyParameter', $exception->getMessage()); |
|
66
|
|
|
} |
|
67
|
|
|
public function testUserFunctionInNameSpaceReflection(): void |
|
68
|
|
|
{ |
|
69
|
|
|
$reflection = new \ReflectionFunction(__NAMESPACE__ . '\\testFunction'); |
|
70
|
|
|
$exception = $this->createException($reflection, 'anyParameter'); |
|
71
|
|
|
|
|
72
|
|
|
$this->assertStringContainsString(__NAMESPACE__ . '\\testFunction', $exception->getMessage()); |
|
73
|
|
|
$this->assertStringContainsString('anyParameter', $exception->getMessage()); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
protected function createException(\ReflectionFunctionAbstract $reflection, string $parameter): ArgumentException |
|
77
|
|
|
{ |
|
78
|
|
|
$class = static::EXCEPTION_CLASS_NAME; |
|
79
|
|
|
/** @var ArgumentException $exception */ |
|
80
|
|
|
$exception = new $class($reflection, $parameter); |
|
81
|
|
|
return $exception; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|