Passed
Pull Request — master (#5)
by Vincent
03:09
created

php$0 ➔ testShardingConnection()   A

Complexity

Conditions 1

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
c 0
b 0
f 0
cc 1
rs 9.392

3 Methods

Rating   Name   Duplication   Size   Complexity  
A BdfPrimeBundleTest.php$0 ➔ configureContainer() 0 3 1
A BdfPrimeBundleTest.php$0 ➔ configureRoutes() 0 2 1
A BdfPrimeBundleTest.php$0 ➔ registerBundles() 0 5 1
1
<?php
2
3
namespace Bdf\PrimeBundle\Tests;
4
5
require_once __DIR__.'/TestKernel.php';
6
7
use Bdf\Prime\Cache\ArrayCache;
8
use Bdf\Prime\Cache\DoctrineCacheAdapter;
9
use Bdf\Prime\Connection\SimpleConnection;
10
use Bdf\Prime\Console\UpgraderCommand;
11
use Bdf\Prime\Migration\MigrationManager;
12
use Bdf\Prime\Schema\RepositoryUpgrader;
13
use Bdf\Prime\Schema\StructureUpgraderResolverAggregate;
14
use Bdf\Prime\Schema\StructureUpgraderResolverInterface;
15
use Bdf\Prime\ServiceLocator;
16
use Bdf\Prime\Sharding\ShardingConnection;
17
use Bdf\Prime\Sharding\ShardingQuery;
18
use Bdf\Prime\Types\ArrayType;
19
use Bdf\Prime\Types\TypeInterface;
20
use Bdf\PrimeBundle\Collector\PrimeDataCollector;
21
use Bdf\PrimeBundle\DependencyInjection\Compiler\PrimeConnectionFactoryPass;
22
use Bdf\PrimeBundle\PrimeBundle;
23
use Doctrine\Common\Cache\Psr6\DoctrineProvider;
24
use PHPUnit\Framework\TestCase;
25
use Symfony\Bundle\FrameworkBundle\Console\Application;
26
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
27
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
28
use Symfony\Component\Config\Loader\LoaderInterface;
29
use Symfony\Component\DependencyInjection\ContainerBuilder;
30
use Symfony\Component\HttpFoundation\Request;
31
use Symfony\Component\HttpKernel\Kernel;
32
use Symfony\Component\Routing\RouteCollectionBuilder;
33
34
/**
35
 * BdfSerializerBundleTest.
36
 */
37
class BdfPrimeBundleTest extends TestCase
38
{
39
    public function testDefaultConfig()
40
    {
41
        $builder = new ContainerBuilder();
42
        $bundle = new PrimeBundle();
43
        $bundle->build($builder);
44
45
        $compilerPasses = $builder->getCompiler()->getPassConfig()->getPasses();
46
        $found = 0;
47
48
        foreach ($compilerPasses as $pass) {
49
            if ($pass instanceof PrimeConnectionFactoryPass) {
50
                ++$found;
51
            }
52
        }
53
54
        $this->assertSame(1, $found);
55
    }
56
57
    public function testKernel()
58
    {
59
        $kernel = new \TestKernel('dev', true);
60
        $kernel->boot();
61
62
        $this->assertInstanceOf(ServiceLocator::class, $kernel->getContainer()->get('prime'));
63
        $this->assertSame($kernel->getContainer()->get('prime'), $kernel->getContainer()->get(ServiceLocator::class));
64
        $this->assertInstanceOf(MigrationManager::class, $kernel->getContainer()->get(MigrationManager::class));
65
        $this->assertInstanceOf(SimpleConnection::class, $kernel->getContainer()->get(ServiceLocator::class)->connection('test'));
66
    }
67
68
    /**
69
     * @return void
70
     */
71
    public function testStructureUpgrader()
72
    {
73
        if (!interface_exists(StructureUpgraderResolverInterface::class)) {
74
            $this->markTestSkipped('StructureUpgraderResolverInterface not present');
75
        }
76
77
        $kernel = new \TestKernel('dev', true);
78
        $kernel->boot();
79
80
        $this->assertInstanceOf(StructureUpgraderResolverAggregate::class, $kernel->getContainer()->get(StructureUpgraderResolverInterface::class));
81
        $this->assertInstanceOf(StructureUpgraderResolverAggregate::class, $kernel->getContainer()->get(StructureUpgraderResolverAggregate::class));
82
83
        $console = new Application($kernel);
84
        $command = $console->get(UpgraderCommand::getDefaultName());
0 ignored issues
show
Bug introduced by
It seems like Bdf\Prime\Console\Upgrad...mmand::getDefaultName() can also be of type null; however, parameter $name of Symfony\Bundle\Framework...sole\Application::get() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

84
        $command = $console->get(/** @scrutinizer ignore-type */ UpgraderCommand::getDefaultName());
Loading history...
85
        $r = new \ReflectionProperty($command, 'resolver');
86
        $r->setAccessible(true);
87
88
        $this->assertSame(
89
            $kernel->getContainer()->get(StructureUpgraderResolverAggregate::class),
90
            $r->getValue($command)
91
        );
92
93
        $upgrader = $kernel->getContainer()->get(StructureUpgraderResolverAggregate::class)->resolveByDomainClass(\TestEntity::class);
94
95
        $this->assertInstanceOf(RepositoryUpgrader::class, $upgrader);
96
        $this->assertEquals('test_', $upgrader->table()->name());
97
98
        $this->assertEquals($upgrader, $kernel->getContainer()->get(StructureUpgraderResolverAggregate::class)->resolveByMapperClass(\TestEntityMapper::class));
99
    }
100
101
    public function testCollector()
102
    {
103
        $kernel = new \TestKernel('dev', true);
104
        $kernel->boot();
105
106
        $collector = $kernel->getContainer()->get(PrimeDataCollector::class);
107
108
        $this->assertInstanceOf(PrimeDataCollector::class, $collector);
109
        $kernel->handle(Request::create('http://127.0.0.1/'));
110
111
        $this->assertGreaterThanOrEqual(3, $collector->getQueryCount());
112
113
        $queries = array_map(function ($entry) { return $entry['sql']; }, $collector->getQueries()['']);
114
        $this->assertContains('SELECT * FROM test_ WHERE id = ? LIMIT 1', $queries);
115
    }
116
117
    public function testFunctional()
118
    {
119
        $kernel = new \TestKernel('dev', true);
120
        $kernel->boot();
121
122
        \TestEntity::repository()->schema()->migrate();
123
124
        $entity = new \TestEntity(['name' => 'foo']);
125
        $entity->insert();
126
127
        $this->assertEquals([$entity], \TestEntity::all());
0 ignored issues
show
Bug introduced by
The method all() does not exist on TestEntity. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

127
        $this->assertEquals([$entity], \TestEntity::/** @scrutinizer ignore-call */ all());
Loading history...
128
        $this->assertEquals($entity, \TestEntity::where('name', 'foo')->first());
129
    }
130
131
    public function testShardingConnection()
132
    {
133
        $kernel = new class('test', true) extends Kernel {
134
            use \Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
0 ignored issues
show
Bug introduced by
The trait Symfony\Bundle\Framework...Kernel\MicroKernelTrait requires the property $instanceof which is not provided by anonymous//Tests/BdfPrimeBundleTest.php$0.
Loading history...
135
136
            public function registerBundles(): iterable
137
            {
138
                return [
139
                    new FrameworkBundle(),
140
                    new PrimeBundle(),
141
                ];
142
            }
143
144
            protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader)
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

144
            protected function configureContainer(/** @scrutinizer ignore-unused */ ContainerBuilder $c, LoaderInterface $loader)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
145
            {
146
                $loader->import(__DIR__.'/Fixtures/sharding.yaml');
0 ignored issues
show
Bug introduced by
The method import() does not exist on Symfony\Component\Config\Loader\LoaderInterface. It seems like you code against a sub-type of Symfony\Component\Config\Loader\LoaderInterface such as Symfony\Component\Config\Loader\Loader. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

146
                $loader->/** @scrutinizer ignore-call */ 
147
                         import(__DIR__.'/Fixtures/sharding.yaml');
Loading history...
147
            }
148
149
            protected function configureRoutes(RouteCollectionBuilder $routes)
0 ignored issues
show
Unused Code introduced by
The parameter $routes is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

149
            protected function configureRoutes(/** @scrutinizer ignore-unused */ RouteCollectionBuilder $routes)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
150
            {
151
            }
152
        };
153
154
        $kernel->boot();
155
156
        /** @var ServiceLocator $prime */
157
        $prime = $kernel->getContainer()->get(ServiceLocator::class);
158
        $this->assertInstanceOf(ShardingConnection::class, $prime->connection('test'));
159
        $this->assertInstanceOf(ShardingQuery::class, $prime->connection('test')->builder());
160
161
        /** @var SimpleConnection $connection */
162
        $connection = $prime->connection('test.shard1');
163
        $this->assertSame($prime->connection('test')->getConfiguration(), $connection->getConfiguration());
164
    }
165
166
    public function testArrayCache()
167
    {
168
        $kernel = new class('test', true) extends Kernel {
169
            use \Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
0 ignored issues
show
Bug introduced by
The trait Symfony\Bundle\Framework...Kernel\MicroKernelTrait requires the property $instanceof which is not provided by anonymous//Tests/BdfPrimeBundleTest.php$1.
Loading history...
170
171
            public function registerBundles(): iterable
172
            {
173
                return [
174
                    new FrameworkBundle(),
175
                    new PrimeBundle(),
176
                ];
177
            }
178
179
            protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader)
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

179
            protected function configureContainer(/** @scrutinizer ignore-unused */ ContainerBuilder $c, LoaderInterface $loader)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
180
            {
181
                $loader->import(__DIR__.'/Fixtures/array_cache.yaml');
182
            }
183
184
            protected function configureRoutes($routes)
0 ignored issues
show
Unused Code introduced by
The parameter $routes is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

184
            protected function configureRoutes(/** @scrutinizer ignore-unused */ $routes)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
185
            {
186
            }
187
        };
188
189
        $kernel->boot();
190
191
        /** @var ServiceLocator $prime */
192
        $prime = $kernel->getContainer()->get(ServiceLocator::class);
193
        $this->assertEquals(new ArrayCache(), $prime->mappers()->getResultCache());
194
    }
195
196
    public function testPoolCache()
197
    {
198
        $kernel = new class('test', true) extends Kernel {
199
            use \Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
0 ignored issues
show
Bug introduced by
The trait Symfony\Bundle\Framework...Kernel\MicroKernelTrait requires the property $instanceof which is not provided by anonymous//Tests/BdfPrimeBundleTest.php$2.
Loading history...
200
201
            public function registerBundles(): iterable
202
            {
203
                return [
204
                    new FrameworkBundle(),
205
                    new PrimeBundle(),
206
                ];
207
            }
208
209
            protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader)
210
            {
211
                $c->register('custom.cache', FilesystemAdapter::class);
212
213
                $loader->import(__DIR__.'/Fixtures/pool_cache.yaml');
214
            }
215
216
            protected function configureRoutes($routes)
0 ignored issues
show
Unused Code introduced by
The parameter $routes is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

216
            protected function configureRoutes(/** @scrutinizer ignore-unused */ $routes)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
217
            {
218
            }
219
        };
220
221
        $kernel->boot();
222
223
        /** @var ServiceLocator $prime */
224
        $prime = $kernel->getContainer()->get(ServiceLocator::class);
225
        $this->assertEquals(new DoctrineCacheAdapter(DoctrineProvider::wrap(new FilesystemAdapter())), $prime->mappers()->getResultCache());
226
    }
227
228
    public function testGlobalConfig()
229
    {
230
        $kernel = new class('test', true) extends Kernel {
231
            use \Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
0 ignored issues
show
Bug introduced by
The trait Symfony\Bundle\Framework...Kernel\MicroKernelTrait requires the property $instanceof which is not provided by anonymous//Tests/BdfPrimeBundleTest.php$3.
Loading history...
232
233
            public function registerBundles(): iterable
234
            {
235
                return [
236
                    new FrameworkBundle(),
237
                    new PrimeBundle(),
238
                ];
239
            }
240
241
            protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader)
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

241
            protected function configureContainer(/** @scrutinizer ignore-unused */ ContainerBuilder $c, LoaderInterface $loader)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
242
            {
243
                $loader->import(__DIR__.'/Fixtures/config.yaml');
244
            }
245
246
            protected function configureRoutes($routes)
0 ignored issues
show
Unused Code introduced by
The parameter $routes is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

246
            protected function configureRoutes(/** @scrutinizer ignore-unused */ $routes)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
247
            {
248
            }
249
        };
250
251
        $kernel->boot();
252
253
        /** @var ServiceLocator $prime */
254
        $prime = $kernel->getContainer()->get(ServiceLocator::class);
255
        /** @var SimpleConnection $connection */
256
        $connection = $prime->connection('test2');
257
258
        $this->assertNotNull($connection->getConfiguration()->getSQLLogger());
259
        $this->assertTrue($connection->getConfiguration()->getAutoCommit());
260
        $this->assertInstanceOf(FooType::class, $connection->getConfiguration()->getTypes()->get('foo'));
261
        $this->assertInstanceOf(BarType::class, $connection->getConfiguration()->getTypes()->get('bar'));
262
        $this->assertInstanceOf(ArrayType::class, $connection->getConfiguration()->getTypes()->get('array'));
263
    }
264
265
    public function testConnectionConfig()
266
    {
267
        $kernel = new class('test', true) extends Kernel {
268
            use \Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
0 ignored issues
show
Bug introduced by
The trait Symfony\Bundle\Framework...Kernel\MicroKernelTrait requires the property $instanceof which is not provided by anonymous//Tests/BdfPrimeBundleTest.php$4.
Loading history...
269
270
            public function registerBundles(): iterable
271
            {
272
                return [
273
                    new FrameworkBundle(),
274
                    new PrimeBundle(),
275
                ];
276
            }
277
278
            protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader)
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

278
            protected function configureContainer(/** @scrutinizer ignore-unused */ ContainerBuilder $c, LoaderInterface $loader)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
279
            {
280
                $loader->import(__DIR__.'/Fixtures/config.yaml');
281
            }
282
283
            protected function configureRoutes($routes)
0 ignored issues
show
Unused Code introduced by
The parameter $routes is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

283
            protected function configureRoutes(/** @scrutinizer ignore-unused */ $routes)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
284
            {
285
            }
286
        };
287
288
        $kernel->boot();
289
290
        /** @var ServiceLocator $prime */
291
        $prime = $kernel->getContainer()->get(ServiceLocator::class);
292
        /** @var SimpleConnection $connection */
293
        $connection = $prime->connection('test');
294
295
        $this->assertNull($connection->getConfiguration()->getSQLLogger());
296
        $this->assertFalse($connection->getConfiguration()->getAutoCommit());
297
        $this->assertInstanceOf(BarType::class, $connection->getConfiguration()->getTypes()->get('foo'));
298
        $this->assertInstanceOf(BarType::class, $connection->getConfiguration()->getTypes()->get('bar'));
299
        $this->assertInstanceOf(ArrayType::class, $connection->getConfiguration()->getTypes()->get('array'));
300
    }
301
}
302
303
class FooType implements TypeInterface
304
{
305
    public function fromDatabase($value, array $fieldOptions = [])
306
    {
307
        // TODO: Implement fromDatabase() method.
308
    }
309
310
    public function toDatabase($value)
311
    {
312
        // TODO: Implement toDatabase() method.
313
    }
314
315
    public function name(): string
316
    {
317
        // TODO: Implement name() method.
318
    }
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return string. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
319
320
    public function phpType(): string
321
    {
322
        // TODO: Implement phpType() method.
323
    }
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return string. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
324
}
325
class BarType implements TypeInterface
326
{
327
    public function fromDatabase($value, array $fieldOptions = [])
328
    {
329
        // TODO: Implement fromDatabase() method.
330
    }
331
332
    public function toDatabase($value)
333
    {
334
        // TODO: Implement toDatabase() method.
335
    }
336
337
    public function name(): string
338
    {
339
        // TODO: Implement name() method.
340
    }
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return string. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
341
342
    public function phpType(): string
343
    {
344
        // TODO: Implement phpType() method.
345
    }
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return string. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
346
}
347