1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Harentius\BlogBundle\Tests\Unit\Controller; |
6
|
|
|
|
7
|
|
|
use Harentius\BlogBundle\BreadCrumbsManager; |
8
|
|
|
use Harentius\BlogBundle\Controller\ShowController; |
9
|
|
|
use Harentius\BlogBundle\Entity\Article; |
10
|
|
|
use Harentius\BlogBundle\Entity\Page; |
11
|
|
|
use PHPUnit\Framework\TestCase; |
12
|
|
|
use Symfony\Component\DependencyInjection\Container; |
13
|
|
|
use Symfony\Component\HttpFoundation\Response; |
14
|
|
|
use Twig\Environment; |
15
|
|
|
|
16
|
|
|
class ShowControllerTest extends TestCase |
17
|
|
|
{ |
18
|
|
View Code Duplication |
public function testInvokeWithArticle(): void |
|
|
|
|
19
|
|
|
{ |
20
|
|
|
$article = new Article(); |
21
|
|
|
$twig = $this->createMock(Environment::class); |
22
|
|
|
$twig |
23
|
|
|
->expects($this->once()) |
24
|
|
|
->method('render') |
25
|
|
|
->with('@HarentiusBlog/Show/article.html.twig', [ |
26
|
|
|
'entity' => $article, |
27
|
|
|
]) |
28
|
|
|
; |
29
|
|
|
$showController = $this->createShowController($twig); |
|
|
|
|
30
|
|
|
$response = $showController($article); |
31
|
|
|
$this->assertInstanceOf(Response::class, $response); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
View Code Duplication |
public function testInvokeWithPage(): void |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
$page = new Page(); |
37
|
|
|
$twig = $this->createMock(Environment::class); |
38
|
|
|
$twig |
39
|
|
|
->expects($this->once()) |
40
|
|
|
->method('render') |
41
|
|
|
->with('@HarentiusBlog/Show/page.html.twig', [ |
42
|
|
|
'entity' => $page, |
43
|
|
|
]) |
44
|
|
|
; |
45
|
|
|
$showController = $this->createShowController($twig); |
|
|
|
|
46
|
|
|
$response = $showController($page); |
47
|
|
|
$this->assertInstanceOf(Response::class, $response); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
private function createShowController(Environment $twig): ShowController |
51
|
|
|
{ |
52
|
|
|
$breadCrumbsManager = $this->createMock(BreadCrumbsManager::class); |
53
|
|
|
$controller = new ShowController($breadCrumbsManager); |
|
|
|
|
54
|
|
|
$container = new Container(); |
55
|
|
|
$container->set('twig', $twig); |
56
|
|
|
$controller->setContainer($container); |
57
|
|
|
|
58
|
|
|
return $controller; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
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.