|
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\NewsBundle\Controller\Api\CommentController; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @author Hugo Briand <[email protected]> |
|
21
|
|
|
*/ |
|
22
|
|
|
class CommentControllerTest extends TestCase |
|
23
|
|
|
{ |
|
24
|
|
|
public function testGetCommentAction(): void |
|
25
|
|
|
{ |
|
26
|
|
|
$comment = $this->createMock('Sonata\NewsBundle\Model\CommentInterface'); |
|
27
|
|
|
|
|
28
|
|
|
$commentManager = $this->createMock('Sonata\NewsBundle\Model\CommentManagerInterface'); |
|
29
|
|
|
$commentManager->expects($this->once())->method('find')->willReturn($comment); |
|
30
|
|
|
|
|
31
|
|
|
$this->assertSame($comment, $this->createCommentController($commentManager)->getCommentAction(1)); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function testGetCommentNotFoundExceptionAction(): void |
|
35
|
|
|
{ |
|
36
|
|
|
$this->expectException(\Symfony\Component\HttpKernel\Exception\NotFoundHttpException::class); |
|
37
|
|
|
$this->expectExceptionMessage('Comment (42) not found'); |
|
38
|
|
|
|
|
39
|
|
|
$this->createCommentController()->getCommentAction(42); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function testDeleteCommentAction(): void |
|
43
|
|
|
{ |
|
44
|
|
|
$comment = $this->createMock('Sonata\NewsBundle\Model\CommentInterface'); |
|
45
|
|
|
|
|
46
|
|
|
$commentManager = $this->createMock('Sonata\NewsBundle\Model\CommentManagerInterface'); |
|
47
|
|
|
$commentManager->expects($this->once())->method('find')->willReturn($comment); |
|
48
|
|
|
$commentManager->expects($this->once())->method('delete'); |
|
49
|
|
|
|
|
50
|
|
|
$view = $this->createCommentController($commentManager)->deleteCommentAction(1); |
|
51
|
|
|
|
|
52
|
|
|
$this->assertSame(['deleted' => true], $view); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function testDeletePostInvalidAction(): void |
|
56
|
|
|
{ |
|
57
|
|
|
$this->expectException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException'); |
|
58
|
|
|
|
|
59
|
|
|
$commentManager = $this->createMock('Sonata\NewsBundle\Model\CommentManagerInterface'); |
|
60
|
|
|
$commentManager->expects($this->once())->method('find')->willReturn(null); |
|
61
|
|
|
$commentManager->expects($this->never())->method('delete'); |
|
62
|
|
|
|
|
63
|
|
|
$this->createCommentController($commentManager)->deleteCommentAction(1); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param null $commentManager |
|
68
|
|
|
* |
|
69
|
|
|
* @return CommentController |
|
70
|
|
|
*/ |
|
71
|
|
|
protected function createCommentController($commentManager = null) |
|
72
|
|
|
{ |
|
73
|
|
|
if (null === $commentManager) { |
|
74
|
|
|
$commentManager = $this->createMock('Sonata\NewsBundle\Model\CommentManagerInterface'); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return new CommentController($commentManager); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|