1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Harentius\BlogBundle\Tests\Unit\Controller; |
6
|
|
|
|
7
|
|
|
use Harentius\BlogBundle\Controller\SidebarController; |
8
|
|
|
use Harentius\BlogBundle\Entity\CategoryRepository; |
9
|
|
|
use Harentius\BlogBundle\Sidebar\Archive; |
10
|
|
|
use Harentius\BlogBundle\Sidebar\Tags; |
11
|
|
|
use PHPUnit\Framework\TestCase; |
12
|
|
|
use Symfony\Component\DependencyInjection\Container; |
13
|
|
|
use Symfony\Component\HttpFoundation\Response; |
14
|
|
|
use Twig\Environment; |
15
|
|
|
|
16
|
|
|
class SidebarControllerTest extends TestCase |
17
|
|
|
{ |
18
|
|
View Code Duplication |
public function testCategories(): void |
|
|
|
|
19
|
|
|
{ |
20
|
|
|
$twig = $this->createMock(Environment::class); |
21
|
|
|
$twig |
22
|
|
|
->expects($this->once()) |
23
|
|
|
->method('render') |
24
|
|
|
->with('@HarentiusBlog/Sidebar/categories.html.twig', [ |
25
|
|
|
'categories' => 'categories', |
26
|
|
|
]) |
27
|
|
|
; |
28
|
|
|
$sidebarController = $this->createSidebarController($twig); |
|
|
|
|
29
|
|
|
$response = $sidebarController->categories(); |
30
|
|
|
$this->assertInstanceOf(Response::class, $response); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
View Code Duplication |
public function testArchive(): void |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
$twig = $this->createMock(Environment::class); |
36
|
|
|
$twig |
37
|
|
|
->expects($this->once()) |
38
|
|
|
->method('render') |
39
|
|
|
->with('@HarentiusBlog/Sidebar/archive.html.twig', [ |
40
|
|
|
'archivesList' => 'list', |
41
|
|
|
]) |
42
|
|
|
; |
43
|
|
|
$sidebarController = $this->createSidebarController($twig); |
|
|
|
|
44
|
|
|
$response = $sidebarController->archive(); |
45
|
|
|
$this->assertInstanceOf(Response::class, $response); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
View Code Duplication |
public function testTags(): void |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
$twig = $this->createMock(Environment::class); |
51
|
|
|
$twig |
52
|
|
|
->expects($this->once()) |
53
|
|
|
->method('render') |
54
|
|
|
->with('@HarentiusBlog/Sidebar/tags.html.twig', [ |
55
|
|
|
'tags' => 'list', |
56
|
|
|
]) |
57
|
|
|
; |
58
|
|
|
$sidebarController = $this->createSidebarController($twig); |
|
|
|
|
59
|
|
|
$response = $sidebarController->tags(); |
60
|
|
|
$this->assertInstanceOf(Response::class, $response); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
private function createSidebarController(Environment $twig): SidebarController |
64
|
|
|
{ |
65
|
|
|
$categoryRepository = $this->createMock(CategoryRepository::class); |
66
|
|
|
$categoryRepository |
|
|
|
|
67
|
|
|
->method('notEmptyChildrenHierarchy') |
68
|
|
|
->willReturn('categories') |
69
|
|
|
; |
70
|
|
|
$archive = $this->createMock(Archive::class); |
71
|
|
|
$archive |
|
|
|
|
72
|
|
|
->method('getList') |
73
|
|
|
->willReturn('list') |
74
|
|
|
; |
75
|
|
|
$tags = $this->createMock(Tags::class); |
76
|
|
|
$tags |
|
|
|
|
77
|
|
|
->method('getList') |
78
|
|
|
->willReturn('list') |
79
|
|
|
; |
80
|
|
|
|
81
|
|
|
$controller = new SidebarController($categoryRepository, $archive, $tags); |
|
|
|
|
82
|
|
|
$container = new Container(); |
83
|
|
|
$container->set('twig', $twig); |
84
|
|
|
$controller->setContainer($container); |
85
|
|
|
|
86
|
|
|
return $controller; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
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.