Added helpers unit test

This commit is contained in:
Igor Scheller 2017-09-20 01:09:11 +02:00
parent c8d32d23e1
commit 86c0713baa
5 changed files with 173 additions and 6 deletions

View File

@ -8,6 +8,7 @@ use Engelsystem\Http\Request;
use Engelsystem\Logger\EngelsystemLogger;
use Engelsystem\Renderer\HtmlEngine;
use Engelsystem\Renderer\Renderer;
use Engelsystem\Routing\UrlGenerator;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Session\Session;
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
*/

View File

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

View File

@ -69,7 +69,7 @@ function request($key = null, $default = null)
*/
function session($key = null, $default = null)
{
$session = request()->getSession();
$session = app('session');
if (is_null($key)) {
return $session;
@ -97,9 +97,15 @@ function view($template = null, $data = null)
/**
* @param string $path
* @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);
}

152
tests/Unit/HelpersTest.php Normal file
View File

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

View File

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