Completed
Push — master ( 27d676...dee7dc )
by Gerrit
03:35
created

shouldBeCallableByInvokingController()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 34

Duplication

Lines 34
Ratio 100 %

Importance

Changes 0
Metric Value
dl 34
loc 34
rs 9.376
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * Copyright (C) 2017  Gerrit Addiks.
4
 * This package (including this file) was released under the terms of the GPL-3.0.
5
 * You should have received a copy of the GNU General Public License along with this program.
6
 * If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy.
7
 * @license GPL-3.0
8
 * @author Gerrit Addiks <[email protected]>
9
 */
10
11
namespace Addiks\SymfonyGenerics\Tests\Unit\Controllers;
12
13
use PHPUnit\Framework\TestCase;
14
use Addiks\SymfonyGenerics\Controllers\GenericTemplateRenderController;
15
use Addiks\SymfonyGenerics\Controllers\ControllerHelperInterface;
16
use Addiks\SymfonyGenerics\Services\ArgumentCompilerInterface;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\HttpFoundation\Response;
19
use Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException;
20
use InvalidArgumentException;
21
use Symfony\Component\HttpFoundation\RequestStack;
22
23
final class GenericTemplateRenderControllerTest extends TestCase
24
{
25
26
    /**
27
     * @var ControllerHelperInterface
28
     */
29
    private $controllerHelper;
30
31
    /**
32
     * @var ArgumentCompilerInterface
33
     */
34
    private $argumentCompiler;
35
36
    public function setUp()
37
    {
38
        $this->controllerHelper = $this->createMock(ControllerHelperInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Addik...HelperInterface::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Addiks\SymfonyGen...trollerHelperInterface> of property $controllerHelper.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
39
        $this->argumentCompiler = $this->createMock(ArgumentCompilerInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Addik...mpilerInterface::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Addiks\SymfonyGen...umentCompilerInterface> of property $argumentCompiler.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
40
    }
41
42
    /**
43
     * @test
44
     */
45 View Code Duplication
    public function shouldRenderTemplate()
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...
46
    {
47
        /** @var Request $request */
48
        $request = $this->createMock(Request::class);
49
50
        /** @var Response $expectedResponse */
51
        $expectedResponse = $this->createMock(Response::class);
52
53
        $this->argumentCompiler->expects($this->once())->method('buildArguments')->with(
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Addiks\SymfonyGen...umentCompilerInterface>.

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...
54
            $this->equalTo(['foo' => 'bar']),
55
            $this->identicalTo($request)
56
        )->willReturn([
57
            'bar' => 'baz'
58
        ]);
59
60
        $this->controllerHelper->expects($this->once())->method('renderTemplate')->with(
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Addiks\SymfonyGen...trollerHelperInterface>.

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...
61
            $this->equalTo("@foo/bar/baz.html"),
62
            $this->equalTo(['bar' => 'baz'])
63
        )->willReturn($expectedResponse);
64
65
        $controller = new GenericTemplateRenderController($this->controllerHelper, $this->argumentCompiler, [
66
            'template' => "@foo/bar/baz.html",
67
            'arguments' => [
68
                'foo' => 'bar'
69
            ]
70
        ]);
71
72
        /** @var Response $actualResponse */
73
        $actualResponse = $controller->renderTemplate($request);
74
75
        $this->assertSame($expectedResponse, $actualResponse);
76
    }
77
78
    /**
79
     * @test
80
     */
81 View Code Duplication
    public function shouldCheckIfAccessIsGranted()
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...
82
    {
83
        $this->expectException(AccessDeniedException::class);
84
85
        /** @var Request $request */
86
        $request = $this->createMock(Request::class);
87
88
        $this->controllerHelper->expects($this->once())->method('denyAccessUnlessGranted')->with(
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Addiks\SymfonyGen...trollerHelperInterface>.

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...
89
            $this->equalTo('some-attribute'),
90
            $this->equalTo($request)
91
        )->will($this->returnCallback(
92
            function () {
93
                throw new AccessDeniedException('Lorem ipsum!');
94
            }
95
        ));
96
97
        /** @var mixed $controller */
98
        $controller = new GenericTemplateRenderController($this->controllerHelper, $this->argumentCompiler, [
99
            'template' => "@foo/bar/baz.html",
100
            'authorization-attribute' => 'some-attribute',
101
        ]);
102
103
        $controller->renderTemplate($request);
104
    }
105
106
    /**
107
     * @test
108
     */
109 View Code Duplication
    public function shouldRejectConstructorCalledAgain()
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...
110
    {
111
        $this->expectException(InvalidArgumentException::class);
112
113
        $controller = new GenericTemplateRenderController($this->controllerHelper, $this->argumentCompiler, [
114
            'template' => "@foo/bar/baz.html",
115
        ]);
116
117
        $controller->__construct($this->controllerHelper, $this->argumentCompiler, [
118
            'template' => "@foo/bar/baz.html",
119
        ]);
120
    }
121
122
    /**
123
     * @test
124
     */
125
    public function shouldThrowExceptionIfTemplatePathIsMissing()
126
    {
127
        $this->expectException(InvalidArgumentException::class);
128
129
        $controller = new GenericTemplateRenderController($this->controllerHelper, $this->argumentCompiler, [
0 ignored issues
show
Unused Code introduced by
$controller is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
130
        ]);
131
    }
132
133
    /**
134
     * @test
135
     */
136 View Code Duplication
    public function shouldBeCallableByInvokingController()
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...
137
    {
138
        /** @var Request $request */
139
        $request = $this->createMock(Request::class);
140
141
        /** @var Response $expectedResponse */
142
        $expectedResponse = $this->createMock(Response::class);
143
144
        $this->argumentCompiler->expects($this->once())->method('buildArguments')->with(
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Addiks\SymfonyGen...umentCompilerInterface>.

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...
145
            $this->equalTo(['foo' => 'bar']),
146
            $this->identicalTo($request)
147
        )->willReturn([
148
            'bar' => 'baz'
149
        ]);
150
151
        $this->controllerHelper->expects($this->once())->method('renderTemplate')->with(
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Addiks\SymfonyGen...trollerHelperInterface>.

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...
152
            $this->equalTo("@foo/bar/baz.html"),
153
            $this->equalTo(['bar' => 'baz'])
154
        )->willReturn($expectedResponse);
155
156
        $controller = new GenericTemplateRenderController($this->controllerHelper, $this->argumentCompiler, [
157
            'template' => "@foo/bar/baz.html",
158
            'arguments' => [
159
                'foo' => 'bar'
160
            ]
161
        ]);
162
163
        $this->controllerHelper->method('getCurrentRequest')->willReturn($request);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\SymfonyGen...trollerHelperInterface>.

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...
164
165
        /** @var Response $actualResponse */
166
        $actualResponse = $controller();
167
168
        $this->assertSame($expectedResponse, $actualResponse);
169
    }
170
171
    /**
172
     * @test
173
     */
174 View Code Duplication
    public function shouldRejectCallWithoutRequest()
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...
175
    {
176
        $this->expectException(InvalidArgumentException::class);
177
178
        $controller = new GenericTemplateRenderController($this->controllerHelper, $this->argumentCompiler, [
179
            'template' => "@foo/bar/baz.html",
180
            'arguments' => []
181
        ]);
182
183
        $this->controllerHelper->method('getCurrentRequest')->willReturn(null);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\SymfonyGen...trollerHelperInterface>.

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...
184
185
        $controller();
186
    }
187
188
}
189