Passed
Push — master ( 17252e...642694 )
by Sébastien
03:17
created

BdfQueueBundleTest.php$0 ➔ testConnectionOptions()   A

Complexity

Conditions 1

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

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