DocumentManagerTestCaseTrait::dm()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
rs 9.4285
cc 2
eloc 14
nc 2
nop 0
1
<?php
2
3
/**
4
 * This file is part of the Cubiche package.
5
 *
6
 * Copyright (c) Cubiche
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Cubiche\Infrastructure\Repository\Doctrine\Tests\Units\ODM\MongoDB;
13
14
use Cubiche\Infrastructure\Collections\Doctrine\ODM\MongoDB\EventListener\EventSubscriber as CollectionsEventSubscriber;
15
use Cubiche\Infrastructure\Doctrine\ODM\MongoDB\EventListener\MetadataEventSubscriber;
16
use Cubiche\Infrastructure\Identity\Doctrine\ODM\MongoDB\EventListener\EventSubscriber as IdentityEventSubscriber;
17
use Cubiche\Infrastructure\Model\Doctrine\ODM\MongoDB\EventListener\EventSubscriber as ModelEventSubscriber;
18
use Cubiche\Infrastructure\Geolocation\Doctrine\ODM\MongoDB\EventListener\EventSubscriber as GeolocationEventSubscriber;
19
use Cubiche\Infrastructure\System\Doctrine\ODM\MongoDB\EventListener\EventSubscriber as SystemEventSubscriber;
20
use Cubiche\Infrastructure\Web\Doctrine\ODM\MongoDB\EventListener\EventSubscriber as WebEventSubscriber;
21
use Cubiche\Infrastructure\Doctrine\ODM\MongoDB\Mapping\ClassMetadataFactory;
22
use Cubiche\Infrastructure\Repository\Doctrine\ODM\MongoDB\DocumentDataSourceFactory;
23
use Cubiche\Infrastructure\Repository\Doctrine\ODM\MongoDB\DocumentDataSourceFactoryInterface;
24
use Cubiche\Infrastructure\Repository\Doctrine\ODM\MongoDB\Query\ComparatorVisitorFactory;
25
use Cubiche\Infrastructure\Repository\Doctrine\ODM\MongoDB\Query\SpecificationVisitorFactory;
26
use Cubiche\Infrastructure\Repository\Doctrine\Tests\Units\ODM\MongoDB\Types\PhonenumberType;
27
use Cubiche\Infrastructure\Repository\Doctrine\Tests\Units\ODM\MongoDB\Types\RoleType;
28
use Doctrine\Common\Cache\FilesystemCache;
29
use Doctrine\MongoDB\Connection;
30
use Doctrine\ODM\MongoDB\Configuration;
31
use Doctrine\ODM\MongoDB\DocumentManager;
32
use Doctrine\ODM\MongoDB\Mapping\Driver\SimplifiedXmlDriver;
33
use Doctrine\ODM\MongoDB\Types\Type;
34
use Doctrine\ODM\MongoDB\UnitOfWork;
35
36
/**
37
 * Document Manager Test Case Trait.
38
 *
39
 * @author Karel Osorio Ramírez <[email protected]>
40
 */
41
trait DocumentManagerTestCaseTrait
42
{
43
    /**
44
     * @var DocumentManager
45
     */
46
    private $dm;
47
48
    /**
49
     * @var UnitOfWork
50
     */
51
    private $uow;
52
53
    /**
54
     * @var DocumentDataSourceFactoryInterface
55
     */
56
    private $documentDataSourceFactory;
57
58
    /**
59
     * @return \Doctrine\ODM\MongoDB\DocumentManager
60
     */
61
    public function dm()
62
    {
63
        if ($this->dm === null) {
64
            $this->dm = $this->createTestDocumentManager();
65
            $this->uow = $this->dm->getUnitOfWork();
66
67
            Type::addType('Phonenumber', PhonenumberType::class);
68
            Type::addType('Role', RoleType::class);
69
70
            $this->dm->getEventManager()->addEventSubscriber(new MetadataEventSubscriber());
71
            $this->dm->getEventManager()->addEventSubscriber(new CollectionsEventSubscriber());
72
            $this->dm->getEventManager()->addEventSubscriber(new ModelEventSubscriber());
73
            $this->dm->getEventManager()->addEventSubscriber(new IdentityEventSubscriber());
74
            $this->dm->getEventManager()->addEventSubscriber(new GeolocationEventSubscriber());
75
            $this->dm->getEventManager()->addEventSubscriber(new SystemEventSubscriber());
76
            $this->dm->getEventManager()->addEventSubscriber(new WebEventSubscriber());
77
        }
78
79
        return $this->dm;
80
    }
81
82
    /**
83
     * @return \Cubiche\Infrastructure\Repository\Doctrine\ODM\MongoDB\DocumentDataSourceFactoryInterface
84
     */
85
    public function documentDataSourceFactory()
86
    {
87
        if ($this->documentDataSourceFactory === null) {
88
            $this->documentDataSourceFactory = new DocumentDataSourceFactory(
89
                $this->dm(),
90
                new SpecificationVisitorFactory(),
91
                new ComparatorVisitorFactory()
92
            );
93
        }
94
95
        return $this->documentDataSourceFactory;
96
    }
97
98
    /**
99
     * @param string $testMethod
100
     */
101
    public function afterTestMethod($testMethod)
0 ignored issues
show
Unused Code introduced by
The parameter $testMethod is not used and could be removed.

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

Loading history...
102
    {
103
        $this->dm()->clear();
104
105
        $collections = $this->dm()->getConnection()->selectDatabase(DOCTRINE_MONGODB_DATABASE)->listCollections();
106
        foreach ($collections as $collection) {
107
            $collection->drop();
108
        }
109
    }
110
111
    /**
112
     * @return \Doctrine\ODM\MongoDB\Configuration
113
     */
114
    protected function getConfiguration()
115
    {
116
        $config = new Configuration();
117
        $config->setProxyDir(__DIR__.'/Proxies');
118
        $config->setProxyNamespace('Proxies');
119
        $config->setHydratorDir(__DIR__.'/Hydrators');
120
        $config->setHydratorNamespace('Hydrators');
121
        $config->setDefaultDB(DOCTRINE_MONGODB_DATABASE);
122
        $config->setClassMetadataFactoryName(ClassMetadataFactory::class);
123
        $config->setMetadataDriverImpl($this->createMetadataDriverImpl());
124
        $config->setMetadataCacheImpl(new FilesystemCache(__DIR__.'/Cache'));
125
126
        return $config;
127
    }
128
129
    /**
130
     * @return \Doctrine\ODM\MongoDB\Mapping\Driver\SimplifiedXmlDriver
131
     */
132
    protected function createMetadataDriverImpl()
133
    {
134
        $prefixs = array(
135
          __DIR__.'/mapping' => 'Cubiche\Domain\Repository\Tests\Fixtures',
136
        );
137
138
        return new SimplifiedXmlDriver($prefixs);
139
    }
140
141
    /**
142
     * @return \Doctrine\ODM\MongoDB\DocumentManager
143
     */
144
    protected function createTestDocumentManager()
145
    {
146
        $config = $this->getConfiguration();
147
        $connection = new Connection(DOCTRINE_MONGODB_SERVER, array(), $config);
148
149
        return DocumentManager::create($connection, $config);
150
    }
151
}
152