Completed
Push — refonte ( 7173e3...bffa4c )
by Arnaud
02:33
created

ActionFactoryTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 119
Duplicated Lines 73.95 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 4
dl 88
loc 119
rs 10
c 0
b 0
f 0

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 LAG\AdminBundle\Tests\Factory;
4
5
use LAG\AdminBundle\Configuration\ActionConfiguration;
6
use LAG\AdminBundle\Configuration\AdminConfiguration;
7
use LAG\AdminBundle\Exception\Exception;
8
use LAG\AdminBundle\Factory\ActionFactory;
9
use LAG\AdminBundle\Factory\ConfigurationFactory;
10
use LAG\AdminBundle\Resource\ResourceCollection;
11
use LAG\AdminBundle\Tests\AdminTestBase;
12
use LAG\AdminBundle\Tests\Fixtures\ActionFixture;
13
use LAG\AdminBundle\Tests\Fixtures\EntityFixture;
14
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
15
16
class ActionFactoryTest extends AdminTestBase
17
{
18
    public function testCreate()
19
    {
20
        $resourceCollection = $this->getMockWithoutConstructor(ResourceCollection::class);
21
        $eventDispatcher = $this->getMockWithoutConstructor(EventDispatcherInterface::class);
22
23
        $adminConfiguration = $this->getMockWithoutConstructor(AdminConfiguration::class);
24
        $adminConfiguration
25
            ->expects($this->atLeastOnce())
26
            ->method('getParameter')
27
            ->willReturnMap([
28
                ['actions', [
29
                    'list' => [],
30
                ]],
31
                ['entity', 'MyLittleTauntaun'],
32
                ['class', EntityFixture::class],
33
            ])
34
        ;
35
36
        $actionConfiguration = $this->getMockWithoutConstructor(ActionConfiguration::class);
37
        $actionConfiguration
38
            ->expects($this->once())
39
            ->method('getParameter')
40
            ->willReturnMap([
41
                ['class', ActionFixture::class],
42
            ])
43
        ;
44
45
        $configurationFactory = $this->getMockWithoutConstructor(ConfigurationFactory::class);
46
        $configurationFactory
47
            ->expects($this->once())
48
            ->method('createActionConfiguration')
49
            ->with('list',  [], 'tauntaun', $adminConfiguration)
50
            ->willReturn($actionConfiguration)
51
        ;
52
53
        $factory = new ActionFactory(
54
            $resourceCollection,
55
            $eventDispatcher,
56
            $configurationFactory
57
        );
58
59
        $factory->create('list', 'tauntaun', $adminConfiguration);
60
    }
61
62
    public function testCreateWithMissingAction()
63
    {
64
        $resourceCollection = $this->getMockWithoutConstructor(ResourceCollection::class);
65
        $eventDispatcher = $this->getMockWithoutConstructor(EventDispatcherInterface::class);
66
        $configurationFactory = $this->getMockWithoutConstructor(ConfigurationFactory::class);
67
68
        $factory = new ActionFactory(
69
            $resourceCollection,
70
            $eventDispatcher,
71
            $configurationFactory
72
        );
73
74
        $adminConfiguration = $this->getMockWithoutConstructor(AdminConfiguration::class);
75
        $adminConfiguration
76
            ->expects($this->atLeastOnce())
77
            ->method('getParameter')
78
            ->willReturnMap([
79
                ['actions', []],
80
                ['entity', 'MyLittleTauntaun'],
81
            ])
82
        ;
83
84
        $this->assertExceptionRaised(Exception::class, function () use ($factory, $adminConfiguration) {
85
            $factory->create('list', 'tauntaun', $adminConfiguration);
86
        });
87
    }
88
89
    public function testCreateWithWrongActionClass()
90
    {
91
        $resourceCollection = $this->getMockWithoutConstructor(ResourceCollection::class);
92
        $eventDispatcher = $this->getMockWithoutConstructor(EventDispatcherInterface::class);
93
94
        $adminConfiguration = $this->getMockWithoutConstructor(AdminConfiguration::class);
95
        $adminConfiguration
96
            ->expects($this->atLeastOnce())
97
            ->method('getParameter')
98
            ->willReturnMap([
99
                ['actions', [
100
                    'list' => [],
101
                ]],
102
                ['entity', 'MyLittleTauntaun'],
103
                ['class', EntityFixture::class],
104
            ])
105
        ;
106
107
        $actionConfiguration = $this->getMockWithoutConstructor(ActionConfiguration::class);
108
        $actionConfiguration
109
            ->expects($this->once())
110
            ->method('getParameter')
111
            ->willReturnMap([
112
                ['class', EntityFixture::class],
113
            ])
114
        ;
115
116
        $configurationFactory = $this->getMockWithoutConstructor(ConfigurationFactory::class);
117
        $configurationFactory
118
            ->expects($this->once())
119
            ->method('createActionConfiguration')
120
            ->with('list',  [], 'tauntaun', $adminConfiguration)
121
            ->willReturn($actionConfiguration)
122
        ;
123
124
        $factory = new ActionFactory(
125
            $resourceCollection,
126
            $eventDispatcher,
127
            $configurationFactory
128
        );
129
130
        $this->assertExceptionRaised(Exception::class, function () use ($factory, $adminConfiguration) {
131
            $factory->create('list', 'tauntaun', $adminConfiguration);
132
        });
133
    }
134
}
135