WithPrimeTest::testPrimeConnection()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 13
nc 4
nop 0
dl 0
loc 23
rs 9.8333
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\Prime\Connection\Result\DoctrineResultSet;
8
use Bdf\Prime\ServiceLocator;
9
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...
10
use Bdf\Queue\Console\Command\Failer\AbstractFailerCommand;
11
use Bdf\Queue\Console\Command\Failer\InitCommand;
0 ignored issues
show
Bug introduced by
The type Bdf\Queue\Console\Command\Failer\InitCommand 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...
12
use Bdf\Queue\Destination\DestinationManager;
13
use Bdf\Queue\Failer\DbFailedJobRepository;
0 ignored issues
show
Bug introduced by
The type Bdf\Queue\Failer\DbFailedJobRepository 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...
14
use Bdf\Queue\Failer\DbFailedJobStorage;
0 ignored issues
show
Bug introduced by
The type Bdf\Queue\Failer\DbFailedJobStorage 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...
15
use Bdf\Queue\Failer\FailedJobRepositoryAdapter;
16
use Bdf\Queue\Failer\FailedJobStorageInterface;
17
use PHPUnit\Framework\TestCase;
18
use Symfony\Bundle\FrameworkBundle\Console\Application;
19
use Symfony\Component\Console\Command\LazyCommand;
20
21
class WithPrimeTest extends TestCase
22
{
23
    protected function setUp(): void
24
    {
25
        if (!class_exists(PrimeConnection::class)) {
26
            $this->markTestSkipped('b2pweb/bdf-queue-prime-adapter not installed');
27
        }
28
    }
29
30
    /**
31
     * @return void
32
     */
33
    public function testPrimeFailer()
34
    {
35
        $kernel = new \TestKernel(__DIR__.'/Fixtures/conf_with_prime_failer.yaml');
36
        $kernel->boot();
37
        $console = new Application($kernel);
38
39
        $command = $console->get('queue:failer:delete');
40
41
        if ($command instanceof LazyCommand) {
42
            $command = $command->getCommand();
43
        }
44
45
        $r = new \ReflectionProperty(AbstractFailerCommand::class, 'repository');
46
        $r->setAccessible(true);
47
48
        /** @var FailedJobStorageInterface $failer */
49
        $failer = $r->getValue($command);
50
51
        $this->assertTrue($this->isPrimeFailer($failer));
52
53
        if (class_exists(DbFailedJobRepository::class)) {
54
            $this->assertInstanceOf(DbFailedJobRepository::class, $failer);
55
            $this->assertEquals(DbFailedJobRepository::make($kernel->getContainer()->get('prime'), ['connection' => 'my_connection', 'table' => 'failed_jobs'], 15), $failer);
56
        } else {
57
            $this->assertInstanceOf(FailedJobRepositoryAdapter::class, $failer);
58
            $this->assertEquals(
59
                FailedJobRepositoryAdapter::adapt(DbFailedJobStorage::make($kernel->getContainer()->get('prime'), ['connection' => 'my_connection', 'table' => 'failed_jobs'], 15)),
60
                $failer
61
            );
62
        }
63
    }
64
65
    /**
66
     * @return void
67
     */
68
    public function testHostMissing()
69
    {
70
        $this->expectException(\InvalidArgumentException::class);
71
        $this->expectExceptionMessage('The connection name is required on prime failer DSN');
72
73
        $kernel = new \TestKernel(__DIR__.'/Fixtures/conf_with_prime_failer_missing_host.yaml');
74
        $kernel->boot();
75
    }
76
77
    /**
78
     * @return void
79
     */
80
    public function testTableNameMissing()
81
    {
82
        $this->expectException(\InvalidArgumentException::class);
83
        $this->expectExceptionMessage('The table name is required on prime failer DSN');
84
85
        $kernel = new \TestKernel(__DIR__.'/Fixtures/conf_with_prime_failer_missing_table.yaml');
86
        $kernel->boot();
87
    }
88
89
    public function testInitCommand()
90
    {
91
        $kernel = new \TestKernel(__DIR__.'/Fixtures/conf_with_prime_failer.yaml');
92
        $kernel->boot();
93
        $console = new Application($kernel);
94
95
        $command = $console->get('queue:prime:failer:init');
96
        if ($command instanceof LazyCommand) {
97
            $command = $command->getCommand();
98
        }
99
100
        $this->assertInstanceOf(InitCommand::class, $command);
101
    }
102
103
    private function isPrimeFailer($storage): bool
104
    {
105
        if ($storage instanceof DbFailedJobRepository) {
106
            return true;
107
        }
108
109
        if ($storage instanceof FailedJobRepositoryAdapter) {
110
            $r = new \ReflectionProperty(FailedJobRepositoryAdapter::class, 'storage');
111
            $r->setAccessible(true);
112
113
            return $r->getValue($storage) instanceof DbFailedJobStorage;
114
        }
115
116
        return false;
117
    }
118
119
    public function testPrimeConnection()
120
    {
121
        $kernel = new \TestKernel(__DIR__.'/Fixtures/conf_with_prime_connection.yaml');
122
        $kernel->boot();
123
124
        /** @var ServiceLocator $prime */
125
        $prime = $kernel->getContainer()->get('prime');
126
127
        /** @var DestinationManager $destinations */
128
        $destinations = $kernel->getContainer()->get('bdf_queue.destination_manager');
129
        $destination = $destinations->queue('prime', 'queue_name');
130
        $destination->declare();
131
132
        $count = $prime->connection('my_connection')->select('select count(*) as nb from queue');
133
        $count = $count instanceof DoctrineResultSet ? $count->all()[0] : $count[0];
134
135
        $this->assertEquals(0, $count->nb);
136
137
        $destination->raw('test');
138
139
        $count = $prime->connection('my_connection')->select('select count(*) as nb from queue');
140
        $count = $count instanceof DoctrineResultSet ? $count->all()[0] : $count[0];
141
        $this->assertEquals(1, $count->nb);
142
    }
143
}
144