| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- <?php
- /**
- * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- * @link http://cakephp.org CakePHP(tm) Project
- * @since 3.3.0
- * @license http://www.opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\Http;
- use Cake\Http\MiddlewareStack;
- use Cake\TestSuite\TestCase;
- use TestApp\Middleware\SampleMiddleware;
- /**
- * Test case for the MiddlewareStack
- */
- class MiddlewareStackTest extends TestCase
- {
- /**
- * Test get()
- *
- * @return void
- */
- public function testGet()
- {
- $stack = new MiddlewareStack();
- $cb = function () {
- };
- $stack->push($cb);
- $this->assertSame($cb, $stack->get(0));
- $this->assertNull($stack->get(1));
- }
- /**
- * Test the return value of push()
- *
- * @return void
- */
- public function testPushReturn()
- {
- $stack = new MiddlewareStack();
- $cb = function () {
- };
- $this->assertSame($stack, $stack->push($cb));
- }
- /**
- * Test the push orders correctly
- *
- * @return void
- */
- public function testPushOrdering()
- {
- $one = function () {
- };
- $two = function () {
- };
- $stack = new MiddlewareStack();
- $this->assertCount(0, $stack);
- $stack->push($one);
- $this->assertCount(1, $stack);
- $stack->push($two);
- $this->assertCount(2, $stack);
- $this->assertSame($one, $stack->get(0));
- $this->assertSame($two, $stack->get(1));
- }
- /**
- * Test the prepend can be chained
- *
- * @return void
- */
- public function testPrependReturn()
- {
- $cb = function () {
- };
- $stack = new MiddlewareStack();
- $this->assertSame($stack, $stack->prepend($cb));
- }
- /**
- * Test the prepend orders correctly.
- *
- * @return void
- */
- public function testPrependOrdering()
- {
- $one = function () {
- };
- $two = function () {
- };
- $stack = new MiddlewareStack();
- $this->assertCount(0, $stack);
- $stack->push($one);
- $this->assertCount(1, $stack);
- $stack->prepend($two);
- $this->assertCount(2, $stack);
- $this->assertSame($two, $stack->get(0));
- $this->assertSame($one, $stack->get(1));
- }
- /**
- * Test insertAt ordering
- *
- * @return void
- */
- public function testInsertAt()
- {
- $one = function () {
- };
- $two = function () {
- };
- $three = function () {
- };
- $stack = new MiddlewareStack();
- $stack->push($one)->push($two)->insertAt(0, $three);
- $this->assertSame($three, $stack->get(0));
- $this->assertSame($one, $stack->get(1));
- $this->assertSame($two, $stack->get(2));
- $stack = new MiddlewareStack();
- $stack->push($one)->push($two)->insertAt(1, $three);
- $this->assertSame($one, $stack->get(0));
- $this->assertSame($three, $stack->get(1));
- $this->assertSame($two, $stack->get(2));
- }
- /**
- * Test insertAt out of the existing range
- *
- * @return void
- */
- public function testInsertAtOutOfBounds()
- {
- $one = function () {
- };
- $two = function () {
- };
- $stack = new MiddlewareStack();
- $stack->push($one)->insertAt(99, $two);
- $this->assertCount(2, $stack);
- $this->assertSame($one, $stack->get(0));
- $this->assertSame($two, $stack->get(1));
- }
- /**
- * Test insertAt with a negative index
- *
- * @return void
- */
- public function testInsertAtNegative()
- {
- $one = function () {
- };
- $two = function () {
- };
- $stack = new MiddlewareStack();
- $stack->push($one)->insertAt(-1, $two);
- $this->assertCount(2, $stack);
- $this->assertSame($two, $stack->get(0));
- $this->assertSame($one, $stack->get(1));
- }
- /**
- * Test insertBefore
- *
- * @return void
- */
- public function testInsertBefore()
- {
- $one = function () {
- };
- $two = new SampleMiddleware();
- $three = function () {
- };
- $stack = new MiddlewareStack();
- $stack->push($one)->push($two)->insertBefore(SampleMiddleware::class, $three);
- $this->assertCount(3, $stack);
- $this->assertSame($one, $stack->get(0));
- $this->assertSame($three, $stack->get(1));
- $this->assertSame($two, $stack->get(2));
- }
- /**
- * Test insertBefore an invalid classname
- *
- * @expectedException LogicException
- * @expectedExceptionMessage No middleware matching 'InvalidClassName' could be found.
- * @return void
- */
- public function testInsertBeforeInvalid()
- {
- $one = function () {
- };
- $two = new SampleMiddleware();
- $three = function () {
- };
- $stack = new MiddlewareStack();
- $stack->push($one)->push($two)->insertBefore('InvalidClassName', $three);
- }
- /**
- * Test insertAfter
- *
- * @return void
- */
- public function testInsertAfter()
- {
- $one = new SampleMiddleware();
- $two = function () {
- };
- $three = function () {
- };
- $stack = new MiddlewareStack();
- $stack->push($one)->push($two)->insertAfter(SampleMiddleware::class, $three);
- $this->assertCount(3, $stack);
- $this->assertSame($one, $stack->get(0));
- $this->assertSame($three, $stack->get(1));
- $this->assertSame($two, $stack->get(2));
- }
- /**
- * Test insertAfter an invalid classname
- *
- * @return void
- */
- public function testInsertAfterInvalid()
- {
- $one = new SampleMiddleware();
- $two = function () {
- };
- $three = function () {
- };
- $stack = new MiddlewareStack();
- $stack->push($one)->push($two)->insertAfter('InvalidClass', $three);
- $this->assertCount(3, $stack);
- $this->assertSame($one, $stack->get(0));
- $this->assertSame($two, $stack->get(1));
- $this->assertSame($three, $stack->get(2));
- }
- }
|