Passed
Push — master ( d86320...6ed86e )
by Vincent
04:15
created

FooType::name()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

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

99
        $this->assertEquals([$entity], \TestEntity::/** @scrutinizer ignore-call */ all());
Loading history...
100
        $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

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

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

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

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

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

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

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

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

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

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

262
            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...
263
        };
264
265
        $kernel->boot();
266
267
        /** @var ServiceLocator $prime */
268
        $prime = $kernel->getContainer()->get(ServiceLocator::class);
269
        /** @var SimpleConnection $connection */
270
        $connection = $prime->connection('test');
271
272
        $this->assertNull($connection->getConfiguration()->getSQLLogger());
273
        $this->assertFalse($connection->getConfiguration()->getAutoCommit());
274
        $this->assertInstanceOf(BarType::class, $connection->getConfiguration()->getTypes()->get('foo'));
275
        $this->assertInstanceOf(BarType::class, $connection->getConfiguration()->getTypes()->get('bar'));
276
        $this->assertInstanceOf(ArrayType::class, $connection->getConfiguration()->getTypes()->get('array'));
277
    }
278
}
279
280
class FooType implements TypeInterface
281
{
282
    public function fromDatabase($value, array $fieldOptions = [])
283
    {
284
        // TODO: Implement fromDatabase() method.
285
    }
286
287
    public function toDatabase($value)
288
    {
289
        // TODO: Implement toDatabase() method.
290
    }
291
292
    public function name(): string
293
    {
294
        // TODO: Implement name() method.
295
    }
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...
296
297
    public function phpType(): string
298
    {
299
        // TODO: Implement phpType() method.
300
    }
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...
301
}
302
class BarType implements TypeInterface
303
{
304
    public function fromDatabase($value, array $fieldOptions = [])
305
    {
306
        // TODO: Implement fromDatabase() method.
307
    }
308
309
    public function toDatabase($value)
310
    {
311
        // TODO: Implement toDatabase() method.
312
    }
313
314
    public function name(): string
315
    {
316
        // TODO: Implement name() method.
317
    }
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...
318
319
    public function phpType(): string
320
    {
321
        // TODO: Implement phpType() method.
322
    }
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...
323
}
324