OSDN Git Service

fix unit tests
authorfrostbane <frostbane@programmer.net>
Wed, 27 Nov 2019 09:19:19 +0000 (18:19 +0900)
committerfrostbane <frostbane@programmer.net>
Wed, 27 Nov 2019 09:19:19 +0000 (18:19 +0900)
failing tests:

 - set dynamic filter

phpunit.xml
src/Route.php
src/Router.php
tests/src/PHPRouterTest/RouterTest.php

index 75d6537..8e4a894 100644 (file)
@@ -7,7 +7,7 @@
          convertNoticesToExceptions="true"
          convertWarningsToExceptions="true"
          processIsolation="false"
-         stopOnFailure="true">
+         stopOnFailure="false">
     <testsuites>
         <testsuite name="PHP Router">
             <directory>tests</directory>
index d4e11fe..2cde91a 100755 (executable)
@@ -1,23 +1,4 @@
 <?php
-/**
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
- * CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
- * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
- * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
- * DAMAGE.
- *
- * This software consists of voluntary contributions made by many
- * individuals and is licensed under the MIT license.
- */
 
 namespace PHPRouter;
 
@@ -180,14 +161,19 @@ class Route
             return null;
         }
 
-        // todo use reflection
-        $instance = new $class();
-
         if (empty($action[1]) || trim($action[1]) === '') {
             $method = "__invoke";
         }
 
-        if ( !method_exists($instance, $method)) {
+        try {
+            $classRexl = new \ReflectionClass($class);
+        } catch (\ReflectionException $ex) {
+            return null;
+        }
+
+        try {
+            $classRexl->getMethod($method);
+        } catch (\ReflectionException $ex) {
             return null;
         }
 
@@ -197,7 +183,7 @@ class Route
     public function dispatch()
     {
         $action   = explode('::', $this->config['_controller']);
-        $instance = new $action[0];
+        $instance = new $action[0]();
 
         if ($this->parametersByName) {
             $this->parameters = array($this->parameters);
index 4963862..885c0af 100755 (executable)
@@ -1,23 +1,4 @@
 <?php
-/**
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
- * CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
- * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
- * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
- * DAMAGE.
- *
- * This software consists of voluntary contributions made by many
- * individuals and is licensed under the MIT license.
- */
 
 namespace PHPRouter;
 
@@ -134,7 +115,7 @@ class Router
 
         foreach ($this->routes->all() as $routes) {
             // compare server request method with route's allowed http methods
-            if ( !in_array($requestMethod, (array)$routes->getMethods(), true)) {
+            if (!in_array($requestMethod, (array)$routes->getMethods(), true)) {
                 continue;
             }
 
@@ -145,7 +126,7 @@ class Router
             $route   = rtrim($routes->getRegex(), '/');
             $pattern = '@^' . preg_quote($this->basePath) . $route . '/?$@i';
 
-            if ( !preg_match($pattern, $requestUrl, $matches)) {
+            if (!preg_match($pattern, $requestUrl, $matches)) {
                 continue;
             }
 
@@ -179,6 +160,20 @@ class Router
         );
     }
 
+    public function getRoute($requestUrl, $requestMethod = RequestMethodInterface::METHOD_GET)
+    {
+        /** @var Route $route */
+        list($route, $params) = $this->findRoute($requestUrl, $requestMethod);
+
+        if ($route !== null) {
+            $route->setParameters($params);
+
+            return $route;
+        } else {
+            return null;
+        }
+    }
+
     /**
      * Match given request _url and request method and see if a route
      * has been defined for it If so, return route's target If called
@@ -187,7 +182,7 @@ class Router
      * @param string $requestUrl
      * @param string $requestMethod
      *
-     * @return bool|Route
+     * @return null|Route
      */
     public function match($requestUrl, $requestMethod = RequestMethodInterface::METHOD_GET)
     {
@@ -196,9 +191,10 @@ class Router
 
         if ($route !== null) {
             $route->setParameters($params);
+
             return $route->dispatch();
         } else {
-            return;
+            return null;
         }
     }
 
@@ -215,7 +211,7 @@ class Router
     public function generate($routeName, array $params = array())
     {
         // Check if route exists
-        if ( !isset($this->namedRoutes[$routeName])) {
+        if (!isset($this->namedRoutes[$routeName])) {
             throw new Exception("No route with the name $routeName has been found.");
         }
 
index 1852d6c..2b08ca5 100644 (file)
@@ -34,15 +34,21 @@ class RouterTest extends PHPUnit_Framework_TestCase
      */
     public function testMatch($router, $path, $expected)
     {
-        $result = $router->match($path);
+        $buffer = $router->match($path);
 
-        $this->assertEquals($expected, $result);
+        if($expected === true) {
+            $this->assertNotNull($buffer);
+        } else {
+            $this->assertNull($buffer);
+        }
     }
 
     public function testMatchWrongMethod()
     {
         $router = $this->getRouter();
-        $this->assertFalse($router->match('/users', 'POST'));
+        $buffer = $router->match('/users', 'POST');
+
+        $this->assertNull($buffer);
     }
 
     public function testBasePathConfigIsSettedProperly()
@@ -66,7 +72,9 @@ class RouterTest extends PHPUnit_Framework_TestCase
 
         foreach ($this->serverProvider() as $server) {
             $_SERVER = $server;
-            $this->assertTrue((bool)$router->matchCurrentRequest());
+            $buffer = $router->matchCurrentRequest();
+
+            $this->assertNotNull($buffer);
         }
     }
 
@@ -101,8 +109,8 @@ class RouterTest extends PHPUnit_Framework_TestCase
 
         $router = new Router($collection);
         $this->assertEquals(
-            array(array('page_id' => 'MySuperPage')),
-            $router->match('/page/MySuperPage')->getParameters()
+            array('page_id' => 'MySuperPage'),
+            $router->getRoute('/page/MySuperPage')->getParameters()
         );
     }
 
@@ -121,18 +129,18 @@ class RouterTest extends PHPUnit_Framework_TestCase
 
         $router = new Router($collection);
         $this->assertEquals(
-            array(array('filename' => 'someJsFile')),
-            $router->match('/js/someJsFile.js')->getParameters()
+            array('filename' => 'someJsFile'),
+            $router->getRoute('/js/someJsFile.js')->getParameters()
         );
 
         $this->assertEquals(
-            array(array('filename' => 'someJsFile.min')),
-            $router->match('/js/someJsFile.min.js')->getParameters()
+            array('filename' => 'someJsFile.min'),
+            $router->getRoute('/js/someJsFile.min.js')->getParameters()
         );
 
         $this->assertEquals(
-            array(array('filename' => 'someJsFile.min.js')),
-            $router->match('/js/someJsFile.min.js.js')->getParameters()
+            array('filename' => 'someJsFile.min.js'),
+            $router->getRoute('/js/someJsFile.min.js.js')->getParameters()
         );
     }