OdmMetadataTest   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 166
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 1
dl 0
loc 166
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A testDocumentNames() 0 9 1
A testDirectoryWithDotInPath() 0 9 1
A testGetMappingDocumentDirectory() 0 11 1
A testGetExtendedMappingDocumentDirectory() 0 11 1
A testGetDocumentDirectory() 0 11 1
A testGetExtendedDocumentDirectory() 0 11 1
A testGetExtendedSerializerDirectory() 0 11 1
A testGetDocumentMappingFiles() 0 18 2
A testGetDocumentMappingFilesWithFilesNotFound() 0 9 1
A testGetRepositoryFiles() 0 18 2
A testGetRepositoryFilesWithFilesNotFound() 0 9 1
A getBundleMetadataMock() 0 20 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\EasyExtendsBundle\Tests\Bundle;
15
16
use PHPUnit\Framework\TestCase;
17
use Sonata\AcmeBundle\SonataAcmeBundle;
18
use Sonata\EasyExtendsBundle\Bundle\BundleMetadata;
19
use Sonata\EasyExtendsBundle\Bundle\OdmMetadata;
20
use Symfony\Component\Finder\SplFileInfo;
21
use Symfony\Component\HttpKernel\Bundle\Bundle;
22
23
class OdmMetadataTest extends TestCase
24
{
25
    public function testDocumentNames(): void
26
    {
27
        $odmMetadata = new OdmMetadata($this->getBundleMetadataMock(__DIR__.'/Fixtures/bundle1'));
28
29
        $documentNames = $odmMetadata->getDocumentNames();
30
31
        $this->assertContains('Block', $documentNames);
32
        $this->assertContains('Page', $documentNames);
33
    }
34
35
    public function testDirectoryWithDotInPath(): void
36
    {
37
        $odmMetadata = new OdmMetadata($this->getBundleMetadataMock(__DIR__.'/Fixtures/bundle2/dot.dot'));
38
39
        $documentNames = $odmMetadata->getDocumentNames();
40
41
        $this->assertContains('Block', $documentNames);
42
        $this->assertContains('Page', $documentNames);
43
    }
44
45
    public function testGetMappingDocumentDirectory(): void
46
    {
47
        $bundlePath = __DIR__.'/Fixtures/bundle1';
48
        $expectedDirectory = $bundlePath.'/Resources/config/doctrine/';
49
50
        $odmMetadata = new OdmMetadata($this->getBundleMetadataMock($bundlePath));
51
52
        $directory = $odmMetadata->getMappingDocumentDirectory();
53
54
        $this->assertSame($expectedDirectory, $directory);
55
    }
56
57
    public function testGetExtendedMappingDocumentDirectory(): void
58
    {
59
        $bundlePath = __DIR__.'/Fixtures/bundle1';
60
        $expectedDirectory = 'Application/Sonata/AcmeBundle/Resources/config/doctrine/';
61
62
        $odmMetadata = new OdmMetadata($this->getBundleMetadataMock($bundlePath));
63
64
        $directory = $odmMetadata->getExtendedMappingDocumentDirectory();
65
66
        $this->assertSame($expectedDirectory, $directory);
67
    }
68
69
    public function testGetDocumentDirectory(): void
70
    {
71
        $bundlePath = __DIR__.'/Fixtures/bundle1';
72
        $expectedDirectory = $bundlePath.'/Document';
73
74
        $odmMetadata = new OdmMetadata($this->getBundleMetadataMock($bundlePath));
75
76
        $directory = $odmMetadata->getDocumentDirectory();
77
78
        $this->assertSame($expectedDirectory, $directory);
79
    }
80
81
    public function testGetExtendedDocumentDirectory(): void
82
    {
83
        $bundlePath = __DIR__.'/Fixtures/bundle1';
84
        $expectedDirectory = 'Application/Sonata/AcmeBundle/Document';
85
86
        $odmMetadata = new OdmMetadata($this->getBundleMetadataMock($bundlePath));
87
88
        $directory = $odmMetadata->getExtendedDocumentDirectory();
89
90
        $this->assertSame($expectedDirectory, $directory);
91
    }
92
93
    public function testGetExtendedSerializerDirectory(): void
94
    {
95
        $bundlePath = __DIR__.'/Fixtures/bundle1';
96
        $expectedDirectory = 'Application/Sonata/AcmeBundle/Resources/config/serializer';
97
98
        $ormMetadata = new OdmMetadata($this->getBundleMetadataMock($bundlePath));
99
100
        $directory = $ormMetadata->getExtendedSerializerDirectory();
101
102
        $this->assertSame($expectedDirectory, $directory);
103
    }
104
105
    public function testGetDocumentMappingFiles(): void
106
    {
107
        $odmMetadata = new OdmMetadata($this->getBundleMetadataMock(__DIR__.'/Fixtures/bundle1'));
108
109
        $filterIterator = $odmMetadata->getDocumentMappingFiles();
110
111
        $files = [];
112
        foreach ($filterIterator as $file) {
113
            $files[] = $file->getFilename();
114
        }
115
116
        $this->assertInstanceOf('Iterator', $filterIterator);
117
        $this->assertContainsOnly(SplFileInfo::class, $filterIterator);
118
        $this->assertContains('Block.mongodb.xml.skeleton', $files);
119
        $this->assertContains('Page.mongodb.xml.skeleton', $files);
120
        $this->assertNotContains('Block.odm.xml.skeleton', $files);
121
        $this->assertNotContains('Page.odm.xml.skeleton', $files);
122
    }
123
124
    public function testGetDocumentMappingFilesWithFilesNotFound(): void
125
    {
126
        $odmMetadata = new OdmMetadata($this->getBundleMetadataMock(__DIR__.'/Fixtures'));
127
128
        $result = $odmMetadata->getDocumentMappingFiles();
129
130
        $this->assertIsArray($result);
131
        $this->assertEmpty($result);
132
    }
133
134
    public function testGetRepositoryFiles(): void
135
    {
136
        $odmMetadata = new OdmMetadata($this->getBundleMetadataMock(__DIR__.'/Fixtures/bundle1'));
137
138
        $filterIterator = $odmMetadata->getRepositoryFiles();
139
140
        $files = [];
141
        foreach ($filterIterator as $file) {
142
            $files[] = $file->getFilename();
143
        }
144
145
        $this->assertInstanceOf('Iterator', $filterIterator);
146
        $this->assertContainsOnly(SplFileInfo::class, $filterIterator);
147
        $this->assertContains('BlockRepository.php', $files);
148
        $this->assertContains('PageRepository.php', $files);
149
        $this->assertNotContains('Block.php', $files);
150
        $this->assertNotContains('Page.php', $files);
151
    }
152
153
    public function testGetRepositoryFilesWithFilesNotFound(): void
154
    {
155
        $odmMetadata = new OdmMetadata($this->getBundleMetadataMock(__DIR__.'/Fixtures'));
156
157
        $result = $odmMetadata->getRepositoryFiles();
158
159
        $this->assertIsArray($result);
160
        $this->assertEmpty($result);
161
    }
162
163
    /**
164
     * @param string $bundlePath
165
     *
166
     * @return BundleMetadata
167
     */
168
    private function getBundleMetadataMock($bundlePath)
169
    {
170
        $bundle = $this->createMock(Bundle::class);
171
        $bundle
172
            ->method('getPath')
173
            ->willReturn($bundlePath);
174
175
        $bundleMetadata = $this->createMock(BundleMetadata::class);
176
        $bundleMetadata
177
            ->method('getBundle')
178
            ->willReturn($bundle);
179
        $bundleMetadata
180
            ->method('getClass')
181
            ->willReturn(SonataAcmeBundle::class);
182
        $bundleMetadata
183
            ->method('getExtendedDirectory')
184
            ->willReturn('Application/Sonata/AcmeBundle');
185
186
        return $bundleMetadata;
187
    }
188
}
189