DataFixturesLoaderTest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 173
Duplicated Lines 7.51 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 9
dl 13
loc 173
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 13 13 1
A testGetFixtures() 0 20 3
A getFixturesClasses() 0 9 2
A createDataFixture() 0 8 1
B getFixturesProvider() 0 95 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace RDV\Bundle\MigrationBundle\Tests\Unit\Migration\Loader;
4
5
use RDV\Bundle\MigrationBundle\Entity\DataFixture;
6
use RDV\Bundle\MigrationBundle\Migration\Loader\DataFixturesLoader;
7
use RDV\Bundle\MigrationBundle\Tests\Unit\Fixture\TestPackage\Test1Bundle\TestPackageTest1Bundle;
8
use RDV\Bundle\MigrationBundle\Tests\Unit\Fixture\TestPackage\Test2Bundle\TestPackageTest2Bundle;
9
use RDV\Bundle\MigrationBundle\Tests\Unit\Fixture\TestPackage\Test3Bundle\TestPackageTest3Bundle;
10
11
class DataFixturesLoaderTest extends \PHPUnit_Framework_TestCase
12
{
13
    /** @var DataFixturesLoader */
14
    protected $loader;
15
16
    /** @var \PHPUnit_Framework_MockObject_MockObject */
17
    protected $em;
18
19
    /** @var \PHPUnit_Framework_MockObject_MockObject */
20
    protected $container;
21
22
    /** @var \PHPUnit_Framework_MockObject_MockObject */
23
    protected $fixtureRepo;
24
25 View Code Duplication
    protected function setUp()
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...
26
    {
27
        $this->container = $this->getMockForAbstractClass('Symfony\Component\DependencyInjection\ContainerInterface');
28
29
        $this->fixtureRepo = $this->getMockBuilder('Doctrine\ORM\EntityRepository')
30
            ->disableOriginalConstructor()->getMock();
31
        $this->em          = $this->getMockBuilder('Doctrine\ORM\EntityManager')
32
            ->disableOriginalConstructor()->getMock();
33
        $this->em->expects($this->any())->method('getRepository')->with('RdvMigrationBundle:DataFixture')
34
            ->willReturn($this->fixtureRepo);
35
36
        $this->loader = new DataFixturesLoader($this->em, $this->container);
37
    }
38
39
    /**
40
     * @dataProvider getFixturesProvider
41
     */
42
    public function testGetFixtures($bundles, $loadedDataFixtureClasses, $expectedFixtureClasses)
43
    {
44
        /** @var \Symfony\Component\HttpKernel\Bundle\Bundle $bundle */
45
        foreach ($bundles as $bundle) {
46
            $this->loader->loadFromDirectory(
47
                $bundle->getPath() . '/Migrations/Data/ORM'
48
            );
49
        }
50
51
        $loadedDataFixtures = [];
52
        foreach ($loadedDataFixtureClasses as $className => $version) {
53
            $loadedDataFixtures[] = $this->createDataFixture($className, $version);
54
        }
55
56
        $this->fixtureRepo->expects($this->any())->method('findAll')->willReturn($loadedDataFixtures);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<PHPUnit_Framework...ilder_InvocationMocker>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
57
58
        $fixtures       = $this->loader->getFixtures();
59
        $fixtureClasses = $this->getFixturesClasses($fixtures);
60
        $this->assertEquals($expectedFixtureClasses, $fixtureClasses);
61
    }
62
63
    protected function getFixturesClasses(array $fixtures)
64
    {
65
        $result = [];
66
        foreach ($fixtures as $fixture) {
67
            $result[] = get_class($fixture);
68
        }
69
70
        return $result;
71
    }
72
73
    /**
74
     * @param string $className
75
     * @param string $version
76
     *
77
     * @return DataFixture
78
     */
79
    protected function createDataFixture($className, $version)
80
    {
81
        $result = new DataFixture();
82
        $result->setClassName($className);
83
        $result->setVersion($version);
84
85
        return $result;
86
    }
87
88
    public function getFixturesProvider()
89
    {
90
        $test1BundleNamespace = 'RDV\Bundle\MigrationBundle\Tests\Unit\Fixture\TestPackage\Test1Bundle';
91
        $test2BundleNamespace = 'RDV\Bundle\MigrationBundle\Tests\Unit\Fixture\TestPackage\Test2Bundle';
92
        $test3BundleNamespace = 'RDV\Bundle\MigrationBundle\Tests\Unit\Fixture\TestPackage\Test3Bundle';
93
94
        return [
95
            [
96
                [],
97
                [],
98
                []
99
            ],
100
            [
101
                [new TestPackageTest1Bundle()],
102
                [],
103
                [
104
                    $test1BundleNamespace . '\Migrations\Data\ORM\LoadTest1BundleData',
105
                    'RDV\Bundle\MigrationBundle\Migration\UpdateDataFixturesFixture',
106
                ]
107
            ],
108
            [
109
                [new TestPackageTest1Bundle()],
110
                [
111
                    $test1BundleNamespace . '\Migrations\Data\ORM\LoadTest1BundleData' => ''
112
                ],
113
                []
114
            ],
115
            [
116
                [new TestPackageTest2Bundle()],
117
                [],
118
                [
119
                    $test1BundleNamespace . '\Migrations\Data\ORM\LoadTest1BundleData',
120
                    $test2BundleNamespace . '\Migrations\Data\ORM\LoadTest2BundleData',
121
                    'RDV\Bundle\MigrationBundle\Migration\UpdateDataFixturesFixture',
122
                ]
123
            ],
124
            [
125
                [new TestPackageTest2Bundle()],
126
                [
127
                    $test1BundleNamespace . '\Migrations\Data\ORM\LoadTest1BundleData' => ''
128
                ],
129
                [
130
                    $test2BundleNamespace . '\Migrations\Data\ORM\LoadTest2BundleData',
131
                    'RDV\Bundle\MigrationBundle\Migration\UpdateDataFixturesFixture',
132
                ]
133
            ],
134
            [
135
                [new TestPackageTest2Bundle(), new TestPackageTest1Bundle()],
136
                [],
137
                [
138
                    $test1BundleNamespace . '\Migrations\Data\ORM\LoadTest1BundleData',
139
                    $test2BundleNamespace . '\Migrations\Data\ORM\LoadTest2BundleData',
140
                    'RDV\Bundle\MigrationBundle\Migration\UpdateDataFixturesFixture',
141
                ]
142
            ],
143
            [
144
                [new TestPackageTest2Bundle(), new TestPackageTest1Bundle()],
145
                [
146
                    $test2BundleNamespace . '\Migrations\Data\ORM\LoadTest2BundleData' => ''
147
                ],
148
                [
149
                    $test1BundleNamespace . '\Migrations\Data\ORM\LoadTest1BundleData',
150
                    'RDV\Bundle\MigrationBundle\Migration\UpdateDataFixturesFixture',
151
                ]
152
            ],
153
            [
154
                [new TestPackageTest3Bundle()],
155
                [],
156
                [
157
                    $test3BundleNamespace . '\Migrations\Data\ORM\LoadTest3BundleData1',
158
                    $test3BundleNamespace . '\Migrations\Data\ORM\LoadTest3BundleData2',
159
                    'RDV\Bundle\MigrationBundle\Migration\UpdateDataFixturesFixture',
160
                ]
161
            ],
162
            [
163
                [new TestPackageTest3Bundle()],
164
                [
165
                    $test3BundleNamespace . '\Migrations\Data\ORM\LoadTest3BundleData1' => '1.0',
166
                    $test3BundleNamespace . '\Migrations\Data\ORM\LoadTest3BundleData2' => '0.0'
167
                ],
168
                [
169
                    $test3BundleNamespace . '\Migrations\Data\ORM\LoadTest3BundleData2',
170
                    'RDV\Bundle\MigrationBundle\Migration\UpdateDataFixturesFixture',
171
                ]
172
            ],
173
            [
174
                [new TestPackageTest3Bundle()],
175
                [
176
                    $test3BundleNamespace . '\Migrations\Data\ORM\LoadTest3BundleData1' => '2.0',
177
                    $test3BundleNamespace . '\Migrations\Data\ORM\LoadTest3BundleData2' => '1.0'
178
                ],
179
                []
180
            ]
181
        ];
182
    }
183
}
184