diff options
Diffstat (limited to 'tests/Unit')
-rw-r--r-- | tests/Unit/Config/ConfigServiceProviderTest.php | 33 | ||||
-rw-r--r-- | tests/Unit/Exceptions/HandlerTest.php | 4 | ||||
-rw-r--r-- | tests/Unit/HelpersTest.php | 7 | ||||
-rw-r--r-- | tests/Unit/Http/RequestServiceProviderTest.php | 46 | ||||
-rw-r--r-- | tests/Unit/Routing/LegacyUrlGeneratorTest.php | 54 | ||||
-rw-r--r-- | tests/Unit/Routing/RoutingServiceProviderTest.php | 49 | ||||
-rw-r--r-- | tests/Unit/Routing/UrlGeneratorTest.php | 12 |
7 files changed, 179 insertions, 26 deletions
diff --git a/tests/Unit/Config/ConfigServiceProviderTest.php b/tests/Unit/Config/ConfigServiceProviderTest.php index c8be4b7d..998c0ba1 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 @@ -27,12 +28,15 @@ class ConfigServiceProviderTest extends ServiceProviderTest $this->setExpects($app, 'get', ['path.config'], __DIR__ . '/../../../config', $this->atLeastOnce()); $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); @@ -42,4 +46,29 @@ 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); + $this->setExpects($app, 'instance', ['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 40202be8..5a6ffe16 100644 --- a/tests/Unit/Exceptions/HandlerTest.php +++ b/tests/Unit/Exceptions/HandlerTest.php @@ -61,10 +61,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 43c29c84..9514ad8c 100644 --- a/tests/Unit/HelpersTest.php +++ b/tests/Unit/HelpersTest.php @@ -7,7 +7,7 @@ use Engelsystem\Config\Config; use Engelsystem\Container\Container; use Engelsystem\Http\Request; use Engelsystem\Renderer\Renderer; -use Engelsystem\Routing\UrlGenerator; +use Engelsystem\Routing\UrlGeneratorInterface; use PHPUnit\Framework\TestCase; use PHPUnit_Framework_MockObject_MockObject as MockObject; use Symfony\Component\HttpFoundation\Session\Session; @@ -171,14 +171,13 @@ class HelpersTest extends TestCase */ public function testUrl() { - $urlGeneratorMock = $this->getMockBuilder(UrlGenerator::class) - ->getMock(); + $urlGeneratorMock = $this->getMockForAbstractClass(UrlGeneratorInterface::class); $this->getAppMock('routing.urlGenerator', $urlGeneratorMock); $this->assertEquals($urlGeneratorMock, url()); $urlGeneratorMock->expects($this->once()) - ->method('to') + ->method('linkTo') ->with('foo/bar', ['param' => 'value']) ->willReturn('http://lorem.ipsum/foo/bar?param=value'); diff --git a/tests/Unit/Http/RequestServiceProviderTest.php b/tests/Unit/Http/RequestServiceProviderTest.php index a137b0ac..eddf7ee5 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; @@ -10,20 +12,50 @@ use PHPUnit_Framework_MockObject_MockObject as MockObject; 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']], + ]; + } - $app = $this->getApp(['call', 'instance']); + /** + * @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', 'get', 'instance']); $this->setExpects($app, 'call', [[Request::class, 'createFromGlobals']], $request); + $this->setExpects($app, 'get', ['config'], $config); $this->setExpects($app, 'instance', ['request', $request]); + $this->setExpects($config, 'get', ['trusted_proxies'], $configuredProxies); - $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/Routing/LegacyUrlGeneratorTest.php b/tests/Unit/Routing/LegacyUrlGeneratorTest.php new file mode 100644 index 00000000..3d42afbd --- /dev/null +++ b/tests/Unit/Routing/LegacyUrlGeneratorTest.php @@ -0,0 +1,54 @@ +<?php + +namespace Engelsystem\Test\Unit\Routing; + +use Engelsystem\Application; +use Engelsystem\Container\Container; +use Engelsystem\Http\Request; +use Engelsystem\Routing\LegacyUrlGenerator; +use Engelsystem\Routing\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\Routing\LegacyUrlGenerator::linkTo + * + * @param string $urlToPath + * @param string $willReturn + * @param string[] $arguments + * @param string $expectedUrl + */ + public function testLinkTo($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->linkTo($urlToPath, $arguments); + $this->assertEquals($expectedUrl, $url); + } +} diff --git a/tests/Unit/Routing/RoutingServiceProviderTest.php b/tests/Unit/Routing/RoutingServiceProviderTest.php index dd9441eb..ce3d7290 100644 --- a/tests/Unit/Routing/RoutingServiceProviderTest.php +++ b/tests/Unit/Routing/RoutingServiceProviderTest.php @@ -2,10 +2,13 @@ namespace Engelsystem\Test\Unit\Routing; +use Engelsystem\Config\Config; +use Engelsystem\Routing\LegacyUrlGenerator; use Engelsystem\Routing\RoutingServiceProvider; use Engelsystem\Routing\UrlGenerator; +use Engelsystem\Routing\UrlGeneratorInterface; use Engelsystem\Test\Unit\ServiceProviderTest; -use PHPUnit_Framework_MockObject_MockObject; +use PHPUnit_Framework_MockObject_MockObject as MockObject; class RoutingServiceProviderTest extends ServiceProviderTest { @@ -14,16 +17,48 @@ class RoutingServiceProviderTest extends ServiceProviderTest */ public function testRegister() { - /** @var PHPUnit_Framework_MockObject_MockObject|UrlGenerator $urlGenerator */ - $urlGenerator = $this->getMockBuilder(UrlGenerator::class) - ->getMock(); + $app = $this->getApp(['make', 'instance', 'bind', 'get']); + /** @var MockObject|Config $config */ + $config = $this->getMockBuilder(Config::class)->getMock(); + /** @var MockObject|UrlGeneratorInterface $urlGenerator */ + $urlGenerator = $this->getMockForAbstractClass(UrlGeneratorInterface::class); + /** @var MockObject|UrlGeneratorInterface $legacyUrlGenerator */ + $legacyUrlGenerator = $this->getMockForAbstractClass(UrlGeneratorInterface::class); - $app = $this->getApp(); + $config->expects($this->atLeastOnce()) + ->method('get') + ->with('rewrite_urls') + ->willReturnOnConsecutiveCalls( + true, + false + ); - $this->setExpects($app, 'make', [UrlGenerator::class], $urlGenerator); - $this->setExpects($app, 'instance', ['routing.urlGenerator', $urlGenerator]); + $this->setExpects($app, 'get', ['config'], $config, $this->atLeastOnce()); + + $app->expects($this->atLeastOnce()) + ->method('make') + ->withConsecutive( + [UrlGenerator::class], + [LegacyUrlGenerator::class] + ) + ->willReturnOnConsecutiveCalls( + $urlGenerator, + $legacyUrlGenerator + ); + $app->expects($this->atLeastOnce()) + ->method('instance') + ->withConsecutive( + ['routing.urlGenerator', $urlGenerator], + ['routing.urlGenerator', $legacyUrlGenerator] + ); + $this->setExpects( + $app, 'bind', + [UrlGeneratorInterface::class, 'routing.urlGenerator'], null, + $this->atLeastOnce() + ); $serviceProvider = new RoutingServiceProvider($app); $serviceProvider->register(); + $serviceProvider->register(); } } diff --git a/tests/Unit/Routing/UrlGeneratorTest.php b/tests/Unit/Routing/UrlGeneratorTest.php index 6da59a4f..e128bfe7 100644 --- a/tests/Unit/Routing/UrlGeneratorTest.php +++ b/tests/Unit/Routing/UrlGeneratorTest.php @@ -6,6 +6,7 @@ use Engelsystem\Application; use Engelsystem\Container\Container; use Engelsystem\Http\Request; use Engelsystem\Routing\UrlGenerator; +use Engelsystem\Routing\UrlGeneratorInterface; use PHPUnit\Framework\TestCase; class UrlGeneratorTest extends TestCase @@ -13,6 +14,7 @@ class UrlGeneratorTest extends TestCase public function provideLinksTo() { return [ + ['/', '/', 'http://foo.bar/', [], 'http://foo.bar/'], ['/foo/path', '/foo/path', 'http://foo.bar/foo/path', [], 'http://foo.bar/foo/path'], ['foo', '/foo', 'https://foo.bar/foo', [], 'https://foo.bar/foo'], ['foo', '/foo', 'http://f.b/foo', ['test' => 'abc', 'bla' => 'foo'], 'http://f.b/foo?test=abc&bla=foo'], @@ -21,7 +23,7 @@ class UrlGeneratorTest extends TestCase /** * @dataProvider provideLinksTo - * @covers \Engelsystem\Routing\UrlGenerator::to + * @covers \Engelsystem\Routing\UrlGenerator::linkTo * * @param string $path * @param string $willReturn @@ -29,10 +31,9 @@ class UrlGeneratorTest extends TestCase * @param string[] $arguments * @param string $expectedUrl */ - public function testTo($urlToPath, $path, $willReturn, $arguments, $expectedUrl) + public function testLinkTo($urlToPath, $path, $willReturn, $arguments, $expectedUrl) { $app = new Container(); - $urlGenerator = new UrlGenerator(); Application::setInstance($app); $request = $this->getMockBuilder(Request::class) @@ -45,7 +46,10 @@ class UrlGeneratorTest extends TestCase $app->instance('request', $request); - $url = $urlGenerator->to($urlToPath, $arguments); + $urlGenerator = new UrlGenerator(); + $this->assertInstanceOf(UrlGeneratorInterface::class, $urlGenerator); + + $url = $urlGenerator->linkTo($urlToPath, $arguments); $this->assertEquals($expectedUrl, $url); } } |