]>
Commit | Line | Data |
---|---|---|
5ec4708c A |
1 | <?php |
2 | ||
3 | declare(strict_types=1); | |
4 | ||
5 | namespace front\controller; | |
6 | ||
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; | |
16 | ||
17 | class OpenSearchControllerTest extends TestCase | |
18 | { | |
19 | /** @var ShaarliContainer */ | |
20 | protected $container; | |
21 | ||
22 | /** @var OpenSearchController */ | |
23 | protected $controller; | |
24 | ||
25 | public function setUp(): void | |
26 | { | |
27 | $this->container = $this->createMock(ShaarliContainer::class); | |
28 | $this->controller = new OpenSearchController($this->container); | |
29 | } | |
30 | ||
31 | public function testOpenSearchController(): void | |
32 | { | |
33 | $this->createValidContainerMockSet(); | |
34 | ||
35 | $request = $this->createMock(Request::class); | |
36 | $response = new Response(); | |
37 | ||
38 | // Save RainTPL assigned variables | |
39 | $assignedVariables = []; | |
40 | $this->assignTemplateVars($assignedVariables); | |
41 | ||
42 | $result = $this->controller->index($request, $response); | |
43 | ||
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']); | |
48 | } | |
49 | ||
50 | protected function createValidContainerMockSet(): void | |
51 | { | |
52 | $loginManager = $this->createMock(LoginManager::class); | |
53 | $this->container->loginManager = $loginManager; | |
54 | ||
55 | // PageBuilder | |
56 | $pageBuilder = $this->createMock(PageBuilder::class); | |
57 | $pageBuilder | |
58 | ->method('render') | |
59 | ->willReturnCallback(function (string $template): string { | |
60 | return $template; | |
61 | }) | |
62 | ; | |
63 | $this->container->pageBuilder = $pageBuilder; | |
64 | ||
65 | $bookmarkService = $this->createMock(BookmarkServiceInterface::class); | |
66 | $this->container->bookmarkService = $bookmarkService; | |
67 | ||
68 | // Plugin Manager | |
69 | $pluginManager = $this->createMock(PluginManager::class); | |
70 | $this->container->pluginManager = $pluginManager; | |
71 | ||
72 | // $_SERVER | |
73 | $this->container->environment = [ | |
74 | 'SERVER_NAME' => 'shaarli', | |
75 | 'SERVER_PORT' => '80', | |
76 | 'REQUEST_URI' => '/open-search', | |
77 | ]; | |
78 | } | |
79 | ||
80 | protected function assignTemplateVars(array &$variables): void | |
81 | { | |
82 | $this->container->pageBuilder | |
83 | ->expects(static::atLeastOnce()) | |
84 | ->method('assign') | |
85 | ->willReturnCallback(function ($key, $value) use (&$variables) { | |
86 | $variables[$key] = $value; | |
87 | ||
88 | return $this; | |
89 | }) | |
90 | ; | |
91 | } | |
92 | } |