]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/front/controller/TagControllerTest.php
Refactor front controller tests to create container mock using a trait
[github/shaarli/Shaarli.git] / tests / front / controller / TagControllerTest.php
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Shaarli\Front\Controller;
6
7 use PHPUnit\Framework\TestCase;
8 use Slim\Http\Request;
9 use Slim\Http\Response;
10
11 class TagControllerTest extends TestCase
12 {
13 use FrontControllerMockHelper;
14
15 /** @var TagController */
16 protected $controller;
17
18 public function setUp(): void
19 {
20 $this->createContainer();
21
22 $this->controller = new TagController($this->container);
23 }
24
25 public function testAddTagWithReferer(): void
26 {
27 $this->createValidContainerMockSet();
28
29 $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/'];
30
31 $request = $this->createMock(Request::class);
32 $response = new Response();
33
34 $tags = ['newTag' => 'abc'];
35
36 $result = $this->controller->addTag($request, $response, $tags);
37
38 static::assertInstanceOf(Response::class, $result);
39 static::assertSame(302, $result->getStatusCode());
40 static::assertSame(['/controller/?searchtags=abc'], $result->getHeader('location'));
41 }
42
43 public function testAddTagWithRefererAndExistingSearch(): void
44 {
45 $this->createValidContainerMockSet();
46
47 $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=def'];
48
49 $request = $this->createMock(Request::class);
50 $response = new Response();
51
52 $tags = ['newTag' => 'abc'];
53
54 $result = $this->controller->addTag($request, $response, $tags);
55
56 static::assertInstanceOf(Response::class, $result);
57 static::assertSame(302, $result->getStatusCode());
58 static::assertSame(['/controller/?searchtags=def+abc'], $result->getHeader('location'));
59 }
60
61 public function testAddTagWithoutRefererAndExistingSearch(): void
62 {
63 $this->createValidContainerMockSet();
64
65 $request = $this->createMock(Request::class);
66 $response = new Response();
67
68 $tags = ['newTag' => 'abc'];
69
70 $result = $this->controller->addTag($request, $response, $tags);
71
72 static::assertInstanceOf(Response::class, $result);
73 static::assertSame(302, $result->getStatusCode());
74 static::assertSame(['./?searchtags=abc'], $result->getHeader('location'));
75 }
76
77 public function testAddTagRemoveLegacyQueryParam(): void
78 {
79 $this->createValidContainerMockSet();
80
81 $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=def&addtag=abc'];
82
83 $request = $this->createMock(Request::class);
84 $response = new Response();
85
86 $tags = ['newTag' => 'abc'];
87
88 $result = $this->controller->addTag($request, $response, $tags);
89
90 static::assertInstanceOf(Response::class, $result);
91 static::assertSame(302, $result->getStatusCode());
92 static::assertSame(['/controller/?searchtags=def+abc'], $result->getHeader('location'));
93 }
94
95 public function testAddTagResetPagination(): void
96 {
97 $this->createValidContainerMockSet();
98
99 $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=def&page=12'];
100
101 $request = $this->createMock(Request::class);
102 $response = new Response();
103
104 $tags = ['newTag' => 'abc'];
105
106 $result = $this->controller->addTag($request, $response, $tags);
107
108 static::assertInstanceOf(Response::class, $result);
109 static::assertSame(302, $result->getStatusCode());
110 static::assertSame(['/controller/?searchtags=def+abc'], $result->getHeader('location'));
111 }
112
113 public function testAddTagWithRefererAndEmptySearch(): void
114 {
115 $this->createValidContainerMockSet();
116
117 $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags='];
118
119 $request = $this->createMock(Request::class);
120 $response = new Response();
121
122 $tags = ['newTag' => 'abc'];
123
124 $result = $this->controller->addTag($request, $response, $tags);
125
126 static::assertInstanceOf(Response::class, $result);
127 static::assertSame(302, $result->getStatusCode());
128 static::assertSame(['/controller/?searchtags=abc'], $result->getHeader('location'));
129 }
130
131 public function testAddTagWithoutNewTagWithReferer(): void
132 {
133 $this->createValidContainerMockSet();
134
135 $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=def'];
136
137 $request = $this->createMock(Request::class);
138 $response = new Response();
139
140 $result = $this->controller->addTag($request, $response, []);
141
142 static::assertInstanceOf(Response::class, $result);
143 static::assertSame(302, $result->getStatusCode());
144 static::assertSame(['/controller/?searchtags=def'], $result->getHeader('location'));
145 }
146
147 public function testAddTagWithoutNewTagWithoutReferer(): void
148 {
149 $this->createValidContainerMockSet();
150
151 $request = $this->createMock(Request::class);
152 $response = new Response();
153
154 $result = $this->controller->addTag($request, $response, []);
155
156 static::assertInstanceOf(Response::class, $result);
157 static::assertSame(302, $result->getStatusCode());
158 static::assertSame(['./'], $result->getHeader('location'));
159 }
160 }