Completed
Pull Request — 2.x (#93)
by
unknown
02:09
created

OdmMetadataTest   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 172
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 5
Bugs 3 Features 2
Metric Value
wmc 14
c 5
b 3
f 2
lcom 1
cbo 1
dl 0
loc 172
rs 10

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