Passed
Pull Request — master (#277)
by Kirill
03:11
created

ScopesTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 47
dl 0
loc 84
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testContainerScope() 0 20 2
A testScopeException() 0 15 2
A testScope() 0 11 1
A testContainerScopeException() 0 30 3
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 {
0 ignored issues
show
Unused Code introduced by
The import $container is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
45
                throw new RuntimeException('exception');
46
            }));
47
        } catch (\Throwable $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
48
        }
49
50
        $this->assertInstanceOf(RuntimeException::class, $e);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $e does not seem to be defined for all execution paths leading up to this point.
Loading history...
51
        $this->assertNull(ContainerScope::getContainer());
52
    }
53
54
    public function testContainerScope(): void
55
    {
56
        $c = new Container();
57
        $c->bind('bucket', new Bucket('a'));
0 ignored issues
show
Bug introduced by
new Spiral\Tests\Core\Fixtures\Bucket('a') of type Spiral\Tests\Core\Fixtures\Bucket is incompatible with the type array|callable|string expected by parameter $resolver of Spiral\Core\Container::bind(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

57
        $c->bind('bucket', /** @scrutinizer ignore-type */ new Bucket('a'));
Loading history...
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'));
0 ignored issues
show
Bug introduced by
new Spiral\Tests\Core\Fixtures\Bucket('a') of type Spiral\Tests\Core\Fixtures\Bucket is incompatible with the type array|callable|string expected by parameter $resolver of Spiral\Core\Container::bind(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

79
        $c->bind('bucket', /** @scrutinizer ignore-type */ new Bucket('a'));
Loading history...
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 {
0 ignored issues
show
Unused Code introduced by
The import $c is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
99
                throw new RuntimeException('exception');
100
            }));
101
        } catch (\Throwable $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
102
        }
103
104
        $this->assertSame('a', $c->get('bucket')->getName());
105
        $this->assertFalse($c->has('other'));
106
    }
107
}
108