Browse Source

Raise an exception in insertBefore()

Raise an exception When attempting to insertBefore an unknown middleware.
Based on feedback in cakephp/docs#4023 this is what the reviewers felt
was a more correct behavior.
Mark Story 9 years ago
parent
commit
33bbf8424e
2 changed files with 4 additions and 6 deletions
  1. 2 1
      src/Http/MiddlewareStack.php
  2. 2 5
      tests/TestCase/Http/MiddlewareStackTest.php

+ 2 - 1
src/Http/MiddlewareStack.php

@@ -15,6 +15,7 @@
 namespace Cake\Http;
 
 use Countable;
+use LogicException;
 
 /**
  * Provides methods for creating and manipulating a 'stack' of
@@ -108,7 +109,7 @@ class MiddlewareStack implements Countable
         if ($found) {
             return $this->insertAt($i, $callable);
         }
-        return $this->push($callable);
+        throw new LogicException(sprintf("No middleware matching '%s' could be found.", $class));
     }
 
     /**

+ 2 - 5
tests/TestCase/Http/MiddlewareStackTest.php

@@ -206,6 +206,8 @@ class MiddlewareStackTest extends TestCase
     /**
      * Test insertBefore an invalid classname
      *
+     * @expectedException LogicException
+     * @expectedExceptionMessage No middleware matching 'InvalidClassName' could be found.
      * @return void
      */
     public function testInsertBeforeInvalid()
@@ -217,11 +219,6 @@ class MiddlewareStackTest extends TestCase
         };
         $stack = new MiddlewareStack();
         $stack->push($one)->push($two)->insertBefore('InvalidClassName', $three);
-
-        $this->assertCount(3, $stack);
-        $this->assertSame($one, $stack->get(0));
-        $this->assertSame($two, $stack->get(1));
-        $this->assertSame($three, $stack->get(2));
     }
 
     /**