Passed
Pull Request — master (#3)
by Vincent
08:42
created

BdfPrimeBundleTest.php$1 ➔ configureRoutes()   A

Complexity

Conditions 1

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
dl 0
loc 1
rs 10
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\Migration\MigrationManager;
11
use Bdf\Prime\ServiceLocator;
12
use Bdf\Prime\Sharding\ShardingConnection;
13
use Bdf\Prime\Sharding\ShardingQuery;
14
use Bdf\Prime\Types\ArrayType;
15
use Bdf\PrimeBundle\Collector\PrimeDataCollector;
16
use Bdf\PrimeBundle\DependencyInjection\Compiler\PrimeConnectionFactoryPass;
17
use Bdf\PrimeBundle\PrimeBundle;
18
use Doctrine\Common\Cache\Psr6\DoctrineProvider;
19
use Doctrine\DBAL\Driver;
20
use PHPUnit\Framework\TestCase;
21
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
22
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
23
use Symfony\Component\Config\Loader\LoaderInterface;
24
use Symfony\Component\DependencyInjection\ContainerBuilder;
25
use Symfony\Component\HttpFoundation\Request;
26
use Symfony\Component\HttpKernel\Kernel;
27
use Symfony\Component\Routing\RouteCollectionBuilder;
28
29
/**
30
 * BdfSerializerBundleTest
31
 */
32
class BdfPrimeBundleTest extends TestCase
33
{
34
    public function test_default_config()
35
    {
36
        $builder = new ContainerBuilder();
37
        $bundle = new PrimeBundle();
38
        $bundle->build($builder);
39
40
        $compilerPasses = $builder->getCompiler()->getPassConfig()->getPasses();
41
        $found = 0;
42
43
        foreach ($compilerPasses as $pass) {
44
            if ($pass instanceof PrimeConnectionFactoryPass) {
45
                $found++;
46
            }
47
        }
48
49
        $this->assertSame(1, $found);
50
    }
51
52
    /**
53
     *
54
     */
55
    public function test_kernel()
56
    {
57
        $kernel = new \TestKernel('dev', true);
58
        $kernel->boot();
59
60
        $this->assertInstanceOf(ServiceLocator::class, $kernel->getContainer()->get('prime'));
61
        $this->assertSame($kernel->getContainer()->get('prime'), $kernel->getContainer()->get(ServiceLocator::class));
62
        $this->assertInstanceOf(MigrationManager::class, $kernel->getContainer()->get(MigrationManager::class));
63
        $this->assertInstanceOf(SimpleConnection::class, $kernel->getContainer()->get(ServiceLocator::class)->connection('test'));
64
    }
65
66
    /**
67
     *
68
     */
69
    public function test_collector()
70
    {
71
        $kernel = new \TestKernel('dev', true);
72
        $kernel->boot();
73
74
        $collector = $kernel->getContainer()->get(PrimeDataCollector::class);
75
76
        $this->assertInstanceOf(PrimeDataCollector::class, $collector);
77
        $kernel->handle(Request::create('http://127.0.0.1/'));
78
79
        $isDoctrine2 = method_exists(Driver::class, 'getDatabase');
80
81
        $this->assertEquals($isDoctrine2 ? 3 : 4, $collector->getQueryCount()); // Doctrine 3 always perform a select query on Connection::getDatabase()
82
        $this->assertEquals('SELECT * FROM test_ WHERE id = ? LIMIT 1', $collector->getQueries()[''][$isDoctrine2 ? 3 : 4]['sql']);
83
    }
84
85
    /**
86
     *
87
     */
88
    public function test_functional()
89
    {
90
        $kernel = new \TestKernel('dev', true);
91
        $kernel->boot();
92
93
        \TestEntity::repository()->schema()->migrate();
94
95
        $entity = new \TestEntity(['name' => 'foo']);
96
        $entity->insert();
97
98
        $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

98
        $this->assertEquals([$entity], \TestEntity::/** @scrutinizer ignore-call */ all());
Loading history...
99
        $this->assertEquals($entity, \TestEntity::where('name', 'foo')->first());
0 ignored issues
show
Bug introduced by
The method where() 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

99
        $this->assertEquals($entity, \TestEntity::/** @scrutinizer ignore-call */ where('name', 'foo')->first());
Loading history...
100
    }
101
102
    /**
103
     *
104
     */
105
    public function test_sharding_connection()
106
    {
107
        $kernel = new class('test', true) extends Kernel {
108
            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...
109
110
            public function registerBundles(): iterable
111
            {
112
                return [
113
                    new FrameworkBundle(),
114
                    new PrimeBundle(),
115
                ];
116
            }
117
118
            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

118
            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...
119
            {
120
                $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

120
                $loader->/** @scrutinizer ignore-call */ 
121
                         import(__DIR__.'/Fixtures/sharding.yaml');
Loading history...
121
            }
122
123
            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

123
            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...
124
        };
125
126
        $kernel->boot();
127
128
        /** @var ServiceLocator $prime */
129
        $prime = $kernel->getContainer()->get(ServiceLocator::class);
130
        $this->assertInstanceOf(ShardingConnection::class, $prime->connection('test'));
131
        $this->assertInstanceOf(ShardingQuery::class, $prime->connection('test')->builder());
132
133
        /** @var SimpleConnection $connection */
134
        $connection = $prime->connection('test.shard1');
135
        $this->assertSame($prime->connection('test')->getConfiguration(), $connection->getConfiguration());
136
    }
137
138
    /**
139
     *
140
     */
141
    public function test_array_cache()
142
    {
143
        $kernel = new class('test', true) extends Kernel {
144
            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...
145
146
            public function registerBundles(): iterable
147
            {
148
                return [
149
                    new FrameworkBundle(),
150
                    new PrimeBundle(),
151
                ];
152
            }
153
154
            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

154
            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...
155
            {
156
                $loader->import(__DIR__.'/Fixtures/array_cache.yaml');
157
            }
158
159
            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

159
            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...
160
        };
161
162
        $kernel->boot();
163
164
        /** @var ServiceLocator $prime */
165
        $prime = $kernel->getContainer()->get(ServiceLocator::class);
166
        $this->assertEquals(new ArrayCache(), $prime->mappers()->getResultCache());
167
    }
168
169
    /**
170
     *
171
     */
172
    public function test_pool_cache()
173
    {
174
        $kernel = new class('test', true) extends Kernel {
175
            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...
176
177
            public function registerBundles(): iterable
178
            {
179
                return [
180
                    new FrameworkBundle(),
181
                    new PrimeBundle(),
182
                ];
183
            }
184
185
            protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader)
186
            {
187
                $c->register('custom.cache', FilesystemAdapter::class);
188
189
                $loader->import(__DIR__.'/Fixtures/pool_cache.yaml');
190
            }
191
192
            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

192
            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...
193
        };
194
195
        $kernel->boot();
196
197
        /** @var ServiceLocator $prime */
198
        $prime = $kernel->getContainer()->get(ServiceLocator::class);
199
        $this->assertEquals(new DoctrineCacheAdapter(DoctrineProvider::wrap(new FilesystemAdapter())), $prime->mappers()->getResultCache());
200
    }
201
202
    /**
203
     *
204
     */
205
    public function test_global_config()
206
    {
207
        $kernel = new class('test', true) extends Kernel {
208
            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...
209
210
            public function registerBundles(): iterable
211
            {
212
                return [
213
                    new FrameworkBundle(),
214
                    new PrimeBundle(),
215
                ];
216
            }
217
218
            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

218
            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...
219
            {
220
                $loader->import(__DIR__.'/Fixtures/config.yaml');
221
            }
222
223
            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

223
            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...
224
        };
225
226
        $kernel->boot();
227
228
        /** @var ServiceLocator $prime */
229
        $prime = $kernel->getContainer()->get(ServiceLocator::class);
230
        /** @var SimpleConnection $connection */
231
        $connection = $prime->connection('test2');
232
233
        $this->assertNotNull($connection->getConfiguration()->getSQLLogger());
234
        $this->assertTrue($connection->getConfiguration()->getAutoCommit());
235
        $this->assertInstanceOf(FooType::class, $connection->getConfiguration()->getTypes()->get('foo'));
236
        $this->assertInstanceOf(BarType::class, $connection->getConfiguration()->getTypes()->get('bar'));
237
        $this->assertInstanceOf(ArrayType::class, $connection->getConfiguration()->getTypes()->get('array'));
238
    }
239
240
    /**
241
     *
242
     */
243
    public function test_connection_config()
244
    {
245
        $kernel = new class('test', true) extends Kernel {
246
            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...
247
248
            public function registerBundles(): iterable
249
            {
250
                return [
251
                    new FrameworkBundle(),
252
                    new PrimeBundle(),
253
                ];
254
            }
255
256
            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

256
            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...
257
            {
258
                $loader->import(__DIR__.'/Fixtures/config.yaml');
259
            }
260
261
            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

261
            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...
262
        };
263
264
        $kernel->boot();
265
266
        /** @var ServiceLocator $prime */
267
        $prime = $kernel->getContainer()->get(ServiceLocator::class);
268
        /** @var SimpleConnection $connection */
269
        $connection = $prime->connection('test');
270
271
        $this->assertNull($connection->getConfiguration()->getSQLLogger());
272
        $this->assertFalse($connection->getConfiguration()->getAutoCommit());
273
        $this->assertInstanceOf(BarType::class, $connection->getConfiguration()->getTypes()->get('foo'));
274
        $this->assertInstanceOf(BarType::class, $connection->getConfiguration()->getTypes()->get('bar'));
275
        $this->assertInstanceOf(ArrayType::class, $connection->getConfiguration()->getTypes()->get('array'));
276
    }
277
}
278
279
class FooType
280
{
281
282
}
283
class BarType
284
{
285
286
}
287