From 5d752f6ee349cd693ce4b68d258367c5d979b8a4 Mon Sep 17 00:00:00 2001 From: Timothy J Warren Date: Mon, 7 Jan 2019 09:08:00 -0500 Subject: [PATCH] Small code cleanup --- src/Dispatcher.php | 9 ++++----- src/RoutingBase.php | 14 ++++++-------- tests/AnimeClientTestCase.php | 6 +++--- tests/RoutingBaseTest.php | 20 +++++++------------- 4 files changed, 20 insertions(+), 29 deletions(-) diff --git a/src/Dispatcher.php b/src/Dispatcher.php index f44e1465..faeb4df5 100644 --- a/src/Dispatcher.php +++ b/src/Dispatcher.php @@ -45,10 +45,10 @@ final class Dispatcher extends RoutingBase { protected $matcher; /** - * Class wrapper for input superglobals - * @var \Psr\Http\Message\ServerRequestInterface + * Routing array + * @var array */ - protected $request; + protected $routes; /** * Routes added to router @@ -67,8 +67,7 @@ final class Dispatcher extends RoutingBase { $router = $this->container->get('aura-router'); $this->router = $router->getMap(); $this->matcher = $router->getMatcher(); - $this->request = $container->get('request'); - + $this->routes = $this->config->get('routes'); $this->outputRoutes = $this->setupRoutes(); } diff --git a/src/RoutingBase.php b/src/RoutingBase.php index 55d9db64..90a6424c 100644 --- a/src/RoutingBase.php +++ b/src/RoutingBase.php @@ -39,10 +39,10 @@ class RoutingBase { protected $config; /** - * Routing array - * @var array + * Class wrapper for input superglobals + * @var \Psr\Http\Message\ServerRequestInterface */ - protected $routes; + protected $request; /** * Constructor @@ -56,7 +56,7 @@ class RoutingBase { { $this->container = $container; $this->config = $container->get('config'); - $this->routes = $this->config->get('routes'); + $this->request = $container->get('request'); } /** @@ -67,8 +67,7 @@ class RoutingBase { */ public function path(): string { - $request = $this->container->get('request'); - $path = $request->getUri()->getPath(); + $path = $this->request->getUri()->getPath(); $cleanedPath = $this->string($path) ->replace('%20', '') ->trim() @@ -116,5 +115,4 @@ class RoutingBase { $segments = $this->segments(); return end($segments); } -} -// End of RoutingBase.php \ No newline at end of file +} \ No newline at end of file diff --git a/tests/AnimeClientTestCase.php b/tests/AnimeClientTestCase.php index caeca83c..c7fa67dd 100644 --- a/tests/AnimeClientTestCase.php +++ b/tests/AnimeClientTestCase.php @@ -124,7 +124,7 @@ class AnimeClientTestCase extends TestCase { * @param array $supers * @return void */ - public function setSuperGlobals($supers = []) + public function setSuperGlobals($supers = []): void { $default = [ '_SERVER' => $_SERVER, @@ -139,7 +139,7 @@ class AnimeClientTestCase extends TestCase { array_merge($default, $supers) ); $this->container->setInstance('request', $request); - $this->container->set('repsone', function() { + $this->container->set('response', function() { return new HttpResponse(); }); } @@ -151,7 +151,7 @@ class AnimeClientTestCase extends TestCase { * * @return string - contents of the data file */ - public function getMockFile() + public function getMockFile(): string { $args = func_get_args(); array_unshift($args, TEST_DATA_DIR); diff --git a/tests/RoutingBaseTest.php b/tests/RoutingBaseTest.php index 9bf83ca9..8ac1bb34 100644 --- a/tests/RoutingBaseTest.php +++ b/tests/RoutingBaseTest.php @@ -19,14 +19,6 @@ namespace Aviat\AnimeClient\Tests; use Aviat\AnimeClient\RoutingBase; class RoutingBaseTest extends AnimeClientTestCase { - - protected $routingBase; - - public function setUp() - { - parent::setUp(); - $this->routingBase = new RoutingBase($this->container); - } public function dataSegments() { @@ -49,7 +41,7 @@ class RoutingBaseTest extends AnimeClientTestCase { /** * @dataProvider dataSegments */ - public function testSegments($requestUri, $path, $segments, $lastSegment) + public function testSegments(string $requestUri, string $path, array $segments, $lastSegment): void { $this->setSuperGlobals([ '_SERVER' => [ @@ -57,13 +49,15 @@ class RoutingBaseTest extends AnimeClientTestCase { ] ]); - $this->assertEquals($path, $this->routingBase->path(), "Path is invalid"); - $this->assertEquals($segments, $this->routingBase->segments(), "Segments array is invalid"); - $this->assertEquals($lastSegment, $this->routingBase->lastSegment(), "Last segment is invalid"); + $routingBase = new RoutingBase($this->container); + + $this->assertEquals($path, $routingBase->path(), "Path is invalid"); + $this->assertEquals($segments, $routingBase->segments(), "Segments array is invalid"); + $this->assertEquals($lastSegment, $routingBase->lastSegment(), "Last segment is invalid"); foreach($segments as $i => $value) { - $this->assertEquals($value, $this->routingBase->getSegment($i), "Segment {$i} is invalid"); + $this->assertEquals($value, $routingBase->getSegment($i), "Segment {$i} is invalid"); } } } \ No newline at end of file