Passed
Pull Request — master (#600)
by Aleksei
08:16 queued 03:02
created

CacheInjectorTest::createInjector()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 35
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 25
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 35
rs 9.52
1
<?php
2
3
namespace Spiral\Tests\Cache\Core;
4
5
use Mockery as m;
6
use PHPUnit\Framework\TestCase;
7
use Psr\SimpleCache\CacheInterface;
8
use ReflectionClass;
9
use RuntimeException;
10
use Spiral\Cache\CacheManager;
11
use Spiral\Cache\Config\CacheConfig;
12
use Spiral\Cache\Core\CacheInjector;
13
use Spiral\Cache\Storage\ArrayStorage;
14
use Spiral\Core\Exception\Container\NotFoundException;
15
use Spiral\Core\FactoryInterface;
16
17
final class CacheInjectorTest extends TestCase
18
{
19
    private ?CacheInterface $defaultCache = null;
20
21
    public function testGetByContext(): void
22
    {
23
        $injector = $this->createInjector();
24
        $reflection = new ReflectionClass(ArrayStorage::class);
25
26
        $result = $injector->createInjection($reflection, 'array');
27
28
        $this->assertInstanceOf(ArrayStorage::class, $result);
29
    }
30
31
    public function testGetByIncorrectContext(): void
32
    {
33
        $injector = $this->createInjector();
34
        $reflection = new ReflectionClass(CacheInterface::class);
35
36
        $result = $injector->createInjection($reflection, 'userCache');
37
38
        // The default connection should be returned
39
        $this->assertSame($this->defaultCache, $result);
40
    }
41
42
    public function testBadArgumentTypeException(): void
43
    {
44
        $injector = $this->createInjector();
45
        $this->expectException(RuntimeException::class);
46
        $this->expectExceptionMessage('The cache obtained by the context');
47
48
        $reflection = new ReflectionClass(ArrayStorage::class);
49
        $injector->createInjection($reflection, 'cache');
50
    }
51
52
    private function createInjector(): CacheInjector
53
    {
54
        $this->defaultCache = m::mock(CacheInterface::class);
55
        $config = new CacheConfig([
56
            'default' => 'test',
57
            'aliases' => [
58
                'config-cache' => 'roadrunner',
59
                'routes-cache' => 'array',
60
                'cache' => 'test',
61
                'test' => 'test',
62
            ],
63
            'typeAliases' => [],
64
            'storages' => [
65
                'array' => [
66
                    'type' => 'array',
67
                ],
68
                'test' => [
69
                    'type' => 'test',
70
                ],
71
            ],
72
        ]);
73
        $factory = m::mock(FactoryInterface::class);
74
        $factory->shouldReceive('make')->andReturnUsing(function (string $name): CacheInterface {
0 ignored issues
show
Bug introduced by
The method andReturnUsing() does not exist on Mockery\ExpectationInterface. Did you maybe mean andReturn()? ( Ignorable by Annotation )

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

74
        $factory->shouldReceive('make')->/** @scrutinizer ignore-call */ andReturnUsing(function (string $name): CacheInterface {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
75
            $result = [
76
                    'test' => $this->defaultCache,
77
                    'array' => new ArrayStorage(),
78
                ][$name] ?? null;
79
            if ($result === null) {
80
                throw new NotFoundException();
81
            }
82
            return $result;
83
        });
84
        $manager = new CacheManager($config, $factory);
0 ignored issues
show
Bug introduced by
$factory of type Mockery\LegacyMockInterface|Mockery\MockInterface is incompatible with the type Spiral\Core\FactoryInterface expected by parameter $factory of Spiral\Cache\CacheManager::__construct(). ( Ignorable by Annotation )

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

84
        $manager = new CacheManager($config, /** @scrutinizer ignore-type */ $factory);
Loading history...
85
86
        return new CacheInjector($manager, $config);
0 ignored issues
show
Unused Code introduced by
The call to Spiral\Cache\Core\CacheInjector::__construct() has too many arguments starting with $config. ( Ignorable by Annotation )

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

86
        return /** @scrutinizer ignore-call */ new CacheInjector($manager, $config);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
87
    }
88
}
89