Completed
Push — master ( 7888fa...8e3a44 )
by Ivannis Suárez
04:39
created

ClassMetadataFactoryTests::testGetMetadataFor()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 33
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
c 0
b 0
f 0
rs 8.8571
cc 1
eloc 30
nc 1
nop 0
1
<?php
2
3
/**
4
 * This file is part of the Cubiche/Metadata component.
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\Core\Metadata\Tests\Units;
13
14
use Cubiche\Core\Metadata\Cache\FileCache;
15
use Cubiche\Core\Metadata\ClassMetadata;
16
use Cubiche\Core\Metadata\ClassMetadataFactory;
17
use Cubiche\Core\Metadata\Tests\Fixtures\Address;
18
use Cubiche\Core\Metadata\Tests\Fixtures\Driver\AnnotationDriver;
19
use Cubiche\Core\Metadata\Tests\Fixtures\User;
20
use Doctrine\Common\Annotations\AnnotationReader;
21
use Doctrine\Common\Annotations\AnnotationRegistry;
22
use Doctrine\Common\Annotations\CachedReader;
23
use Doctrine\Common\Cache\FilesystemCache;
24
use mageekguy\atoum\adapter as Adapter;
25
use mageekguy\atoum\annotations\extractor as Extractor;
26
use mageekguy\atoum\asserter\generator as Generator;
27
use mageekguy\atoum\test\assertion\manager as Manager;
28
use mageekguy\atoum\tools\variable\analyzer as Analyzer;
29
30
/**
31
 * ClassMetadataFactoryTests class.
32
 *
33
 * Generated by TestGenerator on 2017-05-16 at 13:17:21.
34
 *
35
 * @engine isolate
36
 */
37
class ClassMetadataFactoryTests extends TestCase
38
{
39
    /**
40
     * @var string
41
     */
42
    protected $cacheDirectory;
43
44
    /**
45
     * @param Adapter   $adapter
46
     * @param Extractor $annotationExtractor
47
     * @param Generator $asserterGenerator
48
     * @param Manager   $assertionManager
49
     * @param \Closure  $reflectionClassFactory
50
     * @param \Closure  $phpExtensionFactory
51
     * @param Analyzer  $analyzer
52
     */
53 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
54
        Adapter $adapter = null,
55
        Extractor $annotationExtractor = null,
56
        Generator $asserterGenerator = null,
57
        Manager $assertionManager = null,
58
        \Closure $reflectionClassFactory = null,
59
        \Closure $phpExtensionFactory = null,
60
        Analyzer $analyzer = null
61
    ) {
62
        parent::__construct(
63
            $adapter,
64
            $annotationExtractor,
65
            $asserterGenerator,
66
            $assertionManager,
67
            $reflectionClassFactory,
68
            $phpExtensionFactory,
69
            $analyzer
70
        );
71
72
        $this->cacheDirectory = __DIR__.'/Factory/Cache';
73
    }
74
75
    /**
76
     * Create the cache directory.
77
     */
78
    public function setUp()
79
    {
80
        if (!is_dir($this->cacheDirectory)) {
81
            mkdir($this->cacheDirectory);
82
        }
83
    }
84
85
    /**
86
     * Remove the cache directory.
87
     */
88
    public function tearDown()
89
    {
90
        $this->rmdir($this->cacheDirectory);
91
    }
92
93
    /**
94
     * @return AnnotationDriver
95
     */
96 View Code Duplication
    protected function createDriver()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
    {
98
        $reader = new CachedReader(
99
            new AnnotationReader(),
100
            new FilesystemCache($this->cacheDirectory),
101
            $debug = true
102
        );
103
104
        AnnotationRegistry::registerFile(__DIR__.'/../Fixtures/Annotations/AggregateRoot.php');
105
        AnnotationRegistry::registerFile(__DIR__.'/../Fixtures/Annotations/Entity.php');
106
        AnnotationRegistry::registerFile(__DIR__.'/../Fixtures/Annotations/Field.php');
107
        AnnotationRegistry::registerFile(__DIR__.'/../Fixtures/Annotations/Id.php');
108
109
        $driver = new AnnotationDriver($reader, [__DIR__.'/../Fixtures']);
110
        $driver->addExcludePaths([
111
            __DIR__.'/../Fixtures/Annotations',
112
            __DIR__.'/../Fixtures/Driver',
113
            __DIR__.'/../Fixtures/mapping',
114
            __DIR__.'/../Fixtures/mapping-two',
115
        ]);
116
        $driver->setFileExtension('.php');
117
118
        return $driver;
119
    }
120
121
    /**
122
     * @return FileCache
123
     */
124
    protected function createCache()
125
    {
126
        return new FileCache($this->cacheDirectory);
127
    }
128
129
    /**
130
     * @return ClassMetadataFactory
131
     */
132
    protected function createFactory()
133
    {
134
        return new ClassMetadataFactory($this->createDriver(), $this->createCache());
135
    }
136
137
    /**
138
     * @return ClassMetadataFactory
139
     */
140
    protected function createFactoryWithoutCache()
141
    {
142
        return new ClassMetadataFactory($this->createDriver());
143
    }
144
145
    /**
146
     * Test GetAllMetadata method.
147
     */
148
    public function testGetAllMetadata()
149
    {
150
        $this
151
            ->given($factory = $this->createFactoryWithoutCache())
152
            ->when($metadatas = $factory->getAllMetadata())
153
            ->then()
154
                ->array($metadatas)
155
                    ->hasSize(2)
156
        ;
157
158
        $this
159
            ->given($factory = $this->createFactory())
160
            ->when($metadatas = $factory->getAllMetadata())
161
            ->then()
162
                ->array($metadatas)
163
                    ->hasSize(2)
164
        ;
165
    }
166
167
    /**
168
     * Test GetMetadataFor method.
169
     */
170
    public function testGetMetadataFor()
171
    {
172
        $this
173
            ->given($factory = $this->createFactory())
174
            ->when($metadatas = $factory->getAllMetadata())
175
            ->then()
176
                ->array($metadatas)
177
                    ->hasSize(2)
178
                ->object($classMetadata = $factory->getMetadataFor(User::class))
179
                    ->isInstanceOf(ClassMetadata::class)
180
                ->array($classMetadata->propertiesMetadata())
181
                    ->hasSize(7)
182
                    ->hasKey('id')
183
                    ->hasKey('name')
184
                    ->hasKey('username')
185
                    ->hasKey('age')
186
                    ->hasKey('email')
187
                    ->hasKey('addresses')
188
                    ->hasKey('friends')
189
                ->object($propertyMetadata = $classMetadata->propertyMetadata('id'))
190
                    ->isNotNull()
191
                ->boolean($propertyMetadata->getMetadata('identifier'))
192
                    ->isTrue()
193
                ->string($propertyMetadata->getMetadata('name'))
194
                    ->isEqualTo('_id')
195
                ->string($classMetadata->propertyMetadata('name')->getMetadata('name'))
196
                    ->isEqualTo('fullName')
197
                ->string($classMetadata->propertyMetadata('addresses')->getMetadata('type'))
198
                    ->isEqualTo('ArraySet')
199
                ->string($classMetadata->propertyMetadata('addresses')->getMetadata('of'))
200
                    ->isEqualTo('Cubiche\Core\Metadata\Tests\Fixtures\Address')
201
        ;
202
    }
203
204
    /**
205
     * Test HasMetadataFor method.
206
     */
207
    public function testHasMetadataFor()
208
    {
209
        $this
210
            ->given($factory = $this->createFactory())
211
            ->when($metadatas = $factory->getAllMetadata())
212
            ->then()
213
                ->boolean($classMetadata = $factory->hasMetadataFor(Address::class))
214
                    ->isTrue()
215
        ;
216
    }
217
218
    /**
219
     * Test SetMetadataFor method.
220
     */
221
    public function testSetMetadataFor()
222
    {
223
        $this
224
            ->given($factory = $this->createFactory())
225
            ->then()
226
                ->boolean($classMetadata = $factory->hasMetadataFor(Address::class))
227
                    ->isFalse()
228
                ->and()
229
                ->when($factory->setMetadataFor(Address::class, new ClassMetadata(Address::class)))
230
                ->then()
231
                    ->boolean($classMetadata = $factory->hasMetadataFor(Address::class))
232
                        ->isTrue()
233
        ;
234
    }
235
}
236