add($cb); $this->assertSame($cb, $stack->get(0)); $this->assertNull($stack->get(1)); } /** * Test the return value of add() * * @return void */ public function testAddReturn() { $stack = new MiddlewareQueue(); $cb = function () { }; $this->assertSame($stack, $stack->add($cb)); } /** * Test the add orders correctly * * @return void */ public function testAddOrdering() { $one = function () { }; $two = function () { }; $stack = new MiddlewareQueue(); $this->assertCount(0, $stack); $stack->add($one); $this->assertCount(1, $stack); $stack->add($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 MiddlewareQueue(); $this->assertSame($stack, $stack->prepend($cb)); } /** * Test the prepend orders correctly. * * @return void */ public function testPrependOrdering() { $one = function () { }; $two = function () { }; $stack = new MiddlewareQueue(); $this->assertCount(0, $stack); $stack->add($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 MiddlewareQueue(); $stack->add($one)->add($two)->insertAt(0, $three); $this->assertSame($three, $stack->get(0)); $this->assertSame($one, $stack->get(1)); $this->assertSame($two, $stack->get(2)); $stack = new MiddlewareQueue(); $stack->add($one)->add($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 MiddlewareQueue(); $stack->add($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 MiddlewareQueue(); $stack->add($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 MiddlewareQueue(); $stack->add($one)->add($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 MiddlewareQueue(); $stack->add($one)->add($two)->insertBefore('InvalidClassName', $three); } /** * Test insertAfter * * @return void */ public function testInsertAfter() { $one = new SampleMiddleware(); $two = function () { }; $three = function () { }; $stack = new MiddlewareQueue(); $stack->add($one)->add($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 MiddlewareQueue(); $stack->add($one)->add($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)); } }