This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | /* |
||
6 | * This file is part of the Sonata Project package. |
||
7 | * |
||
8 | * (c) Thomas Rabaix <[email protected]> |
||
9 | * |
||
10 | * For the full copyright and license information, please view the LICENSE |
||
11 | * file that was distributed with this source code. |
||
12 | */ |
||
13 | |||
14 | namespace Sonata\NewsBundle\Tests\Controller\Api; |
||
15 | |||
16 | use PHPUnit\Framework\TestCase; |
||
17 | use Sonata\FormatterBundle\Formatter\FormatterInterface; |
||
18 | use Sonata\FormatterBundle\Formatter\Pool; |
||
19 | use Sonata\NewsBundle\Controller\Api\PostController; |
||
20 | use Sonata\NewsBundle\Model\CommentInterface; |
||
21 | use Symfony\Component\HttpFoundation\Request; |
||
22 | |||
23 | /** |
||
24 | * @author Hugo Briand <[email protected]> |
||
25 | */ |
||
26 | class PostControllerTest extends TestCase |
||
27 | { |
||
28 | public function testGetPostsAction(): void |
||
29 | { |
||
30 | $paramFetcher = $this->createMock('FOS\RestBundle\Request\ParamFetcherInterface'); |
||
31 | $paramFetcher->expects($this->once())->method('all')->willReturn([]); |
||
32 | |||
33 | $pager = $this->createMock('Sonata\DatagridBundle\Pager\PagerInterface'); |
||
34 | |||
35 | $postManager = $this->createMock('Sonata\NewsBundle\Model\PostManagerInterface'); |
||
36 | $postManager->expects($this->once())->method('getPager')->willReturn($pager); |
||
37 | |||
38 | $this->assertSame($pager, $this->createPostController($postManager)->getPostsAction($paramFetcher)); |
||
39 | } |
||
40 | |||
41 | public function testGetPostAction(): void |
||
42 | { |
||
43 | $post = $this->createMock('Sonata\NewsBundle\Model\PostInterface'); |
||
44 | |||
45 | $postManager = $this->createMock('Sonata\NewsBundle\Model\PostManagerInterface'); |
||
46 | $postManager->expects($this->once())->method('find')->willReturn($post); |
||
47 | |||
48 | $this->assertSame($post, $this->createPostController($postManager)->getPostAction(1)); |
||
49 | } |
||
50 | |||
51 | public function testGetPostNotFoundExceptionAction(): void |
||
52 | { |
||
53 | $this->expectException(\Symfony\Component\HttpKernel\Exception\NotFoundHttpException::class); |
||
54 | $this->expectExceptionMessage('Post (42) not found'); |
||
55 | |||
56 | $this->createPostController()->getPostAction(42); |
||
57 | } |
||
58 | |||
59 | public function testGetPostCommentsAction(): void |
||
60 | { |
||
61 | $parameters = [ |
||
62 | 'page' => 2, |
||
63 | 'count' => 5, |
||
64 | ]; |
||
65 | |||
66 | $paramFetcher = $this->createMock('FOS\RestBundle\Request\ParamFetcherInterface'); |
||
67 | $paramFetcher->expects($this->once())->method('all')->willReturn([]); |
||
68 | $paramFetcher->expects($this->exactly(2))->method('get') |
||
69 | ->with($this->logicalOr($this->equalTo('page'), $this->equalTo('count'))) |
||
70 | ->willReturnCallback(static function ($parameter) use ($parameters) { |
||
71 | return $parameters[$parameter]; |
||
72 | }); |
||
73 | |||
74 | $post = $this->createMock('Sonata\NewsBundle\Model\PostInterface'); |
||
75 | |||
76 | $postManager = $this->createMock('Sonata\NewsBundle\Model\PostManagerInterface'); |
||
77 | $postManager->expects($this->once())->method('find')->willReturn($post); |
||
78 | |||
79 | $pager = $this->createMock('Sonata\DatagridBundle\Pager\PagerInterface'); |
||
80 | |||
81 | // Will assert that param fetcher parameters are used for the pager |
||
82 | $commentManager = $this->createMock('Sonata\NewsBundle\Model\CommentManagerInterface'); |
||
83 | $commentManager->expects($this->once()) |
||
84 | ->method('getPager') |
||
85 | ->with($this->anything(), $this->equalTo($parameters['page']), $this->equalTo($parameters['count'])) |
||
86 | ->willReturn($pager); |
||
87 | |||
88 | $this->assertSame($pager, $this->createPostController($postManager, $commentManager)->getPostCommentsAction(1, $paramFetcher)); |
||
89 | } |
||
90 | |||
91 | public function testGetPostCommentsActionNotFoundExceptionAction(): void |
||
92 | { |
||
93 | $this->expectException(\Symfony\Component\HttpKernel\Exception\NotFoundHttpException::class); |
||
94 | $this->expectExceptionMessage('Post (42) not found'); |
||
95 | |||
96 | $paramFetcher = $this->createMock('FOS\RestBundle\Request\ParamFetcherInterface'); |
||
97 | |||
98 | $this->createPostController()->getPostCommentsAction(42, $paramFetcher); |
||
99 | } |
||
100 | |||
101 | public function testPostPostAction(): void |
||
102 | { |
||
103 | $post = $this->createMock('Sonata\NewsBundle\Model\PostInterface'); |
||
104 | $post->expects($this->once())->method('setContent'); |
||
105 | $post->expects($this->once())->method('getContentFormatter')->willReturn('text'); |
||
106 | $post->expects($this->once())->method('getRawContent')->willReturn(''); |
||
107 | |||
108 | $postManager = $this->createMock('Sonata\NewsBundle\Model\PostManagerInterface'); |
||
109 | $postManager->expects($this->once())->method('save')->willReturn($post); |
||
110 | |||
111 | $formatter = $this->createMock(FormatterInterface::class); |
||
112 | $formatter->expects($this->once())->method('transform')->willReturn(''); |
||
113 | $formatterPool = new Pool('text'); |
||
114 | $formatterPool->add('text', $formatter); |
||
115 | |||
116 | $form = $this->createMock('Symfony\Component\Form\Form'); |
||
117 | $form->expects($this->once())->method('handleRequest'); |
||
118 | $form->expects($this->once())->method('isValid')->willReturn(true); |
||
119 | $form->expects($this->once())->method('getData')->willReturn($post); |
||
120 | |||
121 | $formFactory = $this->createMock('Symfony\Component\Form\FormFactoryInterface'); |
||
122 | $formFactory->expects($this->once())->method('createNamed')->willReturn($form); |
||
123 | |||
124 | $view = $this->createPostController($postManager, null, null, $formFactory, $formatterPool)->postPostAction(new Request()); |
||
125 | |||
126 | $this->assertInstanceOf('FOS\RestBundle\View\View', $view); |
||
127 | } |
||
128 | |||
129 | public function testPostPostInvalidAction(): void |
||
130 | { |
||
131 | $post = $this->createMock('Sonata\NewsBundle\Model\PostInterface'); |
||
132 | $post->expects($this->never())->method('setContent'); |
||
133 | |||
134 | $postManager = $this->createMock('Sonata\NewsBundle\Model\PostManagerInterface'); |
||
135 | $postManager->expects($this->never())->method('save')->willReturn($post); |
||
136 | |||
137 | $formatter = $this->createMock(FormatterInterface::class); |
||
138 | $formatter->expects($this->never())->method('transform'); |
||
139 | $formatterPool = new Pool('text'); |
||
140 | $formatterPool->add('text', $formatter); |
||
141 | |||
142 | $form = $this->getMockBuilder('Symfony\Component\Form\Form')->disableOriginalConstructor()->getMock(); |
||
143 | $form->expects($this->once())->method('handleRequest'); |
||
144 | $form->expects($this->once())->method('isValid')->willReturn(false); |
||
145 | |||
146 | $formFactory = $this->createMock('Symfony\Component\Form\FormFactoryInterface'); |
||
147 | $formFactory->expects($this->once())->method('createNamed')->willReturn($form); |
||
148 | |||
149 | $view = $this->createPostController($postManager, null, null, $formFactory, $formatterPool)->postPostAction(new Request()); |
||
150 | |||
151 | $this->assertInstanceOf('Symfony\Component\Form\FormInterface', $view); |
||
152 | } |
||
153 | |||
154 | public function testPutPostAction(): void |
||
155 | { |
||
156 | $post = $this->createMock('Sonata\NewsBundle\Model\PostInterface'); |
||
157 | $post->expects($this->once())->method('setContent'); |
||
158 | $post->expects($this->once())->method('getContentFormatter')->willReturn('text'); |
||
159 | $post->expects($this->once())->method('getRawContent')->willReturn(''); |
||
160 | $post->expects($this->once())->method('getContent')->willReturn(''); |
||
161 | |||
162 | $postManager = $this->createMock('Sonata\NewsBundle\Model\PostManagerInterface'); |
||
163 | $postManager->expects($this->once())->method('find')->willReturn($post); |
||
164 | $postManager->expects($this->once())->method('save')->willReturn($post); |
||
165 | |||
166 | $formatter = $this->createMock(FormatterInterface::class); |
||
167 | $formatter->expects($this->once())->method('transform')->willReturn($post->getContent()); |
||
168 | $formatterPool = new Pool('text'); |
||
169 | $formatterPool->add('text', $formatter); |
||
170 | |||
171 | $form = $this->getMockBuilder('Symfony\Component\Form\Form')->disableOriginalConstructor()->getMock(); |
||
172 | $form->expects($this->once())->method('handleRequest'); |
||
173 | $form->expects($this->once())->method('isValid')->willReturn(true); |
||
174 | $form->expects($this->once())->method('getData')->willReturn($post); |
||
175 | |||
176 | $formFactory = $this->createMock('Symfony\Component\Form\FormFactoryInterface'); |
||
177 | $formFactory->expects($this->once())->method('createNamed')->willReturn($form); |
||
178 | |||
179 | $view = $this->createPostController($postManager, null, null, $formFactory, $formatterPool)->putPostAction(1, new Request()); |
||
180 | |||
181 | $this->assertInstanceOf('FOS\RestBundle\View\View', $view); |
||
182 | } |
||
183 | |||
184 | public function testPutPostInvalidAction(): void |
||
185 | { |
||
186 | $post = $this->createMock('Sonata\NewsBundle\Model\PostInterface'); |
||
187 | $post->expects($this->never())->method('setContent'); |
||
188 | |||
189 | $postManager = $this->createMock('Sonata\NewsBundle\Model\PostManagerInterface'); |
||
190 | $postManager->expects($this->once())->method('find')->willReturn($post); |
||
191 | $postManager->expects($this->never())->method('save')->willReturn($post); |
||
192 | |||
193 | $formatter = $this->createMock(FormatterInterface::class); |
||
194 | $formatter->expects($this->never())->method('transform'); |
||
195 | $formatterPool = new Pool('text'); |
||
196 | $formatterPool->add('text', $formatter); |
||
197 | |||
198 | $form = $this->getMockBuilder('Symfony\Component\Form\Form')->disableOriginalConstructor()->getMock(); |
||
199 | $form->expects($this->once())->method('handleRequest'); |
||
200 | $form->expects($this->once())->method('isValid')->willReturn(false); |
||
201 | |||
202 | $formFactory = $this->createMock('Symfony\Component\Form\FormFactoryInterface'); |
||
203 | $formFactory->expects($this->once())->method('createNamed')->willReturn($form); |
||
204 | |||
205 | $view = $this->createPostController($postManager, null, null, $formFactory, $formatterPool)->putPostAction(1, new Request()); |
||
206 | |||
207 | $this->assertInstanceOf('Symfony\Component\Form\FormInterface', $view); |
||
208 | } |
||
209 | |||
210 | public function testDeletePostAction(): void |
||
211 | { |
||
212 | $post = $this->createMock('Sonata\NewsBundle\Model\PostInterface'); |
||
213 | |||
214 | $postManager = $this->createMock('Sonata\NewsBundle\Model\PostManagerInterface'); |
||
215 | $postManager->expects($this->once())->method('find')->willReturn($post); |
||
216 | $postManager->expects($this->once())->method('delete'); |
||
217 | |||
218 | $view = $this->createPostController($postManager)->deletePostAction(1); |
||
219 | |||
220 | $this->assertSame(['deleted' => true], $view); |
||
221 | } |
||
222 | |||
223 | public function testDeletePostInvalidAction(): void |
||
224 | { |
||
225 | $this->expectException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException'); |
||
226 | |||
227 | $postManager = $this->createMock('Sonata\NewsBundle\Model\PostManagerInterface'); |
||
228 | $postManager->expects($this->once())->method('find')->willReturn(null); |
||
229 | $postManager->expects($this->never())->method('delete'); |
||
230 | |||
231 | $this->createPostController($postManager)->deletePostAction(1); |
||
232 | } |
||
233 | |||
234 | public function testPostPostCommentsAction(): void |
||
235 | { |
||
236 | $comment = $this->createMock(CommentInterface::class); |
||
237 | $post = $this->createMock('Sonata\NewsBundle\Model\PostInterface'); |
||
238 | $post->expects($this->once())->method('isCommentable')->willReturn(true); |
||
239 | |||
240 | $postManager = $this->createMock('Sonata\NewsBundle\Model\PostManagerInterface'); |
||
241 | $postManager->expects($this->once())->method('find')->willReturn($post); |
||
242 | |||
243 | $commentManager = $this->createMock('Sonata\NewsBundle\Model\CommentManagerInterface'); |
||
244 | $commentManager->expects($this->once())->method('save'); |
||
245 | $commentManager->expects($this->once())->method('create')->willReturn($comment); |
||
246 | |||
247 | $mailer = $this->createMock('Sonata\NewsBundle\Mailer\MailerInterface'); |
||
248 | $mailer->expects($this->once())->method('sendCommentNotification'); |
||
249 | |||
250 | $form = $this->getMockBuilder('Symfony\Component\Form\Form')->disableOriginalConstructor()->getMock(); |
||
251 | $form->expects($this->once())->method('handleRequest'); |
||
252 | $form->expects($this->once())->method('isValid')->willReturn(true); |
||
253 | $form->expects($this->once())->method('getData')->willReturn($comment); |
||
254 | |||
255 | $formFactory = $this->createMock('Symfony\Component\Form\FormFactoryInterface'); |
||
256 | $formFactory->expects($this->once())->method('createNamed')->willReturn($form); |
||
257 | |||
258 | $postController = $this->createPostController($postManager, $commentManager, $mailer, $formFactory); |
||
259 | |||
260 | $postController->postPostCommentsAction(1, new Request()); |
||
261 | |||
262 | $this->assertInstanceOf(CommentInterface::class, $comment); |
||
263 | } |
||
264 | |||
265 | public function testPostPostCommentsInvalidFormAction(): void |
||
266 | { |
||
267 | $comment = $this->createMock(CommentInterface::class); |
||
268 | $post = $this->createMock('Sonata\NewsBundle\Model\PostInterface'); |
||
269 | $post->expects($this->once())->method('isCommentable')->willReturn(true); |
||
270 | |||
271 | $postManager = $this->createMock('Sonata\NewsBundle\Model\PostManagerInterface'); |
||
272 | $postManager->expects($this->once())->method('find')->willReturn($post); |
||
273 | |||
274 | $commentManager = $this->createMock('Sonata\NewsBundle\Model\CommentManagerInterface'); |
||
275 | $commentManager->expects($this->once())->method('create')->willReturn($comment); |
||
276 | |||
277 | $form = $this->getMockBuilder('Symfony\Component\Form\Form')->disableOriginalConstructor()->getMock(); |
||
278 | $form->expects($this->once())->method('handleRequest'); |
||
279 | $form->expects($this->once())->method('isValid')->willReturn(false); |
||
280 | |||
281 | $formFactory = $this->createMock('Symfony\Component\Form\FormFactoryInterface'); |
||
282 | $formFactory->expects($this->once())->method('createNamed')->willReturn($form); |
||
283 | |||
284 | $this->assertInstanceOf('Symfony\Component\Form\Form', $this->createPostController($postManager, $commentManager, null, $formFactory)->postPostCommentsAction(1, new Request())); |
||
285 | } |
||
286 | |||
287 | public function testPostPostCommentsNotCommentableAction(): void |
||
288 | { |
||
289 | $this->expectException(\Symfony\Component\HttpKernel\Exception\HttpException::class); |
||
290 | $this->expectExceptionMessage('Post (42) not commentable'); |
||
291 | |||
292 | $post = $this->createMock('Sonata\NewsBundle\Model\PostInterface'); |
||
293 | $post->expects($this->once())->method('isCommentable')->willReturn(false); |
||
294 | |||
295 | $postManager = $this->createMock('Sonata\NewsBundle\Model\PostManagerInterface'); |
||
296 | $postManager->expects($this->once())->method('find')->willReturn($post); |
||
297 | |||
298 | $this->createPostController($postManager)->postPostCommentsAction(42, new Request()); |
||
299 | } |
||
300 | |||
301 | public function testPutPostCommentAction(): void |
||
302 | { |
||
303 | $post = $this->createMock('Sonata\NewsBundle\Model\PostInterface'); |
||
304 | $post->expects($this->once())->method('isCommentable')->willReturn(true); |
||
305 | |||
306 | $postManager = $this->createMock('Sonata\NewsBundle\Model\PostManagerInterface'); |
||
307 | $postManager->expects($this->once())->method('find')->willReturn($post); |
||
308 | |||
309 | $comment = $this->createMock(CommentInterface::class); |
||
310 | |||
311 | $commentManager = $this->createMock('Sonata\NewsBundle\Model\CommentManagerInterface'); |
||
312 | $commentManager->expects($this->once())->method('find')->willReturn($comment); |
||
313 | $commentManager->expects($this->once())->method('save'); |
||
314 | |||
315 | $form = $this->getMockBuilder('Symfony\Component\Form\Form')->disableOriginalConstructor()->getMock(); |
||
316 | $form->expects($this->once())->method('handleRequest'); |
||
317 | $form->expects($this->once())->method('getData')->willReturn($comment); |
||
318 | $form->expects($this->once())->method('isValid')->willReturn(true); |
||
319 | |||
320 | $formFactory = $this->createMock('Symfony\Component\Form\FormFactoryInterface'); |
||
321 | $formFactory->expects($this->once())->method('createNamed')->willReturn($form); |
||
322 | |||
323 | $postController = $this->createPostController($postManager, $commentManager, null, $formFactory); |
||
324 | |||
325 | $comment = $postController->putPostCommentsAction(1, 1, new Request()); |
||
326 | |||
327 | $this->assertInstanceOf(CommentInterface::class, $comment); |
||
328 | } |
||
329 | |||
330 | public function testPutPostCommentInvalidAction(): void |
||
331 | { |
||
332 | $post = $this->createMock('Sonata\NewsBundle\Model\PostInterface'); |
||
333 | $post->expects($this->once())->method('isCommentable')->willReturn(true); |
||
334 | |||
335 | $postManager = $this->createMock('Sonata\NewsBundle\Model\PostManagerInterface'); |
||
336 | $postManager->expects($this->once())->method('find')->willReturn($post); |
||
337 | |||
338 | $comment = $this->createMock(CommentInterface::class); |
||
339 | |||
340 | $commentManager = $this->createMock('Sonata\NewsBundle\Model\CommentManagerInterface'); |
||
341 | $commentManager->expects($this->once())->method('find')->willReturn($comment); |
||
342 | $commentManager->expects($this->never())->method('save'); |
||
343 | |||
344 | $form = $this->getMockBuilder('Symfony\Component\Form\Form')->disableOriginalConstructor()->getMock(); |
||
345 | $form->expects($this->once())->method('handleRequest'); |
||
346 | $form->expects($this->once())->method('isValid')->willReturn(false); |
||
347 | |||
348 | $formFactory = $this->createMock('Symfony\Component\Form\FormFactoryInterface'); |
||
349 | $formFactory->expects($this->once())->method('createNamed')->willReturn($form); |
||
350 | |||
351 | $view = $this->createPostController($postManager, $commentManager, null, $formFactory)->putPostCommentsAction(1, 1, new Request()); |
||
352 | |||
353 | $this->assertInstanceOf('Symfony\Component\Form\FormInterface', $view); |
||
354 | } |
||
355 | |||
356 | /** |
||
357 | * @param null $postManager |
||
358 | * @param null $commentManager |
||
359 | * @param null $mailer |
||
360 | * @param null $formFactory |
||
361 | * @param null $formatterPool |
||
362 | * |
||
363 | * @return PostController |
||
364 | */ |
||
365 | protected function createPostController($postManager = null, $commentManager = null, $mailer = null, $formFactory = null, $formatterPool = null) |
||
366 | { |
||
367 | if (null === $postManager) { |
||
368 | $postManager = $this->createMock('Sonata\NewsBundle\Model\PostManagerInterface'); |
||
369 | } |
||
370 | if (null === $commentManager) { |
||
371 | $commentManager = $this->createMock('Sonata\NewsBundle\Model\CommentManagerInterface'); |
||
372 | } |
||
373 | if (null === $mailer) { |
||
374 | $mailer = $this->createMock('Sonata\NewsBundle\Mailer\MailerInterface'); |
||
375 | } |
||
376 | if (null === $formFactory) { |
||
377 | $formFactory = $this->createMock('Symfony\Component\Form\FormFactoryInterface'); |
||
378 | } |
||
379 | if (null === $formatterPool) { |
||
380 | $formatterPool = new Pool('text'); |
||
381 | $formatterPool->add('text', $this->createMock(FormatterInterface::class)); |
||
382 | } |
||
383 | |||
384 | return new PostController($postManager, $commentManager, $mailer, $formFactory, $formatterPool); |
||
0 ignored issues
–
show
|
|||
385 | } |
||
386 | } |
||
387 |
It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.
We recommend to add an additional type check (or disallow null for the parameter):