Passed
Push — master ( 928c25...8579cb )
by Gerrit
02:16
created

GenericTemplateRenderControllerTest.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
22
final class GenericTemplateRenderControllerTest extends TestCase
23
{
24
25
    /**
26
     * @var ControllerHelperInterface
27
     */
28
    private $controllerHelper;
29
30
    /**
31
     * @var ArgumentCompilerInterface
32
     */
33
    private $argumentCompiler;
34
35
    public function setUp()
36
    {
37
        $this->controllerHelper = $this->createMock(ControllerHelperInterface::class);
38
        $this->argumentCompiler = $this->createMock(ArgumentCompilerInterface::class);
39
    }
40
41
    /**
42
     * @test
43
     */
44 View Code Duplication
    public function shouldRenderTemplate()
45
    {
46
        /** @var Request $request */
47
        $request = $this->createMock(Request::class);
48
49
        /** @var Response $expectedResponse */
50
        $expectedResponse = $this->createMock(Response::class);
51
52
        $this->argumentCompiler->expects($this->once())->method('buildArguments')->with(
53
            $this->equalTo(['foo' => 'bar'])
54
        )->willReturn([
55
            'bar' => 'baz'
56
        ]);
57
58
        $this->controllerHelper->expects($this->once())->method('renderTemplate')->with(
59
            $this->equalTo("@foo/bar/baz.html"),
60
            $this->equalTo(['bar' => 'baz'])
61
        )->willReturn($expectedResponse);
62
63
        $controller = new GenericTemplateRenderController($this->controllerHelper, $this->argumentCompiler, [
64
            'template' => "@foo/bar/baz.html",
65
            'arguments' => [
66
                'foo' => 'bar'
67
            ]
68
        ]);
69
70
        /** @var Response $actualResponse */
71
        $actualResponse = $controller->renderTemplate($request);
72
73
        $this->assertSame($expectedResponse, $actualResponse);
74
    }
75
76
    /**
77
     * @test
78
     */
79 View Code Duplication
    public function shouldCheckIfAccessIsGranted()
80
    {
81
        $this->expectException(AccessDeniedException::class);
82
83
        /** @var Request $request */
84
        $request = $this->createMock(Request::class);
85
86
        $this->controllerHelper->expects($this->once())->method('denyAccessUnlessGranted')->with(
87
            $this->equalTo('some-attribute'),
88
            $this->equalTo($request)
89
        )->will($this->returnCallback(
90
            function () {
91
                throw new AccessDeniedException('Lorem ipsum!');
92
            }
93
        ));
94
95
        /** @var mixed $controller */
96
        $controller = new GenericTemplateRenderController($this->controllerHelper, $this->argumentCompiler, [
97
            'template' => "@foo/bar/baz.html",
98
            'authorization-attribute' => 'some-attribute',
99
        ]);
100
101
        $controller->renderTemplate($request);
102
    }
103
104
    /**
105
     * @test
106
     */
107 View Code Duplication
    public function shouldRejectConstructorCalledAgain()
108
    {
109
        $this->expectException(InvalidArgumentException::class);
110
111
        $controller = new GenericTemplateRenderController($this->controllerHelper, $this->argumentCompiler, [
112
            'template' => "@foo/bar/baz.html",
113
        ]);
114
115
        $controller->__construct($this->controllerHelper, $this->argumentCompiler, [
116
            'template' => "@foo/bar/baz.html",
117
        ]);
118
    }
119
120
    /**
121
     * @test
122
     */
123
    public function shouldThrowExceptionIfTemplatePathIsMissing()
124
    {
125
        $this->expectException(InvalidArgumentException::class);
126
127
        $controller = new GenericTemplateRenderController($this->controllerHelper, $this->argumentCompiler, [
128
        ]);
129
    }
130
131
    /**
132
     * @test
133
     */
134 View Code Duplication
    public function shouldBeCallableByInvokingController()
135
    {
136
        /** @var Request $request */
137
        $request = $this->createMock(Request::class);
138
139
        /** @var Response $expectedResponse */
140
        $expectedResponse = $this->createMock(Response::class);
141
142
        $this->argumentCompiler->expects($this->once())->method('buildArguments')->with(
0 ignored issues
show
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...
143
            $this->equalTo(['foo' => 'bar'])
144
        )->willReturn([
145
            'bar' => 'baz'
146
        ]);
147
148
        $this->controllerHelper->expects($this->once())->method('renderTemplate')->with(
149
            $this->equalTo("@foo/bar/baz.html"),
150
            $this->equalTo(['bar' => 'baz'])
151
        )->willReturn($expectedResponse);
152
153
        $controller = new GenericTemplateRenderController($this->controllerHelper, $this->argumentCompiler, [
154
            'template' => "@foo/bar/baz.html",
155
            'arguments' => [
156
                'foo' => 'bar'
157
            ]
158
        ]);
159
160
        $this->controllerHelper->method('getCurrentRequest')->willReturn($request);
161
162
        /** @var Response $actualResponse */
163
        $actualResponse = $controller();
164
165
        $this->assertSame($expectedResponse, $actualResponse);
166
    }
167
168
    /**
169
     * @test
170
     */
171 View Code Duplication
    public function shouldRejectCallWithoutRequest()
172
    {
173
        $this->expectException(InvalidArgumentException::class);
174
175
        $controller = new GenericTemplateRenderController($this->controllerHelper, $this->argumentCompiler, [
176
            'template' => "@foo/bar/baz.html",
177
            'arguments' => []
178
        ]);
179
180
        $this->controllerHelper->method('getCurrentRequest')->willReturn(null);
181
182
        $controller();
183
    }
184
185
}
186