Browse Source

Merge branch 'master' into 3.next

Mark Story 9 years ago
parent
commit
09d7d4e8ab

+ 2 - 2
src/Collection/CollectionInterface.php

@@ -135,8 +135,8 @@ interface CollectionInterface extends Iterator, JsonSerializable
      * ```
      *
      * @param callable $c a callback function
-     * @return bool true if for all elements in this collection the provided
-     * callback returns true, false otherwise
+     * @return bool true if the provided callback returns true for any element in this
+     * collection, false otherwise
      */
     public function some(callable $c);
 

+ 13 - 2
src/Routing/Exception/MissingRouteException.php

@@ -27,12 +27,23 @@ class MissingRouteException extends Exception
     protected $_messageTemplate = 'A route matching "%s" could not be found.';
 
     /**
+     * Message template to use when the requested method is included.
+     *
+     * @var string
+     */
+    protected $_messageTemplateWithMethod = 'A "%s" route matching "%s" could not be found.';
+
+    /**
      * {@inheritDoc}
      */
     public function __construct($message, $code = 404)
     {
-        if (is_array($message) && isset($message['message'])) {
-            $this->_messageTemplate = $message['message'];
+        if (is_array($message)) {
+            if (isset($message['message'])) {
+                $this->_messageTemplate = $message['message'];
+            } elseif (isset($message['method']) && $message['method']) {
+                $this->_messageTemplate = $this->_messageTemplateWithMethod;
+            }
         }
         parent::__construct($message, $code);
     }

+ 9 - 1
src/Routing/RouteCollection.php

@@ -145,7 +145,15 @@ class RouteCollection
                 return $r;
             }
         }
-        throw new MissingRouteException(['url' => $url]);
+
+        $exceptionProperties = ['url' => $url];
+        if ($method !== '') {
+            // Ensure that if the method is included, it is the first element of
+            // the array, to match the order that the strings are printed in the
+            // MissingRouteException error message, $_messageTemplateWithMethod.
+            $exceptionProperties = array_merge(['method' => $method], $exceptionProperties);
+        }
+        throw new MissingRouteException($exceptionProperties);
     }
 
     /**

+ 17 - 0
tests/TestCase/Routing/RouteCollectionTest.php

@@ -52,6 +52,23 @@ class RouteCollectionTest extends TestCase
     }
 
     /**
+     * Test parse() throws an error on known routes called with unknown methods.
+     *
+     * @expectedException \Cake\Routing\Exception\MissingRouteException
+     * @expectedExceptionMessage A "POST" route matching "/b" could not be found
+     */
+    public function testParseMissingRouteMethod()
+    {
+        $routes = new RouteBuilder($this->collection, '/b', ['key' => 'value']);
+        $routes->connect('/', ['controller' => 'Articles', '_method' => ['GET']]);
+
+        $result = $this->collection->parse('/b', 'GET');
+        $this->assertNotEmpty($result, 'Route should be found');
+        $result = $this->collection->parse('/b', 'POST');
+        $this->assertEquals([], $result, 'Should not match with missing method');
+    }
+
+    /**
      * Test parsing routes.
      *
      * @return void