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

WithPrimeTest::testPrimeConnection()   A

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