Passed
Pull Request — master (#592)
by Aleksei
06:09
created

QueueInjectorTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 47
c 1
b 0
f 0
dl 0
loc 80
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testBadArgumentTypeException() 0 8 1
A testGetByIncorrectContext() 0 9 1
A createInjector() 0 45 2
A testGetByContext() 0 8 1
1
<?php
2
3
namespace Spiral\Tests\Queue\Core;
4
5
use Mockery as m;
6
use ReflectionClass;
7
use RuntimeException;
8
use Spiral\Core\Exception\Container\NotFoundException;
9
use Spiral\Core\FactoryInterface;
10
use Spiral\Queue\Config\QueueConfig;
11
use Spiral\Queue\Core\QueueInjector;
12
use Spiral\Queue\QueueInterface;
13
use Spiral\Queue\QueueManager;
14
use Spiral\Tests\Queue\Core\Stub\TestQueueClass;
15
use Spiral\Tests\Queue\TestCase;
16
17
final class QueueInjectorTest extends TestCase
18
{
19
    private ?QueueInterface $defaultQueue = null;
20
21
    public function testGetByContext(): void
22
    {
23
        $injector = $this->createInjector();
24
        $reflection = new ReflectionClass(TestQueueClass::class);
25
26
        $result = $injector->createInjection($reflection, 'test');
27
28
        $this->assertInstanceOf(TestQueueClass::class, $result);
29
    }
30
31
    public function testGetByIncorrectContext(): void
32
    {
33
        $injector = $this->createInjector();
34
        $reflection = new ReflectionClass(QueueInterface::class);
35
36
        $result = $injector->createInjection($reflection, 'userQueue');
37
38
        // The default connection should be returned
39
        $this->assertSame($this->defaultQueue, $result);
40
    }
41
42
    public function testBadArgumentTypeException(): void
43
    {
44
        $injector = $this->createInjector();
45
        $this->expectException(RuntimeException::class);
46
        $this->expectExceptionMessage('The queue obtained by the context');
47
48
        $reflection = new ReflectionClass(TestQueueClass::class);
49
        $injector->createInjection($reflection, 'queue');
50
    }
51
52
    private function createInjector(): QueueInjector
53
    {
54
        $this->defaultQueue = m::mock(QueueInterface::class);
55
        $config = new QueueConfig([
56
            'default' => 'sync',
57
            'aliases' => [
58
                'mail-queue' => 'roadrunner',
59
                'rating-queue' => 'sync',
60
                'queue' => 'sync',
61
                'test' => 'test',
62
            ],
63
            'driverAliases' => [],
64
            'connections' => [
65
                'sync' => [
66
                    'driver' => 'sync',
67
                ],
68
                'test' => [
69
                    'driver' => 'test',
70
                ],
71
                'roadrunner' => [
72
                    'driver' => 'roadrunner',
73
                    'default' => 'local',
74
                    'pipelines' => [
75
                        'local' => [
76
                            'connector' => m::mock(QueueInterface::class),
77
                            'consume' => true,
78
                        ],
79
                    ],
80
                ],
81
            ],
82
        ]);
83
        $factory = m::mock(FactoryInterface::class);
84
        $factory->shouldReceive('make')->andReturnUsing(function (string $name): QueueInterface {
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

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

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...
85
            $result = [
86
                    'sync' => $this->defaultQueue,
87
                    'test' => new TestQueueClass(),
88
                ][$name] ?? null;
89
            if ($result === null) {
90
                throw new NotFoundException();
91
            }
92
            return $result;
93
        });
94
        $manager = new QueueManager($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\Queue\QueueManager::__construct(). ( Ignorable by Annotation )

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

94
        $manager = new QueueManager($config, /** @scrutinizer ignore-type */ $factory);
Loading history...
95
96
        return new QueueInjector($manager, $config);
0 ignored issues
show
Unused Code introduced by
The call to Spiral\Queue\Core\QueueInjector::__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

96
        return /** @scrutinizer ignore-call */ new QueueInjector($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...
97
    }
98
}
99