3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller
;
7 use PHPUnit\Framework\TestCase
;
9 use Slim\Http\Response
;
11 class TagControllerTest
extends TestCase
13 use FrontControllerMockHelper
;
15 /** @var TagController */
16 protected $controller;
18 public function setUp(): void
20 $this->createContainer();
22 $this->controller
= new TagController($this->container
);
25 public function testAddTagWithReferer(): void
27 $this->createValidContainerMockSet();
29 $this->container
->environment
= ['HTTP_REFERER' => 'http://shaarli/controller/'];
31 $request = $this->createMock(Request
::class);
32 $response = new Response();
34 $tags = ['newTag' => 'abc'];
36 $result = $this->controller
->addTag($request, $response, $tags);
38 static::assertInstanceOf(Response
::class, $result);
39 static::assertSame(302, $result->getStatusCode());
40 static::assertSame(['/controller/?searchtags=abc'], $result->getHeader('location'));
43 public function testAddTagWithRefererAndExistingSearch(): void
45 $this->createValidContainerMockSet();
47 $this->container
->environment
= ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=def'];
49 $request = $this->createMock(Request
::class);
50 $response = new Response();
52 $tags = ['newTag' => 'abc'];
54 $result = $this->controller
->addTag($request, $response, $tags);
56 static::assertInstanceOf(Response
::class, $result);
57 static::assertSame(302, $result->getStatusCode());
58 static::assertSame(['/controller/?searchtags=def+abc'], $result->getHeader('location'));
61 public function testAddTagWithoutRefererAndExistingSearch(): void
63 $this->createValidContainerMockSet();
65 $request = $this->createMock(Request
::class);
66 $response = new Response();
68 $tags = ['newTag' => 'abc'];
70 $result = $this->controller
->addTag($request, $response, $tags);
72 static::assertInstanceOf(Response
::class, $result);
73 static::assertSame(302, $result->getStatusCode());
74 static::assertSame(['./?searchtags=abc'], $result->getHeader('location'));
77 public function testAddTagRemoveLegacyQueryParam(): void
79 $this->createValidContainerMockSet();
81 $this->container
->environment
= ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=def&addtag=abc'];
83 $request = $this->createMock(Request
::class);
84 $response = new Response();
86 $tags = ['newTag' => 'abc'];
88 $result = $this->controller
->addTag($request, $response, $tags);
90 static::assertInstanceOf(Response
::class, $result);
91 static::assertSame(302, $result->getStatusCode());
92 static::assertSame(['/controller/?searchtags=def+abc'], $result->getHeader('location'));
95 public function testAddTagResetPagination(): void
97 $this->createValidContainerMockSet();
99 $this->container
->environment
= ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=def&page=12'];
101 $request = $this->createMock(Request
::class);
102 $response = new Response();
104 $tags = ['newTag' => 'abc'];
106 $result = $this->controller
->addTag($request, $response, $tags);
108 static::assertInstanceOf(Response
::class, $result);
109 static::assertSame(302, $result->getStatusCode());
110 static::assertSame(['/controller/?searchtags=def+abc'], $result->getHeader('location'));
113 public function testAddTagWithRefererAndEmptySearch(): void
115 $this->createValidContainerMockSet();
117 $this->container
->environment
= ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags='];
119 $request = $this->createMock(Request
::class);
120 $response = new Response();
122 $tags = ['newTag' => 'abc'];
124 $result = $this->controller
->addTag($request, $response, $tags);
126 static::assertInstanceOf(Response
::class, $result);
127 static::assertSame(302, $result->getStatusCode());
128 static::assertSame(['/controller/?searchtags=abc'], $result->getHeader('location'));
131 public function testAddTagWithoutNewTagWithReferer(): void
133 $this->createValidContainerMockSet();
135 $this->container
->environment
= ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=def'];
137 $request = $this->createMock(Request
::class);
138 $response = new Response();
140 $result = $this->controller
->addTag($request, $response, []);
142 static::assertInstanceOf(Response
::class, $result);
143 static::assertSame(302, $result->getStatusCode());
144 static::assertSame(['/controller/?searchtags=def'], $result->getHeader('location'));
147 public function testAddTagWithoutNewTagWithoutReferer(): void
149 $this->createValidContainerMockSet();
151 $request = $this->createMock(Request
::class);
152 $response = new Response();
154 $result = $this->controller
->addTag($request, $response, []);
156 static::assertInstanceOf(Response
::class, $result);
157 static::assertSame(302, $result->getStatusCode());
158 static::assertSame(['./'], $result->getHeader('location'));