diff options
Diffstat (limited to 'tests/Unit')
-rw-r--r-- | tests/Unit/ApplicationTest.php | 2 | ||||
-rw-r--r-- | tests/Unit/Config/ConfigServiceProviderTest.php | 38 | ||||
-rw-r--r-- | tests/Unit/Exceptions/HandlerTest.php | 4 | ||||
-rw-r--r-- | tests/Unit/HelpersTest.php | 5 | ||||
-rw-r--r-- | tests/Unit/Http/LegacyUrlGeneratorTest.php | 54 | ||||
-rw-r--r-- | tests/Unit/Http/RequestServiceProviderTest.php | 47 | ||||
-rw-r--r-- | tests/Unit/Http/UrlGeneratorServiceProviderTest.php | 2 | ||||
-rw-r--r-- | tests/Unit/Http/UrlGeneratorTest.php | 2 | ||||
-rw-r--r-- | tests/Unit/Middleware/CallableHandlerTest.php | 2 | ||||
-rw-r--r-- | tests/Unit/Middleware/RequestHandlerTest.php | 2 | ||||
-rw-r--r-- | tests/Unit/Middleware/RouteDispatcherTest.php | 10 | ||||
-rw-r--r-- | tests/Unit/Renderer/Twig/Extensions/AssetsTest.php | 47 |
12 files changed, 194 insertions, 21 deletions
diff --git a/tests/Unit/ApplicationTest.php b/tests/Unit/ApplicationTest.php index 012226b2..e6d77c7d 100644 --- a/tests/Unit/ApplicationTest.php +++ b/tests/Unit/ApplicationTest.php @@ -46,8 +46,10 @@ class ApplicationTest extends TestCase $app->setAppPath('.'); $this->assertTrue($app->has('path')); + $this->assertTrue($app->has('path.assets')); $this->assertTrue($app->has('path.config')); $this->assertTrue($app->has('path.lang')); + $this->assertTrue($app->has('path.resources')); $this->assertTrue($app->has('path.views')); $this->assertEquals(realpath('.'), $app->path()); diff --git a/tests/Unit/Config/ConfigServiceProviderTest.php b/tests/Unit/Config/ConfigServiceProviderTest.php index 2e37d0da..925854be 100644 --- a/tests/Unit/Config/ConfigServiceProviderTest.php +++ b/tests/Unit/Config/ConfigServiceProviderTest.php @@ -6,6 +6,7 @@ use Engelsystem\Application; use Engelsystem\Config\Config; use Engelsystem\Config\ConfigServiceProvider; use Engelsystem\Test\Unit\ServiceProviderTest; +use Exception; use PHPUnit_Framework_MockObject_MockObject; class ConfigServiceProviderTest extends ServiceProviderTest @@ -32,12 +33,15 @@ class ConfigServiceProviderTest extends ServiceProviderTest ); $this->setExpects($config, 'set', null, null, $this->exactly(2)); - $this->setExpects($config, 'get', [null], []); + $config->expects($this->exactly(3)) + ->method('get') + ->with(null) + ->willReturnOnConsecutiveCalls([], [], ['lor' => 'em']); $configFile = __DIR__ . '/../../../config/config.php'; $configExists = file_exists($configFile); if (!$configExists) { - file_put_contents($configFile, '<?php return [];'); + file_put_contents($configFile, '<?php return ["lor"=>"em"];'); } $serviceProvider = new ConfigServiceProvider($app); @@ -47,4 +51,34 @@ class ConfigServiceProviderTest extends ServiceProviderTest unlink($configFile); } } + + /** + * @covers \Engelsystem\Config\ConfigServiceProvider::register() + */ + public function testRegisterException() + { + /** @var PHPUnit_Framework_MockObject_MockObject|Config $config */ + $config = $this->getMockBuilder(Config::class) + ->getMock(); + + $app = $this->getApp(['make', 'instance', 'get']); + Application::setInstance($app); + + $this->setExpects($app, 'make', [Config::class], $config); + $app->expects($this->exactly(2)) + ->method('instance') + ->withConsecutive( + [Config::class, $config], + ['config', $config] + ); + $this->setExpects($app, 'get', ['path.config'], __DIR__ . '/not_existing', $this->atLeastOnce()); + + $this->setExpects($config, 'set', null, null, $this->never()); + $this->setExpects($config, 'get', [null], []); + + $this->expectException(Exception::class); + + $serviceProvider = new ConfigServiceProvider($app); + $serviceProvider->register(); + } } diff --git a/tests/Unit/Exceptions/HandlerTest.php b/tests/Unit/Exceptions/HandlerTest.php index 7987f9d6..58d25de3 100644 --- a/tests/Unit/Exceptions/HandlerTest.php +++ b/tests/Unit/Exceptions/HandlerTest.php @@ -65,10 +65,10 @@ class HandlerTest extends TestCase /** @var Handler|Mock $handler */ $handler = $this->getMockBuilder(Handler::class) - ->setMethods(['die']) + ->setMethods(['terminateApplicationImmediately']) ->getMock(); $handler->expects($this->once()) - ->method('die'); + ->method('terminateApplicationImmediately'); $handler->setHandler(Handler::ENV_PRODUCTION, $handlerMock); diff --git a/tests/Unit/HelpersTest.php b/tests/Unit/HelpersTest.php index 20d3c2de..b9cedd30 100644 --- a/tests/Unit/HelpersTest.php +++ b/tests/Unit/HelpersTest.php @@ -8,8 +8,8 @@ use Engelsystem\Container\Container; use Engelsystem\Helpers\Translator; use Engelsystem\Http\Request; use Engelsystem\Http\Response; -use Engelsystem\Http\UrlGenerator; use Engelsystem\Renderer\Renderer; +use Engelsystem\Http\UrlGeneratorInterface; use PHPUnit\Framework\TestCase; use PHPUnit_Framework_MockObject_MockObject as MockObject; use Symfony\Component\HttpFoundation\Session\Session; @@ -223,8 +223,7 @@ class HelpersTest extends TestCase */ public function testUrl() { - $urlGeneratorMock = $this->getMockBuilder(UrlGenerator::class) - ->getMock(); + $urlGeneratorMock = $this->getMockForAbstractClass(UrlGeneratorInterface::class); $this->getAppMock('http.urlGenerator', $urlGeneratorMock); $this->assertEquals($urlGeneratorMock, url()); diff --git a/tests/Unit/Http/LegacyUrlGeneratorTest.php b/tests/Unit/Http/LegacyUrlGeneratorTest.php new file mode 100644 index 00000000..2c087f8f --- /dev/null +++ b/tests/Unit/Http/LegacyUrlGeneratorTest.php @@ -0,0 +1,54 @@ +<?php + +namespace Engelsystem\Test\Unit\Http; + +use Engelsystem\Application; +use Engelsystem\Container\Container; +use Engelsystem\Http\Request; +use Engelsystem\Http\LegacyUrlGenerator; +use Engelsystem\Http\UrlGeneratorInterface; +use PHPUnit\Framework\TestCase; + +class LegacyUrlGeneratorTest extends TestCase +{ + public function provideLinksTo() + { + return [ + ['/', 'http://foo.bar/index.php', [], 'http://foo.bar/'], + ['/foo-path', 'http://foo.bar/index.php/index.php', [], 'http://foo.bar/index.php?p=foo_path'], + ['/foo', 'http://foo.bar/index.php/index.php', [], 'http://foo.bar/index.php?p=foo'], + ['foo', 'http://foo.bar/index.php', ['test' => 'abc'], 'http://foo.bar/index.php?p=foo&test=abc'], + ]; + } + + /** + * @dataProvider provideLinksTo + * @covers \Engelsystem\Http\LegacyUrlGenerator::to + * + * @param string $urlToPath + * @param string $willReturn + * @param string[] $arguments + * @param string $expectedUrl + */ + public function testTo($urlToPath, $willReturn, $arguments, $expectedUrl) + { + $app = new Container(); + Application::setInstance($app); + + $request = $this->getMockBuilder(Request::class) + ->getMock(); + + $request->expects($this->once()) + ->method('getUriForPath') + ->with('/index.php') + ->willReturn($willReturn); + + $app->instance('request', $request); + + $urlGenerator = new LegacyUrlGenerator(); + $this->assertInstanceOf(UrlGeneratorInterface::class, $urlGenerator); + + $url = $urlGenerator->to($urlToPath, $arguments); + $this->assertEquals($expectedUrl, $url); + } +} diff --git a/tests/Unit/Http/RequestServiceProviderTest.php b/tests/Unit/Http/RequestServiceProviderTest.php index 1a42ae86..4e9bb4e0 100644 --- a/tests/Unit/Http/RequestServiceProviderTest.php +++ b/tests/Unit/Http/RequestServiceProviderTest.php @@ -2,6 +2,8 @@ namespace Engelsystem\Test\Unit\Http; +use Engelsystem\Config\Config; +use Engelsystem\Container\ServiceProvider; use Engelsystem\Http\Request; use Engelsystem\Http\RequestServiceProvider; use Engelsystem\Test\Unit\ServiceProviderTest; @@ -11,17 +13,41 @@ use Symfony\Component\HttpFoundation\Request as SymfonyRequest; class RequestServiceProviderTest extends ServiceProviderTest { /** - * @covers \Engelsystem\Http\RequestServiceProvider::register() + * @return array */ - public function testRegister() + public function provideRegister() { - /** @var MockObject|Request $request */ - $request = $this->getMockBuilder(Request::class) - ->getMock(); + return [ + ['', []], + [[], []], + ['192.168.10.99', ['192.168.10.99']], + [' 234.234.234.234 ', ['234.234.234.234']], + ['123.234.123.234,10.0.0.0/8', ['123.234.123.234', '10.0.0.0/8']], + ['123.123.234.234 , ' . PHP_EOL . ' 11.22.33.44/22 ', ['123.123.234.234', '11.22.33.44/22']], + [['10.100.20.0/24'], ['10.100.20.0/24']], + ]; + } + + /** + * @dataProvider provideRegister + * @covers \Engelsystem\Http\RequestServiceProvider::register() + * + * @param string|array $configuredProxies + * @param array $trustedProxies + */ + public function testRegister($configuredProxies, $trustedProxies) + { + /** @var Config|MockObject $config */ + $config = $this->getMockBuilder(Config::class)->getMock(); + /** @var Request|MockObject $request */ + $request = $this->getMockBuilder(Request::class)->getMock(); - $app = $this->getApp(['call', 'instance']); + $app = $this->getApp(['call', 'get', 'instance']); $this->setExpects($app, 'call', [[Request::class, 'createFromGlobals']], $request); + $this->setExpects($app, 'get', ['config'], $config); + $this->setExpects($config, 'get', ['trusted_proxies'], $configuredProxies); + $app->expects($this->exactly(3)) ->method('instance') ->withConsecutive( @@ -30,7 +56,14 @@ class RequestServiceProviderTest extends ServiceProviderTest ['request', $request] ); - $serviceProvider = new RequestServiceProvider($app); + /** @var ServiceProvider|MockObject $serviceProvider */ + $serviceProvider = $this->getMockBuilder(RequestServiceProvider::class) + ->setConstructorArgs([$app]) + ->setMethods(['setTrustedProxies']) + ->getMock(); + $serviceProvider->expects($this->once()) + ->method('setTrustedProxies') + ->with($request, $trustedProxies); $serviceProvider->register(); } } diff --git a/tests/Unit/Http/UrlGeneratorServiceProviderTest.php b/tests/Unit/Http/UrlGeneratorServiceProviderTest.php index 787789fe..720af631 100644 --- a/tests/Unit/Http/UrlGeneratorServiceProviderTest.php +++ b/tests/Unit/Http/UrlGeneratorServiceProviderTest.php @@ -1,6 +1,6 @@ <?php -namespace Engelsystem\Test\Unit\Routing; +namespace Engelsystem\Test\Unit\Http; use Engelsystem\Http\UrlGenerator; use Engelsystem\Http\UrlGeneratorServiceProvider; diff --git a/tests/Unit/Http/UrlGeneratorTest.php b/tests/Unit/Http/UrlGeneratorTest.php index 89ffa7dd..fa2ec36e 100644 --- a/tests/Unit/Http/UrlGeneratorTest.php +++ b/tests/Unit/Http/UrlGeneratorTest.php @@ -1,6 +1,6 @@ <?php -namespace Engelsystem\Test\Unit\Routing; +namespace Engelsystem\Test\Unit\Http; use Engelsystem\Application; use Engelsystem\Container\Container; diff --git a/tests/Unit/Middleware/CallableHandlerTest.php b/tests/Unit/Middleware/CallableHandlerTest.php index 6e6dab58..29424480 100644 --- a/tests/Unit/Middleware/CallableHandlerTest.php +++ b/tests/Unit/Middleware/CallableHandlerTest.php @@ -136,6 +136,6 @@ class CallableHandlerTest extends TestCase $callable = $this->getMockBuilder(stdClass::class) ->setMethods(['__invoke']) ->getMock(); - return array($request, $response, $callable, $handler); + return [$request, $response, $callable, $handler]; } } diff --git a/tests/Unit/Middleware/RequestHandlerTest.php b/tests/Unit/Middleware/RequestHandlerTest.php index b1ffbd33..cb5fc4a6 100644 --- a/tests/Unit/Middleware/RequestHandlerTest.php +++ b/tests/Unit/Middleware/RequestHandlerTest.php @@ -147,6 +147,6 @@ class RequestHandlerTest extends TestCase /** @var MiddlewareInterface $middlewareInterface */ $middlewareInterface = $this->getMockForAbstractClass(MiddlewareInterface::class); - return array($container, $request, $handler, $response, $middlewareInterface); + return [$container, $request, $handler, $response, $middlewareInterface]; } } diff --git a/tests/Unit/Middleware/RouteDispatcherTest.php b/tests/Unit/Middleware/RouteDispatcherTest.php index edb2f158..611d3b7c 100644 --- a/tests/Unit/Middleware/RouteDispatcherTest.php +++ b/tests/Unit/Middleware/RouteDispatcherTest.php @@ -2,6 +2,7 @@ namespace Engelsystem\Test\Unit\Middleware; +use Engelsystem\Http\Request; use Engelsystem\Middleware\RouteDispatcher; use FastRoute\Dispatcher as FastRouteDispatcher; use PHPUnit\Framework\MockObject\MockObject; @@ -127,7 +128,7 @@ class RouteDispatcherTest extends TestCase /** @var ResponseInterface|MockObject $response */ $response = $this->getMockForAbstractClass(ResponseInterface::class); /** @var ServerRequestInterface|MockObject $request */ - $request = $this->getMockForAbstractClass(ServerRequestInterface::class); + $request = $this->createMock(Request::class); /** @var RequestHandlerInterface|MockObject $handler */ $handler = $this->getMockForAbstractClass(RequestHandlerInterface::class); /** @var UriInterface|MockObject $uriInterface */ @@ -139,10 +140,13 @@ class RouteDispatcherTest extends TestCase $request->expects($this->atLeastOnce()) ->method('getUri') ->willReturn($uriInterface); + $request->expects($this->atLeastOnce()) + ->method('getPathInfo') + ->willReturn('/foo%21bar'); $uriInterface->expects($this->atLeastOnce()) ->method('getPath') - ->willReturn('/foo%21bar'); + ->willReturn('/lorem/foo%21bar'); - return array($dispatcher, $response, $request, $handler); + return [$dispatcher, $response, $request, $handler]; } } diff --git a/tests/Unit/Renderer/Twig/Extensions/AssetsTest.php b/tests/Unit/Renderer/Twig/Extensions/AssetsTest.php new file mode 100644 index 00000000..2d7ba6cd --- /dev/null +++ b/tests/Unit/Renderer/Twig/Extensions/AssetsTest.php @@ -0,0 +1,47 @@ +<?php + +namespace Engelsystem\Test\Unit\Renderer\Twig\Extensions; + +use Engelsystem\Http\UrlGenerator; +use Engelsystem\Renderer\Twig\Extensions\Assets; +use PHPUnit\Framework\MockObject\MockObject; + +class AssetsTest extends ExtensionTest +{ + /** + * @covers \Engelsystem\Renderer\Twig\Extensions\Assets::__construct + * @covers \Engelsystem\Renderer\Twig\Extensions\Assets::getFunctions + */ + public function testGetGlobals() + { + /** @var UrlGenerator|MockObject $urlGenerator */ + $urlGenerator = $this->createMock(UrlGenerator::class); + + $extension = new Assets($urlGenerator); + $functions = $extension->getFunctions(); + + $this->assertExtensionExists('asset', [$extension, 'getAsset'], $functions); + } + + /** + * @covers \Engelsystem\Renderer\Twig\Extensions\Assets::getAsset + */ + public function testGetAsset() + { + /** @var UrlGenerator|MockObject $urlGenerator */ + $urlGenerator = $this->createMock(UrlGenerator::class); + + $urlGenerator->expects($this->exactly(2)) + ->method('to') + ->with('/assets/foo.css') + ->willReturn('https://foo.bar/project/assets/foo.css'); + + $extension = new Assets($urlGenerator); + + $return = $extension->getAsset('assets/foo.css'); + $this->assertEquals('https://foo.bar/project/assets/foo.css', $return); + + $return = $extension->getAsset('/assets/foo.css'); + $this->assertEquals('https://foo.bar/project/assets/foo.css', $return); + } +} |