Passed
Push — master ( 0efcf5...db0e0f )
by Vincent
08:47
created

BdfQueueBundleTest.php$0 ➔ getCommand()   A

Complexity

Conditions 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
c 0
b 0
f 0
dl 0
loc 5
rs 10
1
<?php
2
3
namespace Bdf\QueueBundle\Tests;
4
5
require_once __DIR__.'/TestKernel.php';
6
7
use Bdf\Queue\Connection\Prime\PrimeConnection;
0 ignored issues
show
Bug introduced by
The type Bdf\Queue\Connection\Prime\PrimeConnection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Bdf\Queue\Console\Command\BindCommand;
9
use Bdf\Queue\Console\Command\ConsumeCommand;
10
use Bdf\Queue\Console\Command\Failer\AbstractFailerCommand;
11
use Bdf\Queue\Console\Command\Failer\DeleteCommand;
12
use Bdf\Queue\Console\Command\Failer\FlushCommand;
13
use Bdf\Queue\Console\Command\Failer\ForgetCommand;
14
use Bdf\Queue\Console\Command\Failer\RetryCommand;
15
use Bdf\Queue\Console\Command\Failer\ShowCommand;
16
use Bdf\Queue\Console\Command\InfoCommand;
17
use Bdf\Queue\Console\Command\ProduceCommand;
18
use Bdf\Queue\Console\Command\SetupCommand;
19
use Bdf\Queue\Consumer\Receiver\Builder\ReceiverLoaderInterface;
20
use Bdf\Queue\Destination\DestinationInterface;
21
use Bdf\Queue\Destination\DestinationManager;
22
use Bdf\Queue\Failer\FailedJobStorageInterface;
23
use Bdf\Queue\Failer\MemoryFailedJobRepository;
24
use Bdf\Queue\Message\Message;
25
use Bdf\Queue\Message\QueuedMessage;
26
use Bdf\Queue\Testing\QueueHelper;
27
use Bdf\QueueBundle\BdfQueueBundle;
28
use Bdf\QueueBundle\Consumption\ReceiverLoader;
29
use Bdf\QueueBundle\Tests\Fixtures\TestHandler;
30
use PHPUnit\Framework\TestCase;
31
use Symfony\Bundle\FrameworkBundle\Console\Application;
32
use Symfony\Component\Console\Command\Command;
33
use Symfony\Component\Console\Command\LazyCommand;
34
use Symfony\Component\DependencyInjection\ContainerBuilder;
35
36
/**
37
 * BdfSerializerBundleTest.
38
 */
39
class BdfQueueBundleTest extends TestCase
40
{
41
    public function testDefaultConfig()
42
    {
43
        $builder = $this->createMock(ContainerBuilder::class);
44
45
        $bundle = new BdfQueueBundle();
46
47
        $this->assertNull($bundle->build($builder));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $bundle->build($builder) targeting Bdf\QueueBundle\BdfQueueBundle::build() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
48
    }
49
50
    public function testKernel()
51
    {
52
        $kernel = new \TestKernel();
53
        $kernel->boot();
54
55
        $this->assertInstanceOf(DestinationManager::class, $kernel->getContainer()->get('bdf_queue.destination_manager'));
56
        $this->assertSame($kernel->getContainer()->get('bdf_queue.destination_manager'), $kernel->getContainer()->get(DestinationManager::class));
57
        $this->assertInstanceOf(ReceiverLoader::class, $kernel->getContainer()->get('bdf_queue.receiver.loader'));
58
        $this->assertInstanceOf(ReceiverLoader::class, $kernel->getContainer()->get(ReceiverLoaderInterface::class));
59
    }
60
61
    public function testFunctional()
62
    {
63
        $kernel = new \TestKernel(__DIR__.'/Fixtures/conf_functional.yaml');
64
        $kernel->boot();
65
66
        /** @var DestinationManager $destinations */
67
        $destinations = $kernel->getContainer()->get('bdf_queue.destination_manager');
68
        $destination = $destinations->create('test');
69
70
        $this->assertInstanceOf(DestinationInterface::class, $destination);
71
72
        /** @var TestHandler $handler */
73
        $handler = $kernel->getContainer()->get(TestHandler::class);
74
75
        $helper = new QueueHelper($kernel->getContainer());
76
        $destination->send(new Message(['foo' => 'bar']));
77
78
        $helper->consume(1, 'test');
79
        $this->assertEquals([['foo' => 'bar']], $handler->messages);
80
    }
81
82
    public function testWithJsonSerializer()
83
    {
84
        $kernel = new \TestKernel(__DIR__.'/Fixtures/conf_with_json_serializer.yaml');
85
        $kernel->boot();
86
87
        /** @var DestinationManager $destinations */
88
        $destinations = $kernel->getContainer()->get('bdf_queue.destination_manager');
89
        $destination = $destinations->create('test');
90
91
        $this->assertInstanceOf(DestinationInterface::class, $destination);
92
93
        /** @var TestHandler $handler */
94
        $handler = $kernel->getContainer()->get(TestHandler::class);
0 ignored issues
show
Unused Code introduced by
The assignment to $handler is dead and can be removed.
Loading history...
95
96
        $helper = new QueueHelper($kernel->getContainer());
97
        $destination->send(new Message(new class() implements \JsonSerializable {
98
            #[\ReturnTypeWillChange]
99
            public function jsonSerialize()
100
            {
101
                return ['foo' => 'bar'];
102
            }
103
        }));
104
105
        /** @var QueuedMessage $message */
106
        $message = $helper->peek(1, 'test')[0];
107
108
        $this->assertEquals(
109
            '{"data":{"foo":"bar"},"queuedAt":'.json_encode($message->queuedAt()).'}',
110
            $message->raw()
111
        );
112
    }
113
114
    public function testConnectionOptions()
115
    {
116
        $kernel = new \TestKernel();
117
        $kernel->boot();
118
119
        $configs = $kernel->getContainer()->getParameter('bdf_queue.connections');
120
121
        $this->assertEquals([
122
            'gearman' => [
123
                'driver' => null,
124
                'host' => null,
125
                'serializer' => ['id' => 'native'],
126
                'queue' => null,
127
                'url' => 'gearman://127.0.0.1',
128
                'options' => ['client_timeout' => 1],
129
                'vendor' => null,
130
                'port' => null,
131
                'user' => null,
132
                'password' => null,
133
                'connection_factory' => null,
134
            ],
135
        ], $configs);
136
    }
137
138
    public function testDestinationOptions()
139
    {
140
        $kernel = new \TestKernel();
141
        $kernel->boot();
142
143
        $configs = $kernel->getContainer()->getParameter('bdf_queue.destinations');
144
145
        $this->assertEquals([
146
            'b2p_bus' => 'queue://gearman/b2p_bus',
147
            'custom_bus' => 'queue://gearman/custom_bus',
148
        ], $configs);
149
    }
150
151
    public function testCommands()
152
    {
153
        $kernel = new \TestKernel();
154
        $kernel->boot();
155
        $console = new Application($kernel);
156
157
        $this->assertInstanceOf(BindCommand::class, $this->getCommand($console, 'queue:bind'));
158
        $this->assertInstanceOf(ConsumeCommand::class, $this->getCommand($console, 'queue:consume'));
159
        $this->assertInstanceOf(InfoCommand::class, $this->getCommand($console, 'queue:info'));
160
        $this->assertInstanceOf(ProduceCommand::class, $this->getCommand($console, 'queue:produce'));
161
        $this->assertInstanceOf(SetupCommand::class, $this->getCommand($console, 'queue:setup'));
162
163
        $this->assertInstanceOf(DeleteCommand::class, $this->getCommand($console, 'queue:failer:delete'));
164
        $this->assertInstanceOf(RetryCommand::class, $this->getCommand($console, 'queue:failer:retry'));
165
        $this->assertInstanceOf(ShowCommand::class, $this->getCommand($console, 'queue:failer:show'));
166
        $this->assertInstanceOf(FlushCommand::class, $this->getCommand($console, 'queue:failer:flush'));
167
        $this->assertInstanceOf(ForgetCommand::class, $this->getCommand($console, 'queue:failer:forget'));
168
    }
169
170
    /**
171
     * @return void
172
     */
173
    public function testFailer()
174
    {
175
        $kernel = new \TestKernel();
176
        $kernel->boot();
177
        $console = new Application($kernel);
178
179
        $command = $this->getCommand($console, 'queue:failer:delete');
180
181
        $r = new \ReflectionProperty(AbstractFailerCommand::class, 'repository');
182
        $r->setAccessible(true);
183
184
        /** @var FailedJobStorageInterface $failer */
185
        $failer = $r->getValue($command);
186
187
        $this->assertInstanceOf(MemoryFailedJobRepository::class, $failer);
188
    }
189
190
    /**
191
     * @return void
192
     */
193
    public function testCustomFailer()
194
    {
195
        $kernel = new \TestKernel(__DIR__.'/Fixtures/conf_with_custom_failer.yaml');
196
        $kernel->boot();
197
        $console = new Application($kernel);
198
199
        $command = $this->getCommand($console, 'queue:failer:delete');
200
201
        $r = new \ReflectionProperty(AbstractFailerCommand::class, 'repository');
202
        $r->setAccessible(true);
203
204
        /** @var FailedJobStorageInterface $failer */
205
        $failer = $r->getValue($command);
206
207
        $this->assertInstanceOf(MemoryFailedJobRepository::class, $failer);
208
        $this->assertSame($failer, $kernel->getContainer()->get('foo'));
209
    }
210
211
    public function testPrimeConnection()
212
    {
213
        if (class_exists(PrimeConnection::class)) {
214
            $this->markTestSkipped('b2pweb/bdf-queue-prime-adapter installed');
215
        }
216
217
        $this->expectException(\LogicException::class);
218
219
        $kernel = new \TestKernel(__DIR__.'/Fixtures/conf_with_prime_connection.yaml');
220
        $kernel->boot();
221
222
        /** @var DestinationManager $destinations */
223
        $destinations = $kernel->getContainer()->get('bdf_queue.destination_manager');
224
        $destinations->queue('prime', 'queue_name');
225
    }
226
227
    private function getCommand(Application $application, string $name): Command
228
    {
229
        $command = $application->get($name);
230
231
        return $command instanceof LazyCommand ? $command->getCommand() : $command;
232
    }
233
}
234