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\TagController; |
9
|
|
|
use Harentius\BlogBundle\Entity\ArticleRepository; |
10
|
|
|
use Harentius\BlogBundle\Entity\Tag; |
11
|
|
|
use Harentius\BlogBundle\Paginator; |
12
|
|
|
use Knp\Bundle\PaginatorBundle\Pagination\SlidingPagination; |
13
|
|
|
use Knp\Component\Pager\PaginatorInterface; |
14
|
|
|
use PHPUnit\Framework\TestCase; |
15
|
|
|
use Symfony\Component\DependencyInjection\Container; |
16
|
|
|
use Symfony\Component\HttpFoundation\Request; |
17
|
|
|
use Symfony\Component\HttpFoundation\Response; |
18
|
|
|
use Twig\Environment; |
19
|
|
|
|
20
|
|
|
class TagControllerTest extends TestCase |
21
|
|
|
{ |
22
|
|
View Code Duplication |
public function testInvoke(): void |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
$slidingPagination = new SlidingPagination([]); |
25
|
|
|
$slidingPagination->setItemNumberPerPage(10); |
26
|
|
|
$tag = new Tag(); |
27
|
|
|
$twig = $this->createMock(Environment::class); |
28
|
|
|
$twig |
29
|
|
|
->expects($this->once()) |
30
|
|
|
->method('render') |
31
|
|
|
->with('@HarentiusBlog/Blog/list.html.twig', [ |
32
|
|
|
'articles' => $slidingPagination, |
33
|
|
|
'parent' => $tag, |
34
|
|
|
'noIndex' => true, |
35
|
|
|
'hasToPaginate' => false, |
36
|
|
|
]) |
37
|
|
|
; |
38
|
|
|
$tagController = $this->createTagController($twig, $slidingPagination); |
|
|
|
|
39
|
|
|
$request = new Request(); |
40
|
|
|
|
41
|
|
|
$response = $tagController($request, $tag); |
42
|
|
|
|
43
|
|
|
$this->assertInstanceOf(Response::class, $response); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
View Code Duplication |
private function createTagController(Environment $twig, SlidingPagination $slidingPagination): TagController |
|
|
|
|
47
|
|
|
{ |
48
|
|
|
$articleRepository = $this->createMock(ArticleRepository::class); |
49
|
|
|
$breadCrumbsManager = $this->createMock(BreadCrumbsManager::class); |
50
|
|
|
$knpPaginator = $this->createMock(PaginatorInterface::class); |
51
|
|
|
$knpPaginator |
|
|
|
|
52
|
|
|
->method('paginate') |
53
|
|
|
->willReturn($slidingPagination) |
54
|
|
|
; |
55
|
|
|
|
56
|
|
|
$paginator = new Paginator($knpPaginator, 123); |
|
|
|
|
57
|
|
|
$controller = new TagController($articleRepository, $breadCrumbsManager, $paginator); |
|
|
|
|
58
|
|
|
$container = new Container(); |
59
|
|
|
$container->set('twig', $twig); |
60
|
|
|
$controller->setContainer($container); |
61
|
|
|
|
62
|
|
|
return $controller; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
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.