Completed
Push — refonte ( 64e01a...7173e3 )
by Arnaud
03:31
created

EditResponderTest::testRespondWithSave()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 77

Duplication

Lines 77
Ratio 100 %

Importance

Changes 0
Metric Value
dl 77
loc 77
rs 8.5018
c 0
b 0
f 0
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace LAG\AdminBundle\Tests\AdminBundle\Action\Responder;
4
5
use LAG\AdminBundle\Action\Configuration\ActionConfiguration;
6
use LAG\AdminBundle\Action\Responder\EditResponder;
7
use LAG\AdminBundle\Admin\AdminInterface;
8
use LAG\AdminBundle\Admin\Configuration\AdminConfiguration;
9
use LAG\AdminBundle\Tests\AdminTestBase;
10
use Symfony\Component\Form\FormInterface;
11
use Symfony\Component\HttpFoundation\RedirectResponse;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpFoundation\Response;
14
use Symfony\Component\Routing\RouterInterface;
15
use Test\TestBundle\Entity\TestEntity;
16
use Twig_Environment;
17
18
class EditResponderTest extends AdminTestBase
19
{
20 View Code Duplication
    public function testRespondWithSave()
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...
21
    {
22
        $routing = $this->getMockWithoutConstructor(RouterInterface::class);
23
        $routing
24
            ->expects($this->atLeastOnce())
25
            ->method('generate')
26
            ->with('my_little_admin.edit')
27
            ->willReturn('http://test.fr')
28
        ;
29
        
30
        $configuration = $this->getMockWithoutConstructor(ActionConfiguration::class);
31
        $configuration
32
            ->expects($this->atLeastOnce())
33
            ->method('getParameter')
34
            ->willReturnMap([
35
                ['template', 'my_template.twig'],
36
            ])
37
        ;
38
    
39
        $adminConfiguration = $this->getMockWithoutConstructor(AdminConfiguration::class);
40
        $adminConfiguration
41
            ->expects($this->once())
42
            ->method('isResolved')
43
            ->willReturn(true)
44
        ;
45
        $adminConfiguration
46
            ->expects($this->atLeastOnce())
47
            ->method('getParameter')
48
            ->willReturnMap([
49
                ['actions', ['edit' => [], 'delete' => []]],
50
                ['routing_name_pattern', '{admin}.{action}'],
51
            ])
52
        ;
53
    
54
        $admin = $this->getMockWithoutConstructor(AdminInterface::class);
55
        $admin
56
            ->expects($this->atLeastOnce())
57
            ->method('getUniqueEntity')
58
            ->willReturn(new TestEntity())
59
        ;
60
        $admin
61
            ->expects($this->once())
62
            ->method('getConfiguration')
63
            ->willReturn($adminConfiguration)
64
        ;
65
        $admin
66
            ->expects($this->atLeastOnce())
67
            ->method('getName')
68
            ->willReturn('my_little_admin')
69
        ;
70
        
71
        $form = $this->getMockWithoutConstructor(FormInterface::class);
72
        $form
73
            ->expects($this->atLeastOnce())
74
            ->method('isValid')
75
            ->willReturn(true)
76
        ;
77
        $form
78
            ->expects($this->atLeastOnce())
79
            ->method('isSubmitted')
80
            ->willReturn(true)
81
        ;
82
        
83
        $twig = $this->getMockWithoutConstructor(Twig_Environment::class);
84
        
85
        $responder = new EditResponder($routing, $twig);
86
        
87
        $response = $responder->respond(
88
            $configuration,
89
            $admin,
90
            $form,
91
            'save'
92
        );
93
        
94
        $this->assertInstanceOf(RedirectResponse::class, $response);
95
        $this->assertEquals('http://test.fr', $response->getTargetUrl());
96
    }
97
    
98 View Code Duplication
    public function testRespondWithSubmit()
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...
99
    {
100
        $routing = $this->getMockWithoutConstructor(RouterInterface::class);
101
        $routing
102
            ->expects($this->atLeastOnce())
103
            ->method('generate')
104
            ->with('my_little_admin.list')
105
            ->willReturn('http://test.fr')
106
        ;
107
        
108
        $configuration = $this->getMockWithoutConstructor(ActionConfiguration::class);
109
        $configuration
110
            ->expects($this->atLeastOnce())
111
            ->method('getParameter')
112
            ->willReturnMap([
113
                ['template', 'my_template.twig'],
114
            ])
115
        ;
116
    
117
        $adminConfiguration = $this->getMockWithoutConstructor(AdminConfiguration::class);
118
        $adminConfiguration
119
            ->expects($this->once())
120
            ->method('isResolved')
121
            ->willReturn(true)
122
        ;
123
        $adminConfiguration
124
            ->expects($this->atLeastOnce())
125
            ->method('getParameter')
126
            ->willReturnMap([
127
                ['actions', ['edit' => [], 'delete' => [], 'list' => []]],
128
                ['routing_name_pattern', '{admin}.{action}'],
129
            ])
130
        ;
131
    
132
        $admin = $this->getMockWithoutConstructor(AdminInterface::class);
133
        $admin
134
            ->expects($this->once())
135
            ->method('getConfiguration')
136
            ->willReturn($adminConfiguration)
137
        ;
138
        $admin
139
            ->expects($this->atLeastOnce())
140
            ->method('getName')
141
            ->willReturn('my_little_admin')
142
        ;
143
        
144
        $form = $this->getMockWithoutConstructor(FormInterface::class);
145
        $form
146
            ->expects($this->atLeastOnce())
147
            ->method('isValid')
148
            ->willReturn(true)
149
        ;
150
        $form
151
            ->expects($this->atLeastOnce())
152
            ->method('isSubmitted')
153
            ->willReturn(true)
154
        ;
155
        
156
        $twig = $this->getMockWithoutConstructor(Twig_Environment::class);
157
        
158
        $responder = new EditResponder($routing, $twig);
159
        
160
        $response = $responder->respond(
161
            $configuration,
162
            $admin,
163
            $form,
164
            'submit_and_save'
165
        );
166
        
167
        $this->assertInstanceOf(RedirectResponse::class, $response);
168
    }
169
    
170 View Code Duplication
    public function testRespondWithNotSubmittedForm()
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...
171
    {
172
        $routing = $this->getMockWithoutConstructor(RouterInterface::class);
173
        
174
        $configuration = $this->getMockWithoutConstructor(ActionConfiguration::class);
175
        $configuration
176
            ->expects($this->atLeastOnce())
177
            ->method('getParameter')
178
            ->willReturnMap([
179
                ['template', 'my_template.twig'],
180
            ])
181
        ;
182
        
183
        $admin = $this->getMockWithoutConstructor(AdminInterface::class);
184
        
185
        $form = $this->getMockWithoutConstructor(FormInterface::class);
186
        $form
187
            ->expects($this->atLeastOnce())
188
            ->method('isSubmitted')
189
            ->willReturn(false)
190
        ;
191
        
192
        $twig = $this->getMockWithoutConstructor(Twig_Environment::class);
193
        $twig
194
            ->expects($this->atLeastOnce())
195
            ->method('render')
196
            ->with('my_template.twig')
197
            ->willReturn(new Response('content'))
198
        ;
199
        
200
        $request = new Request([], [
201
            'submit' => 'save',
202
        ]);
203
        
204
        $responder = new EditResponder($routing, $twig);
205
        
206
        $response = $responder->respond(
207
            $configuration,
208
            $admin,
209
            $form,
210
            $request
211
        );
212
        
213
        $this->assertInstanceOf(Response::class, $response);
214
        $this->assertStringEndsWith('content', $response->getContent());
215
    }
216
}
217