Completed
Push — master ( 8c5203...c6433c )
by Gerrit
23:20
created

shouldRejectMissingEntityClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
c 0
b 0
f 0
rs 9.8666
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;
12
13
use PHPUnit\Framework\TestCase;
14
use Addiks\SymfonyGenerics\Controllers\GenericEntityInvokeController;
15
use Addiks\SymfonyGenerics\Services\ArgumentCompilerInterface;
16
use Addiks\SymfonyGenerics\Controllers\ControllerHelperInterface;
17
use Symfony\Component\HttpFoundation\Request;
18
use stdClass;
19
use ReflectionMethod;
20
use InvalidArgumentException;
21
22
final class GenericEntityInvokeControllerTest extends TestCase
23
{
24
25
    /**
26
     * @var GenericEntityInvokeController
27
     */
28
    private $controller;
29
30
    /**
31
     * @var ControllerHelperInterface
32
     */
33
    private $controllerHelper;
34
35
    /**
36
     * @var ArgumentCompilerInterface
37
     */
38
    private $argumentCompiler;
39
40
    public function setUp()
41
    {
42
        $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...
43
        $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...
44
    }
45
46
    /**
47
     * @test
48
     */
49
    public function shouldInvodeAnEntityMethod()
50
    {
51
        /** @var Request $request */
52
        $request = $this->createMock(Request::class);
53
54
        $this->controller = new GenericEntityInvokeController(
55
            $this->controllerHelper,
56
            $this->argumentCompiler,
57
            [
58
                'entity-class' => get_class($this->argumentCompiler),
59
                'method' => 'buildRouteArguments',
60
                'arguments' => [
61
                    'argumentsConfiguration' => "Lorem",
62
                    'request' => "ipsum"
63
                ]
64
            ]
65
        );
66
67
        $this->controllerHelper->expects($this->once())->method('findEntity')->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...
68
            $this->equalTo(get_class($this->argumentCompiler)),
69
            $this->equalTo("123")
70
        )->willReturn($this->argumentCompiler);
71
72
        $this->argumentCompiler->expects($this->once())->method('buildCallArguments')->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...
73
            $this->equalTo(new ReflectionMethod(get_class($this->argumentCompiler), 'buildRouteArguments')),
74
            $this->equalTo([
75
                'argumentsConfiguration' => "Lorem",
76
                'request' => "ipsum"
77
            ]),
78
            $this->identicalTo($request)
79
        )->willReturn([
80
            ['foo' => 'bar'],
81
            $request
82
        ]);
83
84
        $this->argumentCompiler->expects($this->once())->method('buildRouteArguments')->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...
85
            $this->equalTo(['foo' => 'bar']),
86
            $this->identicalTo($request)
87
        );
88
89
        $this->controller->invokeEntityMethod($request, "123");
90
    }
91
92
    /**
93
     * @test
94
     */
95
    public function shouldThrowExceptionWhenEntityNotFound()
96
    {
97
        $this->expectException(InvalidArgumentException::class);
98
99
        /** @var Request $request */
100
        $request = $this->createMock(Request::class);
101
102
        $this->controller = new GenericEntityInvokeController(
103
            $this->controllerHelper,
104
            $this->argumentCompiler,
105
            [
106
                'entity-class' => get_class($this->argumentCompiler),
107
                'method' => 'buildRouteArguments',
108
            ]
109
        );
110
111
        $this->controllerHelper->expects($this->once())->method('findEntity')->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...
112
            $this->equalTo(get_class($this->argumentCompiler)),
113
            $this->equalTo("123")
114
        )->willReturn(null);
115
116
        $this->controller->invokeEntityMethod($request, "123");
117
    }
118
119
    /**
120
     * @test
121
     */
122
    public function shouldRejectConstructorCalledAgain()
123
    {
124
        $this->expectException(InvalidArgumentException::class);
125
126
        $this->controller = new GenericEntityInvokeController($this->controllerHelper, $this->argumentCompiler, [
127
            'entity-class' => get_class($this->argumentCompiler),
128
            'method' => 'buildRouteArguments',
129
        ]);
130
131
        $this->controller->__construct($this->controllerHelper, $this->argumentCompiler, [
132
            'entity-class' => get_class($this->argumentCompiler),
133
            'method' => 'buildRouteArguments',
134
        ]);
135
    }
136
137
    /**
138
     * @test
139
     */
140
    public function shouldRejectMissingEntityClass()
141
    {
142
        $this->expectException(InvalidArgumentException::class);
143
144
        new GenericEntityInvokeController(
145
            $this->controllerHelper,
146
            $this->argumentCompiler,
147
            [
148
                'method' => 'buildRouteArguments',
149
            ]
150
        );
151
    }
152
153
    /**
154
     * @test
155
     */
156
    public function shouldRejectMissingMethod()
157
    {
158
        $this->expectException(InvalidArgumentException::class);
159
160
        new GenericEntityInvokeController(
161
            $this->controllerHelper,
162
            $this->argumentCompiler,
163
            [
164
                'entity-class' => get_class($this->argumentCompiler),
165
            ]
166
        );
167
    }
168
169
    /**
170
     * @test
171
     */
172 View Code Duplication
    public function shouldRejectNonEntityClass()
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...
173
    {
174
        $this->expectException(InvalidArgumentException::class);
175
        $this->expectExceptionMessage('Expected an existing class name. Got: "NonExistingClass"');
176
177
        new GenericEntityInvokeController(
178
            $this->controllerHelper,
179
            $this->argumentCompiler,
180
            [
181
                'entity-class' => "NonExistingClass",
182
                'method' => 'buildRouteArguments',
183
            ]
184
        );
185
    }
186
187
    /**
188
     * @test
189
     */
190 View Code Duplication
    public function shouldRejectNonExistingMethod()
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...
191
    {
192
        $this->expectException(InvalidArgumentException::class);
193
194
        new GenericEntityInvokeController(
195
            $this->controllerHelper,
196
            $this->argumentCompiler,
197
            [
198
                'entity-class' => get_class($this->argumentCompiler),
199
                'method' => 'nonExistingMethod',
200
            ]
201
        );
202
    }
203
204
    /**
205
     * @test
206
     */
207 View Code Duplication
    public function shouldRejectNonArrayArguments()
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...
208
    {
209
        $this->expectException(InvalidArgumentException::class);
210
211
        new GenericEntityInvokeController(
212
            $this->controllerHelper,
213
            $this->argumentCompiler,
214
            [
215
                'entity-class' => get_class($this->argumentCompiler),
216
                'method' => 'buildRouteArguments',
217
                'arguments' => "12345",
218
            ]
219
        );
220
    }
221
222
}
223