]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/front/controller/ShaarliControllerTest.php
Refactor front controller tests to create container mock using a trait
[github/shaarli/Shaarli.git] / tests / front / controller / ShaarliControllerTest.php
CommitLineData
0498b209
A
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Front\Controller;
6
7use PHPUnit\Framework\TestCase;
8use Shaarli\Bookmark\BookmarkFilter;
0498b209
A
9
10/**
11 * Class ShaarliControllerTest
12 *
13 * This class is used to test default behavior of ShaarliController abstract class.
14 * It uses a dummy non abstract controller.
15 */
16class ShaarliControllerTest extends TestCase
17{
dd09ec52 18 use FrontControllerMockHelper;
0498b209
A
19
20 /** @var LoginController */
21 protected $controller;
22
23 /** @var mixed[] List of variable assigned to the template */
24 protected $assignedValues;
25
26 public function setUp(): void
27 {
dd09ec52
A
28 $this->createContainer();
29
0498b209
A
30 $this->controller = new class($this->container) extends ShaarliController
31 {
32 public function assignView(string $key, $value): ShaarliController
33 {
34 return parent::assignView($key, $value);
35 }
36
37 public function render(string $template): string
38 {
39 return parent::render($template);
40 }
41 };
42 $this->assignedValues = [];
43 }
44
45 public function testAssignView(): void
46 {
47 $this->createValidContainerMockSet();
48
dd09ec52
A
49 $this->assignTemplateVars($this->assignedValues);
50
0498b209
A
51 $self = $this->controller->assignView('variableName', 'variableValue');
52
53 static::assertInstanceOf(ShaarliController::class, $self);
54 static::assertSame('variableValue', $this->assignedValues['variableName']);
55 }
56
57 public function testRender(): void
58 {
59 $this->createValidContainerMockSet();
60
dd09ec52
A
61 $this->assignTemplateVars($this->assignedValues);
62
63 $this->container->bookmarkService
64 ->method('count')
65 ->willReturnCallback(function (string $visibility): int {
66 return $visibility === BookmarkFilter::$PRIVATE ? 5 : 10;
67 })
68 ;
69
70 $this->container->pluginManager
71 ->method('executeHooks')
72 ->willReturnCallback(function (string $hook, array &$data, array $params): array {
73 return $data[$hook] = $params;
74 });
75 $this->container->pluginManager->method('getErrors')->willReturn(['error']);
76
77 $this->container->loginManager->method('isLoggedIn')->willReturn(true);
78
0498b209
A
79 $render = $this->controller->render('templateName');
80
81 static::assertSame('templateName', $render);
82
83 static::assertSame(10, $this->assignedValues['linkcount']);
84 static::assertSame(5, $this->assignedValues['privateLinkcount']);
85 static::assertSame(['error'], $this->assignedValues['plugin_errors']);
86
87 static::assertSame('templateName', $this->assignedValues['plugins_includes']['render_includes']['target']);
88 static::assertTrue($this->assignedValues['plugins_includes']['render_includes']['loggedin']);
89 static::assertSame('templateName', $this->assignedValues['plugins_header']['render_header']['target']);
90 static::assertTrue($this->assignedValues['plugins_header']['render_header']['loggedin']);
91 static::assertSame('templateName', $this->assignedValues['plugins_footer']['render_footer']['target']);
92 static::assertTrue($this->assignedValues['plugins_footer']['render_footer']['loggedin']);
93 }
0498b209 94}