@@ -27,17 +27,17 @@ |
||
| 27 | 27 | */ |
| 28 | 28 | function path_match(string $path, string $subject, &$attributes = []) : bool |
| 29 | 29 | { |
| 30 | - $regex = path_regex($path); |
|
| 31 | - if (!preg_match($regex, $subject, $matches)) { |
|
| 32 | - return false; |
|
| 33 | - } |
|
| 30 | + $regex = path_regex($path); |
|
| 31 | + if (!preg_match($regex, $subject, $matches)) { |
|
| 32 | + return false; |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | - $attributes = []; |
|
| 36 | - foreach ($matches as $key => $value) { |
|
| 37 | - if (! (is_int($key) || '' === $value)) { |
|
| 38 | - $attributes[$key] = $value; |
|
| 39 | - } |
|
| 40 | - } |
|
| 35 | + $attributes = []; |
|
| 36 | + foreach ($matches as $key => $value) { |
|
| 37 | + if (! (is_int($key) || '' === $value)) { |
|
| 38 | + $attributes[$key] = $value; |
|
| 39 | + } |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | - return true; |
|
| 42 | + return true; |
|
| 43 | 43 | } |
@@ -34,7 +34,7 @@ |
||
| 34 | 34 | |
| 35 | 35 | $attributes = []; |
| 36 | 36 | foreach ($matches as $key => $value) { |
| 37 | - if (! (is_int($key) || '' === $value)) { |
|
| 37 | + if (!(is_int($key) || '' === $value)) { |
|
| 38 | 38 | $attributes[$key] = $value; |
| 39 | 39 | } |
| 40 | 40 | } |
@@ -36,25 +36,25 @@ |
||
| 36 | 36 | */ |
| 37 | 37 | function path_build(string $path, array $attributes = [], bool $strict = false) : string |
| 38 | 38 | { |
| 39 | - $regex = '/{([0-9A-Za-z_]+)(?:<([^<#>]+)>)?}/'; |
|
| 40 | - |
|
| 41 | - return preg_replace_callback($regex, function ($match) use ($path, $attributes, $strict) { |
|
| 42 | - if (!isset($attributes[$match[1]])) { |
|
| 43 | - throw new InvalidArgumentException( |
|
| 44 | - sprintf('[%s] missing attribute "%s".', $path, $match[1]) |
|
| 45 | - ); |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - $attributes[$match[1]] = (string) $attributes[$match[1]]; |
|
| 49 | - |
|
| 50 | - if ($strict && isset($match[2])) { |
|
| 51 | - if (!preg_match('#'.$match[2].'#u', $attributes[$match[1]])) { |
|
| 52 | - throw new InvalidArgumentException( |
|
| 53 | - sprintf('[%s] "%s" must match "%s".', $path, $match[1], $match[2]) |
|
| 54 | - ); |
|
| 55 | - } |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - return $attributes[$match[1]]; |
|
| 59 | - }, $path); |
|
| 39 | + $regex = '/{([0-9A-Za-z_]+)(?:<([^<#>]+)>)?}/'; |
|
| 40 | + |
|
| 41 | + return preg_replace_callback($regex, function ($match) use ($path, $attributes, $strict) { |
|
| 42 | + if (!isset($attributes[$match[1]])) { |
|
| 43 | + throw new InvalidArgumentException( |
|
| 44 | + sprintf('[%s] missing attribute "%s".', $path, $match[1]) |
|
| 45 | + ); |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + $attributes[$match[1]] = (string) $attributes[$match[1]]; |
|
| 49 | + |
|
| 50 | + if ($strict && isset($match[2])) { |
|
| 51 | + if (!preg_match('#'.$match[2].'#u', $attributes[$match[1]])) { |
|
| 52 | + throw new InvalidArgumentException( |
|
| 53 | + sprintf('[%s] "%s" must match "%s".', $path, $match[1], $match[2]) |
|
| 54 | + ); |
|
| 55 | + } |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + return $attributes[$match[1]]; |
|
| 59 | + }, $path); |
|
| 60 | 60 | } |
@@ -38,7 +38,7 @@ discard block |
||
| 38 | 38 | { |
| 39 | 39 | $regex = '/{([0-9A-Za-z_]+)(?:<([^<#>]+)>)?}/'; |
| 40 | 40 | |
| 41 | - return preg_replace_callback($regex, function ($match) use ($path, $attributes, $strict) { |
|
| 41 | + return preg_replace_callback($regex, function($match) use ($path, $attributes, $strict) { |
|
| 42 | 42 | if (!isset($attributes[$match[1]])) { |
| 43 | 43 | throw new InvalidArgumentException( |
| 44 | 44 | sprintf('[%s] missing attribute "%s".', $path, $match[1]) |
@@ -48,7 +48,7 @@ discard block |
||
| 48 | 48 | $attributes[$match[1]] = (string) $attributes[$match[1]]; |
| 49 | 49 | |
| 50 | 50 | if ($strict && isset($match[2])) { |
| 51 | - if (!preg_match('#'.$match[2].'#u', $attributes[$match[1]])) { |
|
| 51 | + if (!preg_match('#' . $match[2] . '#u', $attributes[$match[1]])) { |
|
| 52 | 52 | throw new InvalidArgumentException( |
| 53 | 53 | sprintf('[%s] "%s" must match "%s".', $path, $match[1], $match[2]) |
| 54 | 54 | ); |
@@ -34,18 +34,18 @@ |
||
| 34 | 34 | */ |
| 35 | 35 | function path_regex(string $path) : string |
| 36 | 36 | { |
| 37 | - preg_match_all('/{([0-9A-Za-z_]+)(?:<([^<#>]+)>)?}/', $path, $matches, PREG_SET_ORDER); |
|
| 37 | + preg_match_all('/{([0-9A-Za-z_]+)(?:<([^<#>]+)>)?}/', $path, $matches, PREG_SET_ORDER); |
|
| 38 | 38 | |
| 39 | - foreach ($matches as $match) { |
|
| 40 | - $path = str_replace($match[0], '{'.$match[1].'}', $path); |
|
| 41 | - } |
|
| 39 | + foreach ($matches as $match) { |
|
| 40 | + $path = str_replace($match[0], '{'.$match[1].'}', $path); |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - $path = addcslashes($path, '#$*+-.?[\]^|'); |
|
| 44 | - $path = str_replace(['(', ')'], ['(?:', ')?'], $path); |
|
| 43 | + $path = addcslashes($path, '#$*+-.?[\]^|'); |
|
| 44 | + $path = str_replace(['(', ')'], ['(?:', ')?'], $path); |
|
| 45 | 45 | |
| 46 | - foreach ($matches as $match) { |
|
| 47 | - $path = str_replace('{'.$match[1].'}', '(?<'.$match[1].'>'.($match[2] ?? '[^/]+').')', $path); |
|
| 48 | - } |
|
| 46 | + foreach ($matches as $match) { |
|
| 47 | + $path = str_replace('{'.$match[1].'}', '(?<'.$match[1].'>'.($match[2] ?? '[^/]+').')', $path); |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - return '#^'.$path.'$#uD'; |
|
| 50 | + return '#^'.$path.'$#uD'; |
|
| 51 | 51 | } |
@@ -37,15 +37,15 @@ |
||
| 37 | 37 | preg_match_all('/{([0-9A-Za-z_]+)(?:<([^<#>]+)>)?}/', $path, $matches, PREG_SET_ORDER); |
| 38 | 38 | |
| 39 | 39 | foreach ($matches as $match) { |
| 40 | - $path = str_replace($match[0], '{'.$match[1].'}', $path); |
|
| 40 | + $path = str_replace($match[0], '{' . $match[1] . '}', $path); |
|
| 41 | 41 | } |
| 42 | 42 | |
| 43 | 43 | $path = addcslashes($path, '#$*+-.?[\]^|'); |
| 44 | 44 | $path = str_replace(['(', ')'], ['(?:', ')?'], $path); |
| 45 | 45 | |
| 46 | 46 | foreach ($matches as $match) { |
| 47 | - $path = str_replace('{'.$match[1].'}', '(?<'.$match[1].'>'.($match[2] ?? '[^/]+').')', $path); |
|
| 47 | + $path = str_replace('{' . $match[1] . '}', '(?<' . $match[1] . '>' . ($match[2] ?? '[^/]+') . ')', $path); |
|
| 48 | 48 | } |
| 49 | 49 | |
| 50 | - return '#^'.$path.'$#uD'; |
|
| 50 | + return '#^' . $path . '$#uD'; |
|
| 51 | 51 | } |
@@ -16,62 +16,62 @@ |
||
| 16 | 16 | class BlankRequestHandler implements RequestHandlerInterface |
| 17 | 17 | { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * @var bool |
|
| 21 | - */ |
|
| 22 | - private $isRunned = false; |
|
| 19 | + /** |
|
| 20 | + * @var bool |
|
| 21 | + */ |
|
| 22 | + private $isRunned = false; |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * @var array |
|
| 26 | - */ |
|
| 27 | - private $attributes = []; |
|
| 24 | + /** |
|
| 25 | + * @var array |
|
| 26 | + */ |
|
| 27 | + private $attributes = []; |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * @return bool |
|
| 31 | - */ |
|
| 32 | - public function isRunned() : bool |
|
| 33 | - { |
|
| 34 | - return $this->isRunned; |
|
| 35 | - } |
|
| 29 | + /** |
|
| 30 | + * @return bool |
|
| 31 | + */ |
|
| 32 | + public function isRunned() : bool |
|
| 33 | + { |
|
| 34 | + return $this->isRunned; |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * @return array |
|
| 39 | - */ |
|
| 40 | - public function getAttributes() : array |
|
| 41 | - { |
|
| 42 | - return $this->attributes; |
|
| 43 | - } |
|
| 37 | + /** |
|
| 38 | + * @return array |
|
| 39 | + */ |
|
| 40 | + public function getAttributes() : array |
|
| 41 | + { |
|
| 42 | + return $this->attributes; |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * @param mixed $key |
|
| 47 | - * |
|
| 48 | - * @return mixed |
|
| 49 | - */ |
|
| 50 | - public function getAttribute($key) |
|
| 51 | - { |
|
| 52 | - return $this->attributes[$key] ?? null; |
|
| 53 | - } |
|
| 45 | + /** |
|
| 46 | + * @param mixed $key |
|
| 47 | + * |
|
| 48 | + * @return mixed |
|
| 49 | + */ |
|
| 50 | + public function getAttribute($key) |
|
| 51 | + { |
|
| 52 | + return $this->attributes[$key] ?? null; |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - /** |
|
| 56 | - * {@inheritDoc} |
|
| 57 | - */ |
|
| 58 | - public function handle(ServerRequestInterface $request) : ResponseInterface |
|
| 59 | - { |
|
| 60 | - $this->isRunned = true; |
|
| 61 | - $this->attributes = $request->getAttributes(); |
|
| 55 | + /** |
|
| 56 | + * {@inheritDoc} |
|
| 57 | + */ |
|
| 58 | + public function handle(ServerRequestInterface $request) : ResponseInterface |
|
| 59 | + { |
|
| 60 | + $this->isRunned = true; |
|
| 61 | + $this->attributes = $request->getAttributes(); |
|
| 62 | 62 | |
| 63 | - return (new ResponseFactory)->createResponse(); |
|
| 64 | - } |
|
| 63 | + return (new ResponseFactory)->createResponse(); |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - /** |
|
| 67 | - * @param ServerRequestInterface $request |
|
| 68 | - * |
|
| 69 | - * @return ResponseInterface |
|
| 70 | - * |
|
| 71 | - * @link https://www.php.net/manual/ru/language.oop5.magic.php#object.invoke |
|
| 72 | - */ |
|
| 73 | - public function __invoke(ServerRequestInterface $request) : ResponseInterface |
|
| 74 | - { |
|
| 75 | - return $this->handle($request); |
|
| 76 | - } |
|
| 66 | + /** |
|
| 67 | + * @param ServerRequestInterface $request |
|
| 68 | + * |
|
| 69 | + * @return ResponseInterface |
|
| 70 | + * |
|
| 71 | + * @link https://www.php.net/manual/ru/language.oop5.magic.php#object.invoke |
|
| 72 | + */ |
|
| 73 | + public function __invoke(ServerRequestInterface $request) : ResponseInterface |
|
| 74 | + { |
|
| 75 | + return $this->handle($request); |
|
| 76 | + } |
|
| 77 | 77 | } |
@@ -21,91 +21,91 @@ |
||
| 21 | 21 | class TestRoute extends BaseRoute |
| 22 | 22 | { |
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * @var int |
|
| 26 | - */ |
|
| 27 | - public const WITH_BROKEN_MIDDLEWARE = 1; |
|
| 24 | + /** |
|
| 25 | + * @var int |
|
| 26 | + */ |
|
| 27 | + public const WITH_BROKEN_MIDDLEWARE = 1; |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * Constructor of the class |
|
| 31 | - * |
|
| 32 | - * @param int $flags |
|
| 33 | - */ |
|
| 34 | - public function __construct(int $flags = 0) |
|
| 35 | - { |
|
| 36 | - parent::__construct( |
|
| 37 | - self::getTestRouteName($flags), |
|
| 38 | - self::getTestRoutePath($flags), |
|
| 39 | - self::getTestRouteMethods($flags), |
|
| 40 | - self::getTestRouteRequestHandler($flags), |
|
| 41 | - self::getTestRouteMiddlewares($flags), |
|
| 42 | - self::getTestRouteAttributes($flags) |
|
| 43 | - ); |
|
| 44 | - } |
|
| 29 | + /** |
|
| 30 | + * Constructor of the class |
|
| 31 | + * |
|
| 32 | + * @param int $flags |
|
| 33 | + */ |
|
| 34 | + public function __construct(int $flags = 0) |
|
| 35 | + { |
|
| 36 | + parent::__construct( |
|
| 37 | + self::getTestRouteName($flags), |
|
| 38 | + self::getTestRoutePath($flags), |
|
| 39 | + self::getTestRouteMethods($flags), |
|
| 40 | + self::getTestRouteRequestHandler($flags), |
|
| 41 | + self::getTestRouteMiddlewares($flags), |
|
| 42 | + self::getTestRouteAttributes($flags) |
|
| 43 | + ); |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * @return string |
|
| 48 | - */ |
|
| 49 | - public static function getTestRouteName(int $flags = 0) : string |
|
| 50 | - { |
|
| 51 | - return uniqid() . '.' . uniqid() . '.' . uniqid(); |
|
| 52 | - } |
|
| 46 | + /** |
|
| 47 | + * @return string |
|
| 48 | + */ |
|
| 49 | + public static function getTestRouteName(int $flags = 0) : string |
|
| 50 | + { |
|
| 51 | + return uniqid() . '.' . uniqid() . '.' . uniqid(); |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * @return string |
|
| 56 | - */ |
|
| 57 | - public static function getTestRoutePath(int $flags = 0) : string |
|
| 58 | - { |
|
| 59 | - return '/' . uniqid() . '/' . uniqid() . '/' . uniqid(); |
|
| 60 | - } |
|
| 54 | + /** |
|
| 55 | + * @return string |
|
| 56 | + */ |
|
| 57 | + public static function getTestRoutePath(int $flags = 0) : string |
|
| 58 | + { |
|
| 59 | + return '/' . uniqid() . '/' . uniqid() . '/' . uniqid(); |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * @return string[] |
|
| 64 | - */ |
|
| 65 | - public static function getTestRouteMethods(int $flags = 0) : array |
|
| 66 | - { |
|
| 67 | - return [ |
|
| 68 | - strtoupper(uniqid()), |
|
| 69 | - strtoupper(uniqid()), |
|
| 70 | - strtoupper(uniqid()), |
|
| 71 | - ]; |
|
| 72 | - } |
|
| 62 | + /** |
|
| 63 | + * @return string[] |
|
| 64 | + */ |
|
| 65 | + public static function getTestRouteMethods(int $flags = 0) : array |
|
| 66 | + { |
|
| 67 | + return [ |
|
| 68 | + strtoupper(uniqid()), |
|
| 69 | + strtoupper(uniqid()), |
|
| 70 | + strtoupper(uniqid()), |
|
| 71 | + ]; |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - /** |
|
| 75 | - * @return RequestHandlerInterface |
|
| 76 | - */ |
|
| 77 | - public static function getTestRouteRequestHandler(int $flags = 0) : RequestHandlerInterface |
|
| 78 | - { |
|
| 79 | - return new BlankRequestHandler(); |
|
| 80 | - } |
|
| 74 | + /** |
|
| 75 | + * @return RequestHandlerInterface |
|
| 76 | + */ |
|
| 77 | + public static function getTestRouteRequestHandler(int $flags = 0) : RequestHandlerInterface |
|
| 78 | + { |
|
| 79 | + return new BlankRequestHandler(); |
|
| 80 | + } |
|
| 81 | 81 | |
| 82 | - /** |
|
| 83 | - * @return MiddlewareInterface[] |
|
| 84 | - */ |
|
| 85 | - public static function getTestRouteMiddlewares(int $flags = 0) : array |
|
| 86 | - { |
|
| 87 | - $middlewares = [new BlankMiddleware()]; |
|
| 82 | + /** |
|
| 83 | + * @return MiddlewareInterface[] |
|
| 84 | + */ |
|
| 85 | + public static function getTestRouteMiddlewares(int $flags = 0) : array |
|
| 86 | + { |
|
| 87 | + $middlewares = [new BlankMiddleware()]; |
|
| 88 | 88 | |
| 89 | - if ($flags & self::WITH_BROKEN_MIDDLEWARE) { |
|
| 90 | - $middlewares[] = new BlankMiddleware(true); |
|
| 91 | - } else { |
|
| 92 | - $middlewares[] = new BlankMiddleware(); |
|
| 93 | - } |
|
| 89 | + if ($flags & self::WITH_BROKEN_MIDDLEWARE) { |
|
| 90 | + $middlewares[] = new BlankMiddleware(true); |
|
| 91 | + } else { |
|
| 92 | + $middlewares[] = new BlankMiddleware(); |
|
| 93 | + } |
|
| 94 | 94 | |
| 95 | - $middlewares[] = new BlankMiddleware(); |
|
| 95 | + $middlewares[] = new BlankMiddleware(); |
|
| 96 | 96 | |
| 97 | - return $middlewares; |
|
| 98 | - } |
|
| 97 | + return $middlewares; |
|
| 98 | + } |
|
| 99 | 99 | |
| 100 | - /** |
|
| 101 | - * @return array |
|
| 102 | - */ |
|
| 103 | - public static function getTestRouteAttributes(int $flags = 0) : array |
|
| 104 | - { |
|
| 105 | - return [ |
|
| 106 | - uniqid() => uniqid(), |
|
| 107 | - uniqid() => uniqid(), |
|
| 108 | - uniqid() => uniqid(), |
|
| 109 | - ]; |
|
| 110 | - } |
|
| 100 | + /** |
|
| 101 | + * @return array |
|
| 102 | + */ |
|
| 103 | + public static function getTestRouteAttributes(int $flags = 0) : array |
|
| 104 | + { |
|
| 105 | + return [ |
|
| 106 | + uniqid() => uniqid(), |
|
| 107 | + uniqid() => uniqid(), |
|
| 108 | + uniqid() => uniqid(), |
|
| 109 | + ]; |
|
| 110 | + } |
|
| 111 | 111 | } |
@@ -17,51 +17,51 @@ |
||
| 17 | 17 | class BlankMiddleware implements MiddlewareInterface |
| 18 | 18 | { |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * @var bool |
|
| 22 | - */ |
|
| 23 | - private $isBroken; |
|
| 20 | + /** |
|
| 21 | + * @var bool |
|
| 22 | + */ |
|
| 23 | + private $isBroken; |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * @var bool |
|
| 27 | - */ |
|
| 28 | - private $isRunned = false; |
|
| 25 | + /** |
|
| 26 | + * @var bool |
|
| 27 | + */ |
|
| 28 | + private $isRunned = false; |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * @param bool $isBroken |
|
| 32 | - */ |
|
| 33 | - public function __construct(bool $isBroken = false) |
|
| 34 | - { |
|
| 35 | - $this->isBroken = $isBroken; |
|
| 36 | - } |
|
| 30 | + /** |
|
| 31 | + * @param bool $isBroken |
|
| 32 | + */ |
|
| 33 | + public function __construct(bool $isBroken = false) |
|
| 34 | + { |
|
| 35 | + $this->isBroken = $isBroken; |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * @return bool |
|
| 40 | - */ |
|
| 41 | - public function isBroken() : bool |
|
| 42 | - { |
|
| 43 | - return $this->isBroken; |
|
| 44 | - } |
|
| 38 | + /** |
|
| 39 | + * @return bool |
|
| 40 | + */ |
|
| 41 | + public function isBroken() : bool |
|
| 42 | + { |
|
| 43 | + return $this->isBroken; |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * @return bool |
|
| 48 | - */ |
|
| 49 | - public function isRunned() : bool |
|
| 50 | - { |
|
| 51 | - return $this->isRunned; |
|
| 52 | - } |
|
| 46 | + /** |
|
| 47 | + * @return bool |
|
| 48 | + */ |
|
| 49 | + public function isRunned() : bool |
|
| 50 | + { |
|
| 51 | + return $this->isRunned; |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * {@inheritDoc} |
|
| 56 | - */ |
|
| 57 | - public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface |
|
| 58 | - { |
|
| 59 | - $this->isRunned = true; |
|
| 54 | + /** |
|
| 55 | + * {@inheritDoc} |
|
| 56 | + */ |
|
| 57 | + public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface |
|
| 58 | + { |
|
| 59 | + $this->isRunned = true; |
|
| 60 | 60 | |
| 61 | - if ($this->isBroken) { |
|
| 62 | - return (new ResponseFactory)->createResponse(); |
|
| 63 | - } |
|
| 61 | + if ($this->isBroken) { |
|
| 62 | + return (new ResponseFactory)->createResponse(); |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - return $handler->handle($request); |
|
| 66 | - } |
|
| 65 | + return $handler->handle($request); |
|
| 66 | + } |
|
| 67 | 67 | } |
@@ -23,297 +23,297 @@ |
||
| 23 | 23 | class AnnotationRouteLoaderTest extends TestCase |
| 24 | 24 | { |
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * @return void |
|
| 28 | - */ |
|
| 29 | - public static function setUpBeforeClass() : void |
|
| 30 | - { |
|
| 31 | - if (! class_exists('Route')) { |
|
| 32 | - class_alias(AnnotationRoute::class, 'Route'); |
|
| 33 | - } |
|
| 34 | - } |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * @return void |
|
| 38 | - */ |
|
| 39 | - public function testAnnotationRouteNameMissing() : void |
|
| 40 | - { |
|
| 41 | - $this->expectException(InvalidArgumentException::class); |
|
| 42 | - $this->expectExceptionMessage('@Route.name must be not an empty string.'); |
|
| 43 | - |
|
| 44 | - $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
| 45 | - (new AnnotationRouteLoader)->discover($root . '/AnnotationRouteNameMissing'); |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * @return void |
|
| 50 | - */ |
|
| 51 | - public function testAnnotationRouteNameEmpty() : void |
|
| 52 | - { |
|
| 53 | - $this->expectException(InvalidArgumentException::class); |
|
| 54 | - $this->expectExceptionMessage('@Route.name must be not an empty string.'); |
|
| 55 | - |
|
| 56 | - $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
| 57 | - (new AnnotationRouteLoader)->discover($root . '/AnnotationRouteNameEmpty'); |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * @return void |
|
| 62 | - */ |
|
| 63 | - public function testAnnotationRouteNameNotString() : void |
|
| 64 | - { |
|
| 65 | - $this->expectException(InvalidArgumentException::class); |
|
| 66 | - $this->expectExceptionMessage('@Route.name must be not an empty string.'); |
|
| 67 | - |
|
| 68 | - $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
| 69 | - (new AnnotationRouteLoader)->discover($root . '/AnnotationRouteNameNotString'); |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * @return void |
|
| 74 | - */ |
|
| 75 | - public function testAnnotationRoutePathMissing() : void |
|
| 76 | - { |
|
| 77 | - $this->expectException(InvalidArgumentException::class); |
|
| 78 | - $this->expectExceptionMessage('@Route.path must be not an empty string.'); |
|
| 79 | - |
|
| 80 | - $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
| 81 | - (new AnnotationRouteLoader)->discover($root . '/AnnotationRoutePathMissing'); |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * @return void |
|
| 86 | - */ |
|
| 87 | - public function testAnnotationRoutePathEmpty() : void |
|
| 88 | - { |
|
| 89 | - $this->expectException(InvalidArgumentException::class); |
|
| 90 | - $this->expectExceptionMessage('@Route.path must be not an empty string.'); |
|
| 91 | - |
|
| 92 | - $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
| 93 | - (new AnnotationRouteLoader)->discover($root . '/AnnotationRoutePathEmpty'); |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * @return void |
|
| 98 | - */ |
|
| 99 | - public function testAnnotationRoutePathNotString() : void |
|
| 100 | - { |
|
| 101 | - $this->expectException(InvalidArgumentException::class); |
|
| 102 | - $this->expectExceptionMessage('@Route.path must be not an empty string.'); |
|
| 103 | - |
|
| 104 | - $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
| 105 | - (new AnnotationRouteLoader)->discover($root . '/AnnotationRoutePathNotString'); |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * @return void |
|
| 110 | - */ |
|
| 111 | - public function testAnnotationRouteMethodsMissing() : void |
|
| 112 | - { |
|
| 113 | - $this->expectException(InvalidArgumentException::class); |
|
| 114 | - $this->expectExceptionMessage('@Route.methods must be not an empty array.'); |
|
| 115 | - |
|
| 116 | - $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
| 117 | - (new AnnotationRouteLoader)->discover($root . '/AnnotationRouteMethodsMissing'); |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * @return void |
|
| 122 | - */ |
|
| 123 | - public function testAnnotationRouteMethodsEmpty() : void |
|
| 124 | - { |
|
| 125 | - $this->expectException(InvalidArgumentException::class); |
|
| 126 | - $this->expectExceptionMessage('@Route.methods must be not an empty array.'); |
|
| 127 | - |
|
| 128 | - $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
| 129 | - (new AnnotationRouteLoader)->discover($root . '/AnnotationRouteMethodsEmpty'); |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * @return void |
|
| 134 | - */ |
|
| 135 | - public function testAnnotationRouteMethodsNotArray() : void |
|
| 136 | - { |
|
| 137 | - $this->expectException(InvalidArgumentException::class); |
|
| 138 | - $this->expectExceptionMessage('@Route.methods must be not an empty array.'); |
|
| 139 | - |
|
| 140 | - $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
| 141 | - (new AnnotationRouteLoader)->discover($root . '/AnnotationRouteMethodsNotArray'); |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * @return void |
|
| 146 | - */ |
|
| 147 | - public function testAnnotationRouteMethodsNotStringable() : void |
|
| 148 | - { |
|
| 149 | - $this->expectException(InvalidArgumentException::class); |
|
| 150 | - $this->expectExceptionMessage('@Route.methods must contain only strings.'); |
|
| 151 | - |
|
| 152 | - $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
| 153 | - (new AnnotationRouteLoader)->discover($root . '/AnnotationRouteMethodsNotStringable'); |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - /** |
|
| 157 | - * @return void |
|
| 158 | - */ |
|
| 159 | - public function testAnnotationRouteMiddlewaresNotArray() : void |
|
| 160 | - { |
|
| 161 | - $this->expectException(InvalidArgumentException::class); |
|
| 162 | - $this->expectExceptionMessage('@Route.middlewares must be an array.'); |
|
| 163 | - |
|
| 164 | - $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
| 165 | - (new AnnotationRouteLoader)->discover($root . '/AnnotationRouteMiddlewaresNotArray'); |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - /** |
|
| 169 | - * @return void |
|
| 170 | - */ |
|
| 171 | - public function testAnnotationRouteMiddlewaresNotStringable() : void |
|
| 172 | - { |
|
| 173 | - $this->expectException(InvalidArgumentException::class); |
|
| 174 | - $this->expectExceptionMessage('@Route.middlewares must contain only middlewares.'); |
|
| 175 | - |
|
| 176 | - $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
| 177 | - (new AnnotationRouteLoader)->discover($root . '/AnnotationRouteMiddlewaresNotStringable'); |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * @return void |
|
| 182 | - */ |
|
| 183 | - public function testAnnotationRouteMiddlewaresContainNonexistenceClass() : void |
|
| 184 | - { |
|
| 185 | - $this->expectException(InvalidArgumentException::class); |
|
| 186 | - $this->expectExceptionMessage('@Route.middlewares must contain only middlewares.'); |
|
| 187 | - |
|
| 188 | - $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
| 189 | - (new AnnotationRouteLoader)->discover($root . '/AnnotationRouteMiddlewaresContainNonexistenceClass'); |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - /** |
|
| 193 | - * @return void |
|
| 194 | - */ |
|
| 195 | - public function testAnnotationRouteMiddlewaresContainNotMiddleware() : void |
|
| 196 | - { |
|
| 197 | - $this->expectException(InvalidArgumentException::class); |
|
| 198 | - $this->expectExceptionMessage('@Route.middlewares must contain only middlewares.'); |
|
| 199 | - |
|
| 200 | - $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
| 201 | - (new AnnotationRouteLoader)->discover($root . '/AnnotationRouteMiddlewaresContainNotMiddleware'); |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - /** |
|
| 205 | - * @return void |
|
| 206 | - */ |
|
| 207 | - public function testAnnotationRouteAttributesNotArray() : void |
|
| 208 | - { |
|
| 209 | - $this->expectException(InvalidArgumentException::class); |
|
| 210 | - $this->expectExceptionMessage('@Route.attributes must be an array.'); |
|
| 211 | - |
|
| 212 | - $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
| 213 | - (new AnnotationRouteLoader)->discover($root . '/AnnotationRouteAttributesNotArray'); |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - /** |
|
| 217 | - * @return void |
|
| 218 | - */ |
|
| 219 | - public function testAnnotationRoutePriorityNotInteger() : void |
|
| 220 | - { |
|
| 221 | - $this->expectException(InvalidArgumentException::class); |
|
| 222 | - $this->expectExceptionMessage('@Route.priority must be an integer.'); |
|
| 223 | - |
|
| 224 | - $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
| 225 | - (new AnnotationRouteLoader)->discover($root . '/AnnotationRoutePriorityNotInteger'); |
|
| 226 | - } |
|
| 227 | - |
|
| 228 | - /** |
|
| 229 | - * @return void |
|
| 230 | - * |
|
| 231 | - * @todo This test needs to be improved... |
|
| 232 | - */ |
|
| 233 | - public function testBuildRoutes() : void |
|
| 234 | - { |
|
| 235 | - $loader = new AnnotationRouteLoader(); |
|
| 236 | - $loader->discover(__DIR__ . '/../Fixture/Annotation/AnnotationRouteValid'); |
|
| 237 | - |
|
| 238 | - $routesMap = []; |
|
| 239 | - $builtRoutes = $loader->buildRoutes(); |
|
| 240 | - |
|
| 241 | - foreach ($builtRoutes as $i => $route) { |
|
| 242 | - $routesMap[$i]['name'] = $route->getName(); |
|
| 243 | - $routesMap[$i]['path'] = $route->getPath(); |
|
| 244 | - $routesMap[$i]['methods'] = $route->getMethods(); |
|
| 245 | - $routesMap[$i]['requestHandler'] = get_class($route->getRequestHandler()); |
|
| 246 | - $routesMap[$i]['middlewares'] = []; |
|
| 247 | - $routesMap[$i]['attributes'] = $route->getAttributes(); |
|
| 248 | - |
|
| 249 | - foreach ($route->getMiddlewares() as $middleware) { |
|
| 250 | - $routesMap[$i]['middlewares'][] = get_class($middleware); |
|
| 251 | - } |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - $expectedMap = [ |
|
| 255 | - [ |
|
| 256 | - 'name' => 'quuuux', |
|
| 257 | - 'path' => '/quuuux', |
|
| 258 | - 'methods' => ['PATCH', 'DELETE'], |
|
| 259 | - 'requestHandler' => 'Sunrise\Http\Router\Tests\Fixture\Annotation' . |
|
| 260 | - '\AnnotationRouteValid\Subdirectory\QuuuuxRequestHandler', |
|
| 261 | - 'middlewares' => [ |
|
| 262 | - 'Sunrise\Http\Router\Tests\Fixture\BlankMiddleware', |
|
| 263 | - 'Sunrise\Http\Router\Tests\Fixture\BlankMiddleware', |
|
| 264 | - ], |
|
| 265 | - 'attributes' => [ |
|
| 266 | - 'foo' => 'bar', |
|
| 267 | - 'source' => 'quuuux', |
|
| 268 | - ], |
|
| 269 | - ], |
|
| 270 | - [ |
|
| 271 | - 'name' => 'quuux', |
|
| 272 | - 'path' => '/quuux', |
|
| 273 | - 'methods' => ['PUT', 'PATCH'], |
|
| 274 | - 'requestHandler' => 'Sunrise\Http\Router\Tests\Fixture\Annotation' . |
|
| 275 | - '\AnnotationRouteValid\Subdirectory\QuuuxRequestHandler', |
|
| 276 | - 'middlewares' => [ |
|
| 277 | - 'Sunrise\Http\Router\Tests\Fixture\BlankMiddleware', |
|
| 278 | - 'Sunrise\Http\Router\Tests\Fixture\BlankMiddleware', |
|
| 279 | - ], |
|
| 280 | - 'attributes' => [ |
|
| 281 | - 'foo' => 'bar', |
|
| 282 | - 'source' => 'quuux', |
|
| 283 | - ], |
|
| 284 | - ], |
|
| 285 | - [ |
|
| 286 | - 'name' => 'quux', |
|
| 287 | - 'path' => '/quux', |
|
| 288 | - 'methods' => ['POST', 'PUT'], |
|
| 289 | - 'requestHandler' => 'Sunrise\Http\Router\Tests\Fixture\Annotation' . |
|
| 290 | - '\AnnotationRouteValid\QuuxRequestHandler', |
|
| 291 | - 'middlewares' => [ |
|
| 292 | - 'Sunrise\Http\Router\Tests\Fixture\BlankMiddleware', |
|
| 293 | - 'Sunrise\Http\Router\Tests\Fixture\BlankMiddleware', |
|
| 294 | - ], |
|
| 295 | - 'attributes' => [ |
|
| 296 | - 'foo' => 'bar', |
|
| 297 | - 'source' => 'quux', |
|
| 298 | - ], |
|
| 299 | - ], |
|
| 300 | - [ |
|
| 301 | - 'name' => 'qux', |
|
| 302 | - 'path' => '/qux', |
|
| 303 | - 'methods' => ['GET', 'POST'], |
|
| 304 | - 'requestHandler' => 'Sunrise\Http\Router\Tests\Fixture\Annotation' . |
|
| 305 | - '\AnnotationRouteValid\QuxRequestHandler', |
|
| 306 | - 'middlewares' => [ |
|
| 307 | - 'Sunrise\Http\Router\Tests\Fixture\BlankMiddleware', |
|
| 308 | - 'Sunrise\Http\Router\Tests\Fixture\BlankMiddleware', |
|
| 309 | - ], |
|
| 310 | - 'attributes' => [ |
|
| 311 | - 'foo' => 'bar', |
|
| 312 | - 'source' => 'qux', |
|
| 313 | - ], |
|
| 314 | - ], |
|
| 315 | - ]; |
|
| 316 | - |
|
| 317 | - $this->assertSame($expectedMap, $routesMap); |
|
| 318 | - } |
|
| 26 | + /** |
|
| 27 | + * @return void |
|
| 28 | + */ |
|
| 29 | + public static function setUpBeforeClass() : void |
|
| 30 | + { |
|
| 31 | + if (! class_exists('Route')) { |
|
| 32 | + class_alias(AnnotationRoute::class, 'Route'); |
|
| 33 | + } |
|
| 34 | + } |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * @return void |
|
| 38 | + */ |
|
| 39 | + public function testAnnotationRouteNameMissing() : void |
|
| 40 | + { |
|
| 41 | + $this->expectException(InvalidArgumentException::class); |
|
| 42 | + $this->expectExceptionMessage('@Route.name must be not an empty string.'); |
|
| 43 | + |
|
| 44 | + $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
| 45 | + (new AnnotationRouteLoader)->discover($root . '/AnnotationRouteNameMissing'); |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * @return void |
|
| 50 | + */ |
|
| 51 | + public function testAnnotationRouteNameEmpty() : void |
|
| 52 | + { |
|
| 53 | + $this->expectException(InvalidArgumentException::class); |
|
| 54 | + $this->expectExceptionMessage('@Route.name must be not an empty string.'); |
|
| 55 | + |
|
| 56 | + $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
| 57 | + (new AnnotationRouteLoader)->discover($root . '/AnnotationRouteNameEmpty'); |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * @return void |
|
| 62 | + */ |
|
| 63 | + public function testAnnotationRouteNameNotString() : void |
|
| 64 | + { |
|
| 65 | + $this->expectException(InvalidArgumentException::class); |
|
| 66 | + $this->expectExceptionMessage('@Route.name must be not an empty string.'); |
|
| 67 | + |
|
| 68 | + $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
| 69 | + (new AnnotationRouteLoader)->discover($root . '/AnnotationRouteNameNotString'); |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * @return void |
|
| 74 | + */ |
|
| 75 | + public function testAnnotationRoutePathMissing() : void |
|
| 76 | + { |
|
| 77 | + $this->expectException(InvalidArgumentException::class); |
|
| 78 | + $this->expectExceptionMessage('@Route.path must be not an empty string.'); |
|
| 79 | + |
|
| 80 | + $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
| 81 | + (new AnnotationRouteLoader)->discover($root . '/AnnotationRoutePathMissing'); |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * @return void |
|
| 86 | + */ |
|
| 87 | + public function testAnnotationRoutePathEmpty() : void |
|
| 88 | + { |
|
| 89 | + $this->expectException(InvalidArgumentException::class); |
|
| 90 | + $this->expectExceptionMessage('@Route.path must be not an empty string.'); |
|
| 91 | + |
|
| 92 | + $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
| 93 | + (new AnnotationRouteLoader)->discover($root . '/AnnotationRoutePathEmpty'); |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * @return void |
|
| 98 | + */ |
|
| 99 | + public function testAnnotationRoutePathNotString() : void |
|
| 100 | + { |
|
| 101 | + $this->expectException(InvalidArgumentException::class); |
|
| 102 | + $this->expectExceptionMessage('@Route.path must be not an empty string.'); |
|
| 103 | + |
|
| 104 | + $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
| 105 | + (new AnnotationRouteLoader)->discover($root . '/AnnotationRoutePathNotString'); |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * @return void |
|
| 110 | + */ |
|
| 111 | + public function testAnnotationRouteMethodsMissing() : void |
|
| 112 | + { |
|
| 113 | + $this->expectException(InvalidArgumentException::class); |
|
| 114 | + $this->expectExceptionMessage('@Route.methods must be not an empty array.'); |
|
| 115 | + |
|
| 116 | + $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
| 117 | + (new AnnotationRouteLoader)->discover($root . '/AnnotationRouteMethodsMissing'); |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * @return void |
|
| 122 | + */ |
|
| 123 | + public function testAnnotationRouteMethodsEmpty() : void |
|
| 124 | + { |
|
| 125 | + $this->expectException(InvalidArgumentException::class); |
|
| 126 | + $this->expectExceptionMessage('@Route.methods must be not an empty array.'); |
|
| 127 | + |
|
| 128 | + $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
| 129 | + (new AnnotationRouteLoader)->discover($root . '/AnnotationRouteMethodsEmpty'); |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * @return void |
|
| 134 | + */ |
|
| 135 | + public function testAnnotationRouteMethodsNotArray() : void |
|
| 136 | + { |
|
| 137 | + $this->expectException(InvalidArgumentException::class); |
|
| 138 | + $this->expectExceptionMessage('@Route.methods must be not an empty array.'); |
|
| 139 | + |
|
| 140 | + $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
| 141 | + (new AnnotationRouteLoader)->discover($root . '/AnnotationRouteMethodsNotArray'); |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * @return void |
|
| 146 | + */ |
|
| 147 | + public function testAnnotationRouteMethodsNotStringable() : void |
|
| 148 | + { |
|
| 149 | + $this->expectException(InvalidArgumentException::class); |
|
| 150 | + $this->expectExceptionMessage('@Route.methods must contain only strings.'); |
|
| 151 | + |
|
| 152 | + $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
| 153 | + (new AnnotationRouteLoader)->discover($root . '/AnnotationRouteMethodsNotStringable'); |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + /** |
|
| 157 | + * @return void |
|
| 158 | + */ |
|
| 159 | + public function testAnnotationRouteMiddlewaresNotArray() : void |
|
| 160 | + { |
|
| 161 | + $this->expectException(InvalidArgumentException::class); |
|
| 162 | + $this->expectExceptionMessage('@Route.middlewares must be an array.'); |
|
| 163 | + |
|
| 164 | + $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
| 165 | + (new AnnotationRouteLoader)->discover($root . '/AnnotationRouteMiddlewaresNotArray'); |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + /** |
|
| 169 | + * @return void |
|
| 170 | + */ |
|
| 171 | + public function testAnnotationRouteMiddlewaresNotStringable() : void |
|
| 172 | + { |
|
| 173 | + $this->expectException(InvalidArgumentException::class); |
|
| 174 | + $this->expectExceptionMessage('@Route.middlewares must contain only middlewares.'); |
|
| 175 | + |
|
| 176 | + $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
| 177 | + (new AnnotationRouteLoader)->discover($root . '/AnnotationRouteMiddlewaresNotStringable'); |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * @return void |
|
| 182 | + */ |
|
| 183 | + public function testAnnotationRouteMiddlewaresContainNonexistenceClass() : void |
|
| 184 | + { |
|
| 185 | + $this->expectException(InvalidArgumentException::class); |
|
| 186 | + $this->expectExceptionMessage('@Route.middlewares must contain only middlewares.'); |
|
| 187 | + |
|
| 188 | + $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
| 189 | + (new AnnotationRouteLoader)->discover($root . '/AnnotationRouteMiddlewaresContainNonexistenceClass'); |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + /** |
|
| 193 | + * @return void |
|
| 194 | + */ |
|
| 195 | + public function testAnnotationRouteMiddlewaresContainNotMiddleware() : void |
|
| 196 | + { |
|
| 197 | + $this->expectException(InvalidArgumentException::class); |
|
| 198 | + $this->expectExceptionMessage('@Route.middlewares must contain only middlewares.'); |
|
| 199 | + |
|
| 200 | + $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
| 201 | + (new AnnotationRouteLoader)->discover($root . '/AnnotationRouteMiddlewaresContainNotMiddleware'); |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + /** |
|
| 205 | + * @return void |
|
| 206 | + */ |
|
| 207 | + public function testAnnotationRouteAttributesNotArray() : void |
|
| 208 | + { |
|
| 209 | + $this->expectException(InvalidArgumentException::class); |
|
| 210 | + $this->expectExceptionMessage('@Route.attributes must be an array.'); |
|
| 211 | + |
|
| 212 | + $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
| 213 | + (new AnnotationRouteLoader)->discover($root . '/AnnotationRouteAttributesNotArray'); |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + /** |
|
| 217 | + * @return void |
|
| 218 | + */ |
|
| 219 | + public function testAnnotationRoutePriorityNotInteger() : void |
|
| 220 | + { |
|
| 221 | + $this->expectException(InvalidArgumentException::class); |
|
| 222 | + $this->expectExceptionMessage('@Route.priority must be an integer.'); |
|
| 223 | + |
|
| 224 | + $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
| 225 | + (new AnnotationRouteLoader)->discover($root . '/AnnotationRoutePriorityNotInteger'); |
|
| 226 | + } |
|
| 227 | + |
|
| 228 | + /** |
|
| 229 | + * @return void |
|
| 230 | + * |
|
| 231 | + * @todo This test needs to be improved... |
|
| 232 | + */ |
|
| 233 | + public function testBuildRoutes() : void |
|
| 234 | + { |
|
| 235 | + $loader = new AnnotationRouteLoader(); |
|
| 236 | + $loader->discover(__DIR__ . '/../Fixture/Annotation/AnnotationRouteValid'); |
|
| 237 | + |
|
| 238 | + $routesMap = []; |
|
| 239 | + $builtRoutes = $loader->buildRoutes(); |
|
| 240 | + |
|
| 241 | + foreach ($builtRoutes as $i => $route) { |
|
| 242 | + $routesMap[$i]['name'] = $route->getName(); |
|
| 243 | + $routesMap[$i]['path'] = $route->getPath(); |
|
| 244 | + $routesMap[$i]['methods'] = $route->getMethods(); |
|
| 245 | + $routesMap[$i]['requestHandler'] = get_class($route->getRequestHandler()); |
|
| 246 | + $routesMap[$i]['middlewares'] = []; |
|
| 247 | + $routesMap[$i]['attributes'] = $route->getAttributes(); |
|
| 248 | + |
|
| 249 | + foreach ($route->getMiddlewares() as $middleware) { |
|
| 250 | + $routesMap[$i]['middlewares'][] = get_class($middleware); |
|
| 251 | + } |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + $expectedMap = [ |
|
| 255 | + [ |
|
| 256 | + 'name' => 'quuuux', |
|
| 257 | + 'path' => '/quuuux', |
|
| 258 | + 'methods' => ['PATCH', 'DELETE'], |
|
| 259 | + 'requestHandler' => 'Sunrise\Http\Router\Tests\Fixture\Annotation' . |
|
| 260 | + '\AnnotationRouteValid\Subdirectory\QuuuuxRequestHandler', |
|
| 261 | + 'middlewares' => [ |
|
| 262 | + 'Sunrise\Http\Router\Tests\Fixture\BlankMiddleware', |
|
| 263 | + 'Sunrise\Http\Router\Tests\Fixture\BlankMiddleware', |
|
| 264 | + ], |
|
| 265 | + 'attributes' => [ |
|
| 266 | + 'foo' => 'bar', |
|
| 267 | + 'source' => 'quuuux', |
|
| 268 | + ], |
|
| 269 | + ], |
|
| 270 | + [ |
|
| 271 | + 'name' => 'quuux', |
|
| 272 | + 'path' => '/quuux', |
|
| 273 | + 'methods' => ['PUT', 'PATCH'], |
|
| 274 | + 'requestHandler' => 'Sunrise\Http\Router\Tests\Fixture\Annotation' . |
|
| 275 | + '\AnnotationRouteValid\Subdirectory\QuuuxRequestHandler', |
|
| 276 | + 'middlewares' => [ |
|
| 277 | + 'Sunrise\Http\Router\Tests\Fixture\BlankMiddleware', |
|
| 278 | + 'Sunrise\Http\Router\Tests\Fixture\BlankMiddleware', |
|
| 279 | + ], |
|
| 280 | + 'attributes' => [ |
|
| 281 | + 'foo' => 'bar', |
|
| 282 | + 'source' => 'quuux', |
|
| 283 | + ], |
|
| 284 | + ], |
|
| 285 | + [ |
|
| 286 | + 'name' => 'quux', |
|
| 287 | + 'path' => '/quux', |
|
| 288 | + 'methods' => ['POST', 'PUT'], |
|
| 289 | + 'requestHandler' => 'Sunrise\Http\Router\Tests\Fixture\Annotation' . |
|
| 290 | + '\AnnotationRouteValid\QuuxRequestHandler', |
|
| 291 | + 'middlewares' => [ |
|
| 292 | + 'Sunrise\Http\Router\Tests\Fixture\BlankMiddleware', |
|
| 293 | + 'Sunrise\Http\Router\Tests\Fixture\BlankMiddleware', |
|
| 294 | + ], |
|
| 295 | + 'attributes' => [ |
|
| 296 | + 'foo' => 'bar', |
|
| 297 | + 'source' => 'quux', |
|
| 298 | + ], |
|
| 299 | + ], |
|
| 300 | + [ |
|
| 301 | + 'name' => 'qux', |
|
| 302 | + 'path' => '/qux', |
|
| 303 | + 'methods' => ['GET', 'POST'], |
|
| 304 | + 'requestHandler' => 'Sunrise\Http\Router\Tests\Fixture\Annotation' . |
|
| 305 | + '\AnnotationRouteValid\QuxRequestHandler', |
|
| 306 | + 'middlewares' => [ |
|
| 307 | + 'Sunrise\Http\Router\Tests\Fixture\BlankMiddleware', |
|
| 308 | + 'Sunrise\Http\Router\Tests\Fixture\BlankMiddleware', |
|
| 309 | + ], |
|
| 310 | + 'attributes' => [ |
|
| 311 | + 'foo' => 'bar', |
|
| 312 | + 'source' => 'qux', |
|
| 313 | + ], |
|
| 314 | + ], |
|
| 315 | + ]; |
|
| 316 | + |
|
| 317 | + $this->assertSame($expectedMap, $routesMap); |
|
| 318 | + } |
|
| 319 | 319 | } |
@@ -28,7 +28,7 @@ |
||
| 28 | 28 | */ |
| 29 | 29 | public static function setUpBeforeClass() : void |
| 30 | 30 | { |
| 31 | - if (! class_exists('Route')) { |
|
| 31 | + if (!class_exists('Route')) { |
|
| 32 | 32 | class_alias(AnnotationRoute::class, 'Route'); |
| 33 | 33 | } |
| 34 | 34 | } |
@@ -17,28 +17,28 @@ |
||
| 17 | 17 | class CallableRequestHandlerTest extends TestCase |
| 18 | 18 | { |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * @return void |
|
| 22 | - */ |
|
| 23 | - public function testConstructor() : void |
|
| 24 | - { |
|
| 25 | - $callback = new Fixture\BlankRequestHandler(); |
|
| 26 | - $requestHandler = new CallableRequestHandler($callback); |
|
| 27 | - |
|
| 28 | - $this->assertInstanceOf(RequestHandlerInterface::class, $requestHandler); |
|
| 29 | - } |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * @return void |
|
| 33 | - */ |
|
| 34 | - public function testHandle() : void |
|
| 35 | - { |
|
| 36 | - $callback = new Fixture\BlankRequestHandler(); |
|
| 37 | - $requestHandler = new CallableRequestHandler($callback); |
|
| 38 | - |
|
| 39 | - $request = (new ServerRequestFactory)->createServerRequest('GET', '/'); |
|
| 40 | - $requestHandler->handle($request); |
|
| 41 | - |
|
| 42 | - $this->assertTrue($callback->isRunned()); |
|
| 43 | - } |
|
| 20 | + /** |
|
| 21 | + * @return void |
|
| 22 | + */ |
|
| 23 | + public function testConstructor() : void |
|
| 24 | + { |
|
| 25 | + $callback = new Fixture\BlankRequestHandler(); |
|
| 26 | + $requestHandler = new CallableRequestHandler($callback); |
|
| 27 | + |
|
| 28 | + $this->assertInstanceOf(RequestHandlerInterface::class, $requestHandler); |
|
| 29 | + } |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * @return void |
|
| 33 | + */ |
|
| 34 | + public function testHandle() : void |
|
| 35 | + { |
|
| 36 | + $callback = new Fixture\BlankRequestHandler(); |
|
| 37 | + $requestHandler = new CallableRequestHandler($callback); |
|
| 38 | + |
|
| 39 | + $request = (new ServerRequestFactory)->createServerRequest('GET', '/'); |
|
| 40 | + $requestHandler->handle($request); |
|
| 41 | + |
|
| 42 | + $this->assertTrue($callback->isRunned()); |
|
| 43 | + } |
|
| 44 | 44 | } |
@@ -17,76 +17,76 @@ |
||
| 17 | 17 | class QueueableRequestHandlerTest extends TestCase |
| 18 | 18 | { |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * @return void |
|
| 22 | - */ |
|
| 23 | - public function testConstructor() : void |
|
| 24 | - { |
|
| 25 | - $endpoint = new Fixture\BlankRequestHandler(); |
|
| 26 | - $requestHandler = new QueueableRequestHandler($endpoint); |
|
| 27 | - |
|
| 28 | - $this->assertInstanceOf(RequestHandlerInterface::class, $requestHandler); |
|
| 29 | - } |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * @return void |
|
| 33 | - */ |
|
| 34 | - public function testHandle() : void |
|
| 35 | - { |
|
| 36 | - $endpoint = new Fixture\BlankRequestHandler(); |
|
| 37 | - $requestHandler = new QueueableRequestHandler($endpoint); |
|
| 38 | - |
|
| 39 | - $request = (new ServerRequestFactory)->createServerRequest('GET', '/'); |
|
| 40 | - $requestHandler->handle($request); |
|
| 41 | - |
|
| 42 | - $this->assertTrue($endpoint->isRunned()); |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * @return void |
|
| 47 | - */ |
|
| 48 | - public function testHandleWithMiddlewares() : void |
|
| 49 | - { |
|
| 50 | - $middlewares = [ |
|
| 51 | - new Fixture\BlankMiddleware(), |
|
| 52 | - new Fixture\BlankMiddleware(), |
|
| 53 | - new Fixture\BlankMiddleware(), |
|
| 54 | - ]; |
|
| 55 | - |
|
| 56 | - $endpoint = new Fixture\BlankRequestHandler(); |
|
| 57 | - $requestHandler = new QueueableRequestHandler($endpoint); |
|
| 58 | - $requestHandler->add(...$middlewares); |
|
| 59 | - |
|
| 60 | - $request = (new ServerRequestFactory)->createServerRequest('GET', '/'); |
|
| 61 | - $requestHandler->handle($request); |
|
| 62 | - |
|
| 63 | - $this->assertTrue($middlewares[0]->isRunned()); |
|
| 64 | - $this->assertTrue($middlewares[1]->isRunned()); |
|
| 65 | - $this->assertTrue($middlewares[2]->isRunned()); |
|
| 66 | - $this->assertTrue($endpoint->isRunned()); |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * @return void |
|
| 71 | - */ |
|
| 72 | - public function testHandleWithBrokenMiddleware() : void |
|
| 73 | - { |
|
| 74 | - $middlewares = [ |
|
| 75 | - new Fixture\BlankMiddleware(), |
|
| 76 | - new Fixture\BlankMiddleware(true), |
|
| 77 | - new Fixture\BlankMiddleware(), |
|
| 78 | - ]; |
|
| 79 | - |
|
| 80 | - $endpoint = new Fixture\BlankRequestHandler(); |
|
| 81 | - $requestHandler = new QueueableRequestHandler($endpoint); |
|
| 82 | - $requestHandler->add(...$middlewares); |
|
| 83 | - |
|
| 84 | - $request = (new ServerRequestFactory)->createServerRequest('GET', '/'); |
|
| 85 | - $requestHandler->handle($request); |
|
| 86 | - |
|
| 87 | - $this->assertTrue($middlewares[0]->isRunned()); |
|
| 88 | - $this->assertTrue($middlewares[1]->isRunned()); |
|
| 89 | - $this->assertFalse($middlewares[2]->isRunned()); |
|
| 90 | - $this->assertFalse($endpoint->isRunned()); |
|
| 91 | - } |
|
| 20 | + /** |
|
| 21 | + * @return void |
|
| 22 | + */ |
|
| 23 | + public function testConstructor() : void |
|
| 24 | + { |
|
| 25 | + $endpoint = new Fixture\BlankRequestHandler(); |
|
| 26 | + $requestHandler = new QueueableRequestHandler($endpoint); |
|
| 27 | + |
|
| 28 | + $this->assertInstanceOf(RequestHandlerInterface::class, $requestHandler); |
|
| 29 | + } |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * @return void |
|
| 33 | + */ |
|
| 34 | + public function testHandle() : void |
|
| 35 | + { |
|
| 36 | + $endpoint = new Fixture\BlankRequestHandler(); |
|
| 37 | + $requestHandler = new QueueableRequestHandler($endpoint); |
|
| 38 | + |
|
| 39 | + $request = (new ServerRequestFactory)->createServerRequest('GET', '/'); |
|
| 40 | + $requestHandler->handle($request); |
|
| 41 | + |
|
| 42 | + $this->assertTrue($endpoint->isRunned()); |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * @return void |
|
| 47 | + */ |
|
| 48 | + public function testHandleWithMiddlewares() : void |
|
| 49 | + { |
|
| 50 | + $middlewares = [ |
|
| 51 | + new Fixture\BlankMiddleware(), |
|
| 52 | + new Fixture\BlankMiddleware(), |
|
| 53 | + new Fixture\BlankMiddleware(), |
|
| 54 | + ]; |
|
| 55 | + |
|
| 56 | + $endpoint = new Fixture\BlankRequestHandler(); |
|
| 57 | + $requestHandler = new QueueableRequestHandler($endpoint); |
|
| 58 | + $requestHandler->add(...$middlewares); |
|
| 59 | + |
|
| 60 | + $request = (new ServerRequestFactory)->createServerRequest('GET', '/'); |
|
| 61 | + $requestHandler->handle($request); |
|
| 62 | + |
|
| 63 | + $this->assertTrue($middlewares[0]->isRunned()); |
|
| 64 | + $this->assertTrue($middlewares[1]->isRunned()); |
|
| 65 | + $this->assertTrue($middlewares[2]->isRunned()); |
|
| 66 | + $this->assertTrue($endpoint->isRunned()); |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * @return void |
|
| 71 | + */ |
|
| 72 | + public function testHandleWithBrokenMiddleware() : void |
|
| 73 | + { |
|
| 74 | + $middlewares = [ |
|
| 75 | + new Fixture\BlankMiddleware(), |
|
| 76 | + new Fixture\BlankMiddleware(true), |
|
| 77 | + new Fixture\BlankMiddleware(), |
|
| 78 | + ]; |
|
| 79 | + |
|
| 80 | + $endpoint = new Fixture\BlankRequestHandler(); |
|
| 81 | + $requestHandler = new QueueableRequestHandler($endpoint); |
|
| 82 | + $requestHandler->add(...$middlewares); |
|
| 83 | + |
|
| 84 | + $request = (new ServerRequestFactory)->createServerRequest('GET', '/'); |
|
| 85 | + $requestHandler->handle($request); |
|
| 86 | + |
|
| 87 | + $this->assertTrue($middlewares[0]->isRunned()); |
|
| 88 | + $this->assertTrue($middlewares[1]->isRunned()); |
|
| 89 | + $this->assertFalse($middlewares[2]->isRunned()); |
|
| 90 | + $this->assertFalse($endpoint->isRunned()); |
|
| 91 | + } |
|
| 92 | 92 | } |