OrmMetadataTest::testGetRepositoryFiles()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 2
nc 2
nop 0
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\OrmMetadata;
20
use Symfony\Component\Finder\SplFileInfo;
21
use Symfony\Component\HttpKernel\Bundle\Bundle;
22
23
class OrmMetadataTest extends TestCase
24
{
25
    public function testEntityNames(): void
26
    {
27
        $ormMetadata = new OrmMetadata($this->getBundleMetadataMock(__DIR__.'/Fixtures/bundle1'));
28
29
        $entityNames = $ormMetadata->getEntityNames();
30
31
        $this->assertCount(4, $entityNames);
32
        $this->assertContains('Block', $entityNames);
33
        $this->assertContains('Page', $entityNames);
34
    }
35
36
    public function testDirectoryWithDotInPath(): void
37
    {
38
        $ormMetadata = new OrmMetadata($this->getBundleMetadataMock(__DIR__.'/Fixtures/bundle2/dot.dot'));
39
40
        $entityNames = $ormMetadata->getEntityNames();
41
42
        $this->assertCount(4, $entityNames);
43
        $this->assertContains('Block', $entityNames);
44
        $this->assertContains('Page', $entityNames);
45
    }
46
47
    public function testGetMappingEntityDirectory(): void
48
    {
49
        $bundlePath = __DIR__.'/Fixtures/bundle1';
50
        $expectedDirectory = $bundlePath.'/Resources/config/doctrine/';
51
52
        $ormMetadata = new OrmMetadata($this->getBundleMetadataMock($bundlePath));
53
54
        $directory = $ormMetadata->getMappingEntityDirectory();
55
56
        $this->assertSame($expectedDirectory, $directory);
57
    }
58
59
    public function testGetExtendedMappingEntityDirectory(): void
60
    {
61
        $bundlePath = __DIR__.'/Fixtures/bundle1';
62
        $expectedDirectory = 'Application/Sonata/AcmeBundle/Resources/config/doctrine/';
63
64
        $ormMetadata = new OrmMetadata($this->getBundleMetadataMock($bundlePath));
65
66
        $directory = $ormMetadata->getExtendedMappingEntityDirectory();
67
68
        $this->assertSame($expectedDirectory, $directory);
69
    }
70
71
    public function testGetEntityDirectory(): void
72
    {
73
        $bundlePath = __DIR__.'/Fixtures/bundle1';
74
        $expectedDirectory = $bundlePath.'/Entity';
75
76
        $ormMetadata = new OrmMetadata($this->getBundleMetadataMock($bundlePath));
77
78
        $directory = $ormMetadata->getEntityDirectory();
79
80
        $this->assertSame($expectedDirectory, $directory);
81
    }
82
83
    public function testGetExtendedEntityDirectory(): void
84
    {
85
        $bundlePath = __DIR__.'/Fixtures/bundle1';
86
        $expectedDirectory = 'Application/Sonata/AcmeBundle/Entity';
87
88
        $ormMetadata = new OrmMetadata($this->getBundleMetadataMock($bundlePath));
89
90
        $directory = $ormMetadata->getExtendedEntityDirectory();
91
92
        $this->assertSame($expectedDirectory, $directory);
93
    }
94
95
    public function testGetExtendedSerializerDirectory(): void
96
    {
97
        $bundlePath = __DIR__.'/Fixtures/bundle1';
98
        $expectedDirectory = 'Application/Sonata/AcmeBundle/Resources/config/serializer';
99
100
        $ormMetadata = new OrmMetadata($this->getBundleMetadataMock($bundlePath));
101
102
        $directory = $ormMetadata->getExtendedSerializerDirectory();
103
104
        $this->assertSame($expectedDirectory, $directory);
105
    }
106
107
    public function testGetEntityMappingFiles(): void
108
    {
109
        $ormMetadata = new OrmMetadata($this->getBundleMetadataMock(__DIR__.'/Fixtures/bundle1'));
110
111
        $filterIterator = $ormMetadata->getEntityMappingFiles();
112
113
        $files = [];
114
        foreach ($filterIterator as $file) {
115
            $files[] = $file->getFilename();
116
        }
117
118
        $this->assertInstanceOf('Iterator', $filterIterator);
119
        $this->assertContainsOnly(SplFileInfo::class, $filterIterator);
120
        $this->assertContains('Block.orm.xml.skeleton', $files);
121
        $this->assertContains('Page.orm.xml.skeleton', $files);
122
        $this->assertNotContains('Block.mongodb.xml.skeleton', $files);
123
        $this->assertNotContains('Page.mongodb.xml.skeleton', $files);
124
    }
125
126
    public function testGetEntityMappingFilesWithFilesNotFound(): void
127
    {
128
        $ormMetadata = new OrmMetadata($this->getBundleMetadataMock(__DIR__.'/Fixtures'));
129
130
        $result = $ormMetadata->getEntityMappingFiles();
131
132
        $this->assertIsArray($result);
133
        $this->assertEmpty($result);
134
    }
135
136
    public function testGetRepositoryFiles(): void
137
    {
138
        $ormMetadata = new OrmMetadata($this->getBundleMetadataMock(__DIR__.'/Fixtures/bundle1'));
139
140
        $filterIterator = $ormMetadata->getRepositoryFiles();
141
142
        $files = [];
143
        foreach ($filterIterator as $file) {
144
            $files[] = $file->getFilename();
145
        }
146
147
        $this->assertInstanceOf('Iterator', $filterIterator);
148
        $this->assertContainsOnly(SplFileInfo::class, $filterIterator);
149
        $this->assertContains('BlockRepository.php', $files);
150
        $this->assertContains('PageRepository.php', $files);
151
        $this->assertNotContains('Block.php', $files);
152
        $this->assertNotContains('Page.php', $files);
153
    }
154
155
    public function testGetRepositoryFilesWithFilesNotFound(): void
156
    {
157
        $ormMetadata = new OrmMetadata($this->getBundleMetadataMock(__DIR__.'/Fixtures'));
158
159
        $result = $ormMetadata->getRepositoryFiles();
160
161
        $this->assertIsArray($result);
162
        $this->assertEmpty($result);
163
    }
164
165
    /**
166
     * @param string $bundlePath
167
     *
168
     * @return BundleMetadata
169
     */
170
    private function getBundleMetadataMock($bundlePath)
171
    {
172
        $bundle = $this->createMock(Bundle::class);
173
        $bundle
174
            ->method('getPath')
175
            ->willReturn($bundlePath);
176
177
        $bundleMetadata = $this->createMock(BundleMetadata::class);
178
        $bundleMetadata
179
            ->method('getBundle')
180
            ->willReturn($bundle);
181
        $bundleMetadata
182
            ->method('getClass')
183
            ->willReturn(SonataAcmeBundle::class);
184
        $bundleMetadata
185
            ->method('getExtendedDirectory')
186
            ->willReturn('Application/Sonata/AcmeBundle');
187
188
        return $bundleMetadata;
189
    }
190
}
191