Added helpers unit test

main
Igor Scheller 7 years ago
parent c8d32d23e1
commit 86c0713baa

@ -8,6 +8,7 @@ use Engelsystem\Http\Request;
use Engelsystem\Logger\EngelsystemLogger; use Engelsystem\Logger\EngelsystemLogger;
use Engelsystem\Renderer\HtmlEngine; use Engelsystem\Renderer\HtmlEngine;
use Engelsystem\Renderer\Renderer; use Engelsystem\Renderer\Renderer;
use Engelsystem\Routing\UrlGenerator;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Session\Session; use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
@ -59,6 +60,13 @@ if ($config->get('maintenance')) {
} }
/**
* Register UrlGenerator
*/
$urlGenerator = new UrlGenerator();
$app->instance('routing.urlGenerator', $urlGenerator);
/** /**
* Initialize renderer * Initialize renderer
*/ */

@ -9,7 +9,7 @@ class UrlGenerator
* @param array $parameters * @param array $parameters
* @return string * @return string
*/ */
public static function to($path, $parameters = []) public function to($path, $parameters = [])
{ {
$path = '/' . ltrim($path, '/'); $path = '/' . ltrim($path, '/');
$request = app('request'); $request = app('request');

@ -69,7 +69,7 @@ function request($key = null, $default = null)
*/ */
function session($key = null, $default = null) function session($key = null, $default = null)
{ {
$session = request()->getSession(); $session = app('session');
if (is_null($key)) { if (is_null($key)) {
return $session; return $session;
@ -97,9 +97,15 @@ function view($template = null, $data = null)
/** /**
* @param string $path * @param string $path
* @param array $parameters * @param array $parameters
* @return string * @return UrlGenerator|string
*/ */
function url($path, $parameters = []) function url($path = null, $parameters = [])
{ {
return UrlGenerator::to($path, $parameters); $urlGenerator = app('routing.urlGenerator');
if (is_null($path)) {
return $urlGenerator;
}
return $urlGenerator->to($path, $parameters);
} }

@ -0,0 +1,152 @@
<?php
namespace Engelsystem\Test\Config;
use Engelsystem\Application;
use Engelsystem\Config\Config;
use Engelsystem\Container\Container;
use Engelsystem\Http\Request;
use Engelsystem\Renderer\Renderer;
use Engelsystem\Routing\UrlGenerator;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Session\Session;
class HelpersTest extends TestCase
{
/**
* @covers \app
*/
public function testApp()
{
$class = new class
{
};
$appMock = $this->getAppMock('some.name', $class);
$this->assertEquals($appMock, app());
$this->assertEquals($class, app('some.name'));
}
/**
* @covers \config
*/
public function testConfig()
{
$configMock = $this->getMockBuilder(Config::class)
->getMock();
$this->getAppMock('config', $configMock);
$this->assertEquals($configMock, config());
$configMock->expects($this->once())
->method('set')
->with(['foo' => 'bar']);
$this->assertTrue(config(['foo' => 'bar']));
$configMock->expects($this->once())
->method('get')
->with('mail')
->willReturn(['user' => 'FooBar']);
$this->assertEquals(['user' => 'FooBar'], config('mail'));
}
/**
* @covers \request
*/
public function testRequest()
{
$requestMock = $this->getMockBuilder(Request::class)
->getMock();
$this->getAppMock('request', $requestMock);
$this->assertEquals($requestMock, request());
$requestMock->expects($this->once())
->method('input')
->with('requestKey')
->willReturn('requestValue');
$this->assertEquals('requestValue', request('requestKey'));
}
/**
* @covers \session
*/
public function testSession()
{
$sessionMock = $this->getMockBuilder(Session::class)
->getMock();
$this->getAppMock('session', $sessionMock);
$this->assertEquals($sessionMock, session());
$sessionMock->expects($this->once())
->method('get')
->with('someKey')
->willReturn('someValue');
$this->assertEquals('someValue', session('someKey'));
}
/**
* @covers \view
*/
public function testView()
{
$rendererMock = $this->getMockBuilder(Renderer::class)
->getMock();
$this->getAppMock('renderer', $rendererMock);
$this->assertEquals($rendererMock, view());
$rendererMock->expects($this->once())
->method('render')
->with('template.name', ['template' => 'data'])
->willReturn('rendered template');
$this->assertEquals('rendered template', view('template.name', ['template' => 'data']));
}
/**
* @covers \url
*/
public function testUrl()
{
$urlGeneratorMock = $this->getMockBuilder(UrlGenerator::class)
->getMock();
$this->getAppMock('routing.urlGenerator', $urlGeneratorMock);
$this->assertEquals($urlGeneratorMock, url());
$urlGeneratorMock->expects($this->once())
->method('to')
->with('foo/bar', ['param' => 'value'])
->willReturn('http://lorem.ipsum/foo/bar?param=value');
$this->assertEquals('http://lorem.ipsum/foo/bar?param=value', url('foo/bar', ['param' => 'value']));
}
/**
* @param string $alias
* @param object $object
* @return Application|\PHPUnit_Framework_MockObject_MockObject
*/
protected function getAppMock($alias, $object)
{
$appMock = $this->getMockBuilder(Container::class)
->getMock();
$appMock->expects($this->atLeastOnce())
->method('get')
->with($alias)
->willReturn($object);
/** @var $appMock Application */
Application::setInstance($appMock);
return $appMock;
}
}

@ -32,6 +32,7 @@ class UrlGeneratorTest extends TestCase
public function testTo($urlToPath, $path, $willReturn, $arguments, $expectedUrl) public function testTo($urlToPath, $path, $willReturn, $arguments, $expectedUrl)
{ {
$app = new Container(); $app = new Container();
$urlGenerator = new UrlGenerator();
Application::setInstance($app); Application::setInstance($app);
$request = $this->getMockBuilder(Request::class) $request = $this->getMockBuilder(Request::class)
@ -44,7 +45,7 @@ class UrlGeneratorTest extends TestCase
$app->instance('request', $request); $app->instance('request', $request);
$url = UrlGenerator::to($urlToPath, $arguments); $url = $urlGenerator->to($urlToPath, $arguments);
$this->assertEquals($expectedUrl, $url); $this->assertEquals($expectedUrl, $url);
} }
} }

Loading…
Cancel
Save