Passed
Push — master ( ff3d9b...0efcf5 )
by Sébastien
03:02
created

BdfQueueBundleTest.php$0 ➔ testFailer()   A

Complexity

Conditions 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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