1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Spiral Framework. |
5
|
|
|
* |
6
|
|
|
* @license MIT |
7
|
|
|
* @author Anton Titov (Wolfy-J) |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Spiral\Tests\Core; |
13
|
|
|
|
14
|
|
|
use PHPUnit\Framework\TestCase; |
15
|
|
|
use Psr\Container\ContainerExceptionInterface; |
16
|
|
|
use Spiral\Core\Container; |
17
|
|
|
use Spiral\Core\Exception\Container\ArgumentException; |
18
|
|
|
use Spiral\Core\Exception\Container\AutowireException; |
19
|
|
|
use Spiral\Core\Exception\Container\ContainerException; |
20
|
|
|
use Spiral\Core\Exception\DependencyException; |
21
|
|
|
use Spiral\Core\Exception\LogicException; |
22
|
|
|
|
23
|
|
|
class ExceptionsTest extends TestCase |
24
|
|
|
{ |
25
|
|
|
public function testInvalidBinding(): void |
26
|
|
|
{ |
27
|
|
|
$this->expectExceptionMessage("Invalid binding for 'invalid'"); |
28
|
|
|
$this->expectException(\Spiral\Core\Exception\Container\ContainerException::class); |
29
|
|
|
$container = new Container(); |
30
|
|
|
$container->bind('invalid', ['invalid']); |
31
|
|
|
$container->get('invalid'); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testClone(): void |
35
|
|
|
{ |
36
|
|
|
$this->expectException(LogicException::class); |
37
|
|
|
$container = new Container(); |
38
|
|
|
clone $container; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testInvalidInjectionParameter(): void |
42
|
|
|
{ |
43
|
|
|
$this->expectExceptionMessage("Class Spiral\Tests\Core\InvalidClass does not exist"); |
44
|
|
|
$this->expectException(\Spiral\Core\Exception\Container\ContainerException::class); |
45
|
|
|
$container = new Container(); |
46
|
|
|
|
47
|
|
|
$container->resolveArguments(new \ReflectionMethod($this, 'invalidInjection')); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testArgumentException(string $param = null): void |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
$method = new \ReflectionMethod($this, 'testArgumentException'); |
53
|
|
|
|
54
|
|
|
$e = new ArgumentException( |
55
|
|
|
$method->getParameters()[0], |
56
|
|
|
$method |
57
|
|
|
); |
58
|
|
|
|
59
|
|
|
$this->assertInstanceOf(AutowireException::class, $e); |
60
|
|
|
$this->assertInstanceOf(ContainerException::class, $e); |
61
|
|
|
$this->assertInstanceOf(DependencyException::class, $e); |
62
|
|
|
$this->assertInstanceOf(ContainerExceptionInterface::class, $e); |
63
|
|
|
|
64
|
|
|
$this->assertSame($method, $e->getContext()); |
65
|
|
|
$this->assertSame('param', $e->getParameter()->getName()); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
protected function invalidInjection(InvalidClass $class): void |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.