mirror of
https://github.com/Icinga/icingaweb2-module-businessprocess.git
synced 2026-05-28 04:34:08 -04:00
FakeRequest, Url-Wrapper: allow tests involving...
...Url::fromPath fixes #13301
This commit is contained in:
parent
12a9459eaf
commit
e6c292333b
5 changed files with 94 additions and 6 deletions
|
|
@ -5,6 +5,7 @@ namespace Icinga\Module\Businessprocess\Test;
|
|||
use Icinga\Application\Config;
|
||||
use Icinga\Application\ApplicationBootstrap;
|
||||
use Icinga\Application\Icinga;
|
||||
use Icinga\Module\Businessprocess\Web\FakeRequest;
|
||||
use PHPUnit_Framework_TestCase;
|
||||
|
||||
abstract class BaseTestCase extends PHPUnit_Framework_TestCase
|
||||
|
|
@ -18,6 +19,7 @@ abstract class BaseTestCase extends PHPUnit_Framework_TestCase
|
|||
public function setUp()
|
||||
{
|
||||
$this->app();
|
||||
FakeRequest::setConfiguredBaseUrl('/icingaweb2/');
|
||||
}
|
||||
|
||||
protected function emptyConfigSection()
|
||||
|
|
|
|||
|
|
@ -42,12 +42,17 @@ abstract class Component
|
|||
protected function discoveredView()
|
||||
{
|
||||
if (self::$discoveredView === null) {
|
||||
$viewRenderer = Icinga::app()->getViewRenderer();
|
||||
if ($viewRenderer->view === null) {
|
||||
$viewRenderer->initView();
|
||||
}
|
||||
$icinga = Icinga::app();
|
||||
if ($icinga->isCli()) {
|
||||
self::$discoveredView = new View();
|
||||
} else {
|
||||
$viewRenderer = $icinga->getViewRenderer();
|
||||
if ($viewRenderer->view === null) {
|
||||
$viewRenderer->initView();
|
||||
}
|
||||
|
||||
self::$discoveredView = $viewRenderer->view;
|
||||
self::$discoveredView = $viewRenderer->view;
|
||||
}
|
||||
}
|
||||
|
||||
return self::$discoveredView;
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace Icinga\Module\Businessprocess\Web\Component;
|
||||
|
||||
use Icinga\Web\Url;
|
||||
use Icinga\Module\Businessprocess\Web\Url;
|
||||
|
||||
class Link extends Component
|
||||
{
|
||||
|
|
|
|||
26
library/Businessprocess/Web/FakeRequest.php
Normal file
26
library/Businessprocess/Web/FakeRequest.php
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
<?php
|
||||
|
||||
namespace Icinga\Module\Businessprocess\Web;
|
||||
|
||||
use Icinga\Exception\ProgrammingError;
|
||||
use Icinga\Web\Request;
|
||||
|
||||
class FakeRequest extends Request
|
||||
{
|
||||
/** @var string */
|
||||
private static $baseUrl;
|
||||
|
||||
public static function setConfiguredBaseUrl($url)
|
||||
{
|
||||
self::$baseUrl = $url;
|
||||
}
|
||||
|
||||
public function getBaseUrl($raw = false)
|
||||
{
|
||||
if (self::$baseUrl === null) {
|
||||
throw new ProgrammingError('Cannot determine base URL on CLI if not configured');
|
||||
} else {
|
||||
return self::$baseUrl;
|
||||
}
|
||||
}
|
||||
}
|
||||
55
library/Businessprocess/Web/Url.php
Normal file
55
library/Businessprocess/Web/Url.php
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
<?php
|
||||
|
||||
namespace Icinga\Module\Businessprocess\Web;
|
||||
|
||||
use Icinga\Application\Icinga;
|
||||
use Icinga\Exception\ProgrammingError;
|
||||
use Icinga\Web\Url as WebUrl;
|
||||
|
||||
class Url extends WebUrl
|
||||
{
|
||||
public static function fromPath($url, array $params = array(), $request = null)
|
||||
{
|
||||
if ($request === null) {
|
||||
$request = static::getRequest();
|
||||
}
|
||||
|
||||
if (! is_string($url)) {
|
||||
throw new ProgrammingError(
|
||||
'url "%s" is not a string',
|
||||
$url
|
||||
);
|
||||
}
|
||||
|
||||
$self = new static;
|
||||
|
||||
if ($url === '#') {
|
||||
return $self->setPath($url);
|
||||
}
|
||||
|
||||
$parts = parse_url($url);
|
||||
|
||||
$self->setBasePath($request->getBaseUrl());
|
||||
if (isset($parts['path'])) {
|
||||
$self->setPath($parts['path']);
|
||||
}
|
||||
|
||||
if (isset($parts['fragment'])) {
|
||||
$self->setAnchor($parts['fragment']);
|
||||
}
|
||||
|
||||
$self->setParams($params);
|
||||
return $self;
|
||||
}
|
||||
|
||||
protected static function getRequest()
|
||||
{
|
||||
$app = Icinga::app();
|
||||
if ($app->isCli()) {
|
||||
return new FakeRequest();
|
||||
} else {
|
||||
return $app->getRequest();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in a new issue