Skip to content

Commit

Permalink
Account for middlware as string in insertBefore() and insertAfter().
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Jul 27, 2016
1 parent 88af4a4 commit e2794a4
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 7 deletions.
8 changes: 6 additions & 2 deletions src/Http/MiddlewareQueue.php
Expand Up @@ -168,7 +168,9 @@ public function insertBefore($class, $middleware)
{
$found = false;
foreach ($this->queue as $i => $object) {
if (is_a($object, $class)) {
if ((is_string($object) && $object === $class)
|| is_a($object, $class)
) {
$found = true;
break;
}
Expand All @@ -194,7 +196,9 @@ public function insertAfter($class, $middleware)
{
$found = false;
foreach ($this->queue as $i => $object) {
if (is_a($object, $class)) {
if ((is_string($object) && $object === $class)
|| is_a($object, $class)
) {
$found = true;
break;
}
Expand Down
52 changes: 47 additions & 5 deletions tests/TestCase/Http/MiddlewareQueueTest.php
Expand Up @@ -24,6 +24,29 @@
*/
class MiddlewareQueueTest extends TestCase
{
/**
* setUp
*
* @return void
*/
public function setUp()
{
parent::setUp();

$this->appNamespace = Configure::read('App.namespace');
Configure::write('App.namespace', 'TestApp');
}

/**
* tearDown
*
* @return void
*/
public function tearDown()
{
Configure::write('App.namespace', $this->appNamespace);
}

/**
* Test get()
*
Expand Down Expand Up @@ -122,17 +145,12 @@ public function testPrependOrdering()
*/
public function testAddingPrependingUsingString()
{
$appNamespace = Configure::read('App.namespace');
Configure::write('App.namespace', 'TestApp');

$queue = new MiddlewareQueue();
$queue->add('Sample');
$queue->prepend('TestApp\Middleware\SampleMiddleware');

$this->assertInstanceOf('TestApp\Middleware\SampleMiddleware', $queue->get(0));
$this->assertInstanceOf('TestApp\Middleware\SampleMiddleware', $queue->get(1));

Configure::write('App.namespace', $appNamespace);
}

/**
Expand Down Expand Up @@ -239,6 +257,18 @@ public function testInsertBefore()
$this->assertSame($one, $queue->get(0));
$this->assertSame($three, $queue->get(1));
$this->assertSame($two, $queue->get(2));

$two = SampleMiddleware::class;
$queue = new MiddlewareQueue();
$queue
->add($one)
->add($two)
->insertBefore(SampleMiddleware::class, $three);

$this->assertCount(3, $queue);
$this->assertSame($one, $queue->get(0));
$this->assertSame($three, $queue->get(1));
$this->assertInstanceOf(SampleMiddleware::class, $queue->get(2));
}

/**
Expand Down Expand Up @@ -278,6 +308,18 @@ public function testInsertAfter()
$this->assertSame($one, $queue->get(0));
$this->assertSame($three, $queue->get(1));
$this->assertSame($two, $queue->get(2));

$one = 'Sample';
$queue = new MiddlewareQueue();
$queue
->add($one)
->add($two)
->insertAfter('Sample', $three);

$this->assertCount(3, $queue);
$this->assertInstanceOf(SampleMiddleware::class, $queue->get(0));
$this->assertSame($three, $queue->get(1));
$this->assertSame($two, $queue->get(2));
}

/**
Expand Down

0 comments on commit e2794a4

Please sign in to comment.