3 declare(strict_types
=1);
5 namespace front\controller
;
7 use PHPUnit\Framework\TestCase
;
8 use Shaarli\Bookmark\BookmarkServiceInterface
;
9 use Shaarli\Container\ShaarliContainer
;
10 use Shaarli\Front\Controller\OpenSearchController
;
11 use Shaarli\Plugin\PluginManager
;
12 use Shaarli\Render\PageBuilder
;
13 use Shaarli\Security\LoginManager
;
14 use Slim\Http\Request
;
15 use Slim\Http\Response
;
17 class OpenSearchControllerTest
extends TestCase
19 /** @var ShaarliContainer */
22 /** @var OpenSearchController */
23 protected $controller;
25 public function setUp(): void
27 $this->container
= $this->createMock(ShaarliContainer
::class);
28 $this->controller
= new OpenSearchController($this->container
);
31 public function testOpenSearchController(): void
33 $this->createValidContainerMockSet();
35 $request = $this->createMock(Request
::class);
36 $response = new Response();
38 // Save RainTPL assigned variables
39 $assignedVariables = [];
40 $this->assignTemplateVars($assignedVariables);
42 $result = $this->controller
->index($request, $response);
44 static::assertSame(200, $result->getStatusCode());
45 static::assertStringContainsString('application/xml', $result->getHeader('Content-Type')[0]);
46 static::assertSame('opensearch', (string) $result->getBody());
47 static::assertSame('http://shaarli', $assignedVariables['serverurl']);
50 protected function createValidContainerMockSet(): void
52 $loginManager = $this->createMock(LoginManager
::class);
53 $this->container
->loginManager
= $loginManager;
56 $pageBuilder = $this->createMock(PageBuilder
::class);
59 ->willReturnCallback(function (string $template): string {
63 $this->container
->pageBuilder
= $pageBuilder;
65 $bookmarkService = $this->createMock(BookmarkServiceInterface
::class);
66 $this->container
->bookmarkService
= $bookmarkService;
69 $pluginManager = $this->createMock(PluginManager
::class);
70 $this->container
->pluginManager
= $pluginManager;
73 $this->container
->environment
= [
74 'SERVER_NAME' => 'shaarli',
75 'SERVER_PORT' => '80',
76 'REQUEST_URI' => '/open-search',
80 protected function assignTemplateVars(array &$variables): void
82 $this->container
->pageBuilder
83 ->expects(static::atLeastOnce())
85 ->willReturnCallback(function ($key, $value) use (&$variables) {
86 $variables[$key] = $value;