aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/front/controller/OpenSearchControllerTest.php
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2020-05-20 12:43:40 +0200
committerArthurHoaro <arthur@hoa.ro>2020-07-23 21:19:21 +0200
commitdd09ec52b20b4a548ecf5c847627575e506e3a50 (patch)
treeb598b84727df2c604622b4db1dfee831dc7c5dca /tests/front/controller/OpenSearchControllerTest.php
parent5ec4708ced1cdca01eddd7e52377ab5e5f8b3290 (diff)
downloadShaarli-dd09ec52b20b4a548ecf5c847627575e506e3a50.tar.gz
Shaarli-dd09ec52b20b4a548ecf5c847627575e506e3a50.tar.zst
Shaarli-dd09ec52b20b4a548ecf5c847627575e506e3a50.zip
Refactor front controller tests to create container mock using a trait
Diffstat (limited to 'tests/front/controller/OpenSearchControllerTest.php')
-rw-r--r--tests/front/controller/OpenSearchControllerTest.php60
1 files changed, 8 insertions, 52 deletions
diff --git a/tests/front/controller/OpenSearchControllerTest.php b/tests/front/controller/OpenSearchControllerTest.php
index 7ba0f7df..f3b6f439 100644
--- a/tests/front/controller/OpenSearchControllerTest.php
+++ b/tests/front/controller/OpenSearchControllerTest.php
@@ -5,26 +5,22 @@ declare(strict_types=1);
5namespace front\controller; 5namespace front\controller;
6 6
7use PHPUnit\Framework\TestCase; 7use PHPUnit\Framework\TestCase;
8use Shaarli\Bookmark\BookmarkServiceInterface; 8use Shaarli\Front\Controller\FrontControllerMockHelper;
9use Shaarli\Container\ShaarliContainer;
10use Shaarli\Front\Controller\OpenSearchController; 9use Shaarli\Front\Controller\OpenSearchController;
11use Shaarli\Plugin\PluginManager;
12use Shaarli\Render\PageBuilder;
13use Shaarli\Security\LoginManager;
14use Slim\Http\Request; 10use Slim\Http\Request;
15use Slim\Http\Response; 11use Slim\Http\Response;
16 12
17class OpenSearchControllerTest extends TestCase 13class OpenSearchControllerTest extends TestCase
18{ 14{
19 /** @var ShaarliContainer */ 15 use FrontControllerMockHelper;
20 protected $container;
21 16
22 /** @var OpenSearchController */ 17 /** @var OpenSearchController */
23 protected $controller; 18 protected $controller;
24 19
25 public function setUp(): void 20 public function setUp(): void
26 { 21 {
27 $this->container = $this->createMock(ShaarliContainer::class); 22 $this->createContainer();
23
28 $this->controller = new OpenSearchController($this->container); 24 $this->controller = new OpenSearchController($this->container);
29 } 25 }
30 26
@@ -42,51 +38,11 @@ class OpenSearchControllerTest extends TestCase
42 $result = $this->controller->index($request, $response); 38 $result = $this->controller->index($request, $response);
43 39
44 static::assertSame(200, $result->getStatusCode()); 40 static::assertSame(200, $result->getStatusCode());
45 static::assertStringContainsString('application/xml', $result->getHeader('Content-Type')[0]); 41 static::assertStringContainsString(
42 'application/opensearchdescription+xml',
43 $result->getHeader('Content-Type')[0]
44 );
46 static::assertSame('opensearch', (string) $result->getBody()); 45 static::assertSame('opensearch', (string) $result->getBody());
47 static::assertSame('http://shaarli', $assignedVariables['serverurl']); 46 static::assertSame('http://shaarli', $assignedVariables['serverurl']);
48 } 47 }
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} 48}