Issues (63)

Tests/WithMongoTest.php (2 issues)

1
<?php
2
3
namespace Bdf\PrimeBundle\Tests;
4
5
require_once __DIR__.'/TestKernel.php';
6
7
use Bdf\Prime\MongoDB\Collection\MongoCollectionLocator;
8
use Bdf\Prime\MongoDB\Document\DocumentMapper;
9
use Bdf\Prime\MongoDB\Schema\CollectionStructureUpgrader;
10
use Bdf\Prime\MongoDB\Schema\CollectionStructureUpgraderResolver;
11
use Bdf\Prime\Query\Expression\Like;
12
use Bdf\Prime\Schema\StructureUpgraderResolverAggregate;
13
use Bdf\PrimeBundle\PrimeBundle;
14
use Bdf\PrimeBundle\Tests\Documents\MapperWithDependency;
15
use Bdf\PrimeBundle\Tests\Documents\OtherDocumentMapper;
16
use Bdf\PrimeBundle\Tests\Documents\PersonDocument;
17
use Bdf\PrimeBundle\Tests\Documents\PersonDocumentMapper;
18
use PHPUnit\Framework\TestCase;
19
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
20
use Symfony\Component\Config\Loader\LoaderInterface;
21
use Symfony\Component\DependencyInjection\ContainerBuilder;
22
use Symfony\Component\HttpKernel\Kernel;
23
24
/**
25
 * BdfSerializerBundleTest.
26
 */
27
class WithMongoTest extends TestCase
28
{
29
    private $kernel;
30
31
    protected function setUp(): void
32
    {
33
        if (!class_exists(MongoCollectionLocator::class)) {
34
            $this->markTestSkipped('MongoDB driver not installed');
35
        }
36
37
        $this->kernel = new class('test', true) extends Kernel {
38
            use \Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
39
40
            public function registerBundles(): iterable
41
            {
42
                return [
43
                    new FrameworkBundle(),
44
                    new PrimeBundle(),
45
                ];
46
            }
47
48
            protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader)
0 ignored issues
show
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

48
            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...
49
            {
50
                $loader->import(__DIR__.'/Fixtures/conf_mongo.yaml');
51
            }
52
53
            protected function configureRoutes($routes)
0 ignored issues
show
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

53
            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...
54
            {
55
            }
56
        };
57
        $this->kernel->boot();
58
    }
59
60
    public function testKernel()
61
    {
62
        $this->assertInstanceOf(MongoCollectionLocator::class, $this->kernel->getContainer()->get(MongoCollectionLocator::class));
63
        $this->assertInstanceOf(CollectionStructureUpgraderResolver::class, $this->kernel->getContainer()->get(CollectionStructureUpgraderResolver::class));
64
    }
65
66
    public function testFunctional()
67
    {
68
        $this->kernel->getContainer()->get(CollectionStructureUpgraderResolver::class)->resolveByDomainClass(PersonDocument::class)->migrate();
69
70
        $person1 = new PersonDocument('John', 'Doe');
71
        $person2 = new PersonDocument('Jean', 'Dupont');
72
73
        $person1->save();
74
        $person2->save();
75
76
        $this->assertEquals([$person1], PersonDocument::where('firstName', 'john')->all());
77
        $this->assertEquals([$person1, $person2], PersonDocument::where('lastName', (new Like('o'))->contains())->all());
78
79
        $this->kernel->getContainer()->get(CollectionStructureUpgraderResolver::class)->resolveByDomainClass(PersonDocument::class)->drop();
80
    }
81
82
    public function testMapperWithDependencyInjection()
83
    {
84
        /** @var MongoCollectionLocator $locator */
85
        $locator = $this->kernel->getContainer()->get(MongoCollectionLocator::class);
86
87
        $collection = $locator->collectionByMapper(MapperWithDependency::class);
88
89
        $this->assertInstanceOf(MapperWithDependency::class, $collection->mapper());
90
        $this->assertSame($locator, $collection->mapper()->locator);
91
    }
92
93
    public function testSameHydratorInstanceShouldBeUsed()
94
    {
95
        /** @var MongoCollectionLocator $locator */
96
        $locator = $this->kernel->getContainer()->get(MongoCollectionLocator::class);
97
98
        $c1 = $locator->collectionByMapper(OtherDocumentMapper::class);
99
        $c2 = $locator->collectionByMapper(PersonDocumentMapper::class);
100
101
        $c1->mapper()->fromDatabase([], $c1->connection()->platform()->types());
102
        $c2->mapper()->fromDatabase([], $c1->connection()->platform()->types());
103
104
        $r = new \ReflectionProperty(DocumentMapper::class, 'hydrator');
105
        $r->setAccessible(true);
106
107
        $this->assertSame($r->getValue($c1->mapper()), $r->getValue($c2->mapper()));
108
    }
109
110
    public function testUpgrader()
111
    {
112
        if (!class_exists(StructureUpgraderResolverAggregate::class)) {
113
            $this->markTestSkipped('StructureUpgraderResolverAggregate is not found');
114
        }
115
116
        $this->assertInstanceOf(CollectionStructureUpgrader::class, $this->kernel->getContainer()->get(StructureUpgraderResolverAggregate::class)->resolveByDomainClass(PersonDocument::class));
117
        $this->assertInstanceOf(CollectionStructureUpgrader::class, $this->kernel->getContainer()->get(StructureUpgraderResolverAggregate::class)->resolveByMapperClass(PersonDocumentMapper::class));
118
    }
119
}
120