FakeRequest, Url-Wrapper: allow tests involving...

...Url::fromPath

fixes #13301
This commit is contained in:
Thomas Gelf 2016-11-24 00:20:59 +01:00
parent 12a9459eaf
commit e6c292333b
5 changed files with 94 additions and 6 deletions

View file

@ -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()

View file

@ -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;

View file

@ -2,7 +2,7 @@
namespace Icinga\Module\Businessprocess\Web\Component;
use Icinga\Web\Url;
use Icinga\Module\Businessprocess\Web\Url;
class Link extends Component
{

View 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;
}
}
}

View 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();
}
}
}