require_once 'tests/utils/ReferenceLinkDB.php';
require_once 'tests/utils/ReferenceHistory.php';
require_once 'tests/utils/FakeBookmarkService.php';
+require_once 'tests/container/ShaarliTestContainer.php';
+require_once 'tests/front/controller/FrontControllerMockHelper.php';
--- /dev/null
+<?php
+
+declare(strict_types=1);
+
+namespace Shaarli\Container;
+
+use PHPUnit\Framework\MockObject\MockObject;
+use Shaarli\Bookmark\BookmarkServiceInterface;
+use Shaarli\Config\ConfigManager;
+use Shaarli\Feed\FeedBuilder;
+use Shaarli\Formatter\FormatterFactory;
+use Shaarli\History;
+use Shaarli\Plugin\PluginManager;
+use Shaarli\Render\PageBuilder;
+use Shaarli\Render\PageCacheManager;
+use Shaarli\Security\LoginManager;
+use Shaarli\Security\SessionManager;
+
+/**
+ * Test helper allowing auto-completion for MockObjects.
+ *
+ * @property mixed[] $environment $_SERVER automatically injected by Slim
+ * @property MockObject|ConfigManager $conf
+ * @property MockObject|SessionManager $sessionManager
+ * @property MockObject|LoginManager $loginManager
+ * @property MockObject|string $webPath
+ * @property MockObject|History $history
+ * @property MockObject|BookmarkServiceInterface $bookmarkService
+ * @property MockObject|PageBuilder $pageBuilder
+ * @property MockObject|PluginManager $pluginManager
+ * @property MockObject|FormatterFactory $formatterFactory
+ * @property MockObject|PageCacheManager $pageCacheManager
+ * @property MockObject|FeedBuilder $feedBuilder
+ */
+class ShaarliTestContainer extends ShaarliContainer
+{
+
+}
use PHPUnit\Framework\TestCase;
use Shaarli\Bookmark\Bookmark;
-use Shaarli\Bookmark\BookmarkServiceInterface;
-use Shaarli\Config\ConfigManager;
-use Shaarli\Container\ShaarliContainer;
use Shaarli\Feed\CachedPage;
-use Shaarli\Formatter\BookmarkFormatter;
-use Shaarli\Formatter\BookmarkRawFormatter;
-use Shaarli\Formatter\FormatterFactory;
-use Shaarli\Plugin\PluginManager;
-use Shaarli\Render\PageBuilder;
-use Shaarli\Render\PageCacheManager;
-use Shaarli\Security\LoginManager;
use Slim\Http\Request;
use Slim\Http\Response;
class DailyControllerTest extends TestCase
{
- /** @var ShaarliContainer */
- protected $container;
+ use FrontControllerMockHelper;
/** @var DailyController */
protected $controller;
public function setUp(): void
{
- $this->container = $this->createMock(ShaarliContainer::class);
+ $this->createContainer();
+
$this->controller = new DailyController($this->container);
DailyController::$DAILY_RSS_NB_DAYS = 2;
}
static::assertArrayHasKey('loggedin', $param);
return $data;
- });
+ })
+ ;
$result = $this->controller->index($request, $response);
static::assertCount(0, $assignedVariables['days']);
}
- protected function createValidContainerMockSet(): void
- {
- $loginManager = $this->createMock(LoginManager::class);
- $this->container->loginManager = $loginManager;
-
- // Config
- $conf = $this->createMock(ConfigManager::class);
- $this->container->conf = $conf;
- $this->container->conf->method('get')->willReturnCallback(function (string $parameter, $default) {
- return $default;
- });
-
- // PageBuilder
- $pageBuilder = $this->createMock(PageBuilder::class);
- $pageBuilder
- ->method('render')
- ->willReturnCallback(function (string $template): string {
- return $template;
- })
- ;
- $this->container->pageBuilder = $pageBuilder;
-
- // Plugin Manager
- $pluginManager = $this->createMock(PluginManager::class);
- $this->container->pluginManager = $pluginManager;
-
- // BookmarkService
- $bookmarkService = $this->createMock(BookmarkServiceInterface::class);
- $this->container->bookmarkService = $bookmarkService;
-
- // Formatter
- $formatterFactory = $this->createMock(FormatterFactory::class);
- $formatterFactory
- ->method('getFormatter')
- ->willReturnCallback(function (): BookmarkFormatter {
- return new BookmarkRawFormatter($this->container->conf, true);
- })
- ;
- $this->container->formatterFactory = $formatterFactory;
-
- // CacheManager
- $pageCacheManager = $this->createMock(PageCacheManager::class);
- $this->container->pageCacheManager = $pageCacheManager;
-
- // $_SERVER
- $this->container->environment = [
- 'SERVER_NAME' => 'shaarli',
- 'SERVER_PORT' => '80',
- 'REQUEST_URI' => '/daily-rss',
- ];
- }
-
- protected function assignTemplateVars(array &$variables): void
- {
- $this->container->pageBuilder
- ->expects(static::atLeastOnce())
- ->method('assign')
- ->willReturnCallback(function ($key, $value) use (&$variables) {
- $variables[$key] = $value;
-
- return $this;
- })
- ;
- }
-
protected static function generateContent(int $length): string
{
// bin2hex(random_bytes) generates string twice as long as given parameter
namespace Shaarli\Front\Controller;
use PHPUnit\Framework\TestCase;
-use Shaarli\Bookmark\BookmarkServiceInterface;
-use Shaarli\Config\ConfigManager;
-use Shaarli\Container\ShaarliContainer;
use Shaarli\Feed\FeedBuilder;
-use Shaarli\Formatter\FormatterFactory;
-use Shaarli\Plugin\PluginManager;
-use Shaarli\Render\PageBuilder;
-use Shaarli\Render\PageCacheManager;
-use Shaarli\Security\LoginManager;
use Slim\Http\Request;
use Slim\Http\Response;
class FeedControllerTest extends TestCase
{
- /** @var ShaarliContainer */
- protected $container;
+ use FrontControllerMockHelper;
/** @var FeedController */
protected $controller;
public function setUp(): void
{
- $this->container = $this->createMock(ShaarliContainer::class);
+ $this->createContainer();
+
+ $this->container->feedBuilder = $this->createMock(FeedBuilder::class);
+
$this->controller = new FeedController($this->container);
}
static::assertSame('feed.atom', (string) $result->getBody());
static::assertSame('data', $assignedVariables['content']);
}
-
- protected function createValidContainerMockSet(): void
- {
- $loginManager = $this->createMock(LoginManager::class);
- $this->container->loginManager = $loginManager;
-
- // Config
- $conf = $this->createMock(ConfigManager::class);
- $this->container->conf = $conf;
- $this->container->conf->method('get')->willReturnCallback(function (string $parameter, $default) {
- return $default;
- });
-
- // PageBuilder
- $pageBuilder = $this->createMock(PageBuilder::class);
- $pageBuilder
- ->method('render')
- ->willReturnCallback(function (string $template): string {
- return $template;
- })
- ;
- $this->container->pageBuilder = $pageBuilder;
-
- $bookmarkService = $this->createMock(BookmarkServiceInterface::class);
- $this->container->bookmarkService = $bookmarkService;
-
- // Plugin Manager
- $pluginManager = $this->createMock(PluginManager::class);
- $this->container->pluginManager = $pluginManager;
-
- // Formatter
- $formatterFactory = $this->createMock(FormatterFactory::class);
- $this->container->formatterFactory = $formatterFactory;
-
- // CacheManager
- $pageCacheManager = $this->createMock(PageCacheManager::class);
- $this->container->pageCacheManager = $pageCacheManager;
-
- // FeedBuilder
- $feedBuilder = $this->createMock(FeedBuilder::class);
- $this->container->feedBuilder = $feedBuilder;
-
- // $_SERVER
- $this->container->environment = [
- 'SERVER_NAME' => 'shaarli',
- 'SERVER_PORT' => '80',
- 'REQUEST_URI' => '/daily-rss',
- ];
- }
-
- protected function assignTemplateVars(array &$variables): void
- {
- $this->container->pageBuilder
- ->expects(static::atLeastOnce())
- ->method('assign')
- ->willReturnCallback(function ($key, $value) use (&$variables) {
- $variables[$key] = $value;
-
- return $this;
- })
- ;
- }
}
--- /dev/null
+<?php
+
+declare(strict_types=1);
+
+namespace Shaarli\Front\Controller;
+
+use PHPUnit\Framework\MockObject\MockObject;
+use Shaarli\Bookmark\BookmarkServiceInterface;
+use Shaarli\Config\ConfigManager;
+use Shaarli\Container\ShaarliTestContainer;
+use Shaarli\Formatter\BookmarkFormatter;
+use Shaarli\Formatter\BookmarkRawFormatter;
+use Shaarli\Formatter\FormatterFactory;
+use Shaarli\Plugin\PluginManager;
+use Shaarli\Render\PageBuilder;
+use Shaarli\Render\PageCacheManager;
+use Shaarli\Security\LoginManager;
+use Shaarli\Security\SessionManager;
+
+/**
+ * Trait FrontControllerMockHelper
+ *
+ * Helper trait used to initialize the ShaarliContainer and mock its services for controller tests.
+ *
+ * @property ShaarliTestContainer $container
+ * @package Shaarli\Front\Controller
+ */
+trait FrontControllerMockHelper
+{
+ /** @var ShaarliTestContainer */
+ protected $container;
+
+ /**
+ * Mock the container instance
+ */
+ protected function createContainer(): void
+ {
+ $this->container = $this->createMock(ShaarliTestContainer::class);
+ }
+
+ /**
+ * Initialize container's services used by tests
+ */
+ protected function createValidContainerMockSet(): void
+ {
+ $this->container->loginManager = $this->createMock(LoginManager::class);
+
+ // Config
+ $this->container->conf = $this->createMock(ConfigManager::class);
+ $this->container->conf->method('get')->willReturnCallback(function (string $parameter, $default) {
+ return $default;
+ });
+
+ // PageBuilder
+ $this->container->pageBuilder = $this->createMock(PageBuilder::class);
+ $this->container->pageBuilder
+ ->method('render')
+ ->willReturnCallback(function (string $template): string {
+ return $template;
+ })
+ ;
+
+ // Plugin Manager
+ $this->container->pluginManager = $this->createMock(PluginManager::class);
+
+ // BookmarkService
+ $this->container->bookmarkService = $this->createMock(BookmarkServiceInterface::class);
+
+ // Formatter
+ $this->container->formatterFactory = $this->createMock(FormatterFactory::class);
+ $this->container->formatterFactory
+ ->method('getFormatter')
+ ->willReturnCallback(function (): BookmarkFormatter {
+ return new BookmarkRawFormatter($this->container->conf, true);
+ })
+ ;
+
+ // CacheManager
+ $this->container->pageCacheManager = $this->createMock(PageCacheManager::class);
+
+ // SessionManager
+ $this->container->sessionManager = $this->createMock(SessionManager::class);
+
+ // $_SERVER
+ $this->container->environment = [
+ 'SERVER_NAME' => 'shaarli',
+ 'SERVER_PORT' => '80',
+ 'REQUEST_URI' => '/daily-rss',
+ ];
+ }
+
+ /**
+ * Pass a reference of an array which will be populated by `pageBuilder->assign` calls during execution.
+ *
+ * @param mixed $variables Array reference to populate.
+ */
+ protected function assignTemplateVars(array &$variables): void
+ {
+ $this->container->pageBuilder
+ ->expects(static::atLeastOnce())
+ ->method('assign')
+ ->willReturnCallback(function ($key, $value) use (&$variables) {
+ $variables[$key] = $value;
+
+ return $this;
+ })
+ ;
+ }
+
+ /**
+ * Force to be used in PHPUnit context.
+ */
+ protected abstract function createMock($originalClassName): MockObject;
+}
namespace Shaarli\Front\Controller;
use PHPUnit\Framework\TestCase;
-use Shaarli\Bookmark\BookmarkServiceInterface;
use Shaarli\Config\ConfigManager;
-use Shaarli\Container\ShaarliContainer;
use Shaarli\Front\Exception\LoginBannedException;
-use Shaarli\Plugin\PluginManager;
-use Shaarli\Render\PageBuilder;
-use Shaarli\Security\LoginManager;
use Slim\Http\Request;
use Slim\Http\Response;
class LoginControllerTest extends TestCase
{
- /** @var ShaarliContainer */
- protected $container;
+ use FrontControllerMockHelper;
/** @var LoginController */
protected $controller;
public function setUp(): void
{
- $this->container = $this->createMock(ShaarliContainer::class);
+ $this->createContainer();
+
$this->controller = new LoginController($this->container);
}
})
;
+ $this->container->loginManager->method('canLogin')->willReturn(true);
+
$result = $this->controller->index($request, $response);
static::assertInstanceOf(Response::class, $result);
})
;
+ $this->container->loginManager->expects(static::once())->method('canLogin')->willReturn(true);
+
$result = $this->controller->index($request, $response);
static::assertInstanceOf(Response::class, $result);
public function testLoginControllerWhileLoggedIn(): void
{
+ $this->createValidContainerMockSet();
+
$request = $this->createMock(Request::class);
$response = new Response();
- $loginManager = $this->createMock(LoginManager::class);
- $loginManager->expects(static::once())->method('isLoggedIn')->willReturn(true);
- $this->container->loginManager = $loginManager;
+ $this->container->loginManager->expects(static::once())->method('isLoggedIn')->willReturn(true);
$result = $this->controller->index($request, $response);
$request = $this->createMock(Request::class);
$response = new Response();
- $loginManager = $this->createMock(LoginManager::class);
- $loginManager->method('isLoggedIn')->willReturn(false);
- $loginManager->method('canLogin')->willReturn(false);
- $this->container->loginManager = $loginManager;
+ $this->container->loginManager->method('isLoggedIn')->willReturn(false);
+ $this->container->loginManager->method('canLogin')->willReturn(false);
$this->expectException(LoginBannedException::class);
$this->controller->index($request, $response);
}
-
- protected function createValidContainerMockSet(): void
- {
- // User logged out
- $loginManager = $this->createMock(LoginManager::class);
- $loginManager->method('isLoggedIn')->willReturn(false);
- $loginManager->method('canLogin')->willReturn(true);
- $this->container->loginManager = $loginManager;
-
- // Config
- $conf = $this->createMock(ConfigManager::class);
- $conf->method('get')->willReturnCallback(function (string $parameter, $default) {
- return $default;
- });
- $this->container->conf = $conf;
-
- // PageBuilder
- $pageBuilder = $this->createMock(PageBuilder::class);
- $pageBuilder
- ->method('render')
- ->willReturnCallback(function (string $template): string {
- return $template;
- })
- ;
- $this->container->pageBuilder = $pageBuilder;
-
- $pluginManager = $this->createMock(PluginManager::class);
- $this->container->pluginManager = $pluginManager;
- $bookmarkService = $this->createMock(BookmarkServiceInterface::class);
- $this->container->bookmarkService = $bookmarkService;
- }
}
}
use PHPUnit\Framework\TestCase;
-use Shaarli\Container\ShaarliContainer;
-use Shaarli\Render\PageCacheManager;
use Shaarli\Security\LoginManager;
use Shaarli\Security\SessionManager;
use Slim\Http\Request;
class LogoutControllerTest extends TestCase
{
- /** @var ShaarliContainer */
- protected $container;
+ use FrontControllerMockHelper;
/** @var LogoutController */
protected $controller;
public function setUp(): void
{
- $this->container = $this->createMock(ShaarliContainer::class);
+ $this->createContainer();
+
$this->controller = new LogoutController($this->container);
setcookie(LoginManager::$STAY_SIGNED_IN_COOKIE, $cookie = 'hi there');
public function testValidControllerInvoke(): void
{
+ $this->createValidContainerMockSet();
+
$request = $this->createMock(Request::class);
$response = new Response();
- $pageCacheManager = $this->createMock(PageCacheManager::class);
- $pageCacheManager->expects(static::once())->method('invalidateCaches');
- $this->container->pageCacheManager = $pageCacheManager;
+ $this->container->pageCacheManager->expects(static::once())->method('invalidateCaches');
- $sessionManager = $this->createMock(SessionManager::class);
- $sessionManager->expects(static::once())->method('logout');
- $this->container->sessionManager = $sessionManager;
+ $this->container->sessionManager = $this->createMock(SessionManager::class);
+ $this->container->sessionManager->expects(static::once())->method('logout');
static::assertSame('hi there', $_COOKIE[LoginManager::$STAY_SIGNED_IN_COOKIE]);
namespace front\controller;
use PHPUnit\Framework\TestCase;
-use Shaarli\Bookmark\BookmarkServiceInterface;
-use Shaarli\Container\ShaarliContainer;
+use Shaarli\Front\Controller\FrontControllerMockHelper;
use Shaarli\Front\Controller\OpenSearchController;
-use Shaarli\Plugin\PluginManager;
-use Shaarli\Render\PageBuilder;
-use Shaarli\Security\LoginManager;
use Slim\Http\Request;
use Slim\Http\Response;
class OpenSearchControllerTest extends TestCase
{
- /** @var ShaarliContainer */
- protected $container;
+ use FrontControllerMockHelper;
/** @var OpenSearchController */
protected $controller;
public function setUp(): void
{
- $this->container = $this->createMock(ShaarliContainer::class);
+ $this->createContainer();
+
$this->controller = new OpenSearchController($this->container);
}
$result = $this->controller->index($request, $response);
static::assertSame(200, $result->getStatusCode());
- static::assertStringContainsString('application/xml', $result->getHeader('Content-Type')[0]);
+ static::assertStringContainsString(
+ 'application/opensearchdescription+xml',
+ $result->getHeader('Content-Type')[0]
+ );
static::assertSame('opensearch', (string) $result->getBody());
static::assertSame('http://shaarli', $assignedVariables['serverurl']);
}
-
- protected function createValidContainerMockSet(): void
- {
- $loginManager = $this->createMock(LoginManager::class);
- $this->container->loginManager = $loginManager;
-
- // PageBuilder
- $pageBuilder = $this->createMock(PageBuilder::class);
- $pageBuilder
- ->method('render')
- ->willReturnCallback(function (string $template): string {
- return $template;
- })
- ;
- $this->container->pageBuilder = $pageBuilder;
-
- $bookmarkService = $this->createMock(BookmarkServiceInterface::class);
- $this->container->bookmarkService = $bookmarkService;
-
- // Plugin Manager
- $pluginManager = $this->createMock(PluginManager::class);
- $this->container->pluginManager = $pluginManager;
-
- // $_SERVER
- $this->container->environment = [
- 'SERVER_NAME' => 'shaarli',
- 'SERVER_PORT' => '80',
- 'REQUEST_URI' => '/open-search',
- ];
- }
-
- protected function assignTemplateVars(array &$variables): void
- {
- $this->container->pageBuilder
- ->expects(static::atLeastOnce())
- ->method('assign')
- ->willReturnCallback(function ($key, $value) use (&$variables) {
- $variables[$key] = $value;
-
- return $this;
- })
- ;
- }
}
use PHPUnit\Framework\TestCase;
use Shaarli\Bookmark\Bookmark;
-use Shaarli\Bookmark\BookmarkServiceInterface;
use Shaarli\Config\ConfigManager;
-use Shaarli\Container\ShaarliContainer;
-use Shaarli\Formatter\BookmarkFormatter;
-use Shaarli\Formatter\BookmarkRawFormatter;
-use Shaarli\Formatter\FormatterFactory;
use Shaarli\Front\Exception\ThumbnailsDisabledException;
-use Shaarli\Plugin\PluginManager;
-use Shaarli\Render\PageBuilder;
-use Shaarli\Security\LoginManager;
use Shaarli\Thumbnailer;
use Slim\Http\Request;
use Slim\Http\Response;
class PictureWallControllerTest extends TestCase
{
- /** @var ShaarliContainer */
- protected $container;
+ use FrontControllerMockHelper;
/** @var PictureWallController */
protected $controller;
public function setUp(): void
{
- $this->container = $this->createMock(ShaarliContainer::class);
+ $this->createContainer();
+
$this->controller = new PictureWallController($this->container);
}
$response = new Response();
// ConfigManager: thumbnails are enabled
+ $this->container->conf = $this->createMock(ConfigManager::class);
$this->container->conf->method('get')->willReturnCallback(function (string $parameter, $default) {
if ($parameter === 'thumbnails.mode') {
return Thumbnailer::MODE_COMMON;
// Save RainTPL assigned variables
$assignedVariables = [];
- $this->container->pageBuilder
- ->expects(static::atLeastOnce())
- ->method('assign')
- ->willReturnCallback(function ($key, $value) use (&$assignedVariables) {
- $assignedVariables[$key] = $value;
-
- return $this;
- })
- ;
+ $this->assignTemplateVars($assignedVariables);
// Links dataset: 2 links with thumbnails
$this->container->bookmarkService
$this->controller->index($request, $response);
}
-
- protected function createValidContainerMockSet(): void
- {
- $loginManager = $this->createMock(LoginManager::class);
- $this->container->loginManager = $loginManager;
-
- // Config
- $conf = $this->createMock(ConfigManager::class);
- $this->container->conf = $conf;
-
- // PageBuilder
- $pageBuilder = $this->createMock(PageBuilder::class);
- $pageBuilder
- ->method('render')
- ->willReturnCallback(function (string $template): string {
- return $template;
- })
- ;
- $this->container->pageBuilder = $pageBuilder;
-
- // Plugin Manager
- $pluginManager = $this->createMock(PluginManager::class);
- $this->container->pluginManager = $pluginManager;
-
- // BookmarkService
- $bookmarkService = $this->createMock(BookmarkServiceInterface::class);
- $this->container->bookmarkService = $bookmarkService;
-
- // Formatter
- $formatterFactory = $this->createMock(FormatterFactory::class);
- $formatterFactory
- ->method('getFormatter')
- ->willReturnCallback(function (string $type): BookmarkFormatter {
- if ($type === 'raw') {
- return new BookmarkRawFormatter($this->container->conf, true);
- }
- })
- ;
- $this->container->formatterFactory = $formatterFactory;
- }
}
use PHPUnit\Framework\TestCase;
use Shaarli\Bookmark\BookmarkFilter;
-use Shaarli\Bookmark\BookmarkServiceInterface;
-use Shaarli\Container\ShaarliContainer;
-use Shaarli\Plugin\PluginManager;
-use Shaarli\Render\PageBuilder;
-use Shaarli\Security\LoginManager;
/**
* Class ShaarliControllerTest
*/
class ShaarliControllerTest extends TestCase
{
- /** @var ShaarliContainer */
- protected $container;
+ use FrontControllerMockHelper;
/** @var LoginController */
protected $controller;
public function setUp(): void
{
- $this->container = $this->createMock(ShaarliContainer::class);
+ $this->createContainer();
+
$this->controller = new class($this->container) extends ShaarliController
{
public function assignView(string $key, $value): ShaarliController
{
$this->createValidContainerMockSet();
+ $this->assignTemplateVars($this->assignedValues);
+
$self = $this->controller->assignView('variableName', 'variableValue');
static::assertInstanceOf(ShaarliController::class, $self);
{
$this->createValidContainerMockSet();
+ $this->assignTemplateVars($this->assignedValues);
+
+ $this->container->bookmarkService
+ ->method('count')
+ ->willReturnCallback(function (string $visibility): int {
+ return $visibility === BookmarkFilter::$PRIVATE ? 5 : 10;
+ })
+ ;
+
+ $this->container->pluginManager
+ ->method('executeHooks')
+ ->willReturnCallback(function (string $hook, array &$data, array $params): array {
+ return $data[$hook] = $params;
+ });
+ $this->container->pluginManager->method('getErrors')->willReturn(['error']);
+
+ $this->container->loginManager->method('isLoggedIn')->willReturn(true);
+
$render = $this->controller->render('templateName');
static::assertSame('templateName', $render);
static::assertSame('templateName', $this->assignedValues['plugins_footer']['render_footer']['target']);
static::assertTrue($this->assignedValues['plugins_footer']['render_footer']['loggedin']);
}
-
- protected function createValidContainerMockSet(): void
- {
- $pageBuilder = $this->createMock(PageBuilder::class);
- $pageBuilder
- ->method('assign')
- ->willReturnCallback(function (string $key, $value): void {
- $this->assignedValues[$key] = $value;
- });
- $pageBuilder
- ->method('render')
- ->willReturnCallback(function (string $template): string {
- return $template;
- });
- $this->container->pageBuilder = $pageBuilder;
-
- $bookmarkService = $this->createMock(BookmarkServiceInterface::class);
- $bookmarkService
- ->method('count')
- ->willReturnCallback(function (string $visibility): int {
- return $visibility === BookmarkFilter::$PRIVATE ? 5 : 10;
- });
- $this->container->bookmarkService = $bookmarkService;
-
- $pluginManager = $this->createMock(PluginManager::class);
- $pluginManager
- ->method('executeHooks')
- ->willReturnCallback(function (string $hook, array &$data, array $params): array {
- return $data[$hook] = $params;
- });
- $pluginManager->method('getErrors')->willReturn(['error']);
- $this->container->pluginManager = $pluginManager;
-
- $loginManager = $this->createMock(LoginManager::class);
- $loginManager->method('isLoggedIn')->willReturn(true);
- $this->container->loginManager = $loginManager;
- }
}
use PHPUnit\Framework\TestCase;
use Shaarli\Bookmark\BookmarkFilter;
-use Shaarli\Bookmark\BookmarkServiceInterface;
-use Shaarli\Config\ConfigManager;
-use Shaarli\Container\ShaarliContainer;
-use Shaarli\Plugin\PluginManager;
-use Shaarli\Render\PageBuilder;
-use Shaarli\Security\LoginManager;
-use Shaarli\Security\SessionManager;
use Slim\Http\Request;
use Slim\Http\Response;
class TagCloudControllerTest extends TestCase
{
- /** @var ShaarliContainer */
- protected $container;
+ use FrontControllerMockHelper;
/** @var TagCloudController */
protected $controller;
public function setUp(): void
{
- $this->container = $this->createMock(ShaarliContainer::class);
+ $this->createContainer();
+
$this->controller = new TagCloudController($this->container);
}
static::assertSame('', $assignedVariables['search_tags']);
static::assertCount(0, $assignedVariables['tags']);
}
-
-
- protected function createValidContainerMockSet(): void
- {
- $loginManager = $this->createMock(LoginManager::class);
- $this->container->loginManager = $loginManager;
-
- $sessionManager = $this->createMock(SessionManager::class);
- $this->container->sessionManager = $sessionManager;
-
- // Config
- $conf = $this->createMock(ConfigManager::class);
- $this->container->conf = $conf;
-
- $this->container->conf->method('get')->willReturnCallback(function (string $parameter, $default) {
- return $default;
- });
-
- // PageBuilder
- $pageBuilder = $this->createMock(PageBuilder::class);
- $pageBuilder
- ->method('render')
- ->willReturnCallback(function (string $template): string {
- return $template;
- })
- ;
- $this->container->pageBuilder = $pageBuilder;
-
- // Plugin Manager
- $pluginManager = $this->createMock(PluginManager::class);
- $this->container->pluginManager = $pluginManager;
-
- // BookmarkService
- $bookmarkService = $this->createMock(BookmarkServiceInterface::class);
- $this->container->bookmarkService = $bookmarkService;
- }
-
- protected function assignTemplateVars(array &$variables): void
- {
- $this->container->pageBuilder
- ->expects(static::atLeastOnce())
- ->method('assign')
- ->willReturnCallback(function ($key, $value) use (&$variables) {
- $variables[$key] = $value;
-
- return $this;
- })
- ;
- }
}
namespace Shaarli\Front\Controller;
use PHPUnit\Framework\TestCase;
-use Shaarli\Bookmark\BookmarkServiceInterface;
-use Shaarli\Config\ConfigManager;
-use Shaarli\Container\ShaarliContainer;
-use Shaarli\Plugin\PluginManager;
-use Shaarli\Render\PageBuilder;
-use Shaarli\Security\LoginManager;
use Slim\Http\Request;
use Slim\Http\Response;
class TagControllerTest extends TestCase
{
- /** @var ShaarliContainer */
- protected $container;
+ use FrontControllerMockHelper;
/** @var TagController */
protected $controller;
public function setUp(): void
{
- $this->container = $this->createMock(ShaarliContainer::class);
+ $this->createContainer();
+
$this->controller = new TagController($this->container);
}
public function testAddTagWithReferer(): void
{
$this->createValidContainerMockSet();
+
$this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/'];
$request = $this->createMock(Request::class);
public function testAddTagWithRefererAndExistingSearch(): void
{
$this->createValidContainerMockSet();
+
$this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=def'];
$request = $this->createMock(Request::class);
public function testAddTagRemoveLegacyQueryParam(): void
{
$this->createValidContainerMockSet();
+
$this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=def&addtag=abc'];
$request = $this->createMock(Request::class);
public function testAddTagResetPagination(): void
{
$this->createValidContainerMockSet();
+
$this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=def&page=12'];
$request = $this->createMock(Request::class);
public function testAddTagWithRefererAndEmptySearch(): void
{
$this->createValidContainerMockSet();
+
$this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags='];
$request = $this->createMock(Request::class);
public function testAddTagWithoutNewTagWithReferer(): void
{
$this->createValidContainerMockSet();
+
$this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=def'];
$request = $this->createMock(Request::class);
static::assertSame(302, $result->getStatusCode());
static::assertSame(['./'], $result->getHeader('location'));
}
-
- protected function createValidContainerMockSet(): void
- {
- // User logged out
- $loginManager = $this->createMock(LoginManager::class);
- $loginManager->method('isLoggedIn')->willReturn(false);
- $loginManager->method('canLogin')->willReturn(true);
- $this->container->loginManager = $loginManager;
-
- // Config
- $conf = $this->createMock(ConfigManager::class);
- $conf->method('get')->willReturnCallback(function (string $parameter, $default) {
- return $default;
- });
- $this->container->conf = $conf;
-
- // PageBuilder
- $pageBuilder = $this->createMock(PageBuilder::class);
- $pageBuilder
- ->method('render')
- ->willReturnCallback(function (string $template): string {
- return $template;
- })
- ;
- $this->container->pageBuilder = $pageBuilder;
-
- $pluginManager = $this->createMock(PluginManager::class);
- $this->container->pluginManager = $pluginManager;
- $bookmarkService = $this->createMock(BookmarkServiceInterface::class);
- $this->container->bookmarkService = $bookmarkService;
- }
}