2017-09-20 01:09:11 +02:00
|
|
|
<?php
|
|
|
|
|
2017-10-31 14:23:23 +01:00
|
|
|
namespace Engelsystem\Test\Unit;
|
2017-09-20 01:09:11 +02:00
|
|
|
|
|
|
|
use Engelsystem\Application;
|
|
|
|
use Engelsystem\Config\Config;
|
|
|
|
use Engelsystem\Container\Container;
|
2018-10-08 21:15:56 +02:00
|
|
|
use Engelsystem\Helpers\Authenticator;
|
2018-08-28 22:23:59 +02:00
|
|
|
use Engelsystem\Helpers\Translator;
|
2017-09-20 01:09:11 +02:00
|
|
|
use Engelsystem\Http\Request;
|
2018-08-11 15:05:55 +02:00
|
|
|
use Engelsystem\Http\Response;
|
2018-09-04 18:35:13 +02:00
|
|
|
use Engelsystem\Http\UrlGeneratorInterface;
|
2018-10-08 21:15:56 +02:00
|
|
|
use Engelsystem\Renderer\Renderer;
|
2019-04-24 10:45:00 +02:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2017-09-20 01:09:11 +02:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Symfony\Component\HttpFoundation\Session\Session;
|
2017-11-12 15:38:12 +01:00
|
|
|
use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface as StorageInterface;
|
2017-09-20 01:09:11 +02:00
|
|
|
|
|
|
|
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'));
|
|
|
|
}
|
|
|
|
|
2018-10-08 21:15:56 +02:00
|
|
|
/**
|
|
|
|
* @covers \auth
|
|
|
|
*/
|
|
|
|
public function testAuth()
|
|
|
|
{
|
|
|
|
/** @var Application|MockObject $app */
|
|
|
|
$app = $this->createMock(Container::class);
|
|
|
|
Application::setInstance($app);
|
|
|
|
/** @var Authenticator|MockObject $auth */
|
|
|
|
$auth = $this->getMockBuilder(Authenticator::class)
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$app->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with('authenticator')
|
|
|
|
->willReturn($auth);
|
|
|
|
|
|
|
|
$this->assertEquals($auth, auth());
|
|
|
|
}
|
|
|
|
|
2017-10-31 14:23:23 +01:00
|
|
|
/**
|
2019-05-31 17:09:50 +02:00
|
|
|
* @covers \base_path
|
2017-10-31 14:23:23 +01:00
|
|
|
*/
|
|
|
|
public function testBasePath()
|
|
|
|
{
|
2019-04-24 10:45:00 +02:00
|
|
|
/** @var Application|MockObject $app */
|
2017-10-31 14:23:23 +01:00
|
|
|
$app = $this->getMockBuilder(Container::class)
|
|
|
|
->getMock();
|
|
|
|
Application::setInstance($app);
|
|
|
|
|
|
|
|
$app->expects($this->atLeastOnce())
|
|
|
|
->method('get')
|
|
|
|
->with('path')
|
|
|
|
->willReturn('/foo/bar');
|
|
|
|
|
|
|
|
$this->assertEquals('/foo/bar', base_path());
|
|
|
|
$this->assertEquals('/foo/bar/bla-foo.conf', base_path('bla-foo.conf'));
|
|
|
|
}
|
|
|
|
|
2017-09-20 01:09:11 +02:00
|
|
|
/**
|
|
|
|
* @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'));
|
|
|
|
}
|
|
|
|
|
2017-10-31 14:23:23 +01:00
|
|
|
/**
|
2019-05-31 17:09:50 +02:00
|
|
|
* @covers \config_path
|
2017-10-31 14:23:23 +01:00
|
|
|
*/
|
|
|
|
public function testConfigPath()
|
|
|
|
{
|
2019-04-24 10:45:00 +02:00
|
|
|
/** @var Application|MockObject $app */
|
2017-10-31 14:23:23 +01:00
|
|
|
$app = $this->getMockBuilder(Container::class)
|
|
|
|
->getMock();
|
|
|
|
Application::setInstance($app);
|
|
|
|
|
|
|
|
$app->expects($this->atLeastOnce())
|
|
|
|
->method('get')
|
|
|
|
->with('path.config')
|
|
|
|
->willReturn('/foo/conf');
|
|
|
|
|
|
|
|
$this->assertEquals('/foo/conf', config_path());
|
|
|
|
$this->assertEquals('/foo/conf/bar.php', config_path('bar.php'));
|
|
|
|
}
|
|
|
|
|
2017-09-20 01:09:11 +02:00
|
|
|
/**
|
|
|
|
* @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'));
|
|
|
|
}
|
|
|
|
|
2018-08-11 15:05:55 +02:00
|
|
|
/**
|
|
|
|
* @covers \response
|
|
|
|
*/
|
|
|
|
public function testResponse()
|
|
|
|
{
|
2019-04-24 10:45:00 +02:00
|
|
|
/** @var Response|MockObject $response */
|
2018-08-11 15:05:55 +02:00
|
|
|
$response = $this->getMockBuilder(Response::class)->getMock();
|
|
|
|
$this->getAppMock('psr7.response', $response);
|
|
|
|
|
|
|
|
$response->expects($this->once())
|
|
|
|
->method('withContent')
|
|
|
|
->with('Lorem Ipsum?')
|
|
|
|
->willReturn($response);
|
|
|
|
|
|
|
|
$response->expects($this->once())
|
|
|
|
->method('withStatus')
|
|
|
|
->with(501)
|
|
|
|
->willReturn($response);
|
|
|
|
|
|
|
|
$response->expects($this->exactly(2))
|
|
|
|
->method('withAddedHeader')
|
|
|
|
->withConsecutive(['lor', 'em'], ['foo', 'bar'])
|
|
|
|
->willReturn($response);
|
|
|
|
|
2019-04-24 11:01:37 +02:00
|
|
|
$this->assertEquals($response, response('Lorem Ipsum?', 501, ['lor' => 'em', 'foo' => 'bar']));
|
2018-08-11 15:05:55 +02:00
|
|
|
}
|
|
|
|
|
2017-09-20 01:09:11 +02:00
|
|
|
/**
|
|
|
|
* @covers \session
|
|
|
|
*/
|
|
|
|
public function testSession()
|
|
|
|
{
|
2017-11-12 15:38:12 +01:00
|
|
|
$sessionStorage = $this->getMockForAbstractClass(StorageInterface::class);
|
2017-09-20 01:09:11 +02:00
|
|
|
$sessionMock = $this->getMockBuilder(Session::class)
|
2017-11-12 15:38:12 +01:00
|
|
|
->setConstructorArgs([$sessionStorage])
|
2017-09-20 01:09:11 +02:00
|
|
|
->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']));
|
|
|
|
}
|
|
|
|
|
2018-08-28 22:23:59 +02:00
|
|
|
/**
|
|
|
|
* @covers \__
|
|
|
|
* @covers \trans
|
|
|
|
*/
|
|
|
|
public function testTrans()
|
|
|
|
{
|
|
|
|
/** @var Translator|MockObject $translator */
|
|
|
|
$translator = $this->getMockBuilder(Translator::class)
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$this->getAppMock('translator', $translator);
|
|
|
|
|
|
|
|
$translator->expects($this->exactly(2))
|
|
|
|
->method('translate')
|
|
|
|
->with('Lorem %s Ipsum', ['foo'])
|
|
|
|
->willReturn('Lorem foo Ipsum');
|
|
|
|
|
|
|
|
$this->assertEquals($translator, trans());
|
|
|
|
$this->assertEquals('Lorem foo Ipsum', trans('Lorem %s Ipsum', ['foo']));
|
|
|
|
$this->assertEquals('Lorem foo Ipsum', __('Lorem %s Ipsum', ['foo']));
|
|
|
|
}
|
|
|
|
|
2018-10-18 23:34:18 +02:00
|
|
|
/**
|
|
|
|
* @covers \_e
|
|
|
|
*/
|
|
|
|
public function testTranslatePlural()
|
|
|
|
{
|
|
|
|
/** @var Translator|MockObject $translator */
|
|
|
|
$translator = $this->getMockBuilder(Translator::class)
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$this->getAppMock('translator', $translator);
|
|
|
|
|
|
|
|
$translator->expects($this->once())
|
|
|
|
->method('translatePlural')
|
|
|
|
->with('One: %u', 'Multiple: %u', 4, [4])
|
|
|
|
->willReturn('Multiple: 4');
|
|
|
|
|
|
|
|
$this->assertEquals('Multiple: 4', _e('One: %u', 'Multiple: %u', 4, [4]));
|
|
|
|
}
|
|
|
|
|
2017-09-20 01:09:11 +02:00
|
|
|
/**
|
|
|
|
* @covers \url
|
|
|
|
*/
|
|
|
|
public function testUrl()
|
|
|
|
{
|
2018-03-31 05:19:49 +02:00
|
|
|
$urlGeneratorMock = $this->getMockForAbstractClass(UrlGeneratorInterface::class);
|
2017-09-20 01:09:11 +02:00
|
|
|
|
2018-08-22 03:10:08 +02:00
|
|
|
$this->getAppMock('http.urlGenerator', $urlGeneratorMock);
|
2017-09-20 01:09:11 +02:00
|
|
|
$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
|
2017-10-31 14:23:23 +01:00
|
|
|
* @return Application|MockObject
|
2017-09-20 01:09:11 +02:00
|
|
|
*/
|
|
|
|
protected function getAppMock($alias, $object)
|
|
|
|
{
|
2019-04-24 11:01:37 +02:00
|
|
|
/** @var Application|MockObject $appMock */
|
2017-09-20 01:09:11 +02:00
|
|
|
$appMock = $this->getMockBuilder(Container::class)
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$appMock->expects($this->atLeastOnce())
|
|
|
|
->method('get')
|
|
|
|
->with($alias)
|
|
|
|
->willReturn($object);
|
|
|
|
|
|
|
|
Application::setInstance($appMock);
|
|
|
|
|
|
|
|
return $appMock;
|
|
|
|
}
|
|
|
|
}
|