2020-05-20 12:43:40 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-05-22 13:20:31 +02:00
|
|
|
namespace Shaarli\Front\Controller\Visitor;
|
2020-05-20 12:43:40 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
/**
|
2020-05-27 13:35:48 +02:00
|
|
|
* Mock the container instance and initialize container's services used by tests
|
2020-05-20 12:43:40 +02:00
|
|
|
*/
|
|
|
|
protected function createContainer(): void
|
|
|
|
{
|
|
|
|
$this->container = $this->createMock(ShaarliTestContainer::class);
|
|
|
|
|
|
|
|
$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) {
|
2020-10-22 16:21:03 +02:00
|
|
|
if ($parameter === 'general.tags_separator') {
|
|
|
|
return '@';
|
|
|
|
}
|
|
|
|
|
2020-05-30 14:00:06 +02:00
|
|
|
return $default === null ? $parameter : $default;
|
2020-05-20 12:43:40 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// 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',
|
2020-09-03 14:52:34 +02:00
|
|
|
'REQUEST_URI' => '/subfolder/daily-rss',
|
2020-07-21 20:33:33 +02:00
|
|
|
'REMOTE_ADDR' => '1.2.3.4',
|
2020-09-03 14:52:34 +02:00
|
|
|
'SCRIPT_NAME' => '/subfolder/index.php',
|
2020-05-20 12:43:40 +02:00
|
|
|
];
|
2020-06-13 11:22:14 +02:00
|
|
|
|
|
|
|
$this->container->basePath = '/subfolder';
|
2020-05-20 12:43:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
->method('assign')
|
|
|
|
->willReturnCallback(function ($key, $value) use (&$variables) {
|
|
|
|
$variables[$key] = $value;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
})
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2020-05-30 14:00:06 +02:00
|
|
|
protected static function generateString(int $length): string
|
|
|
|
{
|
|
|
|
// bin2hex(random_bytes) generates string twice as long as given parameter
|
|
|
|
$length = (int) ceil($length / 2);
|
|
|
|
|
|
|
|
return bin2hex(random_bytes($length));
|
|
|
|
}
|
|
|
|
|
2020-05-20 12:43:40 +02:00
|
|
|
/**
|
|
|
|
* Force to be used in PHPUnit context.
|
|
|
|
*/
|
2021-04-05 09:39:34 +02:00
|
|
|
abstract protected function isInTestsContext(): bool;
|
2020-05-20 12:43:40 +02:00
|
|
|
}
|