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\ContainerInterface; |
16
|
|
|
use Spiral\Core\Container; |
17
|
|
|
use Spiral\Core\ContainerScope; |
18
|
|
|
use Spiral\Core\Exception\RuntimeException; |
19
|
|
|
use Spiral\Tests\Core\Fixtures\Bucket; |
20
|
|
|
use Spiral\Tests\Core\Fixtures\SampleClass; |
21
|
|
|
|
22
|
|
|
class ScopesTest extends TestCase |
23
|
|
|
{ |
24
|
|
|
public function testScope(): void |
25
|
|
|
{ |
26
|
|
|
$container = $this->createMock(ContainerInterface::class); |
27
|
|
|
|
28
|
|
|
$this->assertNull(ContainerScope::getContainer()); |
29
|
|
|
|
30
|
|
|
$this->assertTrue(ContainerScope::runScope($container, function () use ($container) { |
31
|
|
|
return $container === ContainerScope::getContainer(); |
32
|
|
|
})); |
33
|
|
|
|
34
|
|
|
$this->assertNull(ContainerScope::getContainer()); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testScopeException(): void |
38
|
|
|
{ |
39
|
|
|
$container = $this->createMock(ContainerInterface::class); |
40
|
|
|
|
41
|
|
|
$this->assertNull(ContainerScope::getContainer()); |
42
|
|
|
|
43
|
|
|
try { |
44
|
|
|
$this->assertTrue(ContainerScope::runScope($container, function () use ($container): void { |
|
|
|
|
45
|
|
|
throw new RuntimeException('exception'); |
46
|
|
|
})); |
47
|
|
|
} catch (\Throwable $e) { |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$this->assertInstanceOf(RuntimeException::class, $e); |
|
|
|
|
51
|
|
|
$this->assertNull(ContainerScope::getContainer()); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testContainerScope(): void |
55
|
|
|
{ |
56
|
|
|
$c = new Container(); |
57
|
|
|
$c->bind('bucket', new Bucket('a')); |
|
|
|
|
58
|
|
|
|
59
|
|
|
$this->assertSame('a', $c->get('bucket')->getName()); |
60
|
|
|
$this->assertFalse($c->has('other')); |
61
|
|
|
|
62
|
|
|
$this->assertTrue($c->runScope([ |
63
|
|
|
'bucket' => new Bucket('b'), |
64
|
|
|
'other' => new SampleClass() |
65
|
|
|
], function () use ($c) { |
66
|
|
|
$this->assertSame('b', $c->get('bucket')->getName()); |
67
|
|
|
$this->assertTrue($c->has('other')); |
68
|
|
|
|
69
|
|
|
return $c->get('bucket')->getName() == 'b' && $c->has('other'); |
70
|
|
|
})); |
71
|
|
|
|
72
|
|
|
$this->assertSame('a', $c->get('bucket')->getName()); |
73
|
|
|
$this->assertFalse($c->has('other')); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function testContainerScopeException(): void |
77
|
|
|
{ |
78
|
|
|
$c = new Container(); |
79
|
|
|
$c->bind('bucket', new Bucket('a')); |
|
|
|
|
80
|
|
|
|
81
|
|
|
$this->assertSame('a', $c->get('bucket')->getName()); |
82
|
|
|
$this->assertFalse($c->has('other')); |
83
|
|
|
|
84
|
|
|
$this->assertTrue($c->runScope([ |
85
|
|
|
'bucket' => new Bucket('b'), |
86
|
|
|
'other' => new SampleClass() |
87
|
|
|
], function () use ($c) { |
88
|
|
|
$this->assertSame('b', $c->get('bucket')->getName()); |
89
|
|
|
$this->assertTrue($c->has('other')); |
90
|
|
|
|
91
|
|
|
return $c->get('bucket')->getName() == 'b' && $c->has('other'); |
92
|
|
|
})); |
93
|
|
|
|
94
|
|
|
try { |
95
|
|
|
$this->assertTrue($c->runScope([ |
96
|
|
|
'bucket' => new Bucket('b'), |
97
|
|
|
'other' => new SampleClass() |
98
|
|
|
], function () use ($c): void { |
|
|
|
|
99
|
|
|
throw new RuntimeException('exception'); |
100
|
|
|
})); |
101
|
|
|
} catch (\Throwable $e) { |
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$this->assertSame('a', $c->get('bucket')->getName()); |
105
|
|
|
$this->assertFalse($c->has('other')); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
This check looks for imports that have been defined, but are not used in the scope.