Completed
Push — master ( cdf8aa...f4c17b )
by Ivannis Suárez
08:02
created

DocumentManagerTestCaseTrait   B

Complexity

Total Complexity 9

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 16

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 16
dl 0
loc 110
rs 8.4614

6 Methods

Rating   Name   Duplication   Size   Complexity  
A dm() 0 19 2
A documentDataSourceFactory() 0 12 2
A afterTestMethod() 0 9 2
A getConfiguration() 0 14 1
A createMetadataDriverImpl() 0 8 1
A createTestDocumentManager() 0 7 1
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
namespace Cubiche\Infrastructure\Repository\Doctrine\Tests\Units\ODM\MongoDB;
12
13
use Cubiche\Infrastructure\Collections\Doctrine\ODM\MongoDB\EventListener\EventSubscriber as CollectionsEventSubscriber;
14
use Cubiche\Infrastructure\Doctrine\ODM\MongoDB\EventListener\MetadataEventSubscriber;
15
use Cubiche\Infrastructure\Identity\Doctrine\ODM\MongoDB\EventListener\EventSubscriber as IdentityEventSubscriber;
16
use Cubiche\Infrastructure\Model\Doctrine\ODM\MongoDB\EventListener\EventSubscriber as ModelEventSubscriber;
17
use Cubiche\Infrastructure\Geolocation\Doctrine\ODM\MongoDB\EventListener\EventSubscriber as GeolocationEventSubscriber;
18
use Cubiche\Infrastructure\System\Doctrine\ODM\MongoDB\EventListener\EventSubscriber as SystemEventSubscriber;
19
use Cubiche\Infrastructure\Doctrine\ODM\MongoDB\Mapping\ClassMetadataFactory;
20
use Cubiche\Infrastructure\Repository\Doctrine\ODM\MongoDB\DocumentDataSourceFactory;
21
use Cubiche\Infrastructure\Repository\Doctrine\ODM\MongoDB\DocumentDataSourceFactoryInterface;
22
use Cubiche\Infrastructure\Repository\Doctrine\ODM\MongoDB\Query\ComparatorVisitorFactory;
23
use Cubiche\Infrastructure\Repository\Doctrine\ODM\MongoDB\Query\SpecificationVisitorFactory;
24
use Cubiche\Infrastructure\Repository\Doctrine\Tests\Units\ODM\MongoDB\Types\PhonenumberType;
25
use Cubiche\Infrastructure\Repository\Doctrine\Tests\Units\ODM\MongoDB\Types\RoleType;
26
use Doctrine\Common\Cache\FilesystemCache;
27
use Doctrine\MongoDB\Connection;
28
use Doctrine\ODM\MongoDB\Configuration;
29
use Doctrine\ODM\MongoDB\DocumentManager;
30
use Doctrine\ODM\MongoDB\Mapping\Driver\SimplifiedXmlDriver;
31
use Doctrine\ODM\MongoDB\Types\Type;
32
use Doctrine\ODM\MongoDB\UnitOfWork;
33
34
/**
35
 * Document Manager Test Case Trait.
36
 *
37
 * @author Karel Osorio Ramírez <[email protected]>
38
 */
39
trait DocumentManagerTestCaseTrait
40
{
41
    /**
42
     * @var DocumentManager
43
     */
44
    private $dm;
45
46
    /**
47
     * @var UnitOfWork
48
     */
49
    private $uow;
50
51
    /**
52
     * @var DocumentDataSourceFactoryInterface
53
     */
54
    private $documentDataSourceFactory;
55
56
    /**
57
     * @return \Doctrine\ODM\MongoDB\DocumentManager
58
     */
59
    public function dm()
60
    {
61
        if ($this->dm === null) {
62
            $this->dm = $this->createTestDocumentManager();
63
            $this->uow = $this->dm->getUnitOfWork();
64
65
            Type::addType('Phonenumber', PhonenumberType::class);
66
            Type::addType('Role', RoleType::class);
67
68
            $this->dm->getEventManager()->addEventSubscriber(new MetadataEventSubscriber());
69
            $this->dm->getEventManager()->addEventSubscriber(new CollectionsEventSubscriber());
70
            $this->dm->getEventManager()->addEventSubscriber(new ModelEventSubscriber());
71
            $this->dm->getEventManager()->addEventSubscriber(new IdentityEventSubscriber());
72
            $this->dm->getEventManager()->addEventSubscriber(new GeolocationEventSubscriber());
73
            $this->dm->getEventManager()->addEventSubscriber(new SystemEventSubscriber());
74
        }
75
76
        return $this->dm;
77
    }
78
79
    /**
80
     * @return \Cubiche\Infrastructure\Repository\Doctrine\ODM\MongoDB\DocumentDataSourceFactoryInterface
81
     */
82
    public function documentDataSourceFactory()
83
    {
84
        if ($this->documentDataSourceFactory === null) {
85
            $this->documentDataSourceFactory = new DocumentDataSourceFactory(
86
                $this->dm(),
87
                new SpecificationVisitorFactory(),
88
                new ComparatorVisitorFactory()
89
            );
90
        }
91
92
        return $this->documentDataSourceFactory;
93
    }
94
95
    /**
96
     * @param string $testMethod
97
     */
98
    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...
99
    {
100
        $this->dm()->clear();
101
102
        $collections = $this->dm()->getConnection()->selectDatabase(DOCTRINE_MONGODB_DATABASE)->listCollections();
103
        foreach ($collections as $collection) {
104
            $collection->drop();
105
        }
106
    }
107
108
    /**
109
     * @return \Doctrine\ODM\MongoDB\Configuration
110
     */
111
    protected function getConfiguration()
112
    {
113
        $config = new Configuration();
114
        $config->setProxyDir(__DIR__.'/Proxies');
115
        $config->setProxyNamespace('Proxies');
116
        $config->setHydratorDir(__DIR__.'/Hydrators');
117
        $config->setHydratorNamespace('Hydrators');
118
        $config->setDefaultDB(DOCTRINE_MONGODB_DATABASE);
119
        $config->setClassMetadataFactoryName(ClassMetadataFactory::class);
120
        $config->setMetadataDriverImpl($this->createMetadataDriverImpl());
121
        $config->setMetadataCacheImpl(new FilesystemCache(__DIR__.'/Cache'));
122
123
        return $config;
124
    }
125
126
    /**
127
     * @return \Doctrine\ODM\MongoDB\Mapping\Driver\SimplifiedXmlDriver
128
     */
129
    protected function createMetadataDriverImpl()
130
    {
131
        $prefixs = array(
132
          __DIR__.'/mapping' => 'Cubiche\Domain\Repository\Tests\Fixtures',
133
        );
134
135
        return new SimplifiedXmlDriver($prefixs);
136
    }
137
138
    /**
139
     * @return \Doctrine\ODM\MongoDB\DocumentManager
140
     */
141
    protected function createTestDocumentManager()
142
    {
143
        $config = $this->getConfiguration();
144
        $connection = new Connection(DOCTRINE_MONGODB_SERVER, array(), $config);
145
146
        return DocumentManager::create($connection, $config);
147
    }
148
}
149