Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge branch 'feature/36' into develop
Browse files Browse the repository at this point in the history
Close #36
  • Loading branch information
weierophinney committed Mar 17, 2016
2 parents 8e40861 + 296318a commit adb5a8f
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 3 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ errors. Starting with 1.2.0, they now raise a `RuntimeException`.

### Added

- Nothing.
- [#36](https://github.com/zendframework/zend-stratigility/pull/36) adds a new
`InvalidMiddlewareException`, with the static factory `fromValue()` that
provides an exception message detailing the invalid type. `MiddlewarePipe` now
throws this exception from the `pipe()` method when a non-callable value is
provided.

### Deprecated

Expand Down
37 changes: 37 additions & 0 deletions src/Exception/InvalidMiddlewareException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @see http://github.com/zendframework/zend-stratigility for the canonical source repository
* @copyright Copyright (c) 2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-stratigility/blob/master/LICENSE.md New BSD License
*/

namespace Zend\Stratigility\Exception;

use InvalidArgumentException;

class InvalidMiddlewareException extends InvalidArgumentException
{
/**
* Create and return an InvalidArgumentException detailing the invalid middleware type.
*
* @param mixed $value
* @return InvalidArgumentException
*/
public static function fromValue($value)
{
$received = gettype($value);

if (is_object($value)) {
$received = get_class($value);
}

return new self(
sprintf(
'Middleware must be callable, %s found',
$received
)
);
}
}
4 changes: 2 additions & 2 deletions src/MiddlewarePipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace Zend\Stratigility;

use InvalidArgumentException;
use Zend\Stratigility\Exception\InvalidMiddlewareException;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use SplQueue;
Expand Down Expand Up @@ -108,7 +108,7 @@ public function pipe($path, $middleware = null)

// Ensure we have a valid handler
if (! is_callable($middleware)) {
throw new InvalidArgumentException('Middleware must be callable');
throw InvalidMiddlewareException::fromValue($middleware);
}

$this->pipeline->enqueue(new Route(
Expand Down
43 changes: 43 additions & 0 deletions test/Exception/InvalidMiddlewareExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @see http://github.com/zendframework/zend-stratigility for the canonical source repository
* @copyright Copyright (c) 2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-stratigility/blob/master/LICENSE.md New BSD License
*/

namespace ZendTest\Stratigility\Exception;

use PHPUnit_Framework_TestCase as TestCase;
use Zend\Stratigility\Exception\InvalidMiddlewareException;

class InvalidMiddlewareExceptionTest extends TestCase
{
public function invalidMiddlewareValues()
{
return [
'null' => [null, 'NULL'],
'true' => [true, 'boolean'],
'false' => [false, 'boolean'],
'empty-string' => ['', 'string'],
'string' => ['not-callable', 'string'],
'int' => [1, 'integer'],
'float' => [1.1, 'double'],
'array' => [['not', 'callable'], 'array'],
'object' => [(object) ['not', 'callable'], 'stdClass'],
];
}

/**
* @dataProvider invalidMiddlewareValues
*/
public function testFromValueProvidesNewExceptionWithMessageRelatedToValue($value, $expected)
{
$e = InvalidMiddlewareException::fromValue($value);
$this->assertEquals(sprintf(
'Middleware must be callable, %s found',
$expected
), $e->getMessage());
}
}

0 comments on commit adb5a8f

Please sign in to comment.