Repository: slimphp/Slim Branch: 4.x Commit: 025043ec303c Files: 145 Total size: 576.9 KB Directory structure: gitextract_5dpb6hp9/ ├── .coveralls.yml ├── .editorconfig ├── .gitattributes ├── .github/ │ ├── FUNDING.yml │ ├── dependabot.yml │ └── workflows/ │ └── tests.yml ├── .gitignore ├── CHANGELOG.md ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── LICENSE.md ├── MAINTAINERS.md ├── README.md ├── SECURITY.md ├── Slim/ │ ├── App.php │ ├── CallableResolver.php │ ├── Error/ │ │ ├── AbstractErrorRenderer.php │ │ └── Renderers/ │ │ ├── HtmlErrorRenderer.php │ │ ├── JsonErrorRenderer.php │ │ ├── PlainTextErrorRenderer.php │ │ └── XmlErrorRenderer.php │ ├── Exception/ │ │ ├── HttpBadRequestException.php │ │ ├── HttpException.php │ │ ├── HttpForbiddenException.php │ │ ├── HttpGoneException.php │ │ ├── HttpInternalServerErrorException.php │ │ ├── HttpMethodNotAllowedException.php │ │ ├── HttpNotFoundException.php │ │ ├── HttpNotImplementedException.php │ │ ├── HttpSpecializedException.php │ │ ├── HttpTooManyRequestsException.php │ │ └── HttpUnauthorizedException.php │ ├── Factory/ │ │ ├── AppFactory.php │ │ ├── Psr17/ │ │ │ ├── GuzzlePsr17Factory.php │ │ │ ├── HttpSoftPsr17Factory.php │ │ │ ├── LaminasDiactorosPsr17Factory.php │ │ │ ├── NyholmPsr17Factory.php │ │ │ ├── Psr17Factory.php │ │ │ ├── Psr17FactoryProvider.php │ │ │ ├── ServerRequestCreator.php │ │ │ ├── SlimHttpPsr17Factory.php │ │ │ ├── SlimHttpServerRequestCreator.php │ │ │ └── SlimPsr17Factory.php │ │ └── ServerRequestCreatorFactory.php │ ├── Handlers/ │ │ ├── ErrorHandler.php │ │ └── Strategies/ │ │ ├── RequestHandler.php │ │ ├── RequestResponse.php │ │ ├── RequestResponseArgs.php │ │ └── RequestResponseNamedArgs.php │ ├── Interfaces/ │ │ ├── AdvancedCallableResolverInterface.php │ │ ├── CallableResolverInterface.php │ │ ├── DispatcherInterface.php │ │ ├── ErrorHandlerInterface.php │ │ ├── ErrorRendererInterface.php │ │ ├── InvocationStrategyInterface.php │ │ ├── MiddlewareDispatcherInterface.php │ │ ├── Psr17FactoryInterface.php │ │ ├── Psr17FactoryProviderInterface.php │ │ ├── RequestHandlerInvocationStrategyInterface.php │ │ ├── RouteCollectorInterface.php │ │ ├── RouteCollectorProxyInterface.php │ │ ├── RouteGroupInterface.php │ │ ├── RouteInterface.php │ │ ├── RouteParserInterface.php │ │ ├── RouteResolverInterface.php │ │ └── ServerRequestCreatorInterface.php │ ├── Logger.php │ ├── Middleware/ │ │ ├── BodyParsingMiddleware.php │ │ ├── ContentLengthMiddleware.php │ │ ├── ErrorMiddleware.php │ │ ├── MethodOverrideMiddleware.php │ │ ├── OutputBufferingMiddleware.php │ │ └── RoutingMiddleware.php │ ├── MiddlewareDispatcher.php │ ├── ResponseEmitter.php │ └── Routing/ │ ├── Dispatcher.php │ ├── FastRouteDispatcher.php │ ├── Route.php │ ├── RouteCollector.php │ ├── RouteCollectorProxy.php │ ├── RouteContext.php │ ├── RouteGroup.php │ ├── RouteParser.php │ ├── RouteResolver.php │ ├── RouteRunner.php │ └── RoutingResults.php ├── UPGRADING.md ├── composer.json ├── phpcs.xml.dist ├── phpstan.neon.dist ├── phpunit.xml.dist ├── psalm.xml └── tests/ ├── AppTest.php ├── Assets/ │ └── HeaderStack.php ├── CallableResolverTest.php ├── Error/ │ └── AbstractErrorRendererTest.php ├── Exception/ │ ├── HttpExceptionTest.php │ └── HttpUnauthorizedExceptionTest.php ├── Factory/ │ ├── AppFactoryTest.php │ ├── Psr17/ │ │ ├── Psr17FactoryProviderTest.php │ │ ├── Psr17FactoryTest.php │ │ └── SlimHttpServerRequestCreatorTest.php │ └── ServerRequestCreatorFactoryTest.php ├── Handlers/ │ ├── ErrorHandlerTest.php │ └── Strategies/ │ └── RequestResponseNamedArgsTest.php ├── Middleware/ │ ├── BodyParsingMiddlewareTest.php │ ├── ContentLengthMiddlewareTest.php │ ├── ErrorMiddlewareTest.php │ ├── MethodOverrideMiddlewareTest.php │ ├── OutputBufferingMiddlewareTest.php │ └── RoutingMiddlewareTest.php ├── MiddlewareDispatcherTest.php ├── Mocks/ │ ├── CallableTest.php │ ├── InvocationStrategyTest.php │ ├── InvokableTest.php │ ├── MiddlewareTest.php │ ├── MockAction.php │ ├── MockCustomException.php │ ├── MockCustomRequestHandlerInvocationStrategy.php │ ├── MockMiddlewareSlimCallable.php │ ├── MockMiddlewareWithConstructor.php │ ├── MockMiddlewareWithoutConstructor.php │ ├── MockMiddlewareWithoutInterface.php │ ├── MockPsr17Factory.php │ ├── MockPsr17FactoryWithoutStreamFactory.php │ ├── MockRequestHandler.php │ ├── MockSequenceMiddleware.php │ ├── MockStream.php │ ├── RequestHandlerTest.php │ ├── SlowPokeStream.php │ └── SmallChunksStream.php ├── Providers/ │ ├── PSR7ObjectProvider.php │ └── PSR7ObjectProviderInterface.php ├── ResponseEmitterTest.php ├── Routing/ │ ├── DispatcherTest.php │ ├── FastRouteDispatcherTest.php │ ├── RouteCollectorProxyTest.php │ ├── RouteCollectorTest.php │ ├── RouteContextTest.php │ ├── RouteParserTest.php │ ├── RouteResolverTest.php │ ├── RouteRunnerTest.php │ └── RouteTest.php ├── TestCase.php └── bootstrap.php ================================================ FILE CONTENTS ================================================ ================================================ FILE: .coveralls.yml ================================================ json_path: coveralls-upload.json ================================================ FILE: .editorconfig ================================================ root = true [*] charset = utf-8 end_of_line = lf insert_final_newline = true indent_style = space indent_size = 4 trim_trailing_whitespace = true [*.md] trim_trailing_whitespace = false [*.{yml,yaml}] indent_size = 2 ================================================ FILE: .gitattributes ================================================ # Enforce Unix newlines * text=auto eol=lf # Exclude unused files # see: https://redd.it/2jzp6k /.coveralls.yml export-ignore /.editorconfig export-ignore /.gitattributes export-ignore /.github export-ignore /.gitignore export-ignore /CODE_OF_CONDUCT.md export-ignore /CONTRIBUTING.md export-ignore /MAINTAINERS.md export-ignore /README.md export-ignore /UPGRADING.md export-ignore /phpcs.xml.dist export-ignore /phpstan.neon.dist export-ignore /phpunit.xml.dist export-ignore /psalm.xml export-ignore /tests export-ignore ================================================ FILE: .github/FUNDING.yml ================================================ open_collective: slimphp tidelift: "packagist/slim/slim" ================================================ FILE: .github/dependabot.yml ================================================ version: 2 updates: - package-ecosystem: "github-actions" directory: "/" schedule: interval: "monthly" - package-ecosystem: composer directory: "/" schedule: interval: monthly open-pull-requests-limit: 10 versioning-strategy: increase ================================================ FILE: .github/workflows/tests.yml ================================================ name: Tests permissions: contents: read on: [push, pull_request] jobs: tests: name: Tests PHP ${{ matrix.php }} runs-on: ubuntu-latest continue-on-error: ${{ matrix.experimental }} strategy: fail-fast: false matrix: php: [7.4, 8.0, 8.1, 8.2, 8.3, 8.4] experimental: [false] composer-options: [''] include: - php: 8.2 analysis: true - php: 8.5 experimental: true composer-options: '--ignore-platform-req=php+' - php: nightly experimental: true composer-options: '--ignore-platform-req=php+' steps: - name: Checkout uses: actions/checkout@v6 - name: Set up PHP ${{ matrix.php }} uses: shivammathur/setup-php@v2 with: php-version: ${{ matrix.php }} coverage: xdebug tools: composer:2.8 - name: Install dependencies with Composer run: composer update --prefer-dist --no-progress --no-interaction --ansi ${{ matrix.composer-options }} - name: Coding standards if: matrix.analysis run: vendor/bin/phpcs - name: Static analysis if: matrix.analysis run: vendor/bin/phpstan - name: Tests run: vendor/bin/phpunit ${{ matrix.analysis && '--coverage-clover clover.xml' || '--no-coverage' }} - name: Upload coverage results to Coveralls if: matrix.analysis uses: coverallsapp/github-action@v2 with: flag-name: php-${{ matrix.php }} files: clover.xml ================================================ FILE: .gitignore ================================================ .DS_Store .idea .phpunit.result.cache .phpunit.cache/ composer.lock phpunit.xml clover.xml vendor coverage ================================================ FILE: CHANGELOG.md ================================================ # Changelog ## [Unreleased] ### Fixed ### Added ### Changed ### Removed ## 4.15.1 - 2025-11-21 ## Fixed - Allow PHPUnit 10, 11 and 12 when testing Slim itself (#3411) ### Added - Add support for PHP 8.5 (#3415) **Full Changelog**: https://github.com/slimphp/Slim/compare/4.15.0...4.15.1 ## 4.15.0 - 2025-08-24 ### Fixed - Fix DocBlocks for callable route handlers (#3389) - Change class keyword to lowercase (#3346) - Fix tests for PHP 8.3 - Fixes the build status badge in Readme (#3331) - Fix text and eol attributes for * selector in .gitattributes (#3391) - Deprecate setArgument/s (#3383) ### Added - Add support for PHP 8.4 - Add phpstan v2 ### Changed - Update http urls in composer.json (#3399) **Full Changelog**: https://github.com/slimphp/Slim/compare/4.14.0...4.15.0 ## 4.14.0 - 2024-06-13 ### Changed - Do not HTML entity encode in PlainTextErrorRenderer by @akrabat in https://github.com/slimphp/Slim/pull/3319 - Only render tip to error log if plain text renderer is used by @akrabat in https://github.com/slimphp/Slim/pull/3321 - Add template generics for PSR-11 implementations in PHPStan and Psalm by @limarkxx in https://github.com/slimphp/Slim/pull/3322 - Update squizlabs/php_codesniffer requirement from ^3.9 to ^3.10 by @dependabot in https://github.com/slimphp/Slim/pull/3324 - Update phpstan/phpstan requirement from ^1.10 to ^1.11 by @dependabot in https://github.com/slimphp/Slim/pull/3325 - Update psr/http-factory requirement from ^1.0 to ^1.1 by @dependabot in https://github.com/slimphp/Slim/pull/3326 #### Type hinting with template generics With the introduction of template generics, if you type-hint `Slim\App` instance variable using `/** @var \Slim\App $app */`, then you will need to change it to either: * `/** @var \Slim\App $app */` if you are not using a DI container, or * `/** @var \Slim\App<\Psr\Container\ContainerInterface> $app */` if you are You can also type-hint to the concrete instance of the container you are using too. For example, if you are using [PHP-DI](https://php-di.org), then you can use: `/** @var \Slim\App $app */`. ### New Contributors * @limarkxx made their first contribution in https://github.com/slimphp/Slim/pull/3322 **Full Changelog**: https://github.com/slimphp/Slim/compare/4.13.0...4.14.0 # 4.13.0 - 2024-03-03 - [3277: Create HttpTooManyRequestsException.php](https://github.com/slimphp/Slim/pull/3277) thanks to @flavioheleno - [3278: Remove HttpGoneException executable flag](https://github.com/slimphp/Slim/pull/3278) thanks to @flavioheleno - [3285: Update guzzlehttp/psr7 requirement from ^2.5 to ^2.6](https://github.com/slimphp/Slim/pull/3285) thanks to @dependabot[bot] - [3290: Bump actions/checkout from 3 to 4](https://github.com/slimphp/Slim/pull/3290) thanks to @dependabot[bot] - [3291: Fix line length](https://github.com/slimphp/Slim/pull/3291) thanks to @l0gicgate - [3296: PSR 7 http-message version requirement](https://github.com/slimphp/Slim/issues/3296) thanks to @rotexdegba - [3297: Allow Diactoros 3](https://github.com/slimphp/Slim/pull/3297) thanks to @derrabus - [3299: Update tests and add PHP 8.3 to the CI matrix](https://github.com/slimphp/Slim/pull/3299) thanks to @akrabat - [3301: Update nyholm/psr7-server requirement from ^1.0 to ^1.1](https://github.com/slimphp/Slim/pull/3301) thanks to @dependabot[bot] - [3302: Add support for psr/http-message ^2.0](https://github.com/slimphp/Slim/pull/3302) thanks to @rotexdegba - [3305: Update phpspec/prophecy-phpunit requirement from ^2.0 to ^2.1](https://github.com/slimphp/Slim/pull/3305) thanks to @dependabot[bot] - [3306: Update phpspec/prophecy requirement from ^1.17 to ^1.18](https://github.com/slimphp/Slim/pull/3306) thanks to @dependabot[bot] - [3308: Update squizlabs/php_codesniffer requirement from ^3.7 to ^3.8](https://github.com/slimphp/Slim/pull/3308) thanks to @dependabot[bot] - [3313: Bump ramsey/composer-install from 2 to 3](https://github.com/slimphp/Slim/pull/3313) thanks to @dependabot[bot] - [3314: Update phpspec/prophecy requirement from ^1.18 to ^1.19](https://github.com/slimphp/Slim/pull/3314) thanks to @dependabot[bot] - [3315: Update squizlabs/php_codesniffer requirement from ^3.8 to ^3.9](https://github.com/slimphp/Slim/pull/3315) thanks to @dependabot[bot] # 4.12.0 - 2023-07-23 - [3220: Refactor](https://github.com/slimphp/Slim/pull/3220) thanks to @amirkhodabande - [3237: Update phpstan/phpstan requirement from ^1.8 to ^1.9](https://github.com/slimphp/Slim/pull/3237) thanks to @dependabot[bot] - [3238: Update slim/http requirement from ^1.2 to ^1.3](https://github.com/slimphp/Slim/pull/3238) thanks to @dependabot[bot] - [3239: Update slim/psr7 requirement from ^1.5 to ^1.6](https://github.com/slimphp/Slim/pull/3239) thanks to @dependabot[bot] - [3240: Update phpspec/prophecy requirement from ^1.15 to ^1.16](https://github.com/slimphp/Slim/pull/3240) thanks to @dependabot[bot] - [3241: Update adriansuter/php-autoload-override requirement from ^1.3 to ^1.4](https://github.com/slimphp/Slim/pull/3241) thanks to @dependabot[bot] - [3245: New ability to override RouteGroupInterface in the Route class](https://github.com/slimphp/Slim/pull/3245) thanks to @githubjeka - [3253: Fix HttpBadRequestException description](https://github.com/slimphp/Slim/pull/3253) thanks to @jsanahuja - [3254: Update phpunit/phpunit requirement from ^9.5 to ^9.6](https://github.com/slimphp/Slim/pull/3254) thanks to @dependabot[bot] - [3255: Update phpstan/phpstan requirement from ^1.9 to ^1.10](https://github.com/slimphp/Slim/pull/3255) thanks to @dependabot[bot] - [3256: Update phpspec/prophecy requirement from ^1.16 to ^1.17](https://github.com/slimphp/Slim/pull/3256) thanks to @dependabot[bot] - [3264: Update psr/http-message requirement from ^1.0 to ^1.1](https://github.com/slimphp/Slim/pull/3264) thanks to @dependabot[bot] - [3265: Update nyholm/psr7 requirement from ^1.5 to ^1.7](https://github.com/slimphp/Slim/pull/3265) thanks to @dependabot[bot] - [3266: Update guzzlehttp/psr7 requirement from ^2.4 to ^2.5](https://github.com/slimphp/Slim/pull/3266) thanks to @dependabot[bot] - [3267: Update nyholm/psr7 requirement from ^1.7 to ^1.8](https://github.com/slimphp/Slim/pull/3267) thanks to @dependabot[bot] - [3269: Update httpsoft/http-server-request requirement from ^1.0 to ^1.1](https://github.com/slimphp/Slim/pull/3269) thanks to @dependabot[bot] - [3270: Update httpsoft/http-message requirement from ^1.0 to ^1.1](https://github.com/slimphp/Slim/pull/3270) thanks to @dependabot[bot] - [3271: prevent multiple entries of same methode in FastRouteDispatcher](https://github.com/slimphp/Slim/pull/3271) thanks to @papparazzo ## 4.11.0 - 2022-11-06 - [3180: Declare types](https://github.com/slimphp/Slim/pull/3180) thanks to @nbayramberdiyev - [3181: Update laminas/laminas-diactoros requirement from ^2.8 to ^2.9](https://github.com/slimphp/Slim/pull/3181) thanks to @dependabot[bot] - [3182: Update guzzlehttp/psr7 requirement from ^2.1 to ^2.2](https://github.com/slimphp/Slim/pull/3182) thanks to @dependabot[bot] - [3183: Update phpstan/phpstan requirement from ^1.4 to ^1.5](https://github.com/slimphp/Slim/pull/3183) thanks to @dependabot[bot] - [3184: Update adriansuter/php-autoload-override requirement from ^1.2 to ^1.3](https://github.com/slimphp/Slim/pull/3184) thanks to @dependabot[bot] - [3189: Update phpstan/phpstan requirement from ^1.5 to ^1.6](https://github.com/slimphp/Slim/pull/3189) thanks to @dependabot[bot] - [3191: Adding property types to Middleware classes](https://github.com/slimphp/Slim/pull/3191) thanks to @ashleycoles - [3193: Handlers types](https://github.com/slimphp/Slim/pull/3193) thanks to @ashleycoles - [3194: Adding types to AbstractErrorRenderer](https://github.com/slimphp/Slim/pull/3194) thanks to @ashleycoles - [3195: Adding prop types for Exception classes](https://github.com/slimphp/Slim/pull/3195) thanks to @ashleycoles - [3196: Adding property type declarations for Factory classes](https://github.com/slimphp/Slim/pull/3196) thanks to @ashleycoles - [3197: Remove redundant docblock types](https://github.com/slimphp/Slim/pull/3197) thanks to @theodorejb - [3199: Update laminas/laminas-diactoros requirement from ^2.9 to ^2.11](https://github.com/slimphp/Slim/pull/3199) thanks to @dependabot[bot] - [3200: Update phpstan/phpstan requirement from ^1.6 to ^1.7](https://github.com/slimphp/Slim/pull/3200) thanks to @dependabot[bot] - [3205: Update guzzlehttp/psr7 requirement from ^2.2 to ^2.4](https://github.com/slimphp/Slim/pull/3205) thanks to @dependabot[bot] - [3206: Update squizlabs/php_codesniffer requirement from ^3.6 to ^3.7](https://github.com/slimphp/Slim/pull/3206) thanks to @dependabot[bot] - [3207: Update phpstan/phpstan requirement from ^1.7 to ^1.8](https://github.com/slimphp/Slim/pull/3207) thanks to @dependabot[bot] - [3211: Assign null coalescing to coalesce equal](https://github.com/slimphp/Slim/pull/3211) thanks to @MathiasReker - [3213: Void return](https://github.com/slimphp/Slim/pull/3213) thanks to @MathiasReker - [3214: Is null](https://github.com/slimphp/Slim/pull/3214) thanks to @MathiasReker - [3216: Refactor](https://github.com/slimphp/Slim/pull/3216) thanks to @mehdihasanpour - [3218: Refactor some code](https://github.com/slimphp/Slim/pull/3218) thanks to @mehdihasanpour - [3221: Cleanup](https://github.com/slimphp/Slim/pull/3221) thanks to @mehdihasanpour - [3225: Update laminas/laminas-diactoros requirement from ^2.11 to ^2.14](https://github.com/slimphp/Slim/pull/3225) thanks to @dependabot[bot] - [3228: Using assertSame to let assert equal be restricted](https://github.com/slimphp/Slim/pull/3228) thanks to @peter279k - [3229: Update laminas/laminas-diactoros requirement from ^2.14 to ^2.17](https://github.com/slimphp/Slim/pull/3229) thanks to @dependabot[bot] - [3235: Persist routes indexed by name in RouteCollector for improved performance.](https://github.com/slimphp/Slim/pull/3235) thanks to @BusterNeece ## 4.10.0 - 2022-03-14 - [3120: Add a new PSR-17 factory to Psr17FactoryProvider](https://github.com/slimphp/Slim/pull/3120) thanks to @solventt - [3123: Replace deprecated setMethods() in tests](https://github.com/slimphp/Slim/pull/3123) thanks to @solventt - [3126: Update guzzlehttp/psr7 requirement from ^2.0 to ^2.1](https://github.com/slimphp/Slim/pull/3126) thanks to @dependabot[bot] - [3127: PHPStan v1.0](https://github.com/slimphp/Slim/pull/3127) thanks to @t0mmy742 - [3128: Update phpstan/phpstan requirement from ^1.0 to ^1.2](https://github.com/slimphp/Slim/pull/3128) thanks to @dependabot[bot] - [3129: Deprecate PHP 7.3](https://github.com/slimphp/Slim/pull/3129) thanks to @l0gicgate - [3130: Removed double defined PHP 7.4](https://github.com/slimphp/Slim/pull/3130) thanks to @flangofas - [3132: Add new `RequestResponseNamedArgs` route strategy](https://github.com/slimphp/Slim/pull/3132) thanks to @adoy - [3133: Improve typehinting for `RouteParserInterface`](https://github.com/slimphp/Slim/pull/3133) thanks to @jerowork - [3135: Update phpstan/phpstan requirement from ^1.2 to ^1.3](https://github.com/slimphp/Slim/pull/3135) thanks to @dependabot[bot] - [3137: Update phpspec/prophecy requirement from ^1.14 to ^1.15](https://github.com/slimphp/Slim/pull/3137) thanks to @dependabot[bot] - [3138: Update license year](https://github.com/slimphp/Slim/pull/3138) thanks to @Awilum - [3139: Fixed #1730 (reintroduced in 4.x)](https://github.com/slimphp/Slim/pull/3139) thanks to @adoy - [3145: Update phpstan/phpstan requirement from ^1.3 to ^1.4](https://github.com/slimphp/Slim/pull/3145) thanks to @dependabot[bot] - [3146: Inherit HttpException from RuntimeException](https://github.com/slimphp/Slim/pull/3146) thanks to @nbayramberdiyev - [3148: Upgrade to HTML5](https://github.com/slimphp/Slim/pull/3148) thanks to @nbayramberdiyev - [3172: Update nyholm/psr7 requirement from ^1.4 to ^1.5](https://github.com/slimphp/Slim/pull/3172) thanks to @dependabot[bot] ## 4.9.0 - 2021-10-05 - [3058: Implement exception class for Gone Http error](https://github.com/slimphp/Slim/pull/3058) thanks to @TheKernelPanic - [3086: Update slim/psr7 requirement from ^1.3 to ^1.4](https://github.com/slimphp/Slim/pull/3086) thanks to @dependabot[bot] - [3087: Update nyholm/psr7-server requirement from ^1.0.1 to ^1.0.2](https://github.com/slimphp/Slim/pull/3087) thanks to @dependabot[bot] - [3093: Update phpstan/phpstan requirement from ^0.12.85 to ^0.12.90](https://github.com/slimphp/Slim/pull/3093) thanks to @dependabot[bot] - [3099: Allow updated psr log](https://github.com/slimphp/Slim/pull/3099) thanks to @t0mmy742 - [3104: Drop php7.2](https://github.com/slimphp/Slim/pull/3104) thanks to @t0mmy742 - [3106: Use PSR-17 factory from Guzzle/psr7 2.0](https://github.com/slimphp/Slim/pull/3106) thanks to @t0mmy742 - [3108: Update README file](https://github.com/slimphp/Slim/pull/3108) thanks to @t0mmy742 - [3112: Update laminas/laminas-diactoros requirement from ^2.6 to ^2.8](https://github.com/slimphp/Slim/pull/3112) thanks to @dependabot[bot] - [3114: Update slim/psr7 requirement from ^1.4 to ^1.5](https://github.com/slimphp/Slim/pull/3114) thanks to @dependabot[bot] - [3115: Update phpstan/phpstan requirement from ^0.12.96 to ^0.12.99](https://github.com/slimphp/Slim/pull/3115) thanks to @dependabot[bot] - [3116: Remove Zend Diactoros references](https://github.com/slimphp/Slim/pull/3116) thanks to @l0gicgate ## 4.8.0 - 2021-05-19 - [3034: Fix phpunit dependency version](https://github.com/slimphp/Slim/pull/3034) thanks to @l0gicgate - [3037: Replace Travis by GitHub Actions](https://github.com/slimphp/Slim/pull/3037) thanks to @t0mmy742 - [3043: Cover App creation from AppFactory with empty Container](https://github.com/slimphp/Slim/pull/3043) thanks to @t0mmy742 - [3045: Update phpstan/phpstan requirement from ^0.12.58 to ^0.12.64](https://github.com/slimphp/Slim/pull/3045) thanks to @dependabot-preview[bot] - [3047: documentation: min php 7.2 required](https://github.com/slimphp/Slim/pull/3047) thanks to @Rotzbua - [3054: Update phpstan/phpstan requirement from ^0.12.64 to ^0.12.70](https://github.com/slimphp/Slim/pull/3054) thanks to @dependabot-preview[bot] - [3056: Fix docblock in ErrorMiddleware](https://github.com/slimphp/Slim/pull/3056) thanks to @piotr-cz - [3060: Update phpstan/phpstan requirement from ^0.12.70 to ^0.12.80](https://github.com/slimphp/Slim/pull/3060) thanks to @dependabot-preview[bot] - [3061: Update nyholm/psr7 requirement from ^1.3 to ^1.4](https://github.com/slimphp/Slim/pull/3061) thanks to @dependabot-preview[bot] - [3063: Allow ^1.0 || ^2.0 in psr/container](https://github.com/slimphp/Slim/pull/3063) thanks to @Ayesh - [3069: Classname/Method Callable Arrays](https://github.com/slimphp/Slim/pull/3069) thanks to @ddrv - [3078: Update squizlabs/php_codesniffer requirement from ^3.5 to ^3.6](https://github.com/slimphp/Slim/pull/3078) thanks to @dependabot[bot] - [3079: Update phpspec/prophecy requirement from ^1.12 to ^1.13](https://github.com/slimphp/Slim/pull/3079) thanks to @dependabot[bot] - [3080: Update guzzlehttp/psr7 requirement from ^1.7 to ^1.8](https://github.com/slimphp/Slim/pull/3080) thanks to @dependabot[bot] - [3082: Update phpstan/phpstan requirement from ^0.12.80 to ^0.12.85](https://github.com/slimphp/Slim/pull/3082) thanks to @dependabot[bot] ## 4.7.0 - 2020-11-30 ### Fixed - [3027: Fix: FastRoute dispatcher and data generator should match](https://github.com/slimphp/Slim/pull/3027) thanks to @edudobay ### Added - [3015: PHP 8 support](https://github.com/slimphp/Slim/pull/3015) thanks to @edudobay ### Optimizations - [3024: Randomize tests](https://github.com/slimphp/Slim/pull/3024) thanks to @pawel-slowik ## 4.6.0 - 2020-11-15 ### Fixed - [2942: Fix PHPdoc for error handlers in ErrorMiddleware ](https://github.com/slimphp/Slim/pull/2942) thanks to @TiMESPLiNTER - [2944: Remove unused function in ErrorHandler](https://github.com/slimphp/Slim/pull/2944) thanks to @l0gicgate - [2960: Fix phpstan 0.12 errors](https://github.com/slimphp/Slim/pull/2960) thanks to @adriansuter - [2982: Removing cloning statements in tests](https://github.com/slimphp/Slim/pull/2982) thanks to @l0gicgate - [3017: Fix request creator factory test](https://github.com/slimphp/Slim/pull/3017) thanks to @pawel-slowik - [3022: Ensure RouteParser Always Present After Routing](https://github.com/slimphp/Slim/pull/3022) thanks to @l0gicgate ### Added - [2949: Add the support in composer.json](https://github.com/slimphp/Slim/pull/2949) thanks to @ddrv - [2958: Strict empty string content type checking in BodyParsingMiddleware::getMediaType](https://github.com/slimphp/Slim/pull/2958) thanks to @Ayesh - [2997: Add hints to methods](https://github.com/slimphp/Slim/pull/2997) thanks to @evgsavosin - [3000: Fix route controller test](https://github.com/slimphp/Slim/pull/3000) thanks to @pawel-slowik - [3001: Add missing `$strategy` parameter in a Route test](https://github.com/slimphp/Slim/pull/3001) thanks to @pawel-slowik ### Optimizations - [2951: Minor optimizations in if() blocks](https://github.com/slimphp/Slim/pull/2951) thanks to @Ayesh - [2959: Micro optimization: Declare closures in BodyParsingMiddleware as static](https://github.com/slimphp/Slim/pull/2959) thanks to @Ayesh - [2978: Split the routing results to its own function.](https://github.com/slimphp/Slim/pull/2978) thanks to @dlundgren ### Dependencies Updated - [2953: Update nyholm/psr7-server requirement from ^0.4.1](https://github.com/slimphp/Slim/pull/2953) thanks to @dependabot-preview[bot] - [2954: Update laminas/laminas-diactoros requirement from ^2.1 to ^2.3](https://github.com/slimphp/Slim/pull/2954) thanks to @dependabot-preview[bot] - [2955: Update guzzlehttp/psr7 requirement from ^1.5 to ^1.6](https://github.com/slimphp/Slim/pull/2955) thanks to @dependabot-preview[bot] - [2956: Update slim/psr7 requirement from ^1.0 to ^1.1](https://github.com/slimphp/Slim/pull/2956) thanks to @dependabot-preview[bot] - [2957: Update nyholm/psr7 requirement from ^1.1 to ^1.2](https://github.com/slimphp/Slim/pull/2957) thanks to @dependabot-preview[bot] - [2963: Update phpstan/phpstan requirement from ^0.12.23 to ^0.12.25](https://github.com/slimphp/Slim/pull/2963) thanks to @dependabot-preview[bot] - [2965: Update adriansuter/php-autoload-override requirement from ^1.0 to ^1.1](https://github.com/slimphp/Slim/pull/2965) thanks to @dependabot-preview[bot] - [2967: Update nyholm/psr7 requirement from ^1.2 to ^1.3](https://github.com/slimphp/Slim/pull/2967) thanks to @dependabot-preview[bot] - [2969: Update nyholm/psr7-server requirement from ^0.4.1 to ^1.0.0](https://github.com/slimphp/Slim/pull/2969) thanks to @dependabot-preview[bot] - [2970: Update phpstan/phpstan requirement from ^0.12.25 to ^0.12.26](https://github.com/slimphp/Slim/pull/2970) thanks to @dependabot-preview[bot] - [2971: Update phpstan/phpstan requirement from ^0.12.26 to ^0.12.27](https://github.com/slimphp/Slim/pull/2971) thanks to @dependabot-preview[bot] - [2972: Update phpstan/phpstan requirement from ^0.12.27 to ^0.12.28](https://github.com/slimphp/Slim/pull/2972) thanks to @dependabot-preview[bot] - [2973: Update phpstan/phpstan requirement from ^0.12.28 to ^0.12.29](https://github.com/slimphp/Slim/pull/2973) thanks to @dependabot-preview[bot] - [2975: Update phpstan/phpstan requirement from ^0.12.29 to ^0.12.30](https://github.com/slimphp/Slim/pull/2975) thanks to @dependabot-preview[bot] - [2976: Update phpstan/phpstan requirement from ^0.12.30 to ^0.12.31](https://github.com/slimphp/Slim/pull/2976) thanks to @dependabot-preview[bot] - [2980: Update phpstan/phpstan requirement from ^0.12.31 to ^0.12.32](https://github.com/slimphp/Slim/pull/2980) thanks to @dependabot-preview[bot] - [2981: Update phpspec/prophecy requirement from ^1.10 to ^1.11](https://github.com/slimphp/Slim/pull/2981) thanks to @dependabot-preview[bot] - [2986: Update phpstan/phpstan requirement from ^0.12.32 to ^0.12.33](https://github.com/slimphp/Slim/pull/2986) thanks to @dependabot-preview[bot] - [2990: Update phpstan/phpstan requirement from ^0.12.33 to ^0.12.34](https://github.com/slimphp/Slim/pull/2990) thanks to @dependabot-preview[bot] - [2991: Update phpstan/phpstan requirement from ^0.12.34 to ^0.12.35](https://github.com/slimphp/Slim/pull/2991) thanks to @dependabot-preview[bot] - [2993: Update phpstan/phpstan requirement from ^0.12.35 to ^0.12.36](https://github.com/slimphp/Slim/pull/2993) thanks to @dependabot-preview[bot] - [2995: Update phpstan/phpstan requirement from ^0.12.36 to ^0.12.37](https://github.com/slimphp/Slim/pull/2995) thanks to @dependabot-preview[bot] - [3010: Update guzzlehttp/psr7 requirement from ^1.6 to ^1.7](https://github.com/slimphp/Slim/pull/3010) thanks to @dependabot-preview[bot] - [3011: Update phpspec/prophecy requirement from ^1.11 to ^1.12](https://github.com/slimphp/Slim/pull/3011) thanks to @dependabot-preview[bot] - [3012: Update slim/http requirement from ^1.0 to ^1.1](https://github.com/slimphp/Slim/pull/3012) thanks to @dependabot-preview[bot] - [3013: Update slim/psr7 requirement from ^1.1 to ^1.2](https://github.com/slimphp/Slim/pull/3013) thanks to @dependabot-preview[bot] - [3014: Update laminas/laminas-diactoros requirement from ^2.3 to ^2.4](https://github.com/slimphp/Slim/pull/3014) thanks to @dependabot-preview[bot] - [3018: Update phpstan/phpstan requirement from ^0.12.37 to ^0.12.54](https://github.com/slimphp/Slim/pull/3018) thanks to @dependabot-preview[bot] ## 4.5.0 - 2020-04-14 ### Added - [2928](https://github.com/slimphp/Slim/pull/2928) Test against PHP 7.4 - [2937](https://github.com/slimphp/Slim/pull/2937) Add support for PSR-3 ### Fixed - [2916](https://github.com/slimphp/Slim/pull/2916) Rename phpcs.xml to phpcs.xml.dist - [2917](https://github.com/slimphp/Slim/pull/2917) Update .editorconfig - [2925](https://github.com/slimphp/Slim/pull/2925) ResponseEmitter: Don't remove Content-Type and Content-Length when body is empt - [2932](https://github.com/slimphp/Slim/pull/2932) Update the Tidelift enterprise language - [2938](https://github.com/slimphp/Slim/pull/2938) Modify usage of deprecated expectExceptionMessageRegExp() method ## 4.4.0 - 2020-01-04 ### Added - [2862](https://github.com/slimphp/Slim/pull/2862) Optionally handle subclasses of exceptions in custom error handler - [2869](https://github.com/slimphp/Slim/pull/2869) php-di/php-di added in composer suggestion - [2874](https://github.com/slimphp/Slim/pull/2874) Add `null` to param type-hints - [2889](https://github.com/slimphp/Slim/pull/2889) Make `RouteContext` attributes customizable and change default to use private names - [2907](https://github.com/slimphp/Slim/pull/2907) Migrate to PSR-12 convention - [2910](https://github.com/slimphp/Slim/pull/2910) Migrate Zend to Laminas - [2912](https://github.com/slimphp/Slim/pull/2912) Add Laminas PSR17 Factory - [2913](https://github.com/slimphp/Slim/pull/2913) Update php-autoload-override version - [2914](https://github.com/slimphp/Slim/pull/2914) Added ability to add handled exceptions as an array ### Fixed - [2864](https://github.com/slimphp/Slim/pull/2864) Optimize error message in error handling if displayErrorDetails was not set - [2876](https://github.com/slimphp/Slim/pull/2876) Update links from http to https - [2877](https://github.com/slimphp/Slim/pull/2877) Fix docblock for `Slim\Routing\RouteCollector::cacheFile` - [2878](https://github.com/slimphp/Slim/pull/2878) check body is writable only on ouput buffering append - [2896](https://github.com/slimphp/Slim/pull/2896) Render errors uniformly - [2902](https://github.com/slimphp/Slim/pull/2902) Fix prophecies - [2908](https://github.com/slimphp/Slim/pull/2908) Use autoload-dev for `Slim\Tests` namespace ### Removed - [2871](https://github.com/slimphp/Slim/pull/2871) Remove explicit type-hint - [2872](https://github.com/slimphp/Slim/pull/2872) Remove type-hint ## 4.3.0 - 2019-10-05 ### Added - [2819](https://github.com/slimphp/Slim/pull/2819) Added description to addRoutingMiddleware() - [2820](https://github.com/slimphp/Slim/pull/2820) Update link to homepage in composer.json - [2828](https://github.com/slimphp/Slim/pull/2828) Allow URIs with leading multi-slashes - [2832](https://github.com/slimphp/Slim/pull/2832) Refactor `FastRouteDispatcher` - [2835](https://github.com/slimphp/Slim/pull/2835) Rename `pathFor` to `urlFor` in docblock - [2846](https://github.com/slimphp/Slim/pull/2846) Correcting the branch name as per issue-2843 - [2849](https://github.com/slimphp/Slim/pull/2849) Create class alias for FastRoute\RouteCollector - [2855](https://github.com/slimphp/Slim/pull/2855) Add list of allowed methods to HttpMethodNotAllowedException - [2860](https://github.com/slimphp/Slim/pull/2860) Add base path to `$request` and use `RouteContext` to read ### Fixed - [2839](https://github.com/slimphp/Slim/pull/2839) Fix description for handler signature in phpdocs - [2844](https://github.com/slimphp/Slim/pull/2844) Handle base path by routeCollector instead of RouteCollectorProxy - [2845](https://github.com/slimphp/Slim/pull/2845) Fix composer scripts - [2851](https://github.com/slimphp/Slim/pull/2851) Fix example of 'Hello World' app - [2854](https://github.com/slimphp/Slim/pull/2854) Fix undefined property in tests ### Removed - [2853](https://github.com/slimphp/Slim/pull/2853) Remove unused classes ## 4.2.0 - 2019-08-20 ### Added - [2787](https://github.com/slimphp/Slim/pull/2787) Add an advanced callable resolver - [2791](https://github.com/slimphp/Slim/pull/2791) Add `inferPrivatePropertyTypeFromConstructor` to phpstan - [2793](https://github.com/slimphp/Slim/pull/2793) Add ability to configure application via a container in `AppFactory` - [2798](https://github.com/slimphp/Slim/pull/2798) Add PSR-7 Agnostic Body Parsing Middleware - [2801](https://github.com/slimphp/Slim/pull/2801) Add `setLogErrorRenderer()` method to `ErrorHandler` - [2807](https://github.com/slimphp/Slim/pull/2807) Add check for Slim callable notation if no resolver given - [2803](https://github.com/slimphp/Slim/pull/2803) Add ability to emit non seekable streams in `ResponseEmitter` - [2817](https://github.com/slimphp/Slim/pull/2817) Add the ability to pass in a custom `MiddlewareDispatcherInterface` to the `App` ### Fixed - [2789](https://github.com/slimphp/Slim/pull/2789) Fix Cookie header detection in `ResponseEmitter` - [2796](https://github.com/slimphp/Slim/pull/2796) Fix http message format - [2800](https://github.com/slimphp/Slim/pull/2800) Fix null comparisons more clear in `ErrorHandler` - [2802](https://github.com/slimphp/Slim/pull/2802) Fix incorrect search of a header in stack - [2806](https://github.com/slimphp/Slim/pull/2806) Simplify `Route::prepare()` method argument preparation - [2809](https://github.com/slimphp/Slim/pull/2809) Eliminate a duplicate code via HOF in `MiddlewareDispatcher` - [2816](https://github.com/slimphp/Slim/pull/2816) Fix RouteCollectorProxy::redirect() bug ### Removed - [2811](https://github.com/slimphp/Slim/pull/2811) Remove `DeferredCallable` ## 4.1.0 - 2019-08-06 ### Added - [#2779](https://github.com/slimphp/Slim/pull/2774) Add support for Slim callables `Class:method` resolution & Container Closure auto-binding in `MiddlewareDispatcher` - [#2774](https://github.com/slimphp/Slim/pull/2774) Add possibility for custom `RequestHandler` invocation strategies ### Fixed - [#2776](https://github.com/slimphp/Slim/pull/2774) Fix group middleware on multiple nested groups ================================================ FILE: CODE_OF_CONDUCT.md ================================================ # Code of Conduct The Slim Framework code of conduct is derived from the Ruby code of conduct. This document provides community guidelines for a safe, respectful, productive, and collaborative place for any person who is willing to contribute to the Slim Framework community. It applies to all “collaborative space”, which is defined as community communications channels (such as mailing lists, IRC, Slack, forums, submitted patches, commit comments, etc.). Any violations of the code of conduct may be reported by contacting one or more of the project maintainers either directly or via email to maintainers@slimframework.com. * Participants will be tolerant of opposing views. * Participants must ensure that their language and actions are free of personal attacks and disparaging personal remarks. * When interpreting the words and actions of others, participants should always assume good intentions. * Behaviour that the project maintainers consider to be harassment will not be tolerated. ================================================ FILE: CONTRIBUTING.md ================================================ # How to Contribute ## Pull Requests 1. Fork the Slim Framework repository 2. Create a new branch for each feature or improvement 3. Send a pull request from each feature branch to the **4.x** branch It is very important to separate new features or improvements into separate feature branches, and to send a pull request for each branch. This allows me to review and pull in new features or improvements individually. ## Style Guide All pull requests must adhere to the [PSR-12 standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-12-extended-coding-style-guide.md). ## Unit Testing All pull requests must be accompanied by passing unit tests and complete code coverage. The Slim Framework uses phpunit for testing. [Learn about PHPUnit](https://github.com/sebastianbergmann/phpunit/) ================================================ FILE: LICENSE.md ================================================ Copyright (c) 2011-2022 Josh Lockhart Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: MAINTAINERS.md ================================================ # Maintainers There aren't many rules for maintainers of Slim to remember; what we have is listed here. ## We don't merge our own PRs Our code is better if more than one set of eyes looks at it. Therefore we do not merge our own pull requests unless there is an exceptional circumstance. This helps to spot errors in the patch and also enables us to share information about the project around the maintainer team. ## PRs tagged `WIP` are not ready to be merged Sometimes it's helpful to collaborate on a patch before it's ready to be merged. We use the text `WIP` (for _Work in Progress_) in the title to mark these PRs. If a PR has `WIP` in its title, then it is not to be merged. The person who raised the PR will remove the `WIP` text when they are ready for a full review and merge. ## Assign a merged PR to a milestone By ensuring that all merged PRs are assigned to a milestone, we can easily find which PRs were in which release. ================================================ FILE: README.md ================================================ # Slim Framework [![Build Status](https://github.com/slimphp/Slim/actions/workflows/tests.yml/badge.svg?branch=4.x)](https://github.com/slimphp/Slim/actions/workflows/tests.yml?query=branch:4.x) [![Coverage Status](https://coveralls.io/repos/github/slimphp/Slim/badge.svg?branch=4.x)](https://coveralls.io/github/slimphp/Slim?branch=4.x) [![Total Downloads](https://poser.pugx.org/slim/slim/downloads)](https://packagist.org/packages/slim/slim) [![License](https://poser.pugx.org/slim/slim/license)](https://packagist.org/packages/slim/slim) Slim is a PHP micro-framework that helps you quickly write simple yet powerful web applications and APIs. ## Installation It's recommended that you use [Composer](https://getcomposer.org/) to install Slim. ```bash $ composer require slim/slim ``` This will install Slim and all required dependencies. Slim requires PHP 7.4 or newer. ## Choose a PSR-7 Implementation & ServerRequest Creator Before you can get up and running with Slim you will need to choose a PSR-7 implementation that best fits your application. A few notable ones: - [Slim-Psr7](https://github.com/slimphp/Slim-Psr7) - This is the Slim Framework PSR-7 implementation - [httpsoft/http-message](https://github.com/httpsoft/http-message) & [httpsoft/http-server-request](https://github.com/httpsoft/http-server-request) - This is the fastest, strictest and most lightweight implementation available - [Nyholm/psr7](https://github.com/Nyholm/psr7) & [Nyholm/psr7-server](https://github.com/Nyholm/psr7-server) - Performance is almost the same as the HttpSoft implementation - [Guzzle/psr7](https://github.com/guzzle/psr7) - This is the implementation used by the Guzzle Client, featuring extra functionality for stream and file handling - [laminas-diactoros](https://github.com/laminas/laminas-diactoros) - This is the Laminas (Zend) PSR-7 implementation ## Slim-Http Decorators [Slim-Http](https://github.com/slimphp/Slim-Http) is a set of decorators for any PSR-7 implementation that we recommend is used with Slim Framework. To install the Slim-Http library simply run the following command: ```bash composer require slim/http ``` The `ServerRequest` and `Response` object decorators are automatically detected and applied by the internal factories. If you have installed Slim-Http and wish to turn off automatic object decoration then you can use the following statements: ```php addErrorMiddleware(true, true, true); // Add routes $app->get('/', function (Request $request, Response $response) { $response->getBody()->write('Try /hello/world'); return $response; }); $app->get('/hello/{name}', function (Request $request, Response $response, $args) { $name = $args['name']; $response->getBody()->write("Hello, $name"); return $response; }); $app->run(); ``` You may quickly test this using the built-in PHP server: ```bash $ php -S localhost:8000 -t public ``` Going to http://localhost:8000/hello/world will now display "Hello, world". For more information on how to configure your web server, see the [Documentation](https://www.slimframework.com/docs/v4/start/web-servers.html). ## Tests To execute the test suite, you'll need to install all development dependencies. ```bash $ git clone https://github.com/slimphp/Slim $ composer install $ composer test ``` ## Contributing Please see [CONTRIBUTING](CONTRIBUTING.md) for details. ## Learn More Learn more at these links: - [Website](https://www.slimframework.com) - [Documentation](https://www.slimframework.com/docs/v4/start/installation.html) - [Slack](https://slimphp.slack.com) - [Support Forum](https://discourse.slimframework.com) - [Twitter](https://twitter.com/slimphp) - [Resources](https://github.com/xssc/awesome-slim) ## Security If you discover security related issues, please email security@slimframework.com instead of using the issue tracker. ## For enterprise Available as part of the Tidelift Subscription. The maintainers of `Slim` and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. [Learn more.](https://tidelift.com/subscription/pkg/packagist-slim-slim?utm_source=packagist-slim-slim&utm_medium=referral&utm_campaign=enterprise&utm_term=repo) ## Contributors ### Code Contributors This project exists thanks to all the people who contribute. [Contribute](CONTRIBUTING.md). ### Financial Contributors Become a financial contributor and help us sustain our community. [Contribute](https://opencollective.com/slimphp/contribute) #### Individuals #### Organizations Support this project with your organization. Your logo will show up here with a link to your website. [Contribute](https://opencollective.com/slimphp/contribute) ## License The Slim Framework is licensed under the MIT license. See [License File](LICENSE.md) for more information. ================================================ FILE: SECURITY.md ================================================ # Security Policy ### Supported Versions | Version | Supported | | ------- | ------------------ | | 3.x.x | :white_check_mark: | | 4.x.x | :white_check_mark: | ### Reporting a Vulnerability To report a vulnerability please send an email to security@slimframework.com ================================================ FILE: Slim/App.php ================================================ */ class App extends RouteCollectorProxy implements RequestHandlerInterface { /** * Current version * * @var string */ public const VERSION = '4.15.1'; protected RouteResolverInterface $routeResolver; protected MiddlewareDispatcherInterface $middlewareDispatcher; /** * @param TContainerInterface $container */ public function __construct( ResponseFactoryInterface $responseFactory, ?ContainerInterface $container = null, ?CallableResolverInterface $callableResolver = null, ?RouteCollectorInterface $routeCollector = null, ?RouteResolverInterface $routeResolver = null, ?MiddlewareDispatcherInterface $middlewareDispatcher = null ) { parent::__construct( $responseFactory, $callableResolver ?? new CallableResolver($container), $container, $routeCollector ); $this->routeResolver = $routeResolver ?? new RouteResolver($this->routeCollector); $routeRunner = new RouteRunner($this->routeResolver, $this->routeCollector->getRouteParser(), $this); if (!$middlewareDispatcher) { $middlewareDispatcher = new MiddlewareDispatcher($routeRunner, $this->callableResolver, $container); } else { $middlewareDispatcher->seedMiddlewareStack($routeRunner); } $this->middlewareDispatcher = $middlewareDispatcher; } /** * @return RouteResolverInterface */ public function getRouteResolver(): RouteResolverInterface { return $this->routeResolver; } /** * @return MiddlewareDispatcherInterface */ public function getMiddlewareDispatcher(): MiddlewareDispatcherInterface { return $this->middlewareDispatcher; } /** * @param MiddlewareInterface|string|callable $middleware * @return App */ public function add($middleware): self { $this->middlewareDispatcher->add($middleware); return $this; } /** * @param MiddlewareInterface $middleware * @return App */ public function addMiddleware(MiddlewareInterface $middleware): self { $this->middlewareDispatcher->addMiddleware($middleware); return $this; } /** * Add the Slim built-in routing middleware to the app middleware stack * * This method can be used to control middleware order and is not required for default routing operation. * * @return RoutingMiddleware */ public function addRoutingMiddleware(): RoutingMiddleware { $routingMiddleware = new RoutingMiddleware( $this->getRouteResolver(), $this->getRouteCollector()->getRouteParser() ); $this->add($routingMiddleware); return $routingMiddleware; } /** * Add the Slim built-in error middleware to the app middleware stack * * @param bool $displayErrorDetails * @param bool $logErrors * @param bool $logErrorDetails * @param LoggerInterface|null $logger * * @return ErrorMiddleware */ public function addErrorMiddleware( bool $displayErrorDetails, bool $logErrors, bool $logErrorDetails, ?LoggerInterface $logger = null ): ErrorMiddleware { $errorMiddleware = new ErrorMiddleware( $this->getCallableResolver(), $this->getResponseFactory(), $displayErrorDetails, $logErrors, $logErrorDetails, $logger ); $this->add($errorMiddleware); return $errorMiddleware; } /** * Add the Slim body parsing middleware to the app middleware stack * * @param callable[] $bodyParsers * * @return BodyParsingMiddleware */ public function addBodyParsingMiddleware(array $bodyParsers = []): BodyParsingMiddleware { $bodyParsingMiddleware = new BodyParsingMiddleware($bodyParsers); $this->add($bodyParsingMiddleware); return $bodyParsingMiddleware; } /** * Run application * * This method traverses the application middleware stack and then sends the * resultant Response object to the HTTP client. * * @param ServerRequestInterface|null $request * @return void */ public function run(?ServerRequestInterface $request = null): void { if (!$request) { $serverRequestCreator = ServerRequestCreatorFactory::create(); $request = $serverRequestCreator->createServerRequestFromGlobals(); } $response = $this->handle($request); $responseEmitter = new ResponseEmitter(); $responseEmitter->emit($response); } /** * Handle a request * * This method traverses the application middleware stack and then returns the * resultant Response object. * * @param ServerRequestInterface $request * @return ResponseInterface */ public function handle(ServerRequestInterface $request): ResponseInterface { $response = $this->middlewareDispatcher->handle($request); /** * This is to be in compliance with RFC 2616, Section 9. * If the incoming request method is HEAD, we need to ensure that the response body * is empty as the request may fall back on a GET route handler due to FastRoute's * routing logic which could potentially append content to the response body * https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.4 */ $method = strtoupper($request->getMethod()); if ($method === 'HEAD') { $emptyBody = $this->responseFactory->createResponse()->getBody(); return $response->withBody($emptyBody); } return $response; } } ================================================ FILE: Slim/CallableResolver.php ================================================ container = $container; } /** * {@inheritdoc} */ public function resolve($toResolve): callable { $toResolve = $this->prepareToResolve($toResolve); if (is_callable($toResolve)) { return $this->bindToContainer($toResolve); } $resolved = $toResolve; if (is_string($toResolve)) { $resolved = $this->resolveSlimNotation($toResolve); $resolved[1] ??= '__invoke'; } $callable = $this->assertCallable($resolved, $toResolve); return $this->bindToContainer($callable); } /** * {@inheritdoc} */ public function resolveRoute($toResolve): callable { return $this->resolveByPredicate($toResolve, [$this, 'isRoute'], 'handle'); } /** * {@inheritdoc} */ public function resolveMiddleware($toResolve): callable { return $this->resolveByPredicate($toResolve, [$this, 'isMiddleware'], 'process'); } /** * @param callable|array{class-string, string}|string $toResolve * * @throws RuntimeException */ private function resolveByPredicate($toResolve, callable $predicate, string $defaultMethod): callable { $toResolve = $this->prepareToResolve($toResolve); if (is_callable($toResolve)) { return $this->bindToContainer($toResolve); } $resolved = $toResolve; if ($predicate($toResolve)) { $resolved = [$toResolve, $defaultMethod]; } if (is_string($toResolve)) { [$instance, $method] = $this->resolveSlimNotation($toResolve); if ($method === null && $predicate($instance)) { $method = $defaultMethod; } $resolved = [$instance, $method ?? '__invoke']; } $callable = $this->assertCallable($resolved, $toResolve); return $this->bindToContainer($callable); } /** * @param mixed $toResolve */ private function isRoute($toResolve): bool { return $toResolve instanceof RequestHandlerInterface; } /** * @param mixed $toResolve */ private function isMiddleware($toResolve): bool { return $toResolve instanceof MiddlewareInterface; } /** * @throws RuntimeException * * @return array{object, string|null} [Instance, Method Name] */ private function resolveSlimNotation(string $toResolve): array { /** @psalm-suppress ArgumentTypeCoercion */ preg_match(CallableResolver::$callablePattern, $toResolve, $matches); [$class, $method] = $matches ? [$matches[1], $matches[2]] : [$toResolve, null]; if ($this->container && $this->container->has($class)) { $instance = $this->container->get($class); if (!is_object($instance)) { throw new RuntimeException(sprintf('%s container entry is not an object', $class)); } } else { if (!class_exists($class)) { if ($method) { $class .= '::' . $method . '()'; } throw new RuntimeException(sprintf('Callable %s does not exist', $class)); } $instance = new $class($this->container); } return [$instance, $method]; } /** * @param mixed $resolved * @param mixed $toResolve * * @throws RuntimeException */ private function assertCallable($resolved, $toResolve): callable { if (!is_callable($resolved)) { if (is_callable($toResolve) || is_object($toResolve) || is_array($toResolve)) { $formatedToResolve = ($toResolveJson = json_encode($toResolve)) !== false ? $toResolveJson : ''; } else { $formatedToResolve = is_string($toResolve) ? $toResolve : ''; } throw new RuntimeException(sprintf('%s is not resolvable', $formatedToResolve)); } return $resolved; } private function bindToContainer(callable $callable): callable { if (is_array($callable) && $callable[0] instanceof Closure) { $callable = $callable[0]; } if ($this->container && $callable instanceof Closure) { /** @var Closure $callable */ $callable = $callable->bindTo($this->container); } return $callable; } /** * @param callable|string|array{class-string, string}|mixed $toResolve * * @return callable|string|array{class-string, string}|mixed */ private function prepareToResolve($toResolve) { if (!is_array($toResolve)) { return $toResolve; } $candidate = $toResolve; $class = array_shift($candidate); $method = array_shift($candidate); if (is_string($class) && is_string($method)) { return $class . ':' . $method; } return $toResolve; } } ================================================ FILE: Slim/Error/AbstractErrorRenderer.php ================================================ getTitle(); } return $this->defaultErrorTitle; } protected function getErrorDescription(Throwable $exception): string { if ($exception instanceof HttpException) { return $exception->getDescription(); } return $this->defaultErrorDescription; } } ================================================ FILE: Slim/Error/Renderers/HtmlErrorRenderer.php ================================================ The application could not run because of the following error:

'; $html .= '

Details

'; $html .= $this->renderExceptionFragment($exception); } else { $html = "

{$this->getErrorDescription($exception)}

"; } return $this->renderHtmlBody($this->getErrorTitle($exception), $html); } private function renderExceptionFragment(Throwable $exception): string { $html = sprintf('
Type: %s
', get_class($exception)); $code = $exception->getCode(); $html .= sprintf('
Code: %s
', $code); $html .= sprintf('
Message: %s
', htmlentities($exception->getMessage())); $html .= sprintf('
File: %s
', $exception->getFile()); $html .= sprintf('
Line: %s
', $exception->getLine()); $html .= '

Trace

'; $html .= sprintf('
%s
', htmlentities($exception->getTraceAsString())); return $html; } public function renderHtmlBody(string $title = '', string $html = ''): string { return sprintf( '' . '' . ' ' . ' ' . ' ' . ' %s' . ' ' . ' ' . ' ' . '

%s

' . '
%s
' . ' Go Back' . ' ' . '', $title, $title, $html ); } } ================================================ FILE: Slim/Error/Renderers/JsonErrorRenderer.php ================================================ $this->getErrorTitle($exception)]; if ($displayErrorDetails) { $error['exception'] = []; do { $error['exception'][] = $this->formatExceptionFragment($exception); } while ($exception = $exception->getPrevious()); } return (string) json_encode($error, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); } /** * @return array */ private function formatExceptionFragment(Throwable $exception): array { $code = $exception->getCode(); return [ 'type' => get_class($exception), 'code' => $code, 'message' => $exception->getMessage(), 'file' => $exception->getFile(), 'line' => $exception->getLine(), ]; } } ================================================ FILE: Slim/Error/Renderers/PlainTextErrorRenderer.php ================================================ getErrorTitle($exception)}\n"; if ($displayErrorDetails) { $text .= $this->formatExceptionFragment($exception); while ($exception = $exception->getPrevious()) { $text .= "\nPrevious Error:\n"; $text .= $this->formatExceptionFragment($exception); } } return $text; } private function formatExceptionFragment(Throwable $exception): string { $text = sprintf("Type: %s\n", get_class($exception)); $code = $exception->getCode(); $text .= sprintf("Code: %s\n", $code); $text .= sprintf("Message: %s\n", $exception->getMessage()); $text .= sprintf("File: %s\n", $exception->getFile()); $text .= sprintf("Line: %s\n", $exception->getLine()); $text .= sprintf('Trace: %s', $exception->getTraceAsString()); return $text; } } ================================================ FILE: Slim/Error/Renderers/XmlErrorRenderer.php ================================================ \n"; $xml .= "\n " . $this->createCdataSection($this->getErrorTitle($exception)) . "\n"; if ($displayErrorDetails) { do { $xml .= " \n"; $xml .= ' ' . get_class($exception) . "\n"; $xml .= ' ' . $exception->getCode() . "\n"; $xml .= ' ' . $this->createCdataSection($exception->getMessage()) . "\n"; $xml .= ' ' . $exception->getFile() . "\n"; $xml .= ' ' . $exception->getLine() . "\n"; $xml .= " \n"; } while ($exception = $exception->getPrevious()); } $xml .= ''; return $xml; } /** * Returns a CDATA section with the given content. */ private function createCdataSection(string $content): string { return sprintf('', str_replace(']]>', ']]]]>', $content)); } } ================================================ FILE: Slim/Exception/HttpBadRequestException.php ================================================ request = $request; } public function getRequest(): ServerRequestInterface { return $this->request; } public function getTitle(): string { return $this->title; } public function setTitle(string $title): self { $this->title = $title; return $this; } public function getDescription(): string { return $this->description; } public function setDescription(string $description): self { $this->description = $description; return $this; } } ================================================ FILE: Slim/Exception/HttpForbiddenException.php ================================================ allowedMethods; } /** * @param string[] $methods */ public function setAllowedMethods(array $methods): self { $this->allowedMethods = $methods; $this->message = 'Method not allowed. Must be one of: ' . implode(', ', $methods); return $this; } } ================================================ FILE: Slim/Exception/HttpNotFoundException.php ================================================ message = $message; } // @phpstan-ignore-next-line parent::__construct($request, $this->message, $this->code, $previous); } } ================================================ FILE: Slim/Exception/HttpTooManyRequestsException.php ================================================ : App) */ public static function create( ?ResponseFactoryInterface $responseFactory = null, ?ContainerInterface $container = null, ?CallableResolverInterface $callableResolver = null, ?RouteCollectorInterface $routeCollector = null, ?RouteResolverInterface $routeResolver = null, ?MiddlewareDispatcherInterface $middlewareDispatcher = null ): App { static::$responseFactory = $responseFactory ?? static::$responseFactory; return new App( self::determineResponseFactory(), $container ?? static::$container, $callableResolver ?? static::$callableResolver, $routeCollector ?? static::$routeCollector, $routeResolver ?? static::$routeResolver, $middlewareDispatcher ?? static::$middlewareDispatcher ); } /** * @template TContainerInterface of (ContainerInterface) * @param TContainerInterface $container * @return App */ public static function createFromContainer(ContainerInterface $container): App { $responseFactory = $container->has(ResponseFactoryInterface::class) && ( $responseFactoryFromContainer = $container->get(ResponseFactoryInterface::class) ) instanceof ResponseFactoryInterface ? $responseFactoryFromContainer : self::determineResponseFactory(); $callableResolver = $container->has(CallableResolverInterface::class) && ( $callableResolverFromContainer = $container->get(CallableResolverInterface::class) ) instanceof CallableResolverInterface ? $callableResolverFromContainer : null; $routeCollector = $container->has(RouteCollectorInterface::class) && ( $routeCollectorFromContainer = $container->get(RouteCollectorInterface::class) ) instanceof RouteCollectorInterface ? $routeCollectorFromContainer : null; $routeResolver = $container->has(RouteResolverInterface::class) && ( $routeResolverFromContainer = $container->get(RouteResolverInterface::class) ) instanceof RouteResolverInterface ? $routeResolverFromContainer : null; $middlewareDispatcher = $container->has(MiddlewareDispatcherInterface::class) && ( $middlewareDispatcherFromContainer = $container->get(MiddlewareDispatcherInterface::class) ) instanceof MiddlewareDispatcherInterface ? $middlewareDispatcherFromContainer : null; return new App( $responseFactory, $container, $callableResolver, $routeCollector, $routeResolver, $middlewareDispatcher ); } /** * @throws RuntimeException */ public static function determineResponseFactory(): ResponseFactoryInterface { if (static::$responseFactory) { if (static::$streamFactory) { return static::attemptResponseFactoryDecoration(static::$responseFactory, static::$streamFactory); } return static::$responseFactory; } $psr17FactoryProvider = static::$psr17FactoryProvider ?? new Psr17FactoryProvider(); /** @var Psr17Factory $psr17factory */ foreach ($psr17FactoryProvider->getFactories() as $psr17factory) { if ($psr17factory::isResponseFactoryAvailable()) { $responseFactory = $psr17factory::getResponseFactory(); if (static::$streamFactory || $psr17factory::isStreamFactoryAvailable()) { $streamFactory = static::$streamFactory ?? $psr17factory::getStreamFactory(); return static::attemptResponseFactoryDecoration($responseFactory, $streamFactory); } return $responseFactory; } } throw new RuntimeException( "Could not detect any PSR-17 ResponseFactory implementations. " . "Please install a supported implementation in order to use `AppFactory::create()`. " . "See https://github.com/slimphp/Slim/blob/4.x/README.md for a list of supported implementations." ); } protected static function attemptResponseFactoryDecoration( ResponseFactoryInterface $responseFactory, StreamFactoryInterface $streamFactory ): ResponseFactoryInterface { if ( static::$slimHttpDecoratorsAutomaticDetectionEnabled && SlimHttpPsr17Factory::isResponseFactoryAvailable() ) { return SlimHttpPsr17Factory::createDecoratedResponseFactory($responseFactory, $streamFactory); } return $responseFactory; } public static function setPsr17FactoryProvider(Psr17FactoryProviderInterface $psr17FactoryProvider): void { static::$psr17FactoryProvider = $psr17FactoryProvider; } public static function setResponseFactory(ResponseFactoryInterface $responseFactory): void { static::$responseFactory = $responseFactory; } public static function setStreamFactory(StreamFactoryInterface $streamFactory): void { static::$streamFactory = $streamFactory; } public static function setContainer(ContainerInterface $container): void { static::$container = $container; } public static function setCallableResolver(CallableResolverInterface $callableResolver): void { static::$callableResolver = $callableResolver; } public static function setRouteCollector(RouteCollectorInterface $routeCollector): void { static::$routeCollector = $routeCollector; } public static function setRouteResolver(RouteResolverInterface $routeResolver): void { static::$routeResolver = $routeResolver; } public static function setMiddlewareDispatcher(MiddlewareDispatcherInterface $middlewareDispatcher): void { static::$middlewareDispatcher = $middlewareDispatcher; } public static function setSlimHttpDecoratorsAutomaticDetection(bool $enabled): void { static::$slimHttpDecoratorsAutomaticDetectionEnabled = $enabled; } } ================================================ FILE: Slim/Factory/Psr17/GuzzlePsr17Factory.php ================================================ serverRequestCreator = $serverRequestCreator; $this->serverRequestCreatorMethod = $serverRequestCreatorMethod; } /** * {@inheritdoc} */ public function createServerRequestFromGlobals(): ServerRequestInterface { /** @var callable $callable */ $callable = [$this->serverRequestCreator, $this->serverRequestCreatorMethod]; /** @var ServerRequestInterface */ return (Closure::fromCallable($callable))(); } } ================================================ FILE: Slim/Factory/Psr17/SlimHttpPsr17Factory.php ================================================ serverRequestCreator = $serverRequestCreator; } /** * {@inheritdoc} */ public function createServerRequestFromGlobals(): ServerRequestInterface { if (!static::isServerRequestDecoratorAvailable()) { throw new RuntimeException('The Slim-Http ServerRequest decorator is not available.'); } $request = $this->serverRequestCreator->createServerRequestFromGlobals(); if ( !(( $decoratedServerRequest = new static::$serverRequestDecoratorClass($request) ) instanceof ServerRequestInterface) ) { throw new RuntimeException(get_called_class() . ' could not instantiate a decorated server request.'); } return $decoratedServerRequest; } public static function isServerRequestDecoratorAvailable(): bool { return class_exists(static::$serverRequestDecoratorClass); } } ================================================ FILE: Slim/Factory/Psr17/SlimPsr17Factory.php ================================================ getFactories() as $psr17Factory) { if ($psr17Factory::isServerRequestCreatorAvailable()) { $serverRequestCreator = $psr17Factory::getServerRequestCreator(); return static::attemptServerRequestCreatorDecoration($serverRequestCreator); } } throw new RuntimeException( "Could not detect any ServerRequest creator implementations. " . "Please install a supported implementation in order to use `App::run()` " . "without having to pass in a `ServerRequest` object. " . "See https://github.com/slimphp/Slim/blob/4.x/README.md for a list of supported implementations." ); } protected static function attemptServerRequestCreatorDecoration( ServerRequestCreatorInterface $serverRequestCreator ): ServerRequestCreatorInterface { if ( static::$slimHttpDecoratorsAutomaticDetectionEnabled && SlimHttpServerRequestCreator::isServerRequestDecoratorAvailable() ) { return new SlimHttpServerRequestCreator($serverRequestCreator); } return $serverRequestCreator; } public static function setPsr17FactoryProvider(Psr17FactoryProviderInterface $psr17FactoryProvider): void { static::$psr17FactoryProvider = $psr17FactoryProvider; } public static function setServerRequestCreator(ServerRequestCreatorInterface $serverRequestCreator): void { self::$serverRequestCreator = $serverRequestCreator; } public static function setSlimHttpDecoratorsAutomaticDetection(bool $enabled): void { static::$slimHttpDecoratorsAutomaticDetectionEnabled = $enabled; } } ================================================ FILE: Slim/Handlers/ErrorHandler.php ================================================ */ protected array $errorRenderers = [ 'application/json' => JsonErrorRenderer::class, 'application/xml' => XmlErrorRenderer::class, 'text/xml' => XmlErrorRenderer::class, 'text/html' => HtmlErrorRenderer::class, 'text/plain' => PlainTextErrorRenderer::class, ]; protected bool $displayErrorDetails = false; protected bool $logErrors; protected bool $logErrorDetails = false; protected ?string $contentType = null; protected ?string $method = null; protected ServerRequestInterface $request; protected Throwable $exception; protected int $statusCode; protected CallableResolverInterface $callableResolver; protected ResponseFactoryInterface $responseFactory; protected LoggerInterface $logger; public function __construct( CallableResolverInterface $callableResolver, ResponseFactoryInterface $responseFactory, ?LoggerInterface $logger = null ) { $this->callableResolver = $callableResolver; $this->responseFactory = $responseFactory; $this->logger = $logger ?: $this->getDefaultLogger(); } /** * Invoke error handler * * @param ServerRequestInterface $request The most recent Request object * @param Throwable $exception The caught Exception object * @param bool $displayErrorDetails Whether or not to display the error details * @param bool $logErrors Whether or not to log errors * @param bool $logErrorDetails Whether or not to log error details */ public function __invoke( ServerRequestInterface $request, Throwable $exception, bool $displayErrorDetails, bool $logErrors, bool $logErrorDetails ): ResponseInterface { $this->displayErrorDetails = $displayErrorDetails; $this->logErrors = $logErrors; $this->logErrorDetails = $logErrorDetails; $this->request = $request; $this->exception = $exception; $this->method = $request->getMethod(); $this->statusCode = $this->determineStatusCode(); if ($this->contentType === null) { $this->contentType = $this->determineContentType($request); } if ($logErrors) { $this->writeToErrorLog(); } return $this->respond(); } /** * Force the content type for all error handler responses. * * @param string|null $contentType The content type */ public function forceContentType(?string $contentType): void { $this->contentType = $contentType; } protected function determineStatusCode(): int { if ($this->method === 'OPTIONS') { return 200; } if ($this->exception instanceof HttpException) { return $this->exception->getCode(); } return 500; } /** * Determine which content type we know about is wanted using Accept header * * Note: This method is a bare-bones implementation designed specifically for * Slim's error handling requirements. Consider a fully-feature solution such * as willdurand/negotiation for any other situation. */ protected function determineContentType(ServerRequestInterface $request): ?string { $acceptHeader = $request->getHeaderLine('Accept'); $selectedContentTypes = array_intersect( explode(',', $acceptHeader), array_keys($this->errorRenderers) ); $count = count($selectedContentTypes); if ($count) { $current = current($selectedContentTypes); /** * Ensure other supported content types take precedence over text/plain * when multiple content types are provided via Accept header. */ if ($current === 'text/plain' && $count > 1) { $next = next($selectedContentTypes); if (is_string($next)) { return $next; } } // @phpstan-ignore-next-line if (is_string($current)) { return $current; } } if (preg_match('/\+(json|xml)/', $acceptHeader, $matches)) { $mediaType = 'application/' . $matches[1]; if (array_key_exists($mediaType, $this->errorRenderers)) { return $mediaType; } } return null; } /** * Determine which renderer to use based on content type * * @throws RuntimeException */ protected function determineRenderer(): callable { if ($this->contentType !== null && array_key_exists($this->contentType, $this->errorRenderers)) { $renderer = $this->errorRenderers[$this->contentType]; } else { $renderer = $this->defaultErrorRenderer; } return $this->callableResolver->resolve($renderer); } /** * Register an error renderer for a specific content-type * * @param string $contentType The content-type this renderer should be registered to * @param ErrorRendererInterface|string|callable $errorRenderer The error renderer */ public function registerErrorRenderer(string $contentType, $errorRenderer): void { $this->errorRenderers[$contentType] = $errorRenderer; } /** * Set the default error renderer * * @param string $contentType The content type of the default error renderer * @param ErrorRendererInterface|string|callable $errorRenderer The default error renderer */ public function setDefaultErrorRenderer(string $contentType, $errorRenderer): void { $this->defaultErrorRendererContentType = $contentType; $this->defaultErrorRenderer = $errorRenderer; } /** * Set the renderer for the error logger * * @param ErrorRendererInterface|string|callable $logErrorRenderer */ public function setLogErrorRenderer($logErrorRenderer): void { $this->logErrorRenderer = $logErrorRenderer; } /** * Write to the error log if $logErrors has been set to true */ protected function writeToErrorLog(): void { $renderer = $this->callableResolver->resolve($this->logErrorRenderer); /** @var string $error */ $error = $renderer($this->exception, $this->logErrorDetails); if ($this->logErrorRenderer === PlainTextErrorRenderer::class && !$this->displayErrorDetails) { $error .= "\nTips: To display error details in HTTP response "; $error .= 'set "displayErrorDetails" to true in the ErrorHandler constructor.'; } $this->logError($error); } /** * Wraps the error_log function so that this can be easily tested */ protected function logError(string $error): void { $this->logger->error($error); } /** * Returns a default logger implementation. */ protected function getDefaultLogger(): LoggerInterface { return new Logger(); } protected function respond(): ResponseInterface { $response = $this->responseFactory->createResponse($this->statusCode); if ($this->contentType !== null && array_key_exists($this->contentType, $this->errorRenderers)) { $response = $response->withHeader('Content-type', $this->contentType); } else { $response = $response->withHeader('Content-type', $this->defaultErrorRendererContentType); } if ($this->exception instanceof HttpMethodNotAllowedException) { $allowedMethods = implode(', ', $this->exception->getAllowedMethods()); $response = $response->withHeader('Allow', $allowedMethods); } $renderer = $this->determineRenderer(); $body = call_user_func($renderer, $this->exception, $this->displayErrorDetails); if ($body !== false) { /** @var string $body */ $response->getBody()->write($body); } return $response; } } ================================================ FILE: Slim/Handlers/Strategies/RequestHandler.php ================================================ appendRouteArgumentsToRequestAttributes = $appendRouteArgumentsToRequestAttributes; } /** * Invoke a route callable that implements RequestHandlerInterface * * @param array $routeArguments */ public function __invoke( callable $callable, ServerRequestInterface $request, ResponseInterface $response, array $routeArguments ): ResponseInterface { if ($this->appendRouteArgumentsToRequestAttributes) { foreach ($routeArguments as $k => $v) { $request = $request->withAttribute($k, $v); } } /** @var ResponseInterface */ return $callable($request); } } ================================================ FILE: Slim/Handlers/Strategies/RequestResponse.php ================================================ $routeArguments */ public function __invoke( callable $callable, ServerRequestInterface $request, ResponseInterface $response, array $routeArguments ): ResponseInterface { foreach ($routeArguments as $k => $v) { $request = $request->withAttribute($k, $v); } /** @var ResponseInterface */ return $callable($request, $response, $routeArguments); } } ================================================ FILE: Slim/Handlers/Strategies/RequestResponseArgs.php ================================================ $routeArguments */ public function __invoke( callable $callable, ServerRequestInterface $request, ResponseInterface $response, array $routeArguments ): ResponseInterface { /** @var ResponseInterface */ return $callable($request, $response, ...array_values($routeArguments)); } } ================================================ FILE: Slim/Handlers/Strategies/RequestResponseNamedArgs.php ================================================ $routeArguments */ public function __invoke( callable $callable, ServerRequestInterface $request, ResponseInterface $response, array $routeArguments ): ResponseInterface { /** @var ResponseInterface */ return $callable($request, $response, ...$routeArguments); } } ================================================ FILE: Slim/Interfaces/AdvancedCallableResolverInterface.php ================================================ $routeArguments The route's placeholder arguments * * @return ResponseInterface The response from the callable. */ public function __invoke( callable $callable, ServerRequestInterface $request, ResponseInterface $response, array $routeArguments ): ResponseInterface; } ================================================ FILE: Slim/Interfaces/MiddlewareDispatcherInterface.php ================================================ */ public function setBasePath(string $basePath): RouteCollectorProxyInterface; /** * Add GET route * * @param string $pattern The route URI pattern * @param callable|array{class-string, string}|string $callable The route callback routine */ public function get(string $pattern, $callable): RouteInterface; /** * Add POST route * * @param string $pattern The route URI pattern * @param callable|array{class-string, string}|string $callable The route callback routine */ public function post(string $pattern, $callable): RouteInterface; /** * Add PUT route * * @param string $pattern The route URI pattern * @param callable|array{class-string, string}|string $callable The route callback routine */ public function put(string $pattern, $callable): RouteInterface; /** * Add PATCH route * * @param string $pattern The route URI pattern * @param callable|array{class-string, string}|string $callable The route callback routine */ public function patch(string $pattern, $callable): RouteInterface; /** * Add DELETE route * * @param string $pattern The route URI pattern * @param callable|array{class-string, string}|string $callable The route callback routine */ public function delete(string $pattern, $callable): RouteInterface; /** * Add OPTIONS route * * @param string $pattern The route URI pattern * @param callable|array{class-string, string}|string $callable The route callback routine */ public function options(string $pattern, $callable): RouteInterface; /** * Add route for any HTTP method * * @param string $pattern The route URI pattern * @param callable|array{class-string, string}|string $callable The route callback routine */ public function any(string $pattern, $callable): RouteInterface; /** * Add route with multiple methods * * @param string[] $methods Numeric array of HTTP method names * @param string $pattern The route URI pattern * @param callable|array{class-string, string}|string $callable The route callback routine */ public function map(array $methods, string $pattern, $callable): RouteInterface; /** * Route Groups * * This method accepts a route pattern and a callback. All route * declarations in the callback will be prepended by the group(s) * that it is in. * @param string|callable $callable */ public function group(string $pattern, $callable): RouteGroupInterface; /** * Add a route that sends an HTTP redirect * * @param string|UriInterface $to */ public function redirect(string $from, $to, int $status = 302): RouteInterface; } ================================================ FILE: Slim/Interfaces/RouteGroupInterface.php ================================================ $dispatcher */ public function appendMiddlewareToDispatcher(MiddlewareDispatcher $dispatcher): RouteGroupInterface; /** * Get the RouteGroup's pattern */ public function getPattern(): string; } ================================================ FILE: Slim/Interfaces/RouteInterface.php ================================================ */ public function getArguments(): array; /** * Set a route argument * * @deprecated 4.14.1 Use a middleware for custom route arguments now. */ public function setArgument(string $name, string $value): RouteInterface; /** * Replace route arguments * * @param array $arguments * * @deprecated 4.14.1 Use a middleware for custom route arguments now. */ public function setArguments(array $arguments): self; /** * @param MiddlewareInterface|string|callable $middleware */ public function add($middleware): self; public function addMiddleware(MiddlewareInterface $middleware): self; /** * Prepare the route for use * * @param array $arguments */ public function prepare(array $arguments): self; /** * Run route * * This method traverses the middleware stack, including the route's callable * and captures the resultant HTTP response object. It then sends the response * back to the Application. */ public function run(ServerRequestInterface $request): ResponseInterface; } ================================================ FILE: Slim/Interfaces/RouteParserInterface.php ================================================ $data Named argument replacement data * @param array> $queryParams Optional query string parameters * * @throws RuntimeException If named route does not exist * @throws InvalidArgumentException If required data not provided */ public function relativeUrlFor(string $routeName, array $data = [], array $queryParams = []): string; /** * Build the path for a named route including the base path * * @param string $routeName Route name * @param array $data Named argument replacement data * @param array> $queryParams Optional query string parameters * * @throws RuntimeException If named route does not exist * @throws InvalidArgumentException If required data not provided */ public function urlFor(string $routeName, array $data = [], array $queryParams = []): string; /** * Get fully qualified URL for named route * * @param UriInterface $uri * @param string $routeName Route name * @param array $data Named argument replacement data * @param array> $queryParams Optional query string parameters */ public function fullUrlFor(UriInterface $uri, string $routeName, array $data = [], array $queryParams = []): string; } ================================================ FILE: Slim/Interfaces/RouteResolverInterface.php ================================================ getPath() */ public function computeRoutingResults(string $uri, string $method): RoutingResults; public function resolveRoute(string $identifier): RouteInterface; } ================================================ FILE: Slim/Interfaces/ServerRequestCreatorInterface.php ================================================ $context * * @throws InvalidArgumentException */ public function log($level, $message, array $context = []): void { error_log((string) $message); } } ================================================ FILE: Slim/Middleware/BodyParsingMiddleware.php ================================================ callable */ public function __construct(array $bodyParsers = []) { $this->registerDefaultBodyParsers(); foreach ($bodyParsers as $mediaType => $parser) { $this->registerBodyParser($mediaType, $parser); } } public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { $parsedBody = $request->getParsedBody(); if (empty($parsedBody)) { $parsedBody = $this->parseBody($request); $request = $request->withParsedBody($parsedBody); } return $handler->handle($request); } /** * @param string $mediaType A HTTP media type (excluding content-type params). * @param callable $callable A callable that returns parsed contents for media type. */ public function registerBodyParser(string $mediaType, callable $callable): self { $this->bodyParsers[$mediaType] = $callable; return $this; } /** * @param string $mediaType A HTTP media type (excluding content-type params). */ public function hasBodyParser(string $mediaType): bool { return isset($this->bodyParsers[$mediaType]); } /** * @param string $mediaType A HTTP media type (excluding content-type params). * @throws RuntimeException */ public function getBodyParser(string $mediaType): callable { if (!isset($this->bodyParsers[$mediaType])) { throw new RuntimeException('No parser for type ' . $mediaType); } return $this->bodyParsers[$mediaType]; } protected function registerDefaultBodyParsers(): void { $this->registerBodyParser('application/json', static function ($input) { /** @var string $input */ $result = json_decode($input, true); if (!is_array($result)) { return null; } return $result; }); $this->registerBodyParser('application/x-www-form-urlencoded', static function ($input) { /** @var string $input */ parse_str($input, $data); return $data; }); $xmlCallable = static function ($input) { /** @var string $input */ $backup = self::disableXmlEntityLoader(true); $backup_errors = libxml_use_internal_errors(true); $result = simplexml_load_string($input); self::disableXmlEntityLoader($backup); libxml_clear_errors(); libxml_use_internal_errors($backup_errors); if ($result === false) { return null; } return $result; }; $this->registerBodyParser('application/xml', $xmlCallable); $this->registerBodyParser('text/xml', $xmlCallable); } /** * @return null|array|object */ protected function parseBody(ServerRequestInterface $request) { $mediaType = $this->getMediaType($request); if ($mediaType === null) { return null; } // Check if this specific media type has a parser registered first if (!isset($this->bodyParsers[$mediaType])) { // If not, look for a media type with a structured syntax suffix (RFC 6839) $parts = explode('+', $mediaType); if (count($parts) >= 2) { $mediaType = 'application/' . $parts[count($parts) - 1]; } } if (isset($this->bodyParsers[$mediaType])) { $body = (string)$request->getBody(); $parsed = $this->bodyParsers[$mediaType]($body); if ($parsed !== null && !is_object($parsed) && !is_array($parsed)) { throw new RuntimeException( 'Request body media type parser return value must be an array, an object, or null' ); } return $parsed; } return null; } /** * @return string|null The serverRequest media type, minus content-type params */ protected function getMediaType(ServerRequestInterface $request): ?string { $contentType = $request->getHeader('Content-Type')[0] ?? null; if (is_string($contentType) && trim($contentType) !== '') { $contentTypeParts = explode(';', $contentType); return strtolower(trim($contentTypeParts[0])); } return null; } protected static function disableXmlEntityLoader(bool $disable): bool { if (LIBXML_VERSION >= 20900) { // libxml >= 2.9.0 disables entity loading by default, so it is // safe to skip the real call (deprecated in PHP 8). return true; } // @codeCoverageIgnoreStart return libxml_disable_entity_loader($disable); // @codeCoverageIgnoreEnd } } ================================================ FILE: Slim/Middleware/ContentLengthMiddleware.php ================================================ handle($request); // Add Content-Length header if not already added $size = $response->getBody()->getSize(); if ($size !== null && !$response->hasHeader('Content-Length')) { $response = $response->withHeader('Content-Length', (string) $size); } return $response; } } ================================================ FILE: Slim/Middleware/ErrorMiddleware.php ================================================ callableResolver = $callableResolver; $this->responseFactory = $responseFactory; $this->displayErrorDetails = $displayErrorDetails; $this->logErrors = $logErrors; $this->logErrorDetails = $logErrorDetails; $this->logger = $logger; } public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { try { return $handler->handle($request); } catch (Throwable $e) { return $this->handleException($request, $e); } } public function handleException(ServerRequestInterface $request, Throwable $exception): ResponseInterface { if ($exception instanceof HttpException) { $request = $exception->getRequest(); } $exceptionType = get_class($exception); $handler = $this->getErrorHandler($exceptionType); /** @var ResponseInterface */ return $handler($request, $exception, $this->displayErrorDetails, $this->logErrors, $this->logErrorDetails); } /** * Get callable to handle scenarios where an error * occurs when processing the current request. * * @param string $type Exception/Throwable name. ie: RuntimeException::class * @return callable|ErrorHandler */ public function getErrorHandler(string $type) { if (isset($this->handlers[$type])) { return $this->callableResolver->resolve($this->handlers[$type]); } if (isset($this->subClassHandlers[$type])) { return $this->callableResolver->resolve($this->subClassHandlers[$type]); } foreach ($this->subClassHandlers as $class => $handler) { if (is_subclass_of($type, $class)) { return $this->callableResolver->resolve($handler); } } return $this->getDefaultErrorHandler(); } /** * Get default error handler * * @return ErrorHandler|callable */ public function getDefaultErrorHandler() { if ($this->defaultErrorHandler === null) { $this->defaultErrorHandler = new ErrorHandler( $this->callableResolver, $this->responseFactory, $this->logger ); } return $this->callableResolver->resolve($this->defaultErrorHandler); } /** * Set callable as the default Slim application error handler. * * The callable signature MUST match the ErrorHandlerInterface * * @param string|callable|ErrorHandler $handler * @see ErrorHandlerInterface * * 1. Instance of \Psr\Http\Message\ServerRequestInterface * 2. Instance of \Throwable * 3. Boolean $displayErrorDetails * 4. Boolean $logErrors * 5. Boolean $logErrorDetails * * The callable MUST return an instance of * \Psr\Http\Message\ResponseInterface. * */ public function setDefaultErrorHandler($handler): self { $this->defaultErrorHandler = $handler; return $this; } /** * Set callable to handle scenarios where an error * occurs when processing the current request. * * The callable signature MUST match the ErrorHandlerInterface * * Pass true to $handleSubclasses to make the handler handle all subclasses of * the type as well. Pass an array of classes to make the same function handle multiple exceptions. * * @param string|string[] $typeOrTypes Exception/Throwable name. * ie: RuntimeException::class or an array of classes * ie: [HttpNotFoundException::class, HttpMethodNotAllowedException::class] * @param string|callable|ErrorHandlerInterface $handler * * @see ErrorHandlerInterface * * 1. Instance of \Psr\Http\Message\ServerRequestInterface * 2. Instance of \Throwable * 3. Boolean $displayErrorDetails * 4. Boolean $logErrors * 5. Boolean $logErrorDetails * * The callable MUST return an instance of * \Psr\Http\Message\ResponseInterface. * */ public function setErrorHandler($typeOrTypes, $handler, bool $handleSubclasses = false): self { if (is_array($typeOrTypes)) { foreach ($typeOrTypes as $type) { $this->addErrorHandler($type, $handler, $handleSubclasses); } } else { $this->addErrorHandler($typeOrTypes, $handler, $handleSubclasses); } return $this; } /** * Used internally to avoid code repetition when passing multiple exceptions to setErrorHandler(). * @param string|callable|ErrorHandlerInterface $handler */ private function addErrorHandler(string $type, $handler, bool $handleSubclasses): void { if ($handleSubclasses) { $this->subClassHandlers[$type] = $handler; } else { $this->handlers[$type] = $handler; } } } ================================================ FILE: Slim/Middleware/MethodOverrideMiddleware.php ================================================ getHeaderLine('X-Http-Method-Override'); if ($methodHeader) { $request = $request->withMethod($methodHeader); } elseif (strtoupper($request->getMethod()) === 'POST') { $body = $request->getParsedBody(); if (is_array($body) && !empty($body['_METHOD']) && is_string($body['_METHOD'])) { $request = $request->withMethod($body['_METHOD']); } if ($request->getBody()->eof()) { $request->getBody()->rewind(); } } return $handler->handle($request); } } ================================================ FILE: Slim/Middleware/OutputBufferingMiddleware.php ================================================ streamFactory = $streamFactory; $this->style = $style; if (!in_array($style, [static::APPEND, static::PREPEND], true)) { throw new InvalidArgumentException("Invalid style `{$style}`. Must be `append` or `prepend`"); } } /** * @throws Throwable */ public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { try { ob_start(); $response = $handler->handle($request); $output = ob_get_clean(); } catch (Throwable $e) { ob_end_clean(); throw $e; } if (!empty($output)) { if ($this->style === static::PREPEND) { $body = $this->streamFactory->createStream(); $body->write($output . $response->getBody()); $response = $response->withBody($body); } elseif ($this->style === static::APPEND && $response->getBody()->isWritable()) { $response->getBody()->write($output); } } return $response; } } ================================================ FILE: Slim/Middleware/RoutingMiddleware.php ================================================ routeResolver = $routeResolver; $this->routeParser = $routeParser; } /** * @throws HttpNotFoundException * @throws HttpMethodNotAllowedException * @throws RuntimeException */ public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { $request = $this->performRouting($request); return $handler->handle($request); } /** * Perform routing * * @param ServerRequestInterface $request PSR7 Server Request * * @throws HttpNotFoundException * @throws HttpMethodNotAllowedException * @throws RuntimeException */ public function performRouting(ServerRequestInterface $request): ServerRequestInterface { $request = $request->withAttribute(RouteContext::ROUTE_PARSER, $this->routeParser); $routingResults = $this->resolveRoutingResultsFromRequest($request); $routeStatus = $routingResults->getRouteStatus(); $request = $request->withAttribute(RouteContext::ROUTING_RESULTS, $routingResults); switch ($routeStatus) { case RoutingResults::FOUND: $routeArguments = $routingResults->getRouteArguments(); $routeIdentifier = $routingResults->getRouteIdentifier() ?? ''; $route = $this->routeResolver ->resolveRoute($routeIdentifier) ->prepare($routeArguments); return $request->withAttribute(RouteContext::ROUTE, $route); case RoutingResults::NOT_FOUND: throw new HttpNotFoundException($request); case RoutingResults::METHOD_NOT_ALLOWED: $exception = new HttpMethodNotAllowedException($request); $exception->setAllowedMethods($routingResults->getAllowedMethods()); throw $exception; default: throw new RuntimeException('An unexpected error occurred while performing routing.'); } } /** * Resolves the route from the given request */ protected function resolveRoutingResultsFromRequest(ServerRequestInterface $request): RoutingResults { return $this->routeResolver->computeRoutingResults( $request->getUri()->getPath(), $request->getMethod() ); } } ================================================ FILE: Slim/MiddlewareDispatcher.php ================================================ seedMiddlewareStack($kernel); $this->callableResolver = $callableResolver; $this->container = $container; } /** * {@inheritdoc} */ public function seedMiddlewareStack(RequestHandlerInterface $kernel): void { $this->tip = $kernel; } /** * Invoke the middleware stack */ public function handle(ServerRequestInterface $request): ResponseInterface { return $this->tip->handle($request); } /** * Add a new middleware to the stack * * Middleware are organized as a stack. That means middleware * that have been added before will be executed after the newly * added one (last in, first out). * * @param MiddlewareInterface|string|callable $middleware */ public function add($middleware): MiddlewareDispatcherInterface { if ($middleware instanceof MiddlewareInterface) { return $this->addMiddleware($middleware); } if (is_string($middleware)) { return $this->addDeferred($middleware); } if (is_callable($middleware)) { return $this->addCallable($middleware); } /** @phpstan-ignore-next-line */ throw new RuntimeException( 'A middleware must be an object/class name referencing an implementation of ' . 'MiddlewareInterface or a callable with a matching signature.' ); } /** * Add a new middleware to the stack * * Middleware are organized as a stack. That means middleware * that have been added before will be executed after the newly * added one (last in, first out). */ public function addMiddleware(MiddlewareInterface $middleware): MiddlewareDispatcherInterface { $next = $this->tip; $this->tip = new class ($middleware, $next) implements RequestHandlerInterface { private MiddlewareInterface $middleware; private RequestHandlerInterface $next; public function __construct(MiddlewareInterface $middleware, RequestHandlerInterface $next) { $this->middleware = $middleware; $this->next = $next; } public function handle(ServerRequestInterface $request): ResponseInterface { return $this->middleware->process($request, $this->next); } }; return $this; } /** * Add a new middleware by class name * * Middleware are organized as a stack. That means middleware * that have been added before will be executed after the newly * added one (last in, first out). * @return MiddlewareDispatcher */ public function addDeferred(string $middleware): self { $next = $this->tip; $this->tip = new class ( $middleware, $next, $this->container, $this->callableResolver ) implements RequestHandlerInterface { private string $middleware; private RequestHandlerInterface $next; private ?ContainerInterface $container; private ?CallableResolverInterface $callableResolver; public function __construct( string $middleware, RequestHandlerInterface $next, ?ContainerInterface $container = null, ?CallableResolverInterface $callableResolver = null ) { $this->middleware = $middleware; $this->next = $next; $this->container = $container; $this->callableResolver = $callableResolver; } public function handle(ServerRequestInterface $request): ResponseInterface { if ($this->callableResolver instanceof AdvancedCallableResolverInterface) { $callable = $this->callableResolver->resolveMiddleware($this->middleware); /** @var ResponseInterface */ return $callable($request, $this->next); } $callable = null; if ($this->callableResolver instanceof CallableResolverInterface) { try { $callable = $this->callableResolver->resolve($this->middleware); } catch (RuntimeException $e) { // Do Nothing } } if (!$callable) { $resolved = $this->middleware; $instance = null; $method = null; /** @psalm-suppress ArgumentTypeCoercion */ // Check for Slim callable as `class:method` if (preg_match(CallableResolver::$callablePattern, $resolved, $matches)) { $resolved = $matches[1]; $method = $matches[2]; } if ($this->container && $this->container->has($resolved)) { $instance = $this->container->get($resolved); if ($instance instanceof MiddlewareInterface) { return $instance->process($request, $this->next); } } elseif (!function_exists($resolved)) { if (!class_exists($resolved)) { throw new RuntimeException(sprintf('Middleware %s does not exist', $resolved)); } $instance = new $resolved($this->container); } if ($instance && $instance instanceof MiddlewareInterface) { return $instance->process($request, $this->next); } $callable = $instance ?? $resolved; if ($instance && $method) { $callable = [$instance, $method]; } if ($this->container && $callable instanceof Closure) { $callable = $callable->bindTo($this->container); } } if (!is_callable($callable)) { throw new RuntimeException( sprintf( 'Middleware %s is not resolvable', $this->middleware ) ); } /** @var ResponseInterface */ return $callable($request, $this->next); } }; return $this; } /** * Add a (non-standard) callable middleware to the stack * * Middleware are organized as a stack. That means middleware * that have been added before will be executed after the newly * added one (last in, first out). * @return MiddlewareDispatcher */ public function addCallable(callable $middleware): self { $next = $this->tip; if ($this->container && $middleware instanceof Closure) { /** @var Closure $middleware */ $middleware = $middleware->bindTo($this->container); } $this->tip = new class ($middleware, $next) implements RequestHandlerInterface { /** * @var callable */ private $middleware; /** * @var RequestHandlerInterface */ private $next; public function __construct(callable $middleware, RequestHandlerInterface $next) { $this->middleware = $middleware; $this->next = $next; } public function handle(ServerRequestInterface $request): ResponseInterface { /** @var ResponseInterface */ return ($this->middleware)($request, $this->next); } }; return $this; } } ================================================ FILE: Slim/ResponseEmitter.php ================================================ responseChunkSize = $responseChunkSize; } /** * Send the response the client */ public function emit(ResponseInterface $response): void { $isEmpty = $this->isResponseEmpty($response); if (headers_sent() === false) { $this->emitHeaders($response); // Set the status _after_ the headers, because of PHP's "helpful" behavior with location headers. // See https://github.com/slimphp/Slim/issues/1730 $this->emitStatusLine($response); } if (!$isEmpty) { $this->emitBody($response); } } /** * Emit Response Headers */ private function emitHeaders(ResponseInterface $response): void { foreach ($response->getHeaders() as $name => $values) { $first = strtolower($name) !== 'set-cookie'; foreach ($values as $value) { $header = sprintf('%s: %s', $name, $value); header($header, $first); $first = false; } } } /** * Emit Status Line */ private function emitStatusLine(ResponseInterface $response): void { $statusLine = sprintf( 'HTTP/%s %s %s', $response->getProtocolVersion(), $response->getStatusCode(), $response->getReasonPhrase() ); header($statusLine, true, $response->getStatusCode()); } /** * Emit Body */ private function emitBody(ResponseInterface $response): void { $body = $response->getBody(); if ($body->isSeekable()) { $body->rewind(); } $amountToRead = (int) $response->getHeaderLine('Content-Length'); if (!$amountToRead) { $amountToRead = $body->getSize(); } if ($amountToRead) { while ($amountToRead > 0 && !$body->eof()) { $length = min($this->responseChunkSize, $amountToRead); $data = $body->read($length); echo $data; $amountToRead -= strlen($data); if (connection_status() !== CONNECTION_NORMAL) { break; } } } else { while (!$body->eof()) { echo $body->read($this->responseChunkSize); if (connection_status() !== CONNECTION_NORMAL) { break; } } } } /** * Asserts response body is empty or status code is 204, 205 or 304 */ public function isResponseEmpty(ResponseInterface $response): bool { if (in_array($response->getStatusCode(), [204, 205, 304], true)) { return true; } $stream = $response->getBody(); $seekable = $stream->isSeekable(); if ($seekable) { $stream->rewind(); } return $seekable ? $stream->read(1) === '' : $stream->eof(); } } ================================================ FILE: Slim/Routing/Dispatcher.php ================================================ routeCollector = $routeCollector; } protected function createDispatcher(): FastRouteDispatcher { if ($this->dispatcher) { return $this->dispatcher; } $routeDefinitionCallback = function (FastRouteCollector $r): void { $basePath = $this->routeCollector->getBasePath(); foreach ($this->routeCollector->getRoutes() as $route) { $r->addRoute($route->getMethods(), $basePath . $route->getPattern(), $route->getIdentifier()); } }; $cacheFile = $this->routeCollector->getCacheFile(); if ($cacheFile) { /** @var FastRouteDispatcher $dispatcher */ $dispatcher = \FastRoute\cachedDispatcher($routeDefinitionCallback, [ 'dataGenerator' => GroupCountBased::class, 'dispatcher' => FastRouteDispatcher::class, 'routeParser' => new Std(), 'cacheFile' => $cacheFile, ]); } else { /** @var FastRouteDispatcher $dispatcher */ $dispatcher = \FastRoute\simpleDispatcher($routeDefinitionCallback, [ 'dataGenerator' => GroupCountBased::class, 'dispatcher' => FastRouteDispatcher::class, 'routeParser' => new Std(), ]); } $this->dispatcher = $dispatcher; return $this->dispatcher; } /** * {@inheritdoc} */ public function dispatch(string $method, string $uri): RoutingResults { $dispatcher = $this->createDispatcher(); $results = $dispatcher->dispatch($method, $uri); return new RoutingResults($this, $method, $uri, $results[0], $results[1], $results[2]); } /** * {@inheritdoc} */ public function getAllowedMethods(string $uri): array { $dispatcher = $this->createDispatcher(); return $dispatcher->getAllowedMethods($uri); } } ================================================ FILE: Slim/Routing/FastRouteDispatcher.php ================================================ } */ public function dispatch($httpMethod, $uri): array { $routingResults = $this->routingResults($httpMethod, $uri); if ($routingResults[0] === self::FOUND) { return $routingResults; } // For HEAD requests, attempt fallback to GET if ($httpMethod === 'HEAD') { $routingResults = $this->routingResults('GET', $uri); if ($routingResults[0] === self::FOUND) { return $routingResults; } } // If nothing else matches, try fallback routes $routingResults = $this->routingResults('*', $uri); if ($routingResults[0] === self::FOUND) { return $routingResults; } if (!empty($this->getAllowedMethods($uri))) { return [self::METHOD_NOT_ALLOWED, null, []]; } return [self::NOT_FOUND, null, []]; } /** * @param string $httpMethod * @param string $uri * * @return array{int, string|null, array} */ private function routingResults(string $httpMethod, string $uri): array { if (isset($this->staticRouteMap[$httpMethod][$uri])) { /** @var string $routeIdentifier */ $routeIdentifier = $this->staticRouteMap[$httpMethod][$uri]; return [self::FOUND, $routeIdentifier, []]; } if (isset($this->variableRouteData[$httpMethod])) { /** @var array{0: int, 1?: string, 2?: array} $result */ $result = $this->dispatchVariableRoute($this->variableRouteData[$httpMethod], $uri); if ($result[0] === self::FOUND) { /** @var array{int, string, array} $result */ return [self::FOUND, $result[1], $result[2]]; } } return [self::NOT_FOUND, null, []]; } /** * @param string $uri * * @return string[] */ public function getAllowedMethods(string $uri): array { if (isset($this->allowedMethods[$uri])) { return $this->allowedMethods[$uri]; } $allowedMethods = []; foreach ($this->staticRouteMap as $method => $uriMap) { if (isset($uriMap[$uri])) { $allowedMethods[$method] = true; } } foreach ($this->variableRouteData as $method => $routeData) { $result = $this->dispatchVariableRoute($routeData, $uri); if ($result[0] === self::FOUND) { $allowedMethods[$method] = true; } } return $this->allowedMethods[$uri] = array_keys($allowedMethods); } } ================================================ FILE: Slim/Routing/Route.php ================================================ */ protected array $arguments = []; /** * Route arguments parameters * * @var array */ protected array $savedArguments = []; /** * Container * @var TContainerInterface $container */ protected ?ContainerInterface $container = null; /** @var MiddlewareDispatcher $middlewareDispatcher */ protected MiddlewareDispatcher $middlewareDispatcher; /** * Route callable * * @var callable|array{class-string, string}|string */ protected $callable; protected CallableResolverInterface $callableResolver; protected ResponseFactoryInterface $responseFactory; /** * Route pattern */ protected string $pattern; protected bool $groupMiddlewareAppended = false; /** * @param string[] $methods The route HTTP methods * @param string $pattern The route pattern * @param callable|array{class-string, string}|string $callable The route callable * @param ResponseFactoryInterface $responseFactory * @param CallableResolverInterface $callableResolver * @param TContainerInterface $container * @param InvocationStrategyInterface|null $invocationStrategy * @param RouteGroupInterface[] $groups The parent route groups * @param int $identifier The route identifier */ public function __construct( array $methods, string $pattern, $callable, ResponseFactoryInterface $responseFactory, CallableResolverInterface $callableResolver, ?ContainerInterface $container = null, ?InvocationStrategyInterface $invocationStrategy = null, array $groups = [], int $identifier = 0 ) { $this->methods = $methods; $this->pattern = $pattern; $this->callable = $callable; $this->responseFactory = $responseFactory; $this->callableResolver = $callableResolver; $this->container = $container; $this->invocationStrategy = $invocationStrategy ?? new RequestResponse(); $this->groups = $groups; $this->identifier = 'route' . $identifier; $this->middlewareDispatcher = new MiddlewareDispatcher($this, $callableResolver, $container); } public function getCallableResolver(): CallableResolverInterface { return $this->callableResolver; } /** * {@inheritdoc} */ public function getInvocationStrategy(): InvocationStrategyInterface { return $this->invocationStrategy; } /** * {@inheritdoc} */ public function setInvocationStrategy(InvocationStrategyInterface $invocationStrategy): RouteInterface { $this->invocationStrategy = $invocationStrategy; return $this; } /** * {@inheritdoc} */ public function getMethods(): array { return $this->methods; } /** * {@inheritdoc} */ public function getPattern(): string { return $this->pattern; } /** * {@inheritdoc} */ public function setPattern(string $pattern): RouteInterface { $this->pattern = $pattern; return $this; } /** * {@inheritdoc} */ public function getCallable() { return $this->callable; } /** * {@inheritdoc} */ public function setCallable($callable): RouteInterface { $this->callable = $callable; return $this; } /** * {@inheritdoc} */ public function getName(): ?string { return $this->name; } /** * {@inheritdoc} */ public function setName(string $name): RouteInterface { $this->name = $name; return $this; } /** * {@inheritdoc} */ public function getIdentifier(): string { return $this->identifier; } /** * {@inheritdoc} */ public function getArgument(string $name, ?string $default = null): ?string { if (array_key_exists($name, $this->arguments)) { return $this->arguments[$name]; } return $default; } /** * {@inheritdoc} */ public function getArguments(): array { return $this->arguments; } /** * {@inheritdoc} */ public function setArguments(array $arguments, bool $includeInSavedArguments = true): RouteInterface { if ($includeInSavedArguments) { $this->savedArguments = $arguments; } $this->arguments = $arguments; return $this; } /** * @return RouteGroupInterface[] */ public function getGroups(): array { return $this->groups; } /** * {@inheritdoc} */ public function add($middleware): RouteInterface { $this->middlewareDispatcher->add($middleware); return $this; } /** * {@inheritdoc} */ public function addMiddleware(MiddlewareInterface $middleware): RouteInterface { $this->middlewareDispatcher->addMiddleware($middleware); return $this; } /** * {@inheritdoc} */ public function prepare(array $arguments): RouteInterface { $this->arguments = array_replace($this->savedArguments, $arguments); return $this; } /** * {@inheritdoc} */ public function setArgument(string $name, string $value, bool $includeInSavedArguments = true): RouteInterface { if ($includeInSavedArguments) { $this->savedArguments[$name] = $value; } $this->arguments[$name] = $value; return $this; } /** * {@inheritdoc} */ public function run(ServerRequestInterface $request): ResponseInterface { if (!$this->groupMiddlewareAppended) { $this->appendGroupMiddlewareToRoute(); } return $this->middlewareDispatcher->handle($request); } /** * @return void */ protected function appendGroupMiddlewareToRoute(): void { $inner = $this->middlewareDispatcher; $this->middlewareDispatcher = new MiddlewareDispatcher($inner, $this->callableResolver, $this->container); foreach (array_reverse($this->groups) as $group) { $group->appendMiddlewareToDispatcher($this->middlewareDispatcher); } $this->groupMiddlewareAppended = true; } /** * {@inheritdoc} */ public function handle(ServerRequestInterface $request): ResponseInterface { if ($this->callableResolver instanceof AdvancedCallableResolverInterface) { $callable = $this->callableResolver->resolveRoute($this->callable); } else { $callable = $this->callableResolver->resolve($this->callable); } $strategy = $this->invocationStrategy; $strategyImplements = class_implements($strategy); if ( is_array($callable) && $callable[0] instanceof RequestHandlerInterface && !in_array(RequestHandlerInvocationStrategyInterface::class, $strategyImplements) ) { $strategy = new RequestHandler(); } $response = $this->responseFactory->createResponse(); return $strategy($callable, $request, $response, $this->arguments); } } ================================================ FILE: Slim/Routing/RouteCollector.php ================================================ responseFactory = $responseFactory; $this->callableResolver = $callableResolver; $this->container = $container; $this->defaultInvocationStrategy = $defaultInvocationStrategy ?? new RequestResponse(); $this->routeParser = $routeParser ?? new RouteParser($this); if ($cacheFile) { $this->setCacheFile($cacheFile); } } public function getRouteParser(): RouteParserInterface { return $this->routeParser; } /** * Get default route invocation strategy */ public function getDefaultInvocationStrategy(): InvocationStrategyInterface { return $this->defaultInvocationStrategy; } public function setDefaultInvocationStrategy(InvocationStrategyInterface $strategy): RouteCollectorInterface { $this->defaultInvocationStrategy = $strategy; return $this; } /** * {@inheritdoc} */ public function getCacheFile(): ?string { return $this->cacheFile; } /** * {@inheritdoc} */ public function setCacheFile(string $cacheFile): RouteCollectorInterface { if (file_exists($cacheFile) && !is_readable($cacheFile)) { throw new RuntimeException( sprintf('Route collector cache file `%s` is not readable', $cacheFile) ); } if (!file_exists($cacheFile) && !is_writable(dirname($cacheFile))) { throw new RuntimeException( sprintf('Route collector cache file directory `%s` is not writable', dirname($cacheFile)) ); } $this->cacheFile = $cacheFile; return $this; } /** * {@inheritdoc} */ public function getBasePath(): string { return $this->basePath; } /** * Set the base path used in urlFor() */ public function setBasePath(string $basePath): RouteCollectorInterface { $this->basePath = $basePath; return $this; } /** * {@inheritdoc} */ public function getRoutes(): array { return $this->routes; } /** * {@inheritdoc} */ public function removeNamedRoute(string $name): RouteCollectorInterface { $route = $this->getNamedRoute($name); /** @psalm-suppress PossiblyNullArrayOffset */ unset($this->routesByName[$route->getName()], $this->routes[$route->getIdentifier()]); return $this; } /** * {@inheritdoc} */ public function getNamedRoute(string $name): RouteInterface { if (isset($this->routesByName[$name])) { $route = $this->routesByName[$name]; if ($route->getName() === $name) { return $route; } unset($this->routesByName[$name]); } foreach ($this->routes as $route) { if ($name === $route->getName()) { $this->routesByName[$name] = $route; return $route; } } throw new RuntimeException('Named route does not exist for name: ' . $name); } /** * {@inheritdoc} */ public function lookupRoute(string $identifier): RouteInterface { if (!isset($this->routes[$identifier])) { throw new RuntimeException('Route not found, looks like your route cache is stale.'); } return $this->routes[$identifier]; } /** * {@inheritdoc} */ public function group(string $pattern, $callable): RouteGroupInterface { $routeGroup = $this->createGroup($pattern, $callable); $this->routeGroups[] = $routeGroup; $routeGroup->collectRoutes(); array_pop($this->routeGroups); return $routeGroup; } /** * @param string|callable $callable */ protected function createGroup(string $pattern, $callable): RouteGroupInterface { $routeCollectorProxy = $this->createProxy($pattern); return new RouteGroup($pattern, $callable, $this->callableResolver, $routeCollectorProxy); } /** * @return RouteCollectorProxyInterface */ protected function createProxy(string $pattern): RouteCollectorProxyInterface { /** @var RouteCollectorProxy */ return new RouteCollectorProxy( $this->responseFactory, $this->callableResolver, $this->container, $this, $pattern ); } /** * {@inheritdoc} */ public function map(array $methods, string $pattern, $handler): RouteInterface { $route = $this->createRoute($methods, $pattern, $handler); $this->routes[$route->getIdentifier()] = $route; $routeName = $route->getName(); if ($routeName !== null && !isset($this->routesByName[$routeName])) { $this->routesByName[$routeName] = $route; } $this->routeCounter++; return $route; } /** * @param string[] $methods * @param callable|array{class-string, string}|string $callable */ protected function createRoute(array $methods, string $pattern, $callable): RouteInterface { return new Route( $methods, $pattern, $callable, $this->responseFactory, $this->callableResolver, $this->container, $this->defaultInvocationStrategy, $this->routeGroups, $this->routeCounter ); } } ================================================ FILE: Slim/Routing/RouteCollectorProxy.php ================================================ */ class RouteCollectorProxy implements RouteCollectorProxyInterface { protected ResponseFactoryInterface $responseFactory; protected CallableResolverInterface $callableResolver; /** @var TContainerInterface */ protected ?ContainerInterface $container = null; protected RouteCollectorInterface $routeCollector; protected string $groupPattern; /** * @param TContainerInterface $container */ public function __construct( ResponseFactoryInterface $responseFactory, CallableResolverInterface $callableResolver, ?ContainerInterface $container = null, ?RouteCollectorInterface $routeCollector = null, string $groupPattern = '' ) { $this->responseFactory = $responseFactory; $this->callableResolver = $callableResolver; $this->container = $container; $this->routeCollector = $routeCollector ?? new RouteCollector($responseFactory, $callableResolver, $container); $this->groupPattern = $groupPattern; } /** * {@inheritdoc} */ public function getResponseFactory(): ResponseFactoryInterface { return $this->responseFactory; } /** * {@inheritdoc} */ public function getCallableResolver(): CallableResolverInterface { return $this->callableResolver; } /** * {@inheritdoc} * @return TContainerInterface */ public function getContainer(): ?ContainerInterface { return $this->container; } /** * {@inheritdoc} */ public function getRouteCollector(): RouteCollectorInterface { return $this->routeCollector; } /** * {@inheritdoc} */ public function getBasePath(): string { return $this->routeCollector->getBasePath(); } /** * {@inheritdoc} */ public function setBasePath(string $basePath): RouteCollectorProxyInterface { $this->routeCollector->setBasePath($basePath); return $this; } /** * {@inheritdoc} */ public function get(string $pattern, $callable): RouteInterface { return $this->map(['GET'], $pattern, $callable); } /** * {@inheritdoc} */ public function post(string $pattern, $callable): RouteInterface { return $this->map(['POST'], $pattern, $callable); } /** * {@inheritdoc} */ public function put(string $pattern, $callable): RouteInterface { return $this->map(['PUT'], $pattern, $callable); } /** * {@inheritdoc} */ public function patch(string $pattern, $callable): RouteInterface { return $this->map(['PATCH'], $pattern, $callable); } /** * {@inheritdoc} */ public function delete(string $pattern, $callable): RouteInterface { return $this->map(['DELETE'], $pattern, $callable); } /** * {@inheritdoc} */ public function options(string $pattern, $callable): RouteInterface { return $this->map(['OPTIONS'], $pattern, $callable); } /** * {@inheritdoc} */ public function any(string $pattern, $callable): RouteInterface { return $this->map(['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'], $pattern, $callable); } /** * {@inheritdoc} */ public function map(array $methods, string $pattern, $callable): RouteInterface { $pattern = $this->groupPattern . $pattern; return $this->routeCollector->map($methods, $pattern, $callable); } /** * {@inheritdoc} */ public function group(string $pattern, $callable): RouteGroupInterface { $pattern = $this->groupPattern . $pattern; return $this->routeCollector->group($pattern, $callable); } /** * {@inheritdoc} */ public function redirect(string $from, $to, int $status = 302): RouteInterface { $responseFactory = $this->responseFactory; $handler = function () use ($to, $status, $responseFactory) { $response = $responseFactory->createResponse($status); return $response->withHeader('Location', (string) $to); }; return $this->get($from, $handler); } } ================================================ FILE: Slim/Routing/RouteContext.php ================================================ getAttribute(self::ROUTE); $routeParser = $serverRequest->getAttribute(self::ROUTE_PARSER); $routingResults = $serverRequest->getAttribute(self::ROUTING_RESULTS); $basePath = $serverRequest->getAttribute(self::BASE_PATH); if ($routeParser === null || $routingResults === null) { throw new RuntimeException('Cannot create RouteContext before routing has been completed'); } /** @var RouteInterface|null $route */ /** @var RouteParserInterface $routeParser */ /** @var RoutingResults $routingResults */ /** @var string|null $basePath */ return new self($route, $routeParser, $routingResults, $basePath); } private ?RouteInterface $route; private RouteParserInterface $routeParser; private RoutingResults $routingResults; private ?string $basePath; private function __construct( ?RouteInterface $route, RouteParserInterface $routeParser, RoutingResults $routingResults, ?string $basePath = null ) { $this->route = $route; $this->routeParser = $routeParser; $this->routingResults = $routingResults; $this->basePath = $basePath; } public function getRoute(): ?RouteInterface { return $this->route; } public function getRouteParser(): RouteParserInterface { return $this->routeParser; } public function getRoutingResults(): RoutingResults { return $this->routingResults; } public function getBasePath(): string { if ($this->basePath === null) { throw new RuntimeException('No base path defined.'); } return $this->basePath; } } ================================================ FILE: Slim/Routing/RouteGroup.php ================================================ */ protected RouteCollectorProxyInterface $routeCollectorProxy; /** * @var MiddlewareInterface[]|string[]|callable[] */ protected array $middleware = []; protected string $pattern; /** * @param callable|string $callable * @param RouteCollectorProxyInterface<\Psr\Container\ContainerInterface|null> $routeCollectorProxy */ public function __construct( string $pattern, $callable, CallableResolverInterface $callableResolver, RouteCollectorProxyInterface $routeCollectorProxy ) { $this->pattern = $pattern; $this->callable = $callable; $this->callableResolver = $callableResolver; $this->routeCollectorProxy = $routeCollectorProxy; } /** * {@inheritdoc} */ public function collectRoutes(): RouteGroupInterface { if ($this->callableResolver instanceof AdvancedCallableResolverInterface) { $callable = $this->callableResolver->resolveRoute($this->callable); } else { $callable = $this->callableResolver->resolve($this->callable); } $callable($this->routeCollectorProxy); return $this; } /** * {@inheritdoc} */ public function add($middleware): RouteGroupInterface { $this->middleware[] = $middleware; return $this; } /** * {@inheritdoc} */ public function addMiddleware(MiddlewareInterface $middleware): RouteGroupInterface { $this->middleware[] = $middleware; return $this; } /** * {@inheritdoc} * @param MiddlewareDispatcher<\Psr\Container\ContainerInterface|null> $dispatcher */ public function appendMiddlewareToDispatcher(MiddlewareDispatcher $dispatcher): RouteGroupInterface { foreach ($this->middleware as $middleware) { $dispatcher->add($middleware); } return $this; } /** * {@inheritdoc} */ public function getPattern(): string { return $this->pattern; } } ================================================ FILE: Slim/Routing/RouteParser.php ================================================ routeCollector = $routeCollector; $this->routeParser = new Std(); } /** * {@inheritdoc} */ public function relativeUrlFor(string $routeName, array $data = [], array $queryParams = []): string { $route = $this->routeCollector->getNamedRoute($routeName); $pattern = $route->getPattern(); $segments = []; $segmentName = ''; /* * $routes is an associative array of expressions representing a route as multiple segments * There is an expression for each optional parameter plus one without the optional parameters * The most specific is last, hence why we reverse the array before iterating over it */ $expressions = array_reverse($this->routeParser->parse($pattern)); foreach ($expressions as $expression) { foreach ($expression as $segment) { /* * Each $segment is either a string or an array of strings * containing optional parameters of an expression */ if (is_string($segment)) { $segments[] = $segment; continue; } /** @var string[] $segment */ /* * If we don't have a data element for this segment in the provided $data * we cancel testing to move onto the next expression with a less specific item */ if (!array_key_exists($segment[0], $data)) { $segments = []; $segmentName = $segment[0]; break; } $segments[] = $data[$segment[0]]; } /* * If we get to this logic block we have found all the parameters * for the provided $data which means we don't need to continue testing * less specific expressions */ if (!empty($segments)) { break; } } if (empty($segments)) { throw new InvalidArgumentException('Missing data for URL segment: ' . $segmentName); } $url = implode('', $segments); if ($queryParams) { $url .= '?' . http_build_query($queryParams); } return $url; } /** * {@inheritdoc} */ public function urlFor(string $routeName, array $data = [], array $queryParams = []): string { $basePath = $this->routeCollector->getBasePath(); $url = $this->relativeUrlFor($routeName, $data, $queryParams); if ($basePath) { $url = $basePath . $url; } return $url; } /** * {@inheritdoc} */ public function fullUrlFor(UriInterface $uri, string $routeName, array $data = [], array $queryParams = []): string { $path = $this->urlFor($routeName, $data, $queryParams); $scheme = $uri->getScheme(); $authority = $uri->getAuthority(); $protocol = ($scheme ? $scheme . ':' : '') . ($authority ? '//' . $authority : ''); return $protocol . $path; } } ================================================ FILE: Slim/Routing/RouteResolver.php ================================================ routeCollector = $routeCollector; $this->dispatcher = $dispatcher ?? new Dispatcher($routeCollector); } /** * @param string $uri Should be $request->getUri()->getPath() */ public function computeRoutingResults(string $uri, string $method): RoutingResults { $uri = rawurldecode($uri); if ($uri === '' || $uri[0] !== '/') { $uri = '/' . $uri; } return $this->dispatcher->dispatch($method, $uri); } /** * @throws RuntimeException */ public function resolveRoute(string $identifier): RouteInterface { return $this->routeCollector->lookupRoute($identifier); } } ================================================ FILE: Slim/Routing/RouteRunner.php ================================================ */ private ?RouteCollectorProxyInterface $routeCollectorProxy; /** * @param RouteCollectorProxyInterface<\Psr\Container\ContainerInterface|null> $routeCollectorProxy */ public function __construct( RouteResolverInterface $routeResolver, RouteParserInterface $routeParser, ?RouteCollectorProxyInterface $routeCollectorProxy = null ) { $this->routeResolver = $routeResolver; $this->routeParser = $routeParser; $this->routeCollectorProxy = $routeCollectorProxy; } /** * This request handler is instantiated automatically in App::__construct() * It is at the very tip of the middleware queue meaning it will be executed * last and it detects whether or not routing has been performed in the user * defined middleware stack. In the event that the user did not perform routing * it is done here * * @throws HttpNotFoundException * @throws HttpMethodNotAllowedException */ public function handle(ServerRequestInterface $request): ResponseInterface { // If routing hasn't been done, then do it now so we can dispatch if ($request->getAttribute(RouteContext::ROUTING_RESULTS) === null) { $routingMiddleware = new RoutingMiddleware($this->routeResolver, $this->routeParser); $request = $routingMiddleware->performRouting($request); } if ($this->routeCollectorProxy !== null) { $request = $request->withAttribute( RouteContext::BASE_PATH, $this->routeCollectorProxy->getBasePath() ); } /** @var Route<\Psr\Container\ContainerInterface|null> $route */ $route = $request->getAttribute(RouteContext::ROUTE); return $route->run($request); } } ================================================ FILE: Slim/Routing/RoutingResults.php ================================================ */ protected array $routeArguments; /** * @param array $routeArguments */ public function __construct( DispatcherInterface $dispatcher, string $method, string $uri, int $routeStatus, ?string $routeIdentifier = null, array $routeArguments = [] ) { $this->dispatcher = $dispatcher; $this->method = $method; $this->uri = $uri; $this->routeStatus = $routeStatus; $this->routeIdentifier = $routeIdentifier; $this->routeArguments = $routeArguments; } public function getDispatcher(): DispatcherInterface { return $this->dispatcher; } public function getMethod(): string { return $this->method; } public function getUri(): string { return $this->uri; } public function getRouteStatus(): int { return $this->routeStatus; } public function getRouteIdentifier(): ?string { return $this->routeIdentifier; } /** * @return array */ public function getRouteArguments(bool $urlDecode = true): array { if (!$urlDecode) { return $this->routeArguments; } $routeArguments = []; foreach ($this->routeArguments as $key => $value) { $routeArguments[$key] = rawurldecode($value); } return $routeArguments; } /** * @return string[] */ public function getAllowedMethods(): array { return $this->dispatcher->getAllowedMethods($this->uri); } } ================================================ FILE: UPGRADING.md ================================================ # How to upgrade * [2654] - `RouteParser::pathFor()` and `RouteParser::relativePathFor()` are deprecated. Use `RouteParser::urlFor()` and `RouteParser::relativeUrlFor()` * [2638] - `RouteCollector::pathFor()` is now deprecated. Use `RouteParser::urlFor()` * [2622] - `Router` has been removed. It is now split into `RouteCollector`, `RouteRunner` and `RouteParser` * [2555] - PSR-15 Middleware support was implemented at the cost of Double-Pass middleware being deprecated. * [2529] - Slim no longer ships with its own PSR-7 implementation you will need to provide your own before you can create/run an app. * [2507] - Method names are now case sensitive when using `App::map()`. * [2404] - Slim 4 requires PHP 7.1 or higher * [2398] - Error handling was extracted into its own middleware. Add `RoutingMiddleware` to your middleware pipeline to handle errors by default. See PR for more information. * [2329] - If you were overriding the HTTP method using either the custom header or the body param, you need to add the `Middleware\MethodOverrideMiddleware` middleware to be able to override the method like before. * [2290] - Slim no longer ships with `Pimple` as container dependency so you need to supply your own. `App::__call()` has been deprecated. * [2288] - If you were using `determineRouteBeforeAppMiddleware`, you need to add the `Middleware\RoutingMiddleware` middleware to your application just before your call `run()` to maintain the previous behaviour. * [2254] - You need to add the `Middleware\ContentLengthMiddleware` middleware if you want Slim to add the Content-Length header this automatically. * [2166] - You need to add the `Middleware\OutputBuffering` middleware to capture echo'd or var_dump'd output from your code. [2654]: https://github.com/slimphp/Slim/pull/2654 [2638]: https://github.com/slimphp/Slim/pull/2638 [2622]: https://github.com/slimphp/Slim/pull/2622 [2555]: https://github.com/slimphp/Slim/pull/2555 [2529]: https://github.com/slimphp/Slim/pull/2529 [2507]: https://github.com/slimphp/Slim/pull/2507 [2496]: https://github.com/slimphp/Slim/pull/2496 [2404]: https://github.com/slimphp/Slim/pull/2404 [2398]: https://github.com/slimphp/Slim/pull/2398 [2329]: https://github.com/slimphp/Slim/pull/2329 [2290]: https://github.com/slimphp/Slim/pull/2290 [2288]: https://github.com/slimphp/Slim/pull/2288 [2254]: https://github.com/slimphp/Slim/pull/2254 [2166]: https://github.com/slimphp/Slim/pull/2166 ================================================ FILE: composer.json ================================================ { "name": "slim/slim", "type": "library", "description": "Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs", "keywords": ["framework","micro","api","router"], "homepage": "https://www.slimframework.com", "license": "MIT", "authors": [ { "name": "Josh Lockhart", "email": "hello@joshlockhart.com", "homepage": "https://joshlockhart.com" }, { "name": "Andrew Smith", "email": "a.smith@silentworks.co.uk", "homepage": "https://silentworks.co.uk" }, { "name": "Rob Allen", "email": "rob@akrabat.com", "homepage": "https://akrabat.com" }, { "name": "Pierre Berube", "email": "pierre@lgse.com", "homepage": "https://www.lgse.com" }, { "name": "Gabriel Manricks", "email": "gmanricks@me.com", "homepage": "http://gabrielmanricks.com" } ], "support": { "docs": "https://www.slimframework.com/docs/v4/", "forum": "https://discourse.slimframework.com/", "irc": "irc://irc.freenode.net:6667/slimphp", "issues": "https://github.com/slimphp/Slim/issues", "rss": "https://www.slimframework.com/blog/feed.rss", "slack": "https://slimphp.slack.com/", "source": "https://github.com/slimphp/Slim", "wiki": "https://github.com/slimphp/Slim/wiki" }, "require": { "php": "~7.4.0 || ~8.0.0 || ~8.1.0 || ~8.2.0 || ~8.3.0 || ~8.4.0 || ~8.5.0", "ext-json": "*", "nikic/fast-route": "^1.3", "psr/container": "^1.0 || ^2.0", "psr/http-factory": "^1.1", "psr/http-message": "^1.1 || ^2.0", "psr/http-server-handler": "^1.0", "psr/http-server-middleware": "^1.0", "psr/log": "^1.1 || ^2.0 || ^3.0" }, "require-dev": { "ext-simplexml": "*", "adriansuter/php-autoload-override": "^1.4 || ^2", "guzzlehttp/psr7": "^2.6", "httpsoft/http-message": "^1.1", "httpsoft/http-server-request": "^1.1", "laminas/laminas-diactoros": "^2.17 || ^3", "nyholm/psr7": "^1.8", "nyholm/psr7-server": "^1.1", "phpspec/prophecy": "^1.19", "phpspec/prophecy-phpunit": "^2.1", "phpstan/phpstan": "^1 || ^2", "phpunit/phpunit": "^9.6 || ^10 || ^11 || ^12", "slim/http": "^1.3", "slim/psr7": "^1.6", "squizlabs/php_codesniffer": "^3.10", "vimeo/psalm": "^5 || ^6" }, "autoload": { "psr-4": { "Slim\\": "Slim" } }, "autoload-dev": { "psr-4": { "Slim\\Tests\\": "tests" } }, "scripts": { "test": [ "@phpunit", "@phpcs", "@phpstan", "@psalm" ], "phpunit": "phpunit", "phpcs": "phpcs", "phpstan": "phpstan --memory-limit=-1", "psalm": "psalm --no-cache" }, "suggest": { "ext-simplexml": "Needed to support XML format in BodyParsingMiddleware", "ext-xml": "Needed to support XML format in BodyParsingMiddleware", "slim/psr7": "Slim PSR-7 implementation. See https://www.slimframework.com/docs/v4/start/installation.html for more information.", "php-di/php-di": "PHP-DI is the recommended container library to be used with Slim" }, "config": { "sort-packages": true } } ================================================ FILE: phpcs.xml.dist ================================================ Slim coding standard Slim tests ================================================ FILE: phpstan.neon.dist ================================================ parameters: level: max paths: - Slim ================================================ FILE: phpunit.xml.dist ================================================ tests tests/Mocks Slim ================================================ FILE: psalm.xml ================================================ ================================================ FILE: tests/AppTest.php ================================================ prophesize(ResponseFactoryInterface::class); $containerProphecy = $this->prophesize(ContainerInterface::class); $app = new App($responseFactoryProphecy->reveal(), $containerProphecy->reveal()); $containerProphecy->has(Argument::type('string'))->shouldNotHaveBeenCalled(); $containerProphecy->get(Argument::type('string'))->shouldNotHaveBeenCalled(); } /******************************************************************************** * Getter methods *******************************************************************************/ public function testGetContainer(): void { $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $containerProphecy = $this->prophesize(ContainerInterface::class); $app = new App($responseFactoryProphecy->reveal(), $containerProphecy->reveal()); $this->assertSame($containerProphecy->reveal(), $app->getContainer()); } public function testGetCallableResolverReturnsInjectedInstance(): void { $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $callableResolverProphecy = $this->prophesize(CallableResolverInterface::class); $app = new App($responseFactoryProphecy->reveal(), null, $callableResolverProphecy->reveal()); $this->assertSame($callableResolverProphecy->reveal(), $app->getCallableResolver()); } public function testCreatesCallableResolverWhenNull(): void { $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $containerProphecy = $this->prophesize(ContainerInterface::class); $callableResolver = new CallableResolver($containerProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal(), $containerProphecy->reveal(), null); $this->assertEquals($callableResolver, $app->getCallableResolver()); } public function testGetRouteCollectorReturnsInjectedInstance(): void { $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $routeCollectorProphecy = $this->prophesize(RouteCollectorInterface::class); $routeParserProphecy = $this->prophesize(RouteParserInterface::class); $routeCollectorProphecy->getRouteParser()->willReturn($routeParserProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal(), null, null, $routeCollectorProphecy->reveal()); $this->assertSame($routeCollectorProphecy->reveal(), $app->getRouteCollector()); } public function testCreatesRouteCollectorWhenNullWithInjectedContainer(): void { $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $containerProphecy = $this->prophesize(ContainerInterface::class); $callableResolverProphecy = $this->prophesize(CallableResolverInterface::class); $routeCollector = new RouteCollector( $responseFactoryProphecy->reveal(), $callableResolverProphecy->reveal(), $containerProphecy->reveal() ); $app = new App( $responseFactoryProphecy->reveal(), $containerProphecy->reveal(), $callableResolverProphecy->reveal() ); $this->assertEquals($routeCollector, $app->getRouteCollector()); } public function testGetMiddlewareDispatcherGetsSeededAndReturnsInjectedInstance(): void { $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $middlewareDispatcherProphecy = $this->prophesize(MiddlewareDispatcherInterface::class); $middlewareDispatcherProphecy ->seedMiddlewareStack(Argument::any()) ->shouldBeCalledOnce(); $app = new App( $responseFactoryProphecy->reveal(), null, null, null, null, $middlewareDispatcherProphecy->reveal() ); $this->assertSame($middlewareDispatcherProphecy->reveal(), $app->getMiddlewareDispatcher()); } public static function lowerCaseRequestMethodsProvider(): array { return [ ['get'], ['post'], ['put'], ['patch'], ['delete'], ['options'], ]; } /** * @param string $method * @dataProvider upperCaseRequestMethodsProvider() */ #[\PHPUnit\Framework\Attributes\DataProvider('upperCaseRequestMethodsProvider')] public function testGetPostPutPatchDeleteOptionsMethods(string $method): void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn('Hello World'); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn('/'); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn($method); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type('string'), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $methodName = strtolower($method); $app = new App($responseFactoryProphecy->reveal()); $app->$methodName('/', function (ServerRequestInterface $request, ResponseInterface $response) { return $response; }); $response = $app->handle($requestProphecy->reveal()); $this->assertSame('Hello World', (string) $response->getBody()); } public function testAnyRoute(): void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn('Hello World'); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal()); $app->any('/', function (ServerRequestInterface $request, ResponseInterface $response) { return $response; }); foreach ($this->upperCaseRequestMethodsProvider() as $methods) { $method = $methods[0]; $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn('/'); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn($method); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type('string'), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()); $this->assertSame('Hello World', (string) $response->getBody()); } } /******************************************************************************** * Route collector proxy methods *******************************************************************************/ public static function upperCaseRequestMethodsProvider(): array { return [ ['GET'], ['POST'], ['PUT'], ['PATCH'], ['DELETE'], ['OPTIONS'], ]; } /** * @param string $method * @dataProvider lowerCaseRequestMethodsProvider * @dataProvider upperCaseRequestMethodsProvider */ #[\PHPUnit\Framework\Attributes\DataProvider('lowerCaseRequestMethodsProvider')] #[\PHPUnit\Framework\Attributes\DataProvider('upperCaseRequestMethodsProvider')] public function testMapRoute(string $method): void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn('Hello World'); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn('/'); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn($method); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type('string'), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $app = new App($responseFactoryProphecy->reveal()); $app->map([$method], '/', function (ServerRequestInterface $request, ResponseInterface $response) { return $response; }); $response = $app->handle($requestProphecy->reveal()); $this->assertSame('Hello World', (string) $response->getBody()); } public function testRedirectRoute(): void { $from = '/from'; $to = '/to'; $routeCreatedResponse = $this->prophesize(ResponseInterface::class); $handlerCreatedResponse = $this->prophesize(ResponseInterface::class); $handlerCreatedResponse->getStatusCode()->willReturn(301); $handlerCreatedResponse->getHeaderLine('Location')->willReturn($to); $handlerCreatedResponse->withHeader( Argument::type('string'), Argument::type('string') )->will(function ($args) { $this->getHeader($args[0])->willReturn($args[1]); return $this; }); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($routeCreatedResponse->reveal()); $responseFactoryProphecy->createResponse(301)->willReturn($handlerCreatedResponse->reveal()); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn($from); $uriProphecy->__toString()->willReturn($to); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn('GET'); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type('string'), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $app = new App($responseFactoryProphecy->reveal()); $app->redirect($from, $to, 301); $response = $app->handle($requestProphecy->reveal()); $responseFactoryProphecy->createResponse(301)->shouldHaveBeenCalled(); $this->assertSame(301, $response->getStatusCode()); $this->assertSame($to, $response->getHeaderLine('Location')); } public function testRouteWithInternationalCharacters(): void { $path = '/новости'; $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn('Hello World'); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal()); $app->get($path, function () use ($responseProphecy) { return $responseProphecy->reveal(); }); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn($path); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn('GET'); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type('string'), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()); $this->assertInstanceOf(ResponseInterface::class, $response); $this->assertSame('Hello World', (string) $response->getBody()); } /******************************************************************************** * Route Patterns *******************************************************************************/ public static function routePatternsProvider(): array { return [ [''], // Empty Route ['/'], // Single Slash Route ['foo'], // Route That Does Not Start With A Slash ['/foo'], // Route That Does Not End In A Slash ['/foo/'], // Route That Ends In A Slash ]; } /** * @param string $pattern * @dataProvider routePatternsProvider */ #[\PHPUnit\Framework\Attributes\DataProvider('routePatternsProvider')] public function testRoutePatterns(string $pattern): void { $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $app = new App($responseFactoryProphecy->reveal()); $app->get($pattern, function () { }); $routeCollector = $app->getRouteCollector(); $route = $routeCollector->lookupRoute('route0'); $this->assertSame($pattern, $route->getPattern()); } /******************************************************************************** * Route Groups *******************************************************************************/ public static function routeGroupsDataProvider(): array { return [ 'empty group with empty route' => [ ['', ''], '' ], 'empty group with single slash route' => [ ['', '/'], '/' ], 'empty group with segment route that does not end in aSlash' => [ ['', '/foo'], '/foo' ], 'empty group with segment route that ends in aSlash' => [ ['', '/foo/'], '/foo/' ], 'group single slash with empty route' => [ ['/', ''], '/' ], 'group single slash with single slash route' => [ ['/', '/'], '//' ], 'group single slash with segment route that does not end in aSlash' => [ ['/', '/foo'], '//foo' ], 'group single slash with segment route that ends in aSlash' => [ ['/', '/foo/'], '//foo/' ], 'group segment with empty route' => [ ['/foo', ''], '/foo' ], 'group segment with single slash route' => [ ['/foo', '/'], '/foo/' ], 'group segment with segment route that does not end in aSlash' => [ ['/foo', '/bar'], '/foo/bar' ], 'group segment with segment route that ends in aSlash' => [ ['/foo', '/bar/'], '/foo/bar/' ], 'empty group with nested group segment with an empty route' => [ ['', '/foo', ''], '/foo' ], 'empty group with nested group segment with single slash route' => [ ['', '/foo', '/'], '/foo/' ], 'group single slash with empty nested group and segment route without leading slash' => [ ['/', '', 'foo'], '/foo' ], 'group single slash with empty nested group and segment route' => [ ['/', '', '/foo'], '//foo' ], 'group single slash with single slash group and segment route without leading slash' => [ ['/', '/', 'foo'], '//foo' ], 'group single slash with single slash nested group and segment route' => [ ['/', '/', '/foo'], '///foo' ], 'group single slash with nested group segment with an empty route' => [ ['/', '/foo', ''], '//foo' ], 'group single slash with nested group segment with single slash route' => [ ['/', '/foo', '/'], '//foo/' ], 'group single slash with nested group segment with segment route' => [ ['/', '/foo', '/bar'], '//foo/bar' ], 'group single slash with nested group segment with segment route that has aTrailing slash' => [ ['/', '/foo', '/bar/'], '//foo/bar/' ], 'empty group with empty nested group and segment route without leading slash' => [ ['', '', 'foo'], 'foo' ], 'empty group with empty nested group and segment route' => [ ['', '', '/foo'], '/foo' ], 'empty group with single slash group and segment route without leading slash' => [ ['', '/', 'foo'], '/foo' ], 'empty group with single slash nested group and segment route' => [ ['', '/', '/foo'], '//foo' ], 'empty group with nested group segment with segment route' => [ ['', '/foo', '/bar'], '/foo/bar' ], 'empty group with nested group segment with segment route that has aTrailing slash' => [ ['', '/foo', '/bar/'], '/foo/bar/' ], 'group segment with empty nested group and segment route without leading slash' => [ ['/foo', '', 'bar'], '/foobar' ], 'group segment with empty nested group and segment route' => [ ['/foo', '', '/bar'], '/foo/bar' ], 'group segment with single slash nested group and segment route' => [ ['/foo', '/', 'bar'], '/foo/bar' ], 'group segment with single slash nested group and slash segment route' => [ ['/foo', '/', '/bar'], '/foo//bar' ], 'two group segments with empty route' => [ ['/foo', '/bar', ''], '/foo/bar' ], 'two group segments with single slash route' => [ ['/foo', '/bar', '/'], '/foo/bar/' ], 'two group segments with segment route' => [ ['/foo', '/bar', '/baz'], '/foo/bar/baz' ], 'two group segments with segment route that has aTrailing slash' => [ ['/foo', '/bar', '/baz/'], '/foo/bar/baz/' ], ]; } public function testGroupClosureIsBoundToThisClass(): void { $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $app = new App($responseFactoryProphecy->reveal()); $testCase = $this; $app->group('/foo', function () use ($testCase) { $testCase->assertSame($testCase, $this); }); } /** * @dataProvider routeGroupsDataProvider * @param array $sequence * @param string $expectedPath */ #[\PHPUnit\Framework\Attributes\DataProvider('routeGroupsDataProvider')] public function testRouteGroupCombinations(array $sequence, string $expectedPath): void { $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $app = new App($responseFactoryProphecy->reveal()); $processSequence = function (RouteCollectorProxy $app, array $sequence, $processSequence) { $path = array_shift($sequence); /** * If sequence isn't on last element we use $app->group() * The very tail of the sequence uses the $app->get() method */ if (count($sequence)) { $app->group($path, function (RouteCollectorProxy $group) use (&$sequence, $processSequence) { $processSequence($group, $sequence, $processSequence); }); } else { $app->get($path, function () { }); } }; $processSequence($app, $sequence, $processSequence); $routeCollector = $app->getRouteCollector(); $route = $routeCollector->lookupRoute('route0'); $this->assertSame($expectedPath, $route->getPattern()); } public function testRouteGroupPattern(): void { $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); /** @var ResponseFactoryInterface $responseFactoryInterface */ $responseFactoryInterface = $responseFactoryProphecy->reveal(); $app = new App($responseFactoryInterface); $group = $app->group('/foo', function () { }); $this->assertSame('/foo', $group->getPattern()); } /******************************************************************************** * Middleware *******************************************************************************/ public function testAddMiddleware(): void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn('Hello World'); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal()); $middlewareProphecy = $this->prophesize(MiddlewareInterface::class); $middlewareProphecy->process(Argument::cetera())->will(function () use ($responseProphecy) { return $responseProphecy->reveal(); }); $middlewareProphecy2 = $this->prophesize(MiddlewareInterface::class); $middlewareProphecy2->process( Argument::type(ServerRequestInterface::class), Argument::type(RequestHandlerInterface::class) )->will(function ($args) { /** @var ServerRequestInterface $request */ $request = $args[0]; /** @var RequestHandlerInterface $handler */ $handler = $args[1]; return $handler->handle($request); }); $app->add($middlewareProphecy->reveal()); $app->addMiddleware($middlewareProphecy2->reveal()); $app->get('/', function (ServerRequestInterface $request, $response) { return $response; }); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn('/'); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn('GET'); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type('string'), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()); $middlewareProphecy->process( Argument::type(ServerRequestInterface::class), Argument::type(RequestHandlerInterface::class) )->shouldHaveBeenCalled(); $middlewareProphecy2->process( Argument::type(ServerRequestInterface::class), Argument::type(RequestHandlerInterface::class) )->shouldHaveBeenCalled(); $this->assertSame($responseProphecy->reveal(), $response); } public function testAddMiddlewareUsingDeferredResolution(): void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn('Hello World'); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $middlewareProphecy = $this->prophesize(MiddlewareInterface::class); $middlewareProphecy->process(Argument::cetera())->willReturn($responseProphecy->reveal()); $containerProphecy = $this->prophesize(ContainerInterface::class); $containerProphecy->has('middleware')->willReturn(true); $containerProphecy->get('middleware')->willReturn($middlewareProphecy); $app = new App($responseFactoryProphecy->reveal(), $containerProphecy->reveal()); $app->add('middleware'); $app->get('/', function (ServerRequestInterface $request, ResponseInterface $response) { return $response; }); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn('/'); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn('GET'); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $response = $app->handle($requestProphecy->reveal()); $this->assertSame('Hello World', (string) $response->getBody()); } public function testAddRoutingMiddleware(): void { /** @var ResponseFactoryInterface $responseFactory */ $responseFactory = $this->prophesize(ResponseFactoryInterface::class)->reveal(); // Create the app. $app = new App($responseFactory); // Add the routing middleware. $routingMiddleware = $app->addRoutingMiddleware(); // Check that the routing middleware really has been added to the tip of the app middleware stack. $middlewareDispatcherProperty = new ReflectionProperty(App::class, 'middlewareDispatcher'); $this->setAccessible($middlewareDispatcherProperty); /** @var MiddlewareDispatcher $middlewareDispatcher */ $middlewareDispatcher = $middlewareDispatcherProperty->getValue($app); $tipProperty = new ReflectionProperty(MiddlewareDispatcher::class, 'tip'); $this->setAccessible($tipProperty); /** @var RequestHandlerInterface $tip */ $tip = $tipProperty->getValue($middlewareDispatcher); $reflection = new ReflectionClass($tip); $middlewareProperty = $reflection->getProperty('middleware'); $this->setAccessible($middlewareProperty); $this->assertSame($routingMiddleware, $middlewareProperty->getValue($tip)); $this->assertInstanceOf(RoutingMiddleware::class, $routingMiddleware); } public function testAddErrorMiddleware(): void { /** @var ResponseFactoryInterface $responseFactory */ $responseFactory = $this->prophesize(ResponseFactoryInterface::class)->reveal(); /** @var LoggerInterface $logger */ $logger = $this->prophesize(LoggerInterface::class)->reveal(); // Create the app. $app = new App($responseFactory); // Add the error middleware. $errorMiddleware = $app->addErrorMiddleware(true, true, true, $logger); // Check that the error middleware really has been added to the tip of the app middleware stack. $middlewareDispatcherProperty = new ReflectionProperty(App::class, 'middlewareDispatcher'); $this->setAccessible($middlewareDispatcherProperty); /** @var MiddlewareDispatcher $middlewareDispatcher */ $middlewareDispatcher = $middlewareDispatcherProperty->getValue($app); $tipProperty = new ReflectionProperty(MiddlewareDispatcher::class, 'tip'); $this->setAccessible($tipProperty); /** @var RequestHandlerInterface $tip */ $tip = $tipProperty->getValue($middlewareDispatcher); $reflection = new ReflectionClass($tip); $middlewareProperty = $reflection->getProperty('middleware'); $this->setAccessible($middlewareProperty); $this->assertSame($errorMiddleware, $middlewareProperty->getValue($tip)); $this->assertInstanceOf(ErrorMiddleware::class, $errorMiddleware); } public function testAddBodyParsingMiddleware(): void { /** @var ResponseFactoryInterface $responseFactory */ $responseFactory = $this->prophesize(ResponseFactoryInterface::class)->reveal(); // Create the app. $app = new App($responseFactory); // Add the error middleware. $bodyParsingMiddleware = $app->addBodyParsingMiddleware(); // Check that the body parsing middleware really has been added to the tip of the app middleware stack. $middlewareDispatcherProperty = new ReflectionProperty(App::class, 'middlewareDispatcher'); $this->setAccessible($middlewareDispatcherProperty); /** @var MiddlewareDispatcher $middlewareDispatcher */ $middlewareDispatcher = $middlewareDispatcherProperty->getValue($app); $tipProperty = new ReflectionProperty(MiddlewareDispatcher::class, 'tip'); $this->setAccessible($tipProperty); /** @var RequestHandlerInterface $tip */ $tip = $tipProperty->getValue($middlewareDispatcher); $reflection = new ReflectionClass($tip); $middlewareProperty = $reflection->getProperty('middleware'); $this->setAccessible($middlewareProperty); $this->assertSame($bodyParsingMiddleware, $middlewareProperty->getValue($tip)); $this->assertInstanceOf(BodyParsingMiddleware::class, $bodyParsingMiddleware); } public function testAddMiddlewareOnRoute(): void { $output = ''; $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn('Hello World'); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $middlewareProphecy = $this->prophesize(MiddlewareInterface::class); $middlewareProphecy->process( Argument::type(ServerRequestInterface::class), Argument::type(RequestHandlerInterface::class) )->will(function ($args) use (&$output) { /** @var ServerRequestInterface $request */ $request = $args[0]; /** @var RequestHandlerInterface $handler */ $handler = $args[1]; $output .= 'In1'; /** @var ResponseInterface $response */ $response = $handler->handle($request); $output .= 'Out1'; return $response; }); $middlewareProphecy2 = $this->prophesize(MiddlewareInterface::class); $middlewareProphecy2->process( Argument::type(ServerRequestInterface::class), Argument::type(RequestHandlerInterface::class) )->will(function ($args) use (&$output) { /** @var ServerRequestInterface $request */ $request = $args[0]; /** @var RequestHandlerInterface $handler */ $handler = $args[1]; $output .= 'In2'; /** @var ResponseInterface $response */ $response = $handler->handle($request); $output .= 'Out2'; return $response; }); $app = new App($responseFactoryProphecy->reveal()); $app->get('/', function (ServerRequestInterface $request, ResponseInterface $response) use (&$output) { $output .= 'Center'; return $response; }) ->add($middlewareProphecy->reveal()) ->addMiddleware($middlewareProphecy2->reveal()); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn('/'); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn('GET'); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type('string'), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $app->handle($requestProphecy->reveal()); $this->assertSame('In2In1CenterOut1Out2', $output); } public function testAddMiddlewareOnRouteGroup(): void { $output = ''; $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn('Hello World'); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $middlewareProphecy = $this->prophesize(MiddlewareInterface::class); $middlewareProphecy->process( Argument::type(ServerRequestInterface::class), Argument::type(RequestHandlerInterface::class) )->will(function ($args) use (&$output) { /** @var ServerRequestInterface $request */ $request = $args[0]; /** @var RequestHandlerInterface $handler */ $handler = $args[1]; $output .= 'In1'; /** @var ResponseInterface $response */ $response = $handler->handle($request); $output .= 'Out1'; return $response; }); $middlewareProphecy2 = $this->prophesize(MiddlewareInterface::class); $middlewareProphecy2->process( Argument::type(ServerRequestInterface::class), Argument::type(RequestHandlerInterface::class) )->will(function ($args) use (&$output) { /** @var ServerRequestInterface $request */ $request = $args[0]; /** @var RequestHandlerInterface $handler */ $handler = $args[1]; $output .= 'In2'; /** @var ResponseInterface $response */ $response = $handler->handle($request); $output .= 'Out2'; return $response; }); $app = new App($responseFactoryProphecy->reveal()); $app->group('/foo', function (RouteCollectorProxy $proxy) use (&$output) { $proxy->get('/bar', function (ServerRequestInterface $request, ResponseInterface $response) use (&$output) { $output .= 'Center'; return $response; }); }) ->add($middlewareProphecy->reveal()) ->addMiddleware($middlewareProphecy2->reveal()); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn('/foo/bar'); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn('GET'); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type('string'), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $app->handle($requestProphecy->reveal()); $this->assertSame('In2In1CenterOut1Out2', $output); } public function testAddMiddlewareOnTwoRouteGroup(): void { $output = ''; $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn('Hello World'); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $middlewareProphecy = $this->prophesize(MiddlewareInterface::class); $middlewareProphecy->process( Argument::type(ServerRequestInterface::class), Argument::type(RequestHandlerInterface::class) )->will(function ($args) use (&$output) { /** @var ServerRequestInterface $request */ $request = $args[0]; /** @var RequestHandlerInterface $handler */ $handler = $args[1]; $output .= 'In1'; /** @var ResponseInterface $response */ $response = $handler->handle($request); $output .= 'Out1'; return $response; }); $middlewareProphecy2 = $this->prophesize(MiddlewareInterface::class); $middlewareProphecy2->process( Argument::type(ServerRequestInterface::class), Argument::type(RequestHandlerInterface::class) )->will(function ($args) use (&$output) { /** @var ServerRequestInterface $request */ $request = $args[0]; /** @var RequestHandlerInterface $handler */ $handler = $args[1]; $output .= 'In2'; /** @var ResponseInterface $response */ $response = $handler->handle($request); $output .= 'Out2'; return $response; }); $middlewareProphecy3 = $this->prophesize(MiddlewareInterface::class); $middlewareProphecy3->process( Argument::type(ServerRequestInterface::class), Argument::type(RequestHandlerInterface::class) )->will(function ($args) use (&$output) { /** @var ServerRequestInterface $request */ $request = $args[0]; /** @var RequestHandlerInterface $handler */ $handler = $args[1]; $output .= 'In3'; /** @var ResponseInterface $response */ $response = $handler->handle($request); $output .= 'Out3'; return $response; }); $app = new App($responseFactoryProphecy->reveal()); $app->group('/foo', function (RouteCollectorProxyInterface $group) use ( $middlewareProphecy2, $middlewareProphecy3, &$output ) { // ensure that more than one nested group at the same level doesn't break middleware $group->group('/fizz', function (RouteCollectorProxyInterface $group) { $group->get('/buzz', function (ServerRequestInterface $request, ResponseInterface $response) { return $response; }); }); $group->group('/bar', function (RouteCollectorProxyInterface $group) use ( $middlewareProphecy3, &$output ) { $group->get('/baz', function ( ServerRequestInterface $request, ResponseInterface $response ) use (&$output) { $output .= 'Center'; return $response; })->add($middlewareProphecy3->reveal()); })->add($middlewareProphecy2->reveal()); })->add($middlewareProphecy->reveal()); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn('/foo/bar/baz'); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn('GET'); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type('string'), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $app->handle($requestProphecy->reveal()); $this->assertSame('In1In2In3CenterOut3Out2Out1', $output); } public function testAddMiddlewareAsStringNotImplementingInterfaceThrowsException(): void { $this->expectException(RuntimeException::class); $this->expectExceptionMessage( 'A middleware must be an object/class name referencing an implementation of ' . 'MiddlewareInterface or a callable with a matching signature.' ); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $app = new App($responseFactoryProphecy->reveal()); $app->add(new stdClass()); } /******************************************************************************** * Runner *******************************************************************************/ public function testInvokeReturnMethodNotAllowed(): void { $this->expectException(HttpMethodNotAllowedException::class); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $app = new App($responseFactoryProphecy->reveal()); $app->get('/', function () { }); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn('/'); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn('POST'); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type('string'), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $app->handle($requestProphecy->reveal()); } public function testInvokeWithMatchingRoute(): void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn('Hello World'); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal()); $app->get('/', function (ServerRequestInterface $request, $response) { return $response; }); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn('/'); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn('GET'); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type('string'), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()); $this->assertInstanceOf(ResponseInterface::class, $response); $this->assertSame('Hello World', (string) $response->getBody()); } public function testInvokeWithMatchingRouteWithSetArgument(): void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn(''); $streamProphecy->write(Argument::type('string'))->will(function ($args) { $body = $this->reveal()->__toString(); $body .= $args[0]; $this->__toString()->willReturn($body); return 0; }); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal()); $app->get('/', function (ServerRequestInterface $request, ResponseInterface $response, $args) { $response->getBody()->write("Hello {$args['name']}"); return $response; })->setArgument('name', 'World'); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn('/'); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn('GET'); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type('string'), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()); $this->assertInstanceOf(ResponseInterface::class, $response); $this->assertSame('Hello World', (string) $response->getBody()); } public function testInvokeWithMatchingRouteWithSetArguments(): void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn(''); $streamProphecy->write(Argument::type('string'))->will(function ($args) { $body = $this->reveal()->__toString(); $body .= $args[0]; $this->__toString()->willReturn($body); return 0; }); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal()); $app->get('/', function (ServerRequestInterface $request, ResponseInterface $response, $args) { $response->getBody()->write("{$args['greeting']} {$args['name']}"); return $response; })->setArguments(['greeting' => 'Hello', 'name' => 'World']); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn('/'); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn('GET'); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type('string'), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()); $this->assertInstanceOf(ResponseInterface::class, $response); $this->assertSame('Hello World', (string) $response->getBody()); } public function testInvokeWithMatchingRouteWithNamedParameterRequestResponseStrategy(): void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn(''); $streamProphecy->write(Argument::type('string'))->will(function ($args) { $body = $this->reveal()->__toString(); $body .= $args[0]; $this->__toString()->willReturn($body); return 0; }); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal()); $app->get('/Hello/{name}', function (ServerRequestInterface $request, ResponseInterface $response, $args) { $response->getBody()->write("Hello {$args['name']}"); return $response; }); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn('/Hello/World'); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn('GET'); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type('string'), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()); $this->assertInstanceOf(ResponseInterface::class, $response); $this->assertSame('Hello World', (string) $response->getBody()); } public function testInvokeWithMatchingRouteWithNamedParameterRequestResponseArgStrategy(): void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn(''); $streamProphecy->write(Argument::type('string'))->will(function ($args) { $body = $this->reveal()->__toString(); $body .= $args[0]; $this->__toString()->willReturn($body); return 0; }); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal()); $app->getRouteCollector()->setDefaultInvocationStrategy(new RequestResponseArgs()); $app->get('/Hello/{name}', function (ServerRequestInterface $request, ResponseInterface $response, $name) { $response->getBody()->write("Hello {$name}"); return $response; }); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn('/Hello/World'); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn('GET'); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type('string'), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()); $this->assertInstanceOf(ResponseInterface::class, $response); $this->assertSame('Hello World', (string) $response->getBody()); } public function testInvokeWithMatchingRouteWithNamedParameterRequestResponseNamedArgsStrategy(): void { if (PHP_VERSION_ID < 80000) { $this->markTestSkipped('Named arguments are not supported in PHP versions prior to 8.0'); } $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn(''); $streamProphecy->write(Argument::type('string'))->will(function ($args) { $body = $this->reveal()->__toString(); $body .= $args[0]; $this->__toString()->willReturn($body); return 0; }); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal()); $app->getRouteCollector()->setDefaultInvocationStrategy(new RequestResponseNamedArgs()); $app->get( '/{greeting}/{name}', function (ServerRequestInterface $request, ResponseInterface $response, $name, $greeting) { $response->getBody()->write("{$greeting} {$name}"); return $response; } ); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn('/Hello/World'); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn('GET'); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type('string'), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()); $this->assertInstanceOf(ResponseInterface::class, $response); $this->assertSame('Hello World', (string) $response->getBody()); } public function testInvokeWithMatchingRouteWithNamedParameterOverwritesSetArgument(): void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn(''); $streamProphecy->write(Argument::type('string'))->will(function ($args) { $body = $this->reveal()->__toString(); $body .= $args[0]; $this->__toString()->willReturn($body); return 0; }); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal()); $app->get('/Hello/{name}', function (ServerRequestInterface $request, ResponseInterface $response, $args) { $response->getBody()->write("Hello {$args['name']}"); return $response; })->setArgument('name', 'World!'); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn('/Hello/World'); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn('GET'); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type('string'), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()); $this->assertInstanceOf(ResponseInterface::class, $response); $this->assertSame('Hello World', (string) $response->getBody()); } public function testInvokeWithoutMatchingRoute(): void { $this->expectException(HttpNotFoundException::class); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $app = new App($responseFactoryProphecy->reveal()); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn('/'); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn('GET'); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type('string'), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $app->handle($requestProphecy->reveal()); } public function testInvokeWithCallableRegisteredInContainer(): void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn('Hello World'); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $handler = new class { public function foo(ServerRequestInterface $request, ResponseInterface $response) { return $response; } }; $containerProphecy = $this->prophesize(ContainerInterface::class); $containerProphecy->has('handler')->willReturn(true); $containerProphecy->get('handler')->willReturn($handler); $app = new App($responseFactoryProphecy->reveal(), $containerProphecy->reveal()); $app->get('/', 'handler:foo'); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn('/'); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn('GET'); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type('string'), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()); $this->assertInstanceOf(ResponseInterface::class, $response); $this->assertSame('Hello World', (string) $response->getBody()); } public function testInvokeWithNonExistentMethodOnCallableRegisteredInContainer(): void { $this->expectException(RuntimeException::class); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $handler = new class { }; $containerProphecy = $this->prophesize(ContainerInterface::class); $containerProphecy->has('handler')->willReturn(true); $containerProphecy->get('handler')->willReturn($handler); $app = new App($responseFactoryProphecy->reveal(), $containerProphecy->reveal()); $app->get('/', 'handler:method_does_not_exist'); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn('/'); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn('GET'); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type('string'), Argument::any()) ->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $app->handle($requestProphecy->reveal()); } public function testInvokeWithCallableInContainerViaCallMagicMethod(): void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn(''); $streamProphecy->write(Argument::type('string'))->will(function ($args) { $body = $this->reveal()->__toString(); $body .= $args[0]; $this->__toString()->willReturn($body); return 0; }); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $mockAction = new MockAction(); $containerProphecy = $this->prophesize(ContainerInterface::class); $containerProphecy->has('handler')->willReturn(true); $containerProphecy->get('handler')->willReturn($mockAction); $app = new App($responseFactoryProphecy->reveal(), $containerProphecy->reveal()); $app->get('/', 'handler:foo'); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn('/'); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn('GET'); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type('string'), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()); $expectedPayload = json_encode(['name' => 'foo', 'arguments' => []]); $this->assertInstanceOf(ResponseInterface::class, $response); $this->assertSame($expectedPayload, (string) $response->getBody()); } public function testInvokeFunctionName(): void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn(''); $streamProphecy->write(Argument::type('string'))->will(function ($args) { $body = $this->reveal()->__toString(); $body .= $args[0]; $this->__toString()->willReturn($body); return 0; }); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); // @codingStandardsIgnoreStart function handle($request, ResponseInterface $response) { $response->getBody()->write('Hello World'); return $response; } // @codingStandardsIgnoreEnd $app = new App($responseFactoryProphecy->reveal()); $app->get('/', __NAMESPACE__ . '\handle'); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn('/'); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn('GET'); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type('string'), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()); $this->assertInstanceOf(ResponseInterface::class, $response); $this->assertSame('Hello World', (string) $response->getBody()); } public function testCurrentRequestAttributesAreNotLostWhenAddingRouteArguments(): void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn(''); $streamProphecy->write(Argument::type('string'))->will(function ($args) { $body = $this->reveal()->__toString(); $body .= $args[0]; $this->__toString()->willReturn($body); return 0; }); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal()); $app->get('/Hello/{name}', function (ServerRequestInterface $request, ResponseInterface $response, $args) { $response->getBody()->write($request->getAttribute('greeting') . ' ' . $args['name']); return $response; }); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn('/Hello/World'); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn('GET'); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type('string'), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()->withAttribute('greeting', 'Hello')); $this->assertInstanceOf(ResponseInterface::class, $response); $this->assertSame('Hello World', (string) $response->getBody()); } public function testCurrentRequestAttributesAreNotLostWhenAddingRouteArgumentsRequestResponseArg(): void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn(''); $streamProphecy->write(Argument::type('string'))->will(function ($args) { $body = $this->reveal()->__toString(); $body .= $args[0]; $this->__toString()->willReturn($body); return 0; }); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal()); $app->getRouteCollector()->setDefaultInvocationStrategy(new RequestResponseArgs()); $app->get('/Hello/{name}', function (ServerRequestInterface $request, ResponseInterface $response, $name) { $response->getBody()->write($request->getAttribute('greeting') . ' ' . $name); return $response; }); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn('/Hello/World'); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn('GET'); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type('string'), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()->withAttribute('greeting', 'Hello')); $this->assertInstanceOf(ResponseInterface::class, $response); $this->assertSame('Hello World', (string) $response->getBody()); } public function testRun(): void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn(''); $streamProphecy->write(Argument::type('string'))->will(function ($args) { $body = $this->reveal()->__toString(); $body .= $args[0]; $this->__toString()->willReturn($body); return 0; }); $streamProphecy->read(1)->willReturn('_'); $streamProphecy->read('11')->will(function () { $this->eof()->willReturn(true); return $this->reveal()->__toString(); }); $streamProphecy->eof()->willReturn(false); $streamProphecy->isSeekable()->willReturn(true); $streamProphecy->rewind()->shouldBeCalled(); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseProphecy->getStatusCode()->willReturn(200); $responseProphecy->getHeaders()->willReturn(['Content-Length' => ['11']]); $responseProphecy->getProtocolVersion()->willReturn('1.1'); $responseProphecy->getReasonPhrase()->willReturn(''); $responseProphecy->getHeaderLine('Content-Length')->willReturn('11'); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal()); $app->get('/', function (ServerRequestInterface $request, ResponseInterface $response) { $response->getBody()->write('Hello World'); return $response; }); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn('/'); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn('GET'); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type('string'), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $app->run($requestProphecy->reveal()); $this->expectOutputString('Hello World'); } public function testRunWithoutPassingInServerRequest(): void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn(''); $streamProphecy->write(Argument::type('string'))->will(function ($args) { $body = $this->reveal()->__toString(); $body .= $args[0]; $this->__toString()->willReturn($body); return 0; }); $streamProphecy->read(1)->willReturn('_'); $streamProphecy->read(11)->will(function () { $this->eof()->willReturn(true); return $this->reveal()->__toString(); }); $streamProphecy->eof()->willReturn(false); $streamProphecy->isSeekable()->willReturn(true); $streamProphecy->rewind()->shouldBeCalled(); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseProphecy->getStatusCode()->willReturn(200); $responseProphecy->getHeaders()->willReturn(['Content-Length' => ['11']]); $responseProphecy->getProtocolVersion()->willReturn('1.1'); $responseProphecy->getReasonPhrase()->willReturn(''); $responseProphecy->getHeaderLine('Content-Length')->willReturn('11'); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal()); $app->get('/', function (ServerRequestInterface $request, ResponseInterface $response) { $response->getBody()->write('Hello World'); return $response; }); $app->run(); $this->expectOutputString('Hello World'); } public function testHandleReturnsEmptyResponseBodyWithHeadRequestMethod(): void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn(''); $streamProphecy->write(Argument::type('string'))->will(function ($args) { $body = $this->reveal()->__toString(); $body .= $args[0]; $this->__toString()->willReturn($body); return 0; }); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseProphecy ->withBody(Argument::type(StreamInterface::class)) ->will(function ($args) { $this->getBody()->willReturn($args[0]); return $this; }); $emptyStreamProphecy = $this->prophesize(StreamInterface::class); $emptyStreamProphecy->__toString()->willReturn(''); $emptyResponseProphecy = $this->prophesize(ResponseInterface::class); $emptyResponseProphecy->getBody()->willReturn($emptyStreamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn( $responseProphecy->reveal(), $emptyResponseProphecy->reveal() ); $called = 0; $app = new App($responseFactoryProphecy->reveal()); $app->get('/', function (ServerRequestInterface $request, ResponseInterface $response) use (&$called) { $called++; $response->getBody()->write('Hello World'); return $response; }); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn('/'); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn('HEAD'); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type('string'), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()); $this->assertSame(1, $called); $this->assertEmpty((string) $response->getBody()); } public function testCanBeReExecutedRecursivelyDuringDispatch(): void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn(''); $streamProphecy->write(Argument::type('string'))->will(function ($args) { $body = $this->reveal()->__toString(); $body .= $args[0]; $this->__toString()->willReturn($body); return 0; }); $responseHeaders = []; $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseProphecy->getStatusCode()->willReturn(200); $responseProphecy->getHeader(Argument::type('string'))->will(function ($args) use (&$responseHeaders) { return $responseHeaders[$args[0]]; }); $responseProphecy->withAddedHeader( Argument::type('string'), Argument::type('string') )->will(function ($args) use (&$responseHeaders) { $key = $args[0]; $value = $args[1]; if (!isset($responseHeaders[$key])) { $responseHeaders[$key] = []; } $responseHeaders[$key][] = $value; return $this; }); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy ->createResponse(Argument::type('integer')) ->will(function ($args) use ($responseProphecy) { $responseProphecy->getStatusCode()->willReturn($args[0]); return $responseProphecy->reveal(); }); $app = new App($responseFactoryProphecy->reveal()); $middlewareProphecy = $this->prophesize(MiddlewareInterface::class); $middlewareProphecy->process( Argument::type(ServerRequestInterface::class), Argument::type(RequestHandlerInterface::class) )->will(function ($args) use ($app, $responseFactoryProphecy) { /** @var ServerRequestInterface $request */ $request = $args[0]; if ($request->hasHeader('X-NESTED')) { return $responseFactoryProphecy ->reveal() ->createResponse(204) ->withAddedHeader('X-TRACE', 'nested'); } /** @var ResponseInterface $response */ $response = $app->handle($request->withAddedHeader('X-NESTED', '1')); return $response->withAddedHeader('X-TRACE', 'outer'); }); $middlewareProphecy2 = $this->prophesize(MiddlewareInterface::class); $middlewareProphecy2->process( Argument::type(ServerRequestInterface::class), Argument::type(RequestHandlerInterface::class) )->will(function ($args) { /** @var ServerRequestInterface $request */ $request = $args[0]; /** @var RequestHandlerInterface $handler */ $handler = $args[1]; /** @var ResponseInterface $response */ $response = $handler->handle($request); $response->getBody()->write('1'); return $response; }); $app ->add($middlewareProphecy->reveal()) ->add($middlewareProphecy2->reveal()); $app->get('/', function (ServerRequestInterface $request, ResponseInterface $response) { return $response; }); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn('/'); $responseHeaders = []; $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn('GET'); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->hasHeader(Argument::type('string'))->will(function ($args) use (&$responseHeaders) { return array_key_exists($args[0], $responseHeaders); }); $requestProphecy->withAddedHeader( Argument::type('string'), Argument::type('string') )->will(function ($args) use (&$responseHeaders) { $key = $args[0]; $value = $args[1]; if (!isset($responseHeaders[$key])) { $responseHeaders[$key] = []; } $responseHeaders[$key][] = $value; return $this; }); $response = $app->handle($requestProphecy->reveal()); $this->assertSame(204, $response->getStatusCode()); $this->assertSame(['nested', 'outer'], $response->getHeader('X-TRACE')); $this->assertSame('11', (string) $response->getBody()); } // TODO: Re-add testUnsupportedMethodWithoutRoute // TODO: Re-add testUnsupportedMethodWithRoute public function testContainerSetToRoute(): void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn('Hello World'); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $containerProphecy = $this->prophesize(ContainerInterface::class); $containerProphecy->has('handler')->willReturn(true); $containerProphecy->get('handler')->willReturn(function () use ($responseProphecy) { return $responseProphecy->reveal(); }); $app = new App($responseFactoryProphecy->reveal(), $containerProphecy->reveal()); $routeCollector = $app->getRouteCollector(); $routeCollector->map(['GET'], '/', 'handler'); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn('/'); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn('GET'); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type('string'), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()); $this->assertSame('Hello World', (string) $response->getBody()); } public function testAppIsARequestHandler(): void { $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $app = new App($responseFactoryProphecy->reveal()); $this->assertInstanceOf(RequestHandlerInterface::class, $app); } public function testInvokeSequentialProcessToAPathWithOptionalArgsAndWithoutOptionalArgs(): void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn(''); $streamProphecy->write(Argument::type('string'))->will(function ($args) { $body = $this->reveal()->__toString(); $body .= $args[0]; $this->__toString()->willReturn($body); return 0; }); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal()); $app->get('/Hello[/{name}]', function (ServerRequestInterface $request, ResponseInterface $response, $args) { $response->getBody()->write((string) count($args)); return $response; }); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn('/Hello/World'); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn('GET'); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type('string'), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()); $this->assertSame('1', (string) $response->getBody()); $uriProphecy2 = $this->prophesize(UriInterface::class); $uriProphecy2->getPath()->willReturn('/Hello'); $requestProphecy2 = $this->prophesize(ServerRequestInterface::class); $requestProphecy2->getMethod()->willReturn('GET'); $requestProphecy2->getUri()->willReturn($uriProphecy2->reveal()); $requestProphecy2->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy2->withAttribute(Argument::type('string'), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $streamProphecy->__toString()->willReturn(''); $response = $app->handle($requestProphecy2->reveal()); $this->assertSame('0', (string) $response->getBody()); } public function testInvokeSequentialProcessToAPathWithOptionalArgsAndWithoutOptionalArgsAndKeepSetedArgs(): void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn(''); $streamProphecy->write(Argument::type('string'))->will(function ($args) { $body = $this->reveal()->__toString(); $body .= $args[0]; $this->__toString()->willReturn($body); return 0; }); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal()); $app->get('/Hello[/{name}]', function (ServerRequestInterface $request, ResponseInterface $response, $args) { $response->getBody()->write((string) count($args)); return $response; })->setArgument('extra', 'value'); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn('/Hello/World'); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn('GET'); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type('string'), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()); $this->assertSame('2', (string) $response->getBody()); $uriProphecy2 = $this->prophesize(UriInterface::class); $uriProphecy2->getPath()->willReturn('/Hello'); $requestProphecy2 = $this->prophesize(ServerRequestInterface::class); $requestProphecy2->getMethod()->willReturn('GET'); $requestProphecy2->getUri()->willReturn($uriProphecy2->reveal()); $requestProphecy2->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy2->withAttribute(Argument::type('string'), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $streamProphecy->__toString()->willReturn(''); $response = $app->handle($requestProphecy2->reveal()); $this->assertSame('1', (string) $response->getBody()); } public function testInvokeSequentialProcessAfterAddingAnotherRouteArgument(): void { $streamProphecy = $this->prophesize(StreamInterface::class); $streamProphecy->__toString()->willReturn(''); $streamProphecy->write(Argument::type('string'))->will(function ($args) { $body = $this->reveal()->__toString(); $body .= $args[0]; $this->__toString()->willReturn($body); return 0; }); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getBody()->willReturn($streamProphecy->reveal()); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal()); $app = new App($responseFactoryProphecy->reveal()); $route = $app->get('/Hello[/{name}]', function ( ServerRequestInterface $request, ResponseInterface $response, $args ) { $response->getBody()->write((string) count($args)); return $response; })->setArgument('extra', 'value'); $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy->getPath()->willReturn('/Hello/World'); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getMethod()->willReturn('GET'); $requestProphecy->getUri()->willReturn($uriProphecy->reveal()); $requestProphecy->getAttribute(RouteContext::ROUTE)->willReturn($route); $requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null); $requestProphecy->withAttribute(Argument::type('string'), Argument::any())->will(function ($args) { $this->getAttribute($args[0])->willReturn($args[1]); return $this; }); $response = $app->handle($requestProphecy->reveal()); $this->assertSame('2', (string) $response->getBody()); $route->setArgument('extra2', 'value2'); $streamProphecy->__toString()->willReturn(''); $response = $app->handle($requestProphecy->reveal()); $this->assertSame('3', (string) $response->getBody()); } } ================================================ FILE: tests/Assets/HeaderStack.php ================================================ containerProphecy = $this->prophesize(ContainerInterface::class); $this->containerProphecy->has(Argument::type('string'))->willReturn(false); } public function testClosure(): void { $test = function () { return true; }; $resolver = new CallableResolver(); // No container injected $callable = $resolver->resolve($test); $callableRoute = $resolver->resolveRoute($test); $callableMiddleware = $resolver->resolveMiddleware($test); $this->assertTrue($callable()); $this->assertTrue($callableRoute()); $this->assertTrue($callableMiddleware()); } public function testClosureContainer(): void { $this->containerProphecy->has('ultimateAnswer')->willReturn(true); $this->containerProphecy->get('ultimateAnswer')->willReturn(42); $that = $this; $test = function () use ($that) { $that->assertInstanceOf(ContainerInterface::class, $this); /** @var ContainerInterface $this */ return $this->get('ultimateAnswer'); }; /** @var ContainerInterface $container */ $container = $this->containerProphecy->reveal(); $resolver = new CallableResolver($container); $callable = $resolver->resolve($test); $callableRoute = $resolver->resolveRoute($test); $callableMiddleware = $resolver->resolveMiddleware($test); $this->assertSame(42, $callable()); $this->assertSame(42, $callableRoute()); $this->assertSame(42, $callableMiddleware()); } public function testFunctionName(): void { $resolver = new CallableResolver(); // No container injected $callable = $resolver->resolve(__NAMESPACE__ . '\testAdvancedCallable'); $callableRoute = $resolver->resolveRoute(__NAMESPACE__ . '\testAdvancedCallable'); $callableMiddleware = $resolver->resolveMiddleware(__NAMESPACE__ . '\testAdvancedCallable'); $this->assertTrue($callable()); $this->assertTrue($callableRoute()); $this->assertTrue($callableMiddleware()); } public function testObjMethodArray(): void { $obj = new CallableTest(); $resolver = new CallableResolver(); // No container injected $callable = $resolver->resolve([$obj, 'toCall']); $callableRoute = $resolver->resolveRoute([$obj, 'toCall']); $callableMiddleware = $resolver->resolveMiddleware([$obj, 'toCall']); $callable(); $this->assertSame(1, CallableTest::$CalledCount); $callableRoute(); $this->assertSame(2, CallableTest::$CalledCount); $callableMiddleware(); $this->assertSame(3, CallableTest::$CalledCount); } public function testSlimCallable(): void { $resolver = new CallableResolver(); // No container injected $callable = $resolver->resolve('Slim\Tests\Mocks\CallableTest:toCall'); $callableRoute = $resolver->resolveRoute('Slim\Tests\Mocks\CallableTest:toCall'); $callableMiddleware = $resolver->resolveMiddleware('Slim\Tests\Mocks\CallableTest:toCall'); $callable(); $this->assertSame(1, CallableTest::$CalledCount); $callableRoute(); $this->assertSame(2, CallableTest::$CalledCount); $callableMiddleware(); $this->assertSame(3, CallableTest::$CalledCount); } public function testSlimCallableAsArray(): void { $resolver = new CallableResolver(); // No container injected $callable = $resolver->resolve([CallableTest::class, 'toCall']); $callableRoute = $resolver->resolveRoute([CallableTest::class, 'toCall']); $callableMiddleware = $resolver->resolveMiddleware([CallableTest::class, 'toCall']); $callable(); $this->assertSame(1, CallableTest::$CalledCount); $callableRoute(); $this->assertSame(2, CallableTest::$CalledCount); $callableMiddleware(); $this->assertSame(3, CallableTest::$CalledCount); } public function testSlimCallableContainer(): void { /** @var ContainerInterface $container */ $container = $this->containerProphecy->reveal(); $resolver = new CallableResolver($container); $resolver->resolve('Slim\Tests\Mocks\CallableTest:toCall'); $this->assertSame($container, CallableTest::$CalledContainer); CallableTest::$CalledContainer = null; $resolver->resolveRoute('Slim\Tests\Mocks\CallableTest:toCall'); $this->assertSame($container, CallableTest::$CalledContainer); CallableTest::$CalledContainer = null; $resolver->resolveMiddleware('Slim\Tests\Mocks\CallableTest:toCall'); $this->assertSame($container, CallableTest::$CalledContainer); } public function testSlimCallableAsArrayContainer(): void { /** @var ContainerInterface $container */ $container = $this->containerProphecy->reveal(); $resolver = new CallableResolver($container); $resolver->resolve([CallableTest::class, 'toCall']); $this->assertSame($container, CallableTest::$CalledContainer); CallableTest::$CalledContainer = null; $resolver->resolveRoute([CallableTest::class, 'toCall']); $this->assertSame($container, CallableTest::$CalledContainer); CallableTest::$CalledContainer = null; $resolver->resolveMiddleware([CallableTest::class ,'toCall']); $this->assertSame($container, CallableTest::$CalledContainer); } public function testContainer(): void { $this->containerProphecy->has('callable_service')->willReturn(true); $this->containerProphecy->get('callable_service')->willReturn(new CallableTest()); /** @var ContainerInterface $container */ $container = $this->containerProphecy->reveal(); $resolver = new CallableResolver($container); $callable = $resolver->resolve('callable_service:toCall'); $callableRoute = $resolver->resolveRoute('callable_service:toCall'); $callableMiddleware = $resolver->resolveMiddleware('callable_service:toCall'); $callable(); $this->assertSame(1, CallableTest::$CalledCount); $callableRoute(); $this->assertSame(2, CallableTest::$CalledCount); $callableMiddleware(); $this->assertSame(3, CallableTest::$CalledCount); } public function testResolutionToAnInvokableClassInContainer(): void { $this->containerProphecy->has('an_invokable')->willReturn(true); $this->containerProphecy->get('an_invokable')->willReturn(new InvokableTest()); /** @var ContainerInterface $container */ $container = $this->containerProphecy->reveal(); $resolver = new CallableResolver($container); $callable = $resolver->resolve('an_invokable'); $callableRoute = $resolver->resolveRoute('an_invokable'); $callableMiddleware = $resolver->resolveMiddleware('an_invokable'); $callable(); $this->assertSame(1, InvokableTest::$CalledCount); $callableRoute(); $this->assertSame(2, InvokableTest::$CalledCount); $callableMiddleware(); $this->assertSame(3, InvokableTest::$CalledCount); } public function testResolutionToAnInvokableClass(): void { $resolver = new CallableResolver(); // No container injected $callable = $resolver->resolve('Slim\Tests\Mocks\InvokableTest'); $callableRoute = $resolver->resolveRoute('Slim\Tests\Mocks\InvokableTest'); $callableMiddleware = $resolver->resolveMiddleware('Slim\Tests\Mocks\InvokableTest'); $callable(); $this->assertSame(1, InvokableTest::$CalledCount); $callableRoute(); $this->assertSame(2, InvokableTest::$CalledCount); $callableMiddleware(); $this->assertSame(3, InvokableTest::$CalledCount); } public function testResolutionToAPsrRequestHandlerClass(): void { $this->expectException(RuntimeException::class); $this->expectExceptionMessage('Slim\\Tests\\Mocks\\RequestHandlerTest is not resolvable'); $resolver = new CallableResolver(); // No container injected $resolver->resolve(RequestHandlerTest::class); } public function testRouteResolutionToAPsrRequestHandlerClass(): void { $request = $this->createServerRequest('/', 'GET'); $resolver = new CallableResolver(); // No container injected $callableRoute = $resolver->resolveRoute(RequestHandlerTest::class); $callableRoute($request); $this->assertSame(1, RequestHandlerTest::$CalledCount); } public function testMiddlewareResolutionToAPsrRequestHandlerClass(): void { $this->expectException(RuntimeException::class); $this->expectExceptionMessage('Slim\\Tests\\Mocks\\RequestHandlerTest is not resolvable'); $resolver = new CallableResolver(); // No container injected $resolver->resolveMiddleware(RequestHandlerTest::class); } public function testObjPsrRequestHandlerClass(): void { $this->expectException(RuntimeException::class); $this->expectExceptionMessage('{} is not resolvable'); $obj = new RequestHandlerTest(); $resolver = new CallableResolver(); // No container injected $resolver->resolve($obj); } public function testRouteObjPsrRequestHandlerClass(): void { $obj = new RequestHandlerTest(); $request = $this->createServerRequest('/', 'GET'); $resolver = new CallableResolver(); // No container injected $callableRoute = $resolver->resolveRoute($obj); $callableRoute($request); $this->assertSame(1, RequestHandlerTest::$CalledCount); } public function testMiddlewareObjPsrRequestHandlerClass(): void { $this->expectException(RuntimeException::class); $this->expectExceptionMessage('{} is not resolvable'); $obj = new RequestHandlerTest(); $resolver = new CallableResolver(); // No container injected $resolver->resolveMiddleware($obj); } public function testObjPsrRequestHandlerClassInContainer(): void { $this->expectException(RuntimeException::class); $this->expectExceptionMessage('a_requesthandler is not resolvable'); $this->containerProphecy->has('a_requesthandler')->willReturn(true); $this->containerProphecy->get('a_requesthandler')->willReturn(new RequestHandlerTest()); /** @var ContainerInterface $container */ $container = $this->containerProphecy->reveal(); $resolver = new CallableResolver($container); $resolver->resolve('a_requesthandler'); } public function testRouteObjPsrRequestHandlerClassInContainer(): void { $this->containerProphecy->has('a_requesthandler')->willReturn(true); $this->containerProphecy->get('a_requesthandler')->willReturn(new RequestHandlerTest()); /** @var ContainerInterface $container */ $container = $this->containerProphecy->reveal(); $request = $this->createServerRequest('/', 'GET'); $resolver = new CallableResolver($container); $callable = $resolver->resolveRoute('a_requesthandler'); $callable($request); $this->assertSame(1, RequestHandlerTest::$CalledCount); } public function testMiddlewareObjPsrRequestHandlerClassInContainer(): void { $this->expectException(RuntimeException::class); $this->expectExceptionMessage('a_requesthandler is not resolvable'); $this->containerProphecy->has('a_requesthandler')->willReturn(true); $this->containerProphecy->get('a_requesthandler')->willReturn(new RequestHandlerTest()); /** @var ContainerInterface $container */ $container = $this->containerProphecy->reveal(); $resolver = new CallableResolver($container); $resolver->resolveMiddleware('a_requesthandler'); } public function testResolutionToAPsrRequestHandlerClassWithCustomMethod(): void { $resolver = new CallableResolver(); // No container injected $callable = $resolver->resolve(RequestHandlerTest::class . ':custom'); $callableRoute = $resolver->resolveRoute(RequestHandlerTest::class . ':custom'); $callableMiddleware = $resolver->resolveMiddleware(RequestHandlerTest::class . ':custom'); $this->assertIsArray($callable); $this->assertInstanceOf(RequestHandlerTest::class, $callable[0]); $this->assertSame('custom', $callable[1]); $this->assertIsArray($callableRoute); $this->assertInstanceOf(RequestHandlerTest::class, $callableRoute[0]); $this->assertSame('custom', $callableRoute[1]); $this->assertIsArray($callableMiddleware); $this->assertInstanceOf(RequestHandlerTest::class, $callableMiddleware[0]); $this->assertSame('custom', $callableMiddleware[1]); } public function testObjMiddlewareClass(): void { $this->expectException(RuntimeException::class); $this->expectExceptionMessage('{} is not resolvable'); $obj = new MiddlewareTest(); $resolver = new CallableResolver(); // No container injected $resolver->resolve($obj); } public function testRouteObjMiddlewareClass(): void { $this->expectException(RuntimeException::class); $this->expectExceptionMessage('{} is not resolvable'); $obj = new MiddlewareTest(); $resolver = new CallableResolver(); // No container injected $resolver->resolveRoute($obj); } public function testMiddlewareObjMiddlewareClass(): void { $obj = new MiddlewareTest(); $request = $this->createServerRequest('/', 'GET'); $resolver = new CallableResolver(); // No container injected $callableRouteMiddleware = $resolver->resolveMiddleware($obj); $callableRouteMiddleware($request, $this->createMock(RequestHandlerInterface::class)); $this->assertSame(1, MiddlewareTest::$CalledCount); } public function testNotObjectInContainerThrowException(): void { $this->expectException(RuntimeException::class); $this->expectExceptionMessage('callable_service container entry is not an object'); $this->containerProphecy->has('callable_service')->willReturn(true); $this->containerProphecy->get('callable_service')->willReturn('NOT AN OBJECT'); /** @var ContainerInterface $container */ $container = $this->containerProphecy->reveal(); $resolver = new CallableResolver($container); $resolver->resolve('callable_service'); } public function testMethodNotFoundThrowException(): void { $this->expectException(RuntimeException::class); $this->expectExceptionMessage('callable_service:notFound is not resolvable'); $this->containerProphecy->has('callable_service')->willReturn(true); $this->containerProphecy->get('callable_service')->willReturn(new CallableTest()); /** @var ContainerInterface $container */ $container = $this->containerProphecy->reveal(); $resolver = new CallableResolver($container); $resolver->resolve('callable_service:notFound'); } public function testRouteMethodNotFoundThrowException(): void { $this->expectException(RuntimeException::class); $this->expectExceptionMessage('callable_service:notFound is not resolvable'); $this->containerProphecy->has('callable_service')->willReturn(true); $this->containerProphecy->get('callable_service')->willReturn(new CallableTest()); /** @var ContainerInterface $container */ $container = $this->containerProphecy->reveal(); $resolver = new CallableResolver($container); $resolver->resolveRoute('callable_service:notFound'); } public function testMiddlewareMethodNotFoundThrowException(): void { $this->expectException(RuntimeException::class); $this->expectExceptionMessage('callable_service:notFound is not resolvable'); $this->containerProphecy->has('callable_service')->willReturn(true); $this->containerProphecy->get('callable_service')->willReturn(new CallableTest()); /** @var ContainerInterface $container */ $container = $this->containerProphecy->reveal(); $resolver = new CallableResolver($container); $resolver->resolveMiddleware('callable_service:notFound'); } public function testFunctionNotFoundThrowException(): void { $this->expectException(RuntimeException::class); $this->expectExceptionMessage('Callable notFound does not exist'); /** @var ContainerInterface $container */ $container = $this->containerProphecy->reveal(); $resolver = new CallableResolver($container); $resolver->resolve('notFound'); } public function testRouteFunctionNotFoundThrowException(): void { $this->expectException(RuntimeException::class); $this->expectExceptionMessage('Callable notFound does not exist'); /** @var ContainerInterface $container */ $container = $this->containerProphecy->reveal(); $resolver = new CallableResolver($container); $resolver->resolveRoute('notFound'); } public function testMiddlewareFunctionNotFoundThrowException(): void { $this->expectException(RuntimeException::class); $this->expectExceptionMessage('Callable notFound does not exist'); /** @var ContainerInterface $container */ $container = $this->containerProphecy->reveal(); $resolver = new CallableResolver($container); $resolver->resolveMiddleware('notFound'); } public function testClassNotFoundThrowException(): void { $this->expectException(RuntimeException::class); $this->expectExceptionMessage('Callable Unknown::notFound() does not exist'); /** @var ContainerInterface $container */ $container = $this->containerProphecy->reveal(); $resolver = new CallableResolver($container); $resolver->resolve('Unknown:notFound'); } public function testRouteClassNotFoundThrowException(): void { $this->expectException(RuntimeException::class); $this->expectExceptionMessage('Callable Unknown::notFound() does not exist'); /** @var ContainerInterface $container */ $container = $this->containerProphecy->reveal(); $resolver = new CallableResolver($container); $resolver->resolveRoute('Unknown:notFound'); } public function testMiddlewareClassNotFoundThrowException(): void { $this->expectException(RuntimeException::class); $this->expectExceptionMessage('Callable Unknown::notFound() does not exist'); /** @var ContainerInterface $container */ $container = $this->containerProphecy->reveal(); $resolver = new CallableResolver($container); $resolver->resolveMiddleware('Unknown:notFound'); } public function testCallableClassNotFoundThrowException(): void { $this->expectException(RuntimeException::class); $this->expectExceptionMessage('Callable Unknown::notFound() does not exist'); /** @var ContainerInterface $container */ $container = $this->containerProphecy->reveal(); $resolver = new CallableResolver($container); $resolver->resolve(['Unknown', 'notFound']); } public function testRouteCallableClassNotFoundThrowException(): void { $this->expectException(RuntimeException::class); $this->expectExceptionMessage('Callable Unknown::notFound() does not exist'); /** @var ContainerInterface $container */ $container = $this->containerProphecy->reveal(); $resolver = new CallableResolver($container); $resolver->resolveRoute(['Unknown', 'notFound']); } public function testMiddlewareCallableClassNotFoundThrowException(): void { $this->expectException(RuntimeException::class); $this->expectExceptionMessage('Callable Unknown::notFound() does not exist'); /** @var ContainerInterface $container */ $container = $this->containerProphecy->reveal(); $resolver = new CallableResolver($container); $resolver->resolveMiddleware(['Unknown', 'notFound']); } } ================================================ FILE: tests/Error/AbstractErrorRendererTest.php ================================================ __invoke($exception, true); $this->assertMatchesRegularExpression( '/.*The application could not run because of the following error:.*/', $output ); $this->assertStringContainsString('Oops..', $output); } public function testHTMLErrorRendererNoErrorDetails() { $exception = new RuntimeException('Oops..'); $renderer = new HtmlErrorRenderer(); $output = $renderer->__invoke($exception, false); $this->assertMatchesRegularExpression( '/.*A website error has occurred. Sorry for the temporary inconvenience.*/', $output ); $this->assertStringNotContainsString('Oops..', $output); } public function testHTMLErrorRendererRenderFragmentMethod() { $exception = new Exception('Oops..', 500); $renderer = new HtmlErrorRenderer(); $reflectionRenderer = new ReflectionClass(HtmlErrorRenderer::class); $method = $reflectionRenderer->getMethod('renderExceptionFragment'); $this->setAccessible($method); $output = $method->invoke($renderer, $exception); $this->assertMatchesRegularExpression('/.*Type:*/', $output); $this->assertMatchesRegularExpression('/.*Code:*/', $output); $this->assertMatchesRegularExpression('/.*Message*/', $output); $this->assertMatchesRegularExpression('/.*File*/', $output); $this->assertMatchesRegularExpression('/.*Line*/', $output); } public function testHTMLErrorRendererRenderHttpException() { $exceptionTitle = 'title'; $exceptionDescription = 'description'; $httpExceptionProphecy = $this->prophesize(HttpException::class); $httpExceptionProphecy ->getTitle() ->willReturn($exceptionTitle) ->shouldBeCalledOnce(); $httpExceptionProphecy ->getDescription() ->willReturn($exceptionDescription) ->shouldBeCalledOnce(); $renderer = new HtmlErrorRenderer(); $output = $renderer->__invoke($httpExceptionProphecy->reveal(), false); $this->assertStringContainsString($exceptionTitle, $output, 'Should contain http exception title'); $this->assertStringContainsString($exceptionDescription, $output, 'Should contain http exception description'); } public function testJSONErrorRendererDisplaysErrorDetails() { $exception = new Exception('Oops..'); $renderer = new JsonErrorRenderer(); $reflectionRenderer = new ReflectionClass(JsonErrorRenderer::class); $method = $reflectionRenderer->getMethod('formatExceptionFragment'); $this->setAccessible($method); $fragment = $method->invoke($renderer, $exception); $output = json_encode(json_decode($renderer->__invoke($exception, true))); $expectedString = json_encode(['message' => 'Slim Application Error', 'exception' => [$fragment]]); $this->assertSame($output, $expectedString); } public function testJSONErrorRendererDoesNotDisplayErrorDetails() { $exception = new Exception('Oops..'); $renderer = new JsonErrorRenderer(); $output = json_encode(json_decode($renderer->__invoke($exception, false))); $this->assertSame($output, json_encode(['message' => 'Slim Application Error'])); } public function testJSONErrorRendererDisplaysPreviousError() { $previousException = new Exception('Oh no!'); $exception = new Exception('Oops..', 0, $previousException); $renderer = new JsonErrorRenderer(); $reflectionRenderer = new ReflectionClass(JsonErrorRenderer::class); $method = $reflectionRenderer->getMethod('formatExceptionFragment'); $this->setAccessible($method); $output = json_encode(json_decode($renderer->__invoke($exception, true))); $fragments = [ $method->invoke($renderer, $exception), $method->invoke($renderer, $previousException), ]; $expectedString = json_encode(['message' => 'Slim Application Error', 'exception' => $fragments]); $this->assertSame($output, $expectedString); } public function testJSONErrorRendererRenderHttpException() { $exceptionTitle = 'title'; $httpExceptionProphecy = $this->prophesize(HttpException::class); $httpExceptionProphecy ->getTitle() ->willReturn($exceptionTitle) ->shouldBeCalledOnce(); $renderer = new JsonErrorRenderer(); $output = json_encode(json_decode($renderer->__invoke($httpExceptionProphecy->reveal(), false))); $this->assertSame( $output, json_encode(['message' => $exceptionTitle]), 'Should contain http exception title' ); } public function testXMLErrorRendererDisplaysErrorDetails() { $previousException = new RuntimeException('Oops..'); $exception = new Exception('Ooops...', 0, $previousException); $renderer = new XmlErrorRenderer(); /** @var stdClass $output */ $output = simplexml_load_string($renderer->__invoke($exception, true)); $this->assertSame((string) $output->message[0], 'Slim Application Error'); $this->assertSame((string) $output->exception[0]->type, 'Exception'); $this->assertSame((string) $output->exception[0]->message, 'Ooops...'); $this->assertSame((string) $output->exception[1]->type, 'RuntimeException'); $this->assertSame((string) $output->exception[1]->message, 'Oops..'); } public function testXMLErrorRendererRenderHttpException() { $exceptionTitle = 'title'; $httpExceptionProphecy = $this->prophesize(HttpException::class); $httpExceptionProphecy ->getTitle() ->willReturn($exceptionTitle) ->shouldBeCalledOnce(); $renderer = new XmlErrorRenderer(); /** @var stdClass $output */ $output = simplexml_load_string($renderer->__invoke($httpExceptionProphecy->reveal(), true)); $this->assertSame((string) $output->message[0], $exceptionTitle, 'Should contain http exception title'); } public function testPlainTextErrorRendererFormatFragmentMethod() { $message = 'Oops..
'; $exception = new Exception($message, 500); $renderer = new PlainTextErrorRenderer(); $reflectionRenderer = new ReflectionClass(PlainTextErrorRenderer::class); $method = $reflectionRenderer->getMethod('formatExceptionFragment'); $this->setAccessible($method); $output = $method->invoke($renderer, $exception); $this->assertIsString($output); $this->assertMatchesRegularExpression('/.*Type:*/', $output); $this->assertMatchesRegularExpression('/.*Code:*/', $output); $this->assertMatchesRegularExpression('/.*Message*/', $output); $this->assertMatchesRegularExpression('/.*File*/', $output); $this->assertMatchesRegularExpression('/.*Line*/', $output); // ensure the renderer doesn't reformat the message $this->assertMatchesRegularExpression("/.*$message/", $output); } public function testPlainTextErrorRendererDisplaysErrorDetails() { $previousException = new RuntimeException('Oops..'); $exception = new Exception('Ooops...', 0, $previousException); $renderer = new PlainTextErrorRenderer(); $output = $renderer->__invoke($exception, true); $this->assertMatchesRegularExpression('/Ooops.../', $output); } public function testPlainTextErrorRendererNotDisplaysErrorDetails() { $previousException = new RuntimeException('Oops..'); $exception = new Exception('Ooops...', 0, $previousException); $renderer = new PlainTextErrorRenderer(); $output = $renderer->__invoke($exception, false); $this->assertSame("Slim Application Error\n", $output, 'Should show only one string'); } public function testPlainTextErrorRendererRenderHttpException() { $exceptionTitle = 'title'; $httpExceptionProphecy = $this->prophesize(HttpException::class); $httpExceptionProphecy ->getTitle() ->willReturn($exceptionTitle) ->shouldBeCalledOnce(); $renderer = new PlainTextErrorRenderer(); $output = $renderer->__invoke($httpExceptionProphecy->reveal(), true); $this->assertStringContainsString($exceptionTitle, $output, 'Should contain http exception title'); } } ================================================ FILE: tests/Exception/HttpExceptionTest.php ================================================ createServerRequest('/'); $exception = new HttpNotFoundException($request); $this->assertInstanceOf(ServerRequestInterface::class, $exception->getRequest()); } public function testHttpExceptionAttributeGettersSetters() { $request = $this->createServerRequest('/'); $exception = new HttpNotFoundException($request); $exception->setTitle('Title'); $exception->setDescription('Description'); $this->assertSame('Title', $exception->getTitle()); $this->assertSame('Description', $exception->getDescription()); } public function testHttpNotAllowedExceptionGetAllowedMethods() { $request = $this->createServerRequest('/'); $exception = new HttpMethodNotAllowedException($request); $exception->setAllowedMethods(['GET']); $this->assertSame(['GET'], $exception->getAllowedMethods()); $this->assertSame('Method not allowed. Must be one of: GET', $exception->getMessage()); $exception = new HttpMethodNotAllowedException($request); $this->assertSame([], $exception->getAllowedMethods()); $this->assertSame('Method not allowed.', $exception->getMessage()); } } ================================================ FILE: tests/Exception/HttpUnauthorizedExceptionTest.php ================================================ createServerRequest('/'); $exception = new HttpUnauthorizedException($request); $this->assertInstanceOf(HttpUnauthorizedException::class, $exception); } public function testHttpUnauthorizedExceptionWithMessage() { $request = $this->createServerRequest('/'); $exception = new HttpUnauthorizedException($request, 'Hello World'); $this->assertSame('Hello World', $exception->getMessage()); } } ================================================ FILE: tests/Factory/AppFactoryTest.php ================================================ setStaticPropertyValue('responseFactoryClass', DecoratedResponseFactory::class); } public static function provideImplementations() { return [ [SlimPsr17Factory::class, SlimResponseFactory::class], [HttpSoftPsr17Factory::class, HttpSoftResponseFactory::class], [NyholmPsr17Factory::class, Psr17Factory::class], [GuzzlePsr17Factory::class, HttpFactory::class], [LaminasDiactorosPsr17Factory::class, LaminasDiactorosResponseFactory::class], ]; } /** * @dataProvider provideImplementations * @param string $psr17factory * @param string $expectedResponseFactoryClass */ #[\PHPUnit\Framework\Attributes\DataProvider('provideImplementations')] public function testCreateAppWithAllImplementations(string $psr17factory, string $expectedResponseFactoryClass) { Psr17FactoryProvider::setFactories([$psr17factory]); AppFactory::setSlimHttpDecoratorsAutomaticDetection(false); $app = AppFactory::create(); $routeCollector = $app->getRouteCollector(); $responseFactoryProperty = new ReflectionProperty(RouteCollector::class, 'responseFactory'); $this->setAccessible($responseFactoryProperty); $responseFactory = $responseFactoryProperty->getValue($routeCollector); $this->assertInstanceOf($expectedResponseFactoryClass, $responseFactory); } public function testDetermineResponseFactoryReturnsDecoratedFactory() { Psr17FactoryProvider::setFactories([SlimPsr17Factory::class]); AppFactory::setSlimHttpDecoratorsAutomaticDetection(true); $app = AppFactory::create(); $this->assertInstanceOf(DecoratedResponseFactory::class, $app->getResponseFactory()); } public function testDetermineResponseFactoryThrowsRuntimeExceptionIfDecoratedNotInstanceOfResponseInterface() { $this->expectException(RuntimeException::class); $this->expectExceptionMessage( 'Slim\\Factory\\Psr17\\SlimHttpPsr17Factory could not instantiate a decorated response factory.' ); $reflectionClass = new ReflectionClass(SlimHttpPsr17Factory::class); $reflectionClass->setStaticPropertyValue('responseFactoryClass', SlimHttpPsr17Factory::class); Psr17FactoryProvider::setFactories([SlimPsr17Factory::class]); AppFactory::setSlimHttpDecoratorsAutomaticDetection(true); AppFactory::create(); } /** * @runInSeparateProcess - Psr17FactoryProvider::setFactories breaks other tests */ public function testDetermineResponseFactoryThrowsRuntimeException() { $this->expectException(RuntimeException::class); Psr17FactoryProvider::setFactories([]); AppFactory::create(); } public function testSetPsr17FactoryProvider() { $psr17FactoryProvider = new Psr17FactoryProvider(); $psr17FactoryProvider::setFactories([SlimPsr17Factory::class]); AppFactory::setPsr17FactoryProvider($psr17FactoryProvider); AppFactory::setSlimHttpDecoratorsAutomaticDetection(false); $this->assertInstanceOf(SlimResponseFactory::class, AppFactory::determineResponseFactory()); } /** * @runInSeparateProcess - Psr17FactoryProvider::setFactories breaks other tests */ #[\PHPUnit\Framework\Attributes\RunInSeparateProcess] public function testResponseFactoryIsStillReturnedIfStreamFactoryIsNotAvailable() { Psr17FactoryProvider::setFactories([MockPsr17FactoryWithoutStreamFactory::class]); AppFactory::setSlimHttpDecoratorsAutomaticDetection(true); $app = AppFactory::create(); $this->assertInstanceOf(SlimResponseFactory::class, $app->getResponseFactory()); } /** * @runInSeparateProcess - AppFactory::setResponseFactory breaks other tests */ #[\PHPUnit\Framework\Attributes\RunInSeparateProcess] public function testAppIsCreatedWithInstancesFromSetters() { $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $containerProphecy = $this->prophesize(ContainerInterface::class); $callableResolverProphecy = $this->prophesize(CallableResolverInterface::class); $routeCollectorProphecy = $this->prophesize(RouteCollectorInterface::class); $routeParserProphecy = $this->prophesize(RouteParserInterface::class); $routeResolverProphecy = $this->prophesize(RouteResolverInterface::class); $middlewareDispatcherProphecy = $this->prophesize(MiddlewareDispatcherInterface::class); $routeCollectorProphecy->getRouteParser()->willReturn($routeParserProphecy); AppFactory::setSlimHttpDecoratorsAutomaticDetection(false); AppFactory::setResponseFactory($responseFactoryProphecy->reveal()); AppFactory::setContainer($containerProphecy->reveal()); AppFactory::setCallableResolver($callableResolverProphecy->reveal()); AppFactory::setRouteCollector($routeCollectorProphecy->reveal()); AppFactory::setRouteResolver($routeResolverProphecy->reveal()); AppFactory::setMiddlewareDispatcher($middlewareDispatcherProphecy->reveal()); $app = AppFactory::create(); $this->assertSame( $responseFactoryProphecy->reveal(), $app->getResponseFactory() ); $this->assertSame( $containerProphecy->reveal(), $app->getContainer() ); $this->assertSame( $callableResolverProphecy->reveal(), $app->getCallableResolver() ); $this->assertSame( $routeCollectorProphecy->reveal(), $app->getRouteCollector() ); $this->assertSame( $routeResolverProphecy->reveal(), $app->getRouteResolver() ); $this->assertSame( $middlewareDispatcherProphecy->reveal(), $app->getMiddlewareDispatcher() ); } /** * @runInSeparateProcess - AppFactory::create saves $responseFactory into static::$responseFactory, * this breaks other tests */ #[\PHPUnit\Framework\Attributes\RunInSeparateProcess] public function testAppIsCreatedWithInjectedInstancesFromFunctionArguments() { $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $containerProphecy = $this->prophesize(ContainerInterface::class); $callableResolverProphecy = $this->prophesize(CallableResolverInterface::class); $routeCollectorProphecy = $this->prophesize(RouteCollectorInterface::class); $routeParserProphecy = $this->prophesize(RouteParserInterface::class); $routeResolverProphecy = $this->prophesize(RouteResolverInterface::class); $routeCollectorProphecy->getRouteParser()->willReturn($routeParserProphecy->reveal()); AppFactory::setSlimHttpDecoratorsAutomaticDetection(false); $app = AppFactory::create( $responseFactoryProphecy->reveal(), $containerProphecy->reveal(), $callableResolverProphecy->reveal(), $routeCollectorProphecy->reveal(), $routeResolverProphecy->reveal() ); $this->assertSame( $responseFactoryProphecy->reveal(), $app->getResponseFactory() ); $this->assertSame( $containerProphecy->reveal(), $app->getContainer() ); $this->assertSame( $callableResolverProphecy->reveal(), $app->getCallableResolver() ); $this->assertSame( $routeCollectorProphecy->reveal(), $app->getRouteCollector() ); $this->assertSame( $routeResolverProphecy->reveal(), $app->getRouteResolver() ); } /** * @runInSeparateProcess - AppFactory::setResponseFactory breaks other tests */ #[\PHPUnit\Framework\Attributes\RunInSeparateProcess] public function testResponseAndStreamFactoryIsBeingInjectedInDecoratedResponseFactory() { $responseProphecy = $this->prophesize(ResponseInterface::class); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy ->createResponse(200, '') ->willReturn($responseProphecy->reveal()) ->shouldBeCalledOnce(); $streamFactoryProphecy = $this->prophesize(StreamFactoryInterface::class); AppFactory::setResponseFactory($responseFactoryProphecy->reveal()); AppFactory::setStreamFactory($streamFactoryProphecy->reveal()); AppFactory::setSlimHttpDecoratorsAutomaticDetection(true); $app = AppFactory::create(); $responseFactory = $app->getResponseFactory(); $response = $responseFactory->createResponse(); $streamFactoryProperty = new ReflectionProperty(DecoratedResponse::class, 'streamFactory'); $this->setAccessible($streamFactoryProperty); $this->assertSame($streamFactoryProphecy->reveal(), $streamFactoryProperty->getValue($response)); } public function testCreateAppWithContainerUsesContainerDependenciesWhenPresent() { $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $callableResolverProphecy = $this->prophesize(CallableResolverInterface::class); $routeResolverProphecy = $this->prophesize(RouteResolverInterface::class); $routeParserProphecy = $this->prophesize(RouteParserInterface::class); $routeCollectorProphecy = $this->prophesize(RouteCollectorInterface::class); $routeCollectorProphecy ->getRouteParser() ->willReturn($routeParserProphecy->reveal()) ->shouldBeCalledOnce(); $middlewareDispatcherProphecy = $this->prophesize(MiddlewareDispatcherInterface::class); $containerProphecy = $this->prophesize(ContainerInterface::class); $containerProphecy ->has(ResponseFactoryInterface::class) ->willReturn(true) ->shouldBeCalledOnce(); $containerProphecy ->get(ResponseFactoryInterface::class) ->willReturn($responseFactoryProphecy->reveal()) ->shouldBeCalledOnce(); $containerProphecy ->has(CallableResolverInterface::class) ->willReturn(true) ->shouldBeCalledOnce(); $containerProphecy ->get(CallableResolverInterface::class) ->willReturn($callableResolverProphecy->reveal()) ->shouldBeCalledOnce(); $containerProphecy ->has(RouteCollectorInterface::class) ->willReturn(true) ->shouldBeCalledOnce(); $containerProphecy ->get(RouteCollectorInterface::class) ->willReturn($routeCollectorProphecy->reveal()) ->shouldBeCalledOnce(); $containerProphecy ->has(RouteResolverInterface::class) ->willReturn(true) ->shouldBeCalledOnce(); $containerProphecy ->get(RouteResolverInterface::class) ->willReturn($routeResolverProphecy->reveal()) ->shouldBeCalledOnce(); $containerProphecy ->has(MiddlewareDispatcherInterface::class) ->willReturn(true) ->shouldBeCalledOnce(); $containerProphecy ->get(MiddlewareDispatcherInterface::class) ->willReturn($middlewareDispatcherProphecy->reveal()) ->shouldBeCalledOnce(); AppFactory::setSlimHttpDecoratorsAutomaticDetection(false); $app = AppFactory::createFromContainer($containerProphecy->reveal()); $this->assertSame($app->getResponseFactory(), $responseFactoryProphecy->reveal()); $this->assertSame($app->getContainer(), $containerProphecy->reveal()); $this->assertSame($app->getCallableResolver(), $callableResolverProphecy->reveal()); $this->assertSame($app->getRouteCollector(), $routeCollectorProphecy->reveal()); $this->assertSame($app->getRouteResolver(), $routeResolverProphecy->reveal()); $this->assertSame($app->getMiddlewareDispatcher(), $middlewareDispatcherProphecy->reveal()); } public function testCreateAppWithEmptyContainer() { $containerProphecy = $this->prophesize(ContainerInterface::class); $containerProphecy ->has(ResponseFactoryInterface::class) ->willReturn(false) ->shouldBeCalledOnce(); $containerProphecy ->has(CallableResolverInterface::class) ->willReturn(false) ->shouldBeCalledOnce(); $containerProphecy ->has(RouteCollectorInterface::class) ->willReturn(false) ->shouldBeCalledOnce(); $containerProphecy ->has(RouteResolverInterface::class) ->willReturn(false) ->shouldBeCalledOnce(); $containerProphecy ->has(MiddlewareDispatcherInterface::class) ->willReturn(false) ->shouldBeCalledOnce(); AppFactory::setSlimHttpDecoratorsAutomaticDetection(false); AppFactory::createFromContainer($containerProphecy->reveal()); } } ================================================ FILE: tests/Factory/Psr17/Psr17FactoryProviderTest.php ================================================ assertSame([], Psr17FactoryProvider::getFactories()); } /** * @runInSeparateProcess - Psr17FactoryProvider::setFactories breaks other tests */ #[\PHPUnit\Framework\Attributes\RunInSeparateProcess] public function testAddFactory() { Psr17FactoryProvider::setFactories(['Factory 1']); Psr17FactoryProvider::addFactory('Factory 2'); $this->assertSame(['Factory 2', 'Factory 1'], Psr17FactoryProvider::getFactories()); } } ================================================ FILE: tests/Factory/Psr17/Psr17FactoryTest.php ================================================ expectException(RuntimeException::class); $this->expectExceptionMessage('Slim\\Tests\\Mocks\\MockPsr17Factory could not instantiate a response factory.'); MockPsr17Factory::getResponseFactory(); } public function testGetStreamFactoryThrowsRuntimeException() { $this->expectException(RuntimeException::class); $this->expectExceptionMessage('Slim\\Tests\\Mocks\\MockPsr17Factory could not instantiate a stream factory.'); MockPsr17Factory::getStreamFactory(); } public function testGetServerRequestCreatorThrowsRuntimeException() { $this->expectException(RuntimeException::class); $this->expectExceptionMessage('Slim\\Tests\\Mocks\\MockPsr17Factory' . ' could not instantiate a server request creator.'); MockPsr17Factory::getServerRequestCreator(); } } ================================================ FILE: tests/Factory/Psr17/SlimHttpServerRequestCreatorTest.php ================================================ prophesize(ServerRequestCreatorInterface::class); $slimHttpServerRequestCreator = new SlimHttpServerRequestCreator($serverRequestCreatorProphecy->reveal()); $serverRequestDecoratorClassProperty = new ReflectionProperty( SlimHttpServerRequestCreator::class, 'serverRequestDecoratorClass' ); $this->setAccessible($serverRequestDecoratorClassProperty); $serverRequestDecoratorClassProperty->setValue($slimHttpServerRequestCreator, ServerRequest::class); } public function testCreateServerRequestFromGlobals() { $serverRequestProphecy = $this->prophesize(ServerRequestInterface::class); $serverRequestCreatorProphecy = $this->prophesize(ServerRequestCreatorInterface::class); $serverRequestCreatorProphecy ->createServerRequestFromGlobals() ->willReturn($serverRequestProphecy->reveal()) ->shouldBeCalledOnce(); $slimHttpServerRequestCreator = new SlimHttpServerRequestCreator($serverRequestCreatorProphecy->reveal()); $this->assertInstanceOf(ServerRequest::class, $slimHttpServerRequestCreator->createServerRequestFromGlobals()); } public function testCreateServerRequestFromGlobalsThrowsRuntimeException() { $this->expectException(RuntimeException::class); $this->expectExceptionMessage('The Slim-Http ServerRequest decorator is not available.'); $serverRequestCreatorProphecy = $this->prophesize(ServerRequestCreatorInterface::class); $slimHttpServerRequestCreator = new SlimHttpServerRequestCreator($serverRequestCreatorProphecy->reveal()); $serverRequestDecoratorClassProperty = new ReflectionProperty( SlimHttpServerRequestCreator::class, 'serverRequestDecoratorClass' ); $this->setAccessible($serverRequestDecoratorClassProperty); $serverRequestDecoratorClassProperty->setValue($slimHttpServerRequestCreator, ''); $slimHttpServerRequestCreator->createServerRequestFromGlobals(); } public function testCreateServerRequestFromGlobalsThrowsRuntimeExceptionIfNotInstanceOfServerRequestInterface() { $this->expectException(RuntimeException::class); $this->expectExceptionMessage( 'Slim\\Factory\\Psr17\\SlimHttpServerRequestCreator could not instantiate a decorated server request.' ); $serverRequestProphecy = $this->prophesize(ServerRequestInterface::class); $serverRequestCreatorProphecy = $this->prophesize(ServerRequestCreatorInterface::class); $serverRequestCreatorProphecy ->createServerRequestFromGlobals() ->willReturn($serverRequestProphecy->reveal()) ->shouldBeCalledOnce(); $slimHttpServerRequestCreator = new SlimHttpServerRequestCreator($serverRequestCreatorProphecy->reveal()); $reflectionClass = new ReflectionClass(SlimHttpServerRequestCreator::class); $reflectionClass->setStaticPropertyValue('serverRequestDecoratorClass', stdClass::class); $slimHttpServerRequestCreator->createServerRequestFromGlobals(); } } ================================================ FILE: tests/Factory/ServerRequestCreatorFactoryTest.php ================================================ createServerRequestFromGlobals(); $this->assertInstanceOf($expectedServerRequestClass, $serverRequest); } public function testDetermineServerRequestCreatorReturnsDecoratedServerRequestCreator() { Psr17FactoryProvider::setFactories([SlimPsr17Factory::class]); ServerRequestCreatorFactory::setSlimHttpDecoratorsAutomaticDetection(true); $serverRequestCreator = ServerRequestCreatorFactory::create(); $this->assertInstanceOf(SlimHttpServerRequestCreator::class, $serverRequestCreator); $this->assertInstanceOf(ServerRequest::class, $serverRequestCreator->createServerRequestFromGlobals()); } /** * @runInSeparateProcess - Psr17FactoryProvider::setFactories breaks other tests */ #[\PHPUnit\Framework\Attributes\RunInSeparateProcess] public function testDetermineServerRequestCreatorThrowsRuntimeException() { $this->expectException(RuntimeException::class); Psr17FactoryProvider::setFactories([]); ServerRequestCreatorFactory::create(); } public function testSetPsr17FactoryProvider() { $psr17FactoryProvider = new Psr17FactoryProvider(); $psr17FactoryProvider::setFactories([SlimPsr17Factory::class]); ServerRequestCreatorFactory::setPsr17FactoryProvider($psr17FactoryProvider); ServerRequestCreatorFactory::setSlimHttpDecoratorsAutomaticDetection(false); $serverRequestCreator = ServerRequestCreatorFactory::create(); $this->assertInstanceOf(SlimServerRequest::class, $serverRequestCreator->createServerRequestFromGlobals()); } /** * @runInSeparateProcess - ServerRequestCreatorFactory::setServerRequestCreator breaks other tests */ #[\PHPUnit\Framework\Attributes\RunInSeparateProcess] public function testSetServerRequestCreatorWithoutDecorators() { ServerRequestCreatorFactory::setSlimHttpDecoratorsAutomaticDetection(false); $serverRequestProphecy = $this->prophesize(ServerRequestInterface::class); $serverRequestCreatorProphecy = $this->prophesize(ServerRequestCreatorInterface::class); $serverRequestCreatorProphecy ->createServerRequestFromGlobals() ->willReturn($serverRequestProphecy->reveal()) ->shouldBeCalledOnce(); ServerRequestCreatorFactory::setServerRequestCreator($serverRequestCreatorProphecy->reveal()); $serverRequestCreator = ServerRequestCreatorFactory::create(); $this->assertSame($serverRequestProphecy->reveal(), $serverRequestCreator->createServerRequestFromGlobals()); } /** * @runInSeparateProcess - ServerRequestCreatorFactory::setServerRequestCreator breaks other tests */ #[\PHPUnit\Framework\Attributes\RunInSeparateProcess] public function testSetServerRequestCreatorWithDecorators() { ServerRequestCreatorFactory::setSlimHttpDecoratorsAutomaticDetection(true); $serverRequestProphecy = $this->prophesize(ServerRequestInterface::class); $serverRequestCreatorProphecy = $this->prophesize(ServerRequestCreatorInterface::class); $serverRequestCreatorProphecy ->createServerRequestFromGlobals() ->willReturn($serverRequestProphecy->reveal()) ->shouldBeCalledOnce(); ServerRequestCreatorFactory::setServerRequestCreator($serverRequestCreatorProphecy->reveal()); $serverRequestCreator = ServerRequestCreatorFactory::create(); $this->assertInstanceOf(ServerRequest::class, $serverRequestCreator->createServerRequestFromGlobals()); } } ================================================ FILE: tests/Handlers/ErrorHandlerTest.php ================================================ createMock(LoggerInterface::class); } public function testDetermineRenderer() { $handler = $this->createMock(ErrorHandler::class); $class = new ReflectionClass(ErrorHandler::class); $callableResolverProperty = $class->getProperty('callableResolver'); $this->setAccessible($callableResolverProperty); $callableResolverProperty->setValue($handler, $this->getCallableResolver()); $reflectionProperty = $class->getProperty('contentType'); $this->setAccessible($reflectionProperty); $reflectionProperty->setValue($handler, 'application/json'); $method = $class->getMethod('determineRenderer'); $this->setAccessible($method); $renderer = $method->invoke($handler); $this->assertIsCallable($renderer); $this->assertInstanceOf(JsonErrorRenderer::class, $renderer[0]); $reflectionProperty->setValue($handler, 'application/xml'); $renderer = $method->invoke($handler); $this->assertIsCallable($renderer); $this->assertInstanceOf(XmlErrorRenderer::class, $renderer[0]); $reflectionProperty->setValue($handler, 'text/plain'); $renderer = $method->invoke($handler); $this->assertIsCallable($renderer); $this->assertInstanceOf(PlainTextErrorRenderer::class, $renderer[0]); // Test the default error renderer $reflectionProperty->setValue($handler, 'text/unknown'); $renderer = $method->invoke($handler); $this->assertIsCallable($renderer); $this->assertInstanceOf(HtmlErrorRenderer::class, $renderer[0]); } public function testDetermineStatusCode() { $request = $this->createServerRequest('/'); $handler = $this->createMock(ErrorHandler::class); $class = new ReflectionClass(ErrorHandler::class); $reflectionProperty = $class->getProperty('responseFactory'); $this->setAccessible($reflectionProperty); $reflectionProperty->setValue($handler, $this->getResponseFactory()); $reflectionProperty = $class->getProperty('exception'); $this->setAccessible($reflectionProperty); $reflectionProperty->setValue($handler, new HttpNotFoundException($request)); $method = $class->getMethod('determineStatusCode'); $this->setAccessible($method); $statusCode = $method->invoke($handler); $this->assertSame($statusCode, 404); $reflectionProperty->setValue($handler, new MockCustomException()); $statusCode = $method->invoke($handler); $this->assertSame($statusCode, 500); } /** * Test if we can force the content type of all error handler responses. */ public function testForceContentType() { $request = $this ->createServerRequest('/not-defined', 'GET') ->withHeader('Accept', 'text/plain,text/xml'); $handler = new ErrorHandler($this->getCallableResolver(), $this->getResponseFactory()); $handler->forceContentType('application/json'); $exception = new HttpNotFoundException($request); /** @var ResponseInterface $response */ $response = $handler->__invoke($request, $exception, false, false, false); $this->assertSame(['application/json'], $response->getHeader('Content-Type')); } public function testHalfValidContentType() { $request = $this ->createServerRequest('/', 'GET') ->withHeader('Content-Type', 'unknown/json+'); $handler = $this->createMock(ErrorHandler::class); $newErrorRenderers = [ 'application/xml' => XmlErrorRenderer::class, 'text/xml' => XmlErrorRenderer::class, 'text/html' => HtmlErrorRenderer::class, ]; $class = new ReflectionClass(ErrorHandler::class); $reflectionProperty = $class->getProperty('responseFactory'); $this->setAccessible($reflectionProperty); $reflectionProperty->setValue($handler, $this->getResponseFactory()); $reflectionProperty = $class->getProperty('errorRenderers'); $this->setAccessible($reflectionProperty); $reflectionProperty->setValue($handler, $newErrorRenderers); $method = $class->getMethod('determineContentType'); $this->setAccessible($method); $contentType = $method->invoke($handler, $request); $this->assertNull($contentType); } public function testDetermineContentTypeTextPlainMultiAcceptHeader() { $request = $this ->createServerRequest('/', 'GET') ->withHeader('Content-Type', 'text/plain') ->withHeader('Accept', 'text/plain,text/xml'); $handler = $this->createMock(ErrorHandler::class); $errorRenderers = [ 'text/plain' => PlainTextErrorRenderer::class, 'text/xml' => XmlErrorRenderer::class, ]; $class = new ReflectionClass(ErrorHandler::class); $reflectionProperty = $class->getProperty('responseFactory'); $this->setAccessible($reflectionProperty); $reflectionProperty->setValue($handler, $this->getResponseFactory()); $reflectionProperty = $class->getProperty('errorRenderers'); $this->setAccessible($reflectionProperty); $reflectionProperty->setValue($handler, $errorRenderers); $method = $class->getMethod('determineContentType'); $this->setAccessible($method); $contentType = $method->invoke($handler, $request); $this->assertSame('text/xml', $contentType); } public function testDetermineContentTypeApplicationJsonOrXml() { $request = $this ->createServerRequest('/', 'GET') ->withHeader('Content-Type', 'text/json') ->withHeader('Accept', 'application/xhtml+xml'); $handler = $this->createMock(ErrorHandler::class); $errorRenderers = [ 'application/xml' => XmlErrorRenderer::class ]; $class = new ReflectionClass(ErrorHandler::class); $reflectionProperty = $class->getProperty('responseFactory'); $this->setAccessible($reflectionProperty); $reflectionProperty->setValue($handler, $this->getResponseFactory()); $reflectionProperty = $class->getProperty('errorRenderers'); $this->setAccessible($reflectionProperty); $reflectionProperty->setValue($handler, $errorRenderers); $method = $class->getMethod('determineContentType'); $this->setAccessible($method); $contentType = $method->invoke($handler, $request); $this->assertSame('application/xml', $contentType); } /** * Ensure that an acceptable media-type is found in the Accept header even * if it's not the first in the list. */ public function testAcceptableMediaTypeIsNotFirstInList() { $request = $this ->createServerRequest('/', 'GET') ->withHeader('Accept', 'text/plain,text/html'); // provide access to the determineContentType() as it's a protected method $class = new ReflectionClass(ErrorHandler::class); $method = $class->getMethod('determineContentType'); $this->setAccessible($method); // use a mock object here as ErrorHandler cannot be directly instantiated $handler = $this->createMock(ErrorHandler::class); // call determineContentType() $return = $method->invoke($handler, $request); $this->assertSame('text/html', $return); } public function testRegisterErrorRenderer() { $handler = new ErrorHandler($this->getCallableResolver(), $this->getResponseFactory()); $handler->registerErrorRenderer('application/slim', PlainTextErrorRenderer::class); $reflectionClass = new ReflectionClass(ErrorHandler::class); $reflectionProperty = $reflectionClass->getProperty('errorRenderers'); $this->setAccessible($reflectionProperty); $errorRenderers = $reflectionProperty->getValue($handler); $this->assertArrayHasKey('application/slim', $errorRenderers); } public function testSetDefaultErrorRenderer() { $handler = new ErrorHandler($this->getCallableResolver(), $this->getResponseFactory()); $handler->setDefaultErrorRenderer('text/plain', PlainTextErrorRenderer::class); $reflectionClass = new ReflectionClass(ErrorHandler::class); $reflectionProperty = $reflectionClass->getProperty('defaultErrorRenderer'); $this->setAccessible($reflectionProperty); $defaultErrorRenderer = $reflectionProperty->getValue($handler); $defaultErrorRendererContentTypeProperty = $reflectionClass->getProperty('defaultErrorRendererContentType'); $this->setAccessible($defaultErrorRendererContentTypeProperty); $defaultErrorRendererContentType = $defaultErrorRendererContentTypeProperty->getValue($handler); $this->assertSame(PlainTextErrorRenderer::class, $defaultErrorRenderer); $this->assertSame('text/plain', $defaultErrorRendererContentType); } public function testOptions() { $request = $this->createServerRequest('/', 'OPTIONS'); $handler = new ErrorHandler($this->getCallableResolver(), $this->getResponseFactory()); $exception = new HttpMethodNotAllowedException($request); $exception->setAllowedMethods(['POST', 'PUT']); /** @var ResponseInterface $res */ $res = $handler->__invoke($request, $exception, true, false, true); $this->assertSame(200, $res->getStatusCode()); $this->assertTrue($res->hasHeader('Allow')); $this->assertSame('POST, PUT', $res->getHeaderLine('Allow')); } public function testWriteToErrorLog() { $request = $this ->createServerRequest('/', 'GET') ->withHeader('Accept', 'application/json'); $logger = $this->getMockLogger(); $handler = new ErrorHandler( $this->getCallableResolver(), $this->getResponseFactory(), $logger ); $logger->expects(self::once()) ->method('error') ->willReturnCallback(static function (string $error) { self::assertStringNotContainsString( 'set "displayErrorDetails" to true in the ErrorHandler constructor', $error ); }); $exception = new HttpNotFoundException($request); $handler->__invoke($request, $exception, true, true, true); } public function testWriteToErrorLogShowTip() { $request = $this ->createServerRequest('/', 'GET') ->withHeader('Accept', 'application/json'); $logger = $this->getMockLogger(); $handler = new ErrorHandler( $this->getCallableResolver(), $this->getResponseFactory(), $logger ); $logger->expects(self::once()) ->method('error') ->willReturnCallback(static function (string $error) { self::assertStringContainsString( 'set "displayErrorDetails" to true in the ErrorHandler constructor', $error ); }); $exception = new HttpNotFoundException($request); $handler->__invoke($request, $exception, false, true, true); } public function testWriteToErrorLogDoesNotShowTipIfErrorLogRendererIsNotPlainText() { $request = $this ->createServerRequest('/', 'GET') ->withHeader('Accept', 'application/json'); $logger = $this->getMockLogger(); $handler = new ErrorHandler( $this->getCallableResolver(), $this->getResponseFactory(), $logger ); $handler->setLogErrorRenderer(HtmlErrorRenderer::class); $logger->expects(self::once()) ->method('error') ->willReturnCallback(static function (string $error) { self::assertStringNotContainsString( 'set "displayErrorDetails" to true in the ErrorHandler constructor', $error ); }); $exception = new HttpNotFoundException($request); $handler->__invoke($request, $exception, false, true, true); } public function testDefaultErrorRenderer() { $request = $this ->createServerRequest('/', 'GET') ->withHeader('Accept', 'application/unknown'); $handler = new ErrorHandler($this->getCallableResolver(), $this->getResponseFactory()); $exception = new RuntimeException(); /** @var ResponseInterface $res */ $res = $handler->__invoke($request, $exception, true, false, true); $this->assertTrue($res->hasHeader('Content-Type')); $this->assertSame('text/html', $res->getHeaderLine('Content-Type')); } public function testLogErrorRenderer() { $renderer = function () { return ''; }; $callableResolverProphecy = $this->prophesize(CallableResolverInterface::class); $callableResolverProphecy ->resolve('logErrorRenderer') ->willReturn($renderer) ->shouldBeCalledOnce(); $loggerProphecy = $this->prophesize(LoggerInterface::class); $loggerProphecy ->error(Argument::type('string')) ->shouldBeCalled(); $handler = new ErrorHandler( $callableResolverProphecy->reveal(), $this->getResponseFactory(), $loggerProphecy->reveal() ); $handler->setLogErrorRenderer('logErrorRenderer'); $displayErrorDetailsProperty = new ReflectionProperty($handler, 'displayErrorDetails'); $this->setAccessible($displayErrorDetailsProperty); $displayErrorDetailsProperty->setValue($handler, true); $exception = new RuntimeException(); $exceptionProperty = new ReflectionProperty($handler, 'exception'); $this->setAccessible($exceptionProperty); $exceptionProperty->setValue($handler, $exception); $writeToErrorLogMethod = new ReflectionMethod($handler, 'writeToErrorLog'); $this->setAccessible($writeToErrorLogMethod); $writeToErrorLogMethod->invoke($handler); } } ================================================ FILE: tests/Handlers/Strategies/RequestResponseNamedArgsTest.php ================================================ request = $this->createMock(ServerRequestInterface::class); $this->response = $this->createMock(ResponseInterface::class); } public function testCallingWithEmptyArguments() { if (PHP_VERSION_ID < self::PHP_8_0_VERSION_ID) { $this->markTestSkipped('Named arguments are not supported in PHP versions prior to 8.0'); } $args = []; $invocationStrategy = new RequestResponseNamedArgs(); $callback = function ($request, $response) { $this->assertSame($this->request, $request); $this->assertSame($this->response, $response); return $response; }; $this->assertSame($this->response, $invocationStrategy($callback, $this->request, $this->response, $args)); } public function testCallingWithKnownArguments() { if (PHP_VERSION_ID < self::PHP_8_0_VERSION_ID) { $this->markTestSkipped('Named arguments are not supported in PHP versions prior to 8.0'); } $args = [ 'name' => 'world', 'greeting' => 'hello', ]; $invocationStrategy = new RequestResponseNamedArgs(); $callback = function ($request, $response, $greeting, $name) use ($args) { $this->assertSame($this->request, $request); $this->assertSame($this->response, $response); $this->assertSame($greeting, $args['greeting']); $this->assertSame($name, $args['name']); return $response; }; $this->assertSame($this->response, $invocationStrategy($callback, $this->request, $this->response, $args)); } public function testCallingWithOptionalArguments() { if (PHP_VERSION_ID < 80000) { $this->markTestSkipped('Named arguments are not supported in PHP versions prior to 8.0'); } $args = [ 'name' => 'world', ]; $invocationStrategy = new RequestResponseNamedArgs(); $callback = function ($request, $response, $greeting = 'Hello', $name = 'Rob') use ($args) { $this->assertSame($this->request, $request); $this->assertSame($this->response, $response); $this->assertSame($greeting, 'Hello'); $this->assertSame($name, $args['name']); return $response; }; $this->assertSame($this->response, $invocationStrategy($callback, $this->request, $this->response, $args)); } public function testCallingWithUnknownAndVariadic() { if (PHP_VERSION_ID < self::PHP_8_0_VERSION_ID) { $this->markTestSkipped('Named arguments are not supported in PHP versions prior to 8.0'); } $args = [ 'name' => 'world', 'greeting' => 'hello', ]; $invocationStrategy = new RequestResponseNamedArgs(); $callback = function ($request, $response, ...$arguments) use ($args) { $this->assertSame($this->request, $request); $this->assertSame($this->response, $response); $this->assertSame($args, $arguments); return $response; }; $this->assertSame($this->response, $invocationStrategy($callback, $this->request, $this->response, $args)); } public function testCallingWithMixedKnownAndUnknownParametersAndVariadic() { if (PHP_VERSION_ID < self::PHP_8_0_VERSION_ID) { $this->markTestSkipped('Named arguments are not supported in PHP versions prior to 8.0'); } $known = [ 'name' => 'world', 'greeting' => 'hello', ]; $unknown = [ 'foo' => 'foo', 'bar' => 'bar', ]; $args = array_merge($known, $unknown); $invocationStrategy = new RequestResponseNamedArgs(); $callback = function ($request, $response, $name, $greeting, ...$arguments) use ($known, $unknown) { $this->assertSame($this->request, $request); $this->assertSame($this->response, $response); $this->assertSame($name, $known['name']); $this->assertSame($greeting, $known['greeting']); $this->assertSame($unknown, $arguments); return $response; }; $this->assertSame($this->response, $invocationStrategy($callback, $this->request, $this->response, $args)); } } ================================================ FILE: tests/Middleware/BodyParsingMiddlewareTest.php ================================================ createResponse(); return new class ($response) implements RequestHandlerInterface { private $response; public $request; public function __construct(ResponseInterface $response) { $this->response = $response; } public function handle(ServerRequestInterface $request): ResponseInterface { $this->request = $request; return $this->response; } }; } /** * @param string $contentType * @param string $body * @return ServerRequestInterface */ protected function createRequestWithBody($contentType, $body) { $request = $this->createServerRequest('/', 'POST'); if (is_string($contentType)) { $request = $request->withHeader('Content-Type', $contentType); } if (is_string($body)) { $request = $request->withBody($this->createStream($body)); } return $request; } public static function parsingProvider() { return [ 'form' => [ 'application/x-www-form-urlencoded;charset=utf8', 'foo=bar', ['foo' => 'bar'], ], 'json' => [ "application/json", '{"foo":"bar"}', ['foo' => 'bar'], ], 'json-with-charset' => [ "application/json\t ; charset=utf8", '{"foo":"bar"}', ['foo' => 'bar'], ], 'json-suffix' => [ 'application/vnd.api+json;charset=utf8', '{"foo":"bar"}', ['foo' => 'bar'], ], 'xml' => [ 'application/xml', 'John', simplexml_load_string('John'), ], 'xml-suffix' => [ 'application/hal+xml;charset=utf8', 'John', simplexml_load_string('John'), ], 'text-xml' => [ 'text/xml', 'John', simplexml_load_string('John'), ], 'invalid-json' => [ 'application/json;charset=utf8', '{"foo"}/bar', null, ], 'valid-json-but-not-an-array' => [ 'application/json;charset=utf8', '"foo bar"', null, ], 'unknown-contenttype' => [ 'text/foo+bar', '"foo bar"', null, ], 'empty-contenttype' => [ '', '"foo bar"', null, ], 'no-contenttype' => [ null, '"foo bar"', null, ], 'invalid-contenttype' => [ 'foo', '"foo bar"', null, ], 'invalid-xml' => [ 'application/xml', 'John', null, ], 'invalid-textxml' => [ 'text/xml', 'John', null, ], ]; } /** * @dataProvider parsingProvider */ #[\PHPUnit\Framework\Attributes\DataProvider('parsingProvider')] public function testParsing($contentType, $body, $expected) { $request = $this->createRequestWithBody($contentType, $body); $middleware = new BodyParsingMiddleware(); $requestHandler = $this->createRequestHandler(); $middleware->process($request, $requestHandler); $this->assertEquals($expected, $requestHandler->request->getParsedBody()); } public function testParsingWithARegisteredParser() { $request = $this->createRequestWithBody('application/vnd.api+json', '{"foo":"bar"}'); $parsers = [ 'application/vnd.api+json' => function ($input) { return ['data' => $input]; }, ]; $middleware = new BodyParsingMiddleware($parsers); $requestHandler = $this->createRequestHandler(); $middleware->process($request, $requestHandler); $this->assertSame(['data' => '{"foo":"bar"}'], $requestHandler->request->getParsedBody()); } public function testParsingFailsWhenAnInvalidTypeIsReturned() { $request = $this->createRequestWithBody('application/json;charset=utf8', '{"foo":"bar"}'); $parsers = [ 'application/json' => function ($input) { return 10; // invalid - should return null, array or object }, ]; $middleware = new BodyParsingMiddleware($parsers); $this->expectException(RuntimeException::class); $middleware->process($request, $this->createRequestHandler()); } public function testSettingAndGettingAParser() { $middleware = new BodyParsingMiddleware(); $parser = function ($input) { return ['data' => $input]; }; $this->assertFalse($middleware->hasBodyParser('text/foo')); $middleware->registerBodyParser('text/foo', $parser); $this->assertTrue($middleware->hasBodyParser('text/foo')); $this->assertSame($parser, $middleware->getBodyParser('text/foo')); } public function testGettingUnknownParser() { $middleware = new BodyParsingMiddleware(); $this->expectException(RuntimeException::class); $middleware->getBodyParser('text/foo'); } } ================================================ FILE: tests/Middleware/ContentLengthMiddlewareTest.php ================================================ createServerRequest('/'); $responseFactory = $this->getResponseFactory(); $mw = function ($request, $handler) use ($responseFactory) { $response = $responseFactory->createResponse(); $response->getBody()->write('Body'); return $response; }; $mw2 = new ContentLengthMiddleware(); $middlewareDispatcher = $this->createMiddlewareDispatcher( $this->createMock(RequestHandlerInterface::class), null ); $middlewareDispatcher->addCallable($mw); $middlewareDispatcher->addMiddleware($mw2); $response = $middlewareDispatcher->handle($request); $this->assertSame('4', $response->getHeaderLine('Content-Length')); } } ================================================ FILE: tests/Middleware/ErrorMiddlewareTest.php ================================================ createMock(LoggerInterface::class); } public function testSetErrorHandler() { $responseFactory = $this->getResponseFactory(); $app = new App($responseFactory); $callableResolver = $app->getCallableResolver(); $logger = $this->getMockLogger(); $routingMiddleware = new RoutingMiddleware( $app->getRouteResolver(), $app->getRouteCollector()->getRouteParser() ); $app->add($routingMiddleware); $exception = HttpNotFoundException::class; $handler = (function () { $response = $this->createResponse(500); $response->getBody()->write('Oops..'); return $response; })->bindTo($this); $errorMiddleware = new ErrorMiddleware( $callableResolver, $this->getResponseFactory(), false, false, false, $logger ); $errorMiddleware->setErrorHandler($exception, $handler); $app->add($errorMiddleware); $request = $this->createServerRequest('/foo/baz/'); $app->run($request); $this->expectOutputString('Oops..'); } public function testSetDefaultErrorHandler() { $responseFactory = $this->getResponseFactory(); $app = new App($responseFactory); $callableResolver = $app->getCallableResolver(); $logger = $this->getMockLogger(); $routingMiddleware = new RoutingMiddleware( $app->getRouteResolver(), $app->getRouteCollector()->getRouteParser() ); $app->add($routingMiddleware); $handler = (function () { $response = $this->createResponse(); $response->getBody()->write('Oops..'); return $response; })->bindTo($this); $errorMiddleware = new ErrorMiddleware( $callableResolver, $this->getResponseFactory(), false, false, false, $logger ); $errorMiddleware->setDefaultErrorHandler($handler); $app->add($errorMiddleware); $request = $this->createServerRequest('/foo/baz/'); $app->run($request); $this->expectOutputString('Oops..'); } public function testSetDefaultErrorHandlerThrowsException() { $this->expectException(RuntimeException::class); $responseFactory = $this->getResponseFactory(); $app = new App($responseFactory); $callableResolver = $app->getCallableResolver(); $logger = $this->getMockLogger(); $errorMiddleware = new ErrorMiddleware( $callableResolver, $this->getResponseFactory(), false, false, false, $logger ); $errorMiddleware->setDefaultErrorHandler('Uncallable'); $errorMiddleware->getDefaultErrorHandler(); } public function testGetErrorHandlerWillReturnDefaultErrorHandlerForUnhandledExceptions() { $responseFactory = $this->getResponseFactory(); $app = new App($responseFactory); $callableResolver = $app->getCallableResolver(); $logger = $this->getMockLogger(); $middleware = new ErrorMiddleware($callableResolver, $this->getResponseFactory(), false, false, false, $logger); $exception = MockCustomException::class; $handler = $middleware->getErrorHandler($exception); $this->assertInstanceOf(ErrorHandler::class, $handler); } public function testSuperclassExceptionHandlerHandlesExceptionWithSubclassExactMatch() { $responseFactory = $this->getResponseFactory(); $app = new App($responseFactory); $callableResolver = $app->getCallableResolver(); $logger = $this->getMockLogger(); $middleware = new ErrorMiddleware($callableResolver, $this->getResponseFactory(), false, false, false, $logger); $app->add(function ($request, $handler) { throw new LogicException('This is a LogicException...'); }); $middleware->setErrorHandler(LogicException::class, (function (ServerRequestInterface $request, $exception) { $response = $this->createResponse(); $response->getBody()->write($exception->getMessage()); return $response; })->bindTo($this), true); // - true; handle subclass but also LogicException explicitly $middleware->setDefaultErrorHandler((function () { $response = $this->createResponse(); $response->getBody()->write('Oops..'); return $response; })->bindTo($this)); $app->add($middleware); $app->get('/foo', function (ServerRequestInterface $request, ResponseInterface $response) { $response->getBody()->write('...'); return $response; }); $request = $this->createServerRequest('/foo'); $app->run($request); $this->expectOutputString('This is a LogicException...'); } public function testSuperclassExceptionHandlerHandlesSubclassException() { $responseFactory = $this->getResponseFactory(); $app = new App($responseFactory); $callableResolver = $app->getCallableResolver(); $logger = $this->getMockLogger(); $middleware = new ErrorMiddleware($callableResolver, $this->getResponseFactory(), false, false, false, $logger); $app->add(function ($request, $handler) { throw new InvalidArgumentException('This is a subclass of LogicException...'); }); $middleware->setErrorHandler(LogicException::class, (function (ServerRequestInterface $request, $exception) { $response = $this->createResponse(); $response->getBody()->write($exception->getMessage()); return $response; })->bindTo($this), true); // - true; handle subclass $middleware->setDefaultErrorHandler((function () { $response = $this->createResponse(); $response->getBody()->write('Oops..'); return $response; })->bindTo($this)); $app->add($middleware); $app->get('/foo', function (ServerRequestInterface $request, ResponseInterface $response) { $response->getBody()->write('...'); return $response; }); $request = $this->createServerRequest('/foo'); $app->run($request); $this->expectOutputString('This is a subclass of LogicException...'); } public function testSuperclassExceptionHandlerDoesNotHandleSubclassException() { $responseFactory = $this->getResponseFactory(); $app = new App($responseFactory); $callableResolver = $app->getCallableResolver(); $logger = $this->getMockLogger(); $middleware = new ErrorMiddleware($callableResolver, $this->getResponseFactory(), false, false, false, $logger); $app->add(function ($request, $handler) { throw new InvalidArgumentException('This is a subclass of LogicException...'); }); $middleware->setErrorHandler(LogicException::class, (function (ServerRequestInterface $request, $exception) { $response = $this->createResponse(); $response->getBody()->write($exception->getMessage()); return $response; })->bindTo($this), false); // - false; don't handle subclass $middleware->setDefaultErrorHandler((function () { $response = $this->createResponse(); $response->getBody()->write('Oops..'); return $response; })->bindTo($this)); $app->add($middleware); $app->get('/foo', function (ServerRequestInterface $request, ResponseInterface $response) { $response->getBody()->write('...'); return $response; }); $request = $this->createServerRequest('/foo'); $app->run($request); $this->expectOutputString('Oops..'); } public function testHandleMultipleExceptionsAddedAsArray() { $responseFactory = $this->getResponseFactory(); $app = new App($responseFactory); $callableResolver = $app->getCallableResolver(); $logger = $this->getMockLogger(); $middleware = new ErrorMiddleware($callableResolver, $this->getResponseFactory(), false, false, false, $logger); $app->add(function ($request, $handler) { throw new InvalidArgumentException('This is an invalid argument exception...'); }); $handler = (function (ServerRequestInterface $request, $exception) { $response = $this->createResponse(); $response->getBody()->write($exception->getMessage()); return $response; }); $middleware->setErrorHandler([LogicException::class, InvalidArgumentException::class], $handler->bindTo($this)); $middleware->setDefaultErrorHandler((function () { $response = $this->createResponse(); $response->getBody()->write('Oops..'); return $response; })->bindTo($this)); $app->add($middleware); $app->get('/foo', function (ServerRequestInterface $request, ResponseInterface $response) { $response->getBody()->write('...'); return $response; }); $request = $this->createServerRequest('/foo'); $app->run($request); $this->expectOutputString('This is an invalid argument exception...'); } public function testErrorHandlerHandlesThrowables() { $responseFactory = $this->getResponseFactory(); $app = new App($responseFactory); $callableResolver = $app->getCallableResolver(); $logger = $this->getMockLogger(); $middleware = new ErrorMiddleware($callableResolver, $this->getResponseFactory(), false, false, false, $logger); $app->add(function ($request, $handler) { throw new Error('Oops..'); }); $middleware->setDefaultErrorHandler((function (ServerRequestInterface $request, $exception) { $response = $this->createResponse(); $response->getBody()->write($exception->getMessage()); return $response; })->bindTo($this)); $app->add($middleware); $app->get('/foo', function (ServerRequestInterface $request, ResponseInterface $response) { $response->getBody()->write('...'); return $response; }); $request = $this->createServerRequest('/foo'); $app->run($request); $this->expectOutputString('Oops..'); } } ================================================ FILE: tests/Middleware/MethodOverrideMiddlewareTest.php ================================================ getResponseFactory(); $middleware = (function (Request $request, RequestHandler $handler) use ($responseFactory) { $this->assertSame('PUT', $request->getMethod()); return $responseFactory->createResponse(); })->bindTo($this); $methodOverrideMiddleware = new MethodOverrideMiddleware(); $request = $this ->createServerRequest('/', 'POST') ->withHeader('X-Http-Method-Override', 'PUT'); $middlewareDispatcher = $this->createMiddlewareDispatcher( $this->createMock(RequestHandler::class), null ); $middlewareDispatcher->addCallable($middleware); $middlewareDispatcher->addMiddleware($methodOverrideMiddleware); $middlewareDispatcher->handle($request); } public function testBodyParam() { $responseFactory = $this->getResponseFactory(); $middleware = (function (Request $request, RequestHandler $handler) use ($responseFactory) { $this->assertSame('PUT', $request->getMethod()); return $responseFactory->createResponse(); })->bindTo($this); $methodOverrideMiddleware = new MethodOverrideMiddleware(); $request = $this ->createServerRequest('/', 'POST') ->withParsedBody(['_METHOD' => 'PUT']); $middlewareDispatcher = $this->createMiddlewareDispatcher( $this->createMock(RequestHandler::class), null ); $middlewareDispatcher->addCallable($middleware); $middlewareDispatcher->addMiddleware($methodOverrideMiddleware); $middlewareDispatcher->handle($request); } public function testHeaderPreferred() { $responseFactory = $this->getResponseFactory(); $middleware = (function (Request $request, RequestHandler $handler) use ($responseFactory) { $this->assertSame('DELETE', $request->getMethod()); return $responseFactory->createResponse(); })->bindTo($this); $methodOverrideMiddleware = new MethodOverrideMiddleware(); $request = $this ->createServerRequest('/', 'POST') ->withHeader('X-Http-Method-Override', 'DELETE') ->withParsedBody((object) ['_METHOD' => 'PUT']); $middlewareDispatcher = $this->createMiddlewareDispatcher( $this->createMock(RequestHandler::class), null ); $middlewareDispatcher->addCallable($middleware); $middlewareDispatcher->addMiddleware($methodOverrideMiddleware); $middlewareDispatcher->handle($request); } public function testNoOverride() { $responseFactory = $this->getResponseFactory(); $middleware = (function (Request $request, RequestHandler $handler) use ($responseFactory) { $this->assertSame('POST', $request->getMethod()); return $responseFactory->createResponse(); })->bindTo($this); $methodOverrideMiddleware = new MethodOverrideMiddleware(); $request = $this->createServerRequest('/', 'POST'); $middlewareDispatcher = $this->createMiddlewareDispatcher( $this->createMock(RequestHandler::class), null ); $middlewareDispatcher->addCallable($middleware); $middlewareDispatcher->addMiddleware($methodOverrideMiddleware); $middlewareDispatcher->handle($request); } public function testNoOverrideRewindEofBodyStream() { $responseFactory = $this->getResponseFactory(); $middleware = (function (Request $request, RequestHandler $handler) use ($responseFactory) { $this->assertSame('POST', $request->getMethod()); return $responseFactory->createResponse(); })->bindTo($this); $methodOverrideMiddleware = new MethodOverrideMiddleware(); $request = $this->createServerRequest('/', 'POST'); // Prophesize the body stream for which `eof()` returns `true` and the // `rewind()` has to be called. $bodyProphecy = $this->prophesize(StreamInterface::class); /** @noinspection PhpUndefinedMethodInspection */ $bodyProphecy->eof() ->willReturn(true) ->shouldBeCalled(); /** @noinspection PhpUndefinedMethodInspection */ $bodyProphecy->rewind() ->shouldBeCalled(); /** @var StreamInterface $body */ $body = $bodyProphecy->reveal(); $request = $request->withBody($body); $middlewareDispatcher = $this->createMiddlewareDispatcher( $this->createMock(RequestHandler::class), null ); $middlewareDispatcher->addCallable($middleware); $middlewareDispatcher->addMiddleware($methodOverrideMiddleware); $middlewareDispatcher->handle($request); } } ================================================ FILE: tests/Middleware/OutputBufferingMiddlewareTest.php ================================================ getStreamFactory()); $reflectionProperty = new ReflectionProperty($middleware, 'style'); $this->setAccessible($reflectionProperty); $value = $reflectionProperty->getValue($middleware); $this->assertSame('append', $value); } public function testStyleCustomValid() { $middleware = new OutputBufferingMiddleware($this->getStreamFactory(), 'prepend'); $reflectionProperty = new ReflectionProperty($middleware, 'style'); $this->setAccessible($reflectionProperty); $value = $reflectionProperty->getValue($middleware); $this->assertSame('prepend', $value); } public function testStyleCustomInvalid() { $this->expectException(InvalidArgumentException::class); new OutputBufferingMiddleware($this->getStreamFactory(), 'foo'); } public function testAppend() { $responseFactory = $this->getResponseFactory(); $middleware = function ($request, $handler) use ($responseFactory) { $response = $responseFactory->createResponse(); $response->getBody()->write('Body'); echo 'Test'; return $response; }; $outputBufferingMiddleware = new OutputBufferingMiddleware($this->getStreamFactory(), 'append'); $request = $this->createServerRequest('/', 'GET'); $middlewareDispatcher = $this->createMiddlewareDispatcher( $this->createMock(RequestHandlerInterface::class), null ); $middlewareDispatcher->addCallable($middleware); $middlewareDispatcher->addMiddleware($outputBufferingMiddleware); $response = $middlewareDispatcher->handle($request); $this->assertSame('BodyTest', (string) $response->getBody()); } public function testPrepend() { $responseFactory = $this->getResponseFactory(); $middleware = function ($request, $handler) use ($responseFactory) { $response = $responseFactory->createResponse(); $response->getBody()->write('Body'); echo 'Test'; return $response; }; $outputBufferingMiddleware = new OutputBufferingMiddleware($this->getStreamFactory(), 'prepend'); $request = $this->createServerRequest('/', 'GET'); $middlewareDispatcher = $this->createMiddlewareDispatcher( $this->createMock(RequestHandlerInterface::class), null ); $middlewareDispatcher->addCallable($middleware); $middlewareDispatcher->addMiddleware($outputBufferingMiddleware); $response = $middlewareDispatcher->handle($request); $this->assertSame('TestBody', (string) $response->getBody()); } public function testOutputBufferIsCleanedWhenThrowableIsCaught() { $this->getResponseFactory(); $middleware = (function ($request, $handler) { echo "Test"; $this->assertSame('Test', ob_get_contents()); throw new Exception('Oops...'); })->bindTo($this); $outputBufferingMiddleware = new OutputBufferingMiddleware($this->getStreamFactory(), 'prepend'); $request = $this->createServerRequest('/', 'GET'); $middlewareDispatcher = $this->createMiddlewareDispatcher( $this->createMock(RequestHandlerInterface::class), null ); $middlewareDispatcher->addCallable($middleware); $middlewareDispatcher->addMiddleware($outputBufferingMiddleware); try { $middlewareDispatcher->handle($request); } catch (Exception $e) { $this->assertSame('', ob_get_contents()); } } } ================================================ FILE: tests/Middleware/RoutingMiddlewareTest.php ================================================ getResponseFactory(); $routeCollector = new RouteCollector($responseFactory, $callableResolver); $routeCollector->map(['GET'], '/hello/{name}', null); return $routeCollector; } public function testRouteIsStoredOnSuccessfulMatch() { $responseFactory = $this->getResponseFactory(); $middleware = (function (ServerRequestInterface $request) use ($responseFactory) { // route is available $route = $request->getAttribute(RouteContext::ROUTE); $this->assertNotNull($route); $this->assertSame('foo', $route->getArgument('name')); // routeParser is available $routeParser = $request->getAttribute(RouteContext::ROUTE_PARSER); $this->assertNotNull($routeParser); $this->assertInstanceOf(RouteParserInterface::class, $routeParser); // routingResults is available $routingResults = $request->getAttribute(RouteContext::ROUTING_RESULTS); $this->assertInstanceOf(RoutingResults::class, $routingResults); return $responseFactory->createResponse(); })->bindTo($this); $routeCollector = $this->getRouteCollector(); $routeParser = new RouteParser($routeCollector); $routeResolver = new RouteResolver($routeCollector); $routingMiddleware = new RoutingMiddleware($routeResolver, $routeParser); $request = $this->createServerRequest('https://example.com:443/hello/foo', 'GET'); $middlewareDispatcher = $this->createMiddlewareDispatcher( $this->createMock(RequestHandlerInterface::class), null ); $middlewareDispatcher->addCallable($middleware); $middlewareDispatcher->addMiddleware($routingMiddleware); $middlewareDispatcher->handle($request); } public function testRouteIsNotStoredOnMethodNotAllowed() { $routeCollector = $this->getRouteCollector(); $routeParser = new RouteParser($routeCollector); $routeResolver = new RouteResolver($routeCollector); $routingMiddleware = new RoutingMiddleware($routeResolver, $routeParser); $request = $this->createServerRequest('https://example.com:443/hello/foo', 'POST'); $requestHandlerProphecy = $this->prophesize(RequestHandlerInterface::class); /** @var RequestHandlerInterface $requestHandler */ $requestHandler = $requestHandlerProphecy->reveal(); $middlewareDispatcher = $this->createMiddlewareDispatcher($requestHandler, null); $middlewareDispatcher->addMiddleware($routingMiddleware); try { $middlewareDispatcher->handle($request); $this->fail('HTTP method should not have been allowed'); } catch (HttpMethodNotAllowedException $exception) { $request = $exception->getRequest(); // route is not available $route = $request->getAttribute(RouteContext::ROUTE); $this->assertNull($route); // routeParser is available $routeParser = $request->getAttribute(RouteContext::ROUTE_PARSER); $this->assertNotNull($routeParser); $this->assertInstanceOf(RouteParserInterface::class, $routeParser); // routingResults is available $routingResults = $request->getAttribute(RouteContext::ROUTING_RESULTS); $this->assertInstanceOf(RoutingResults::class, $routingResults); $this->assertSame(Dispatcher::METHOD_NOT_ALLOWED, $routingResults->getRouteStatus()); } } public function testRouteIsNotStoredOnNotFound() { $routeCollector = $this->getRouteCollector(); $routeParser = new RouteParser($routeCollector); $routeResolver = new RouteResolver($routeCollector); $routingMiddleware = new RoutingMiddleware($routeResolver, $routeParser); $request = $this->createServerRequest('https://example.com:443/goodbye', 'GET'); $requestHandlerProphecy = $this->prophesize(RequestHandlerInterface::class); /** @var RequestHandlerInterface $requestHandler */ $requestHandler = $requestHandlerProphecy->reveal(); $middlewareDispatcher = $this->createMiddlewareDispatcher($requestHandler, null); $middlewareDispatcher->addMiddleware($routingMiddleware); try { $middlewareDispatcher->handle($request); $this->fail('HTTP route should not have been found'); } catch (HttpNotFoundException $exception) { $request = $exception->getRequest(); // route is not available $route = $request->getAttribute(RouteContext::ROUTE); $this->assertNull($route); // routeParser is available $routeParser = $request->getAttribute(RouteContext::ROUTE_PARSER); $this->assertNotNull($routeParser); $this->assertInstanceOf(RouteParserInterface::class, $routeParser); // routingResults is available $routingResults = $request->getAttribute(RouteContext::ROUTING_RESULTS); $this->assertInstanceOf(RoutingResults::class, $routingResults); $this->assertSame(Dispatcher::NOT_FOUND, $routingResults->getRouteStatus()); } } public function testPerformRoutingThrowsExceptionOnInvalidRoutingResultsRouteStatus() { $this->expectException(RuntimeException::class); $this->expectExceptionMessage('An unexpected error occurred while performing routing.'); // Prophesize the `RoutingResults` instance that would return an invalid route // status when the method `getRouteStatus()` gets called. $routingResultsProphecy = $this->prophesize(RoutingResults::class); /** @noinspection PhpUndefinedMethodInspection */ $routingResultsProphecy->getRouteStatus() ->willReturn(-1) ->shouldBeCalledOnce(); /** @var RoutingResults $routingResults */ $routingResults = $routingResultsProphecy->reveal(); // Prophesize the `RouteParserInterface` instance will be created. $routeParserProphecy = $this->prophesize(RouteParser::class); /** @var RouteParserInterface $routeParser */ $routeParser = $routeParserProphecy->reveal(); // Prophesize the `RouteResolverInterface` that would return the `RoutingResults` // defined above, when the method `computeRoutingResults()` gets called. $routeResolverProphecy = $this->prophesize(RouteResolverInterface::class); /** @noinspection PhpUndefinedMethodInspection */ $routeResolverProphecy->computeRoutingResults(Argument::any(), Argument::any()) ->willReturn($routingResults) ->shouldBeCalled(); /** @var RouteResolverInterface $routeResolver */ $routeResolver = $routeResolverProphecy->reveal(); // Create the server request. $request = $this->createServerRequest('https://example.com:443/hello/foo', 'GET'); // Create the routing middleware with the `RouteResolverInterface` defined // above. Perform the routing, which should throw the RuntimeException. $middleware = new RoutingMiddleware($routeResolver, $routeParser); /** @noinspection PhpUnhandledExceptionInspection */ $middleware->performRouting($request); } } ================================================ FILE: tests/MiddlewareDispatcherTest.php ================================================ handle($request); } } public function testAddMiddleware(): void { $responseFactory = $this->getResponseFactory(); $callable = function ($request, $handler) use ($responseFactory) { return $responseFactory->createResponse(); }; $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestHandlerProphecy = $this->prophesize(RequestHandlerInterface::class); $middlewareDispatcher = $this->createMiddlewareDispatcher($requestHandlerProphecy->reveal()); $middlewareDispatcher->add($callable); $response = $middlewareDispatcher->handle($requestProphecy->reveal()); $this->assertInstanceOf(ResponseInterface::class, $response); } public function testNamedFunctionIsResolved(): void { $handler = new MockRequestHandler(); $middlewareDispatcher = $this->createMiddlewareDispatcher($handler, null); $middlewareDispatcher->addDeferred(__NAMESPACE__ . '\testProcessRequest'); $request = $this->createServerRequest('/'); $middlewareDispatcher->handle($request); $this->assertSame(1, $handler->getCalledCount()); } public function testDeferredResolvedCallable(): void { $callable = function (ServerRequestInterface $request, RequestHandlerInterface $handler) { return $handler->handle($request); }; $containerProphecy = $this->prophesize(ContainerInterface::class); $containerProphecy ->has('callable') ->willReturn(true) ->shouldBeCalledOnce(); $containerProphecy ->get('callable') ->willReturn($callable) ->shouldBeCalledOnce(); $handler = new MockRequestHandler(); $middlewareDispatcher = $this->createMiddlewareDispatcher($handler, $containerProphecy->reveal()); $middlewareDispatcher->addDeferred('callable'); $request = $this->createServerRequest('/'); $middlewareDispatcher->handle($request); $this->assertSame(1, $handler->getCalledCount()); } public function testDeferredResolvedCallableWithoutContainerAndNonAdvancedCallableResolver(): void { $callable = function (ServerRequestInterface $request, RequestHandlerInterface $handler) { return $handler->handle($request); }; $callableResolverProphecy = $this->prophesize(CallableResolverInterface::class); $callableResolverProphecy ->resolve('callable') ->willReturn($callable) ->shouldBeCalledOnce(); $handler = new MockRequestHandler(); $middlewareDispatcher = $this->createMiddlewareDispatcher($handler, null, $callableResolverProphecy->reveal()); $middlewareDispatcher->addDeferred('callable'); $request = $this->createServerRequest('/'); $middlewareDispatcher->handle($request); $this->assertSame(1, $handler->getCalledCount()); } public function testDeferredResolvedCallableWithDirectConstructorCall(): void { $callableResolverProphecy = $this->prophesize(CallableResolverInterface::class); $callableResolverProphecy ->resolve(MockMiddlewareWithoutConstructor::class) ->willThrow(new RuntimeException('Callable not available from resolver')) ->shouldBeCalledOnce(); $handler = new MockRequestHandler(); $middlewareDispatcher = $this->createMiddlewareDispatcher($handler, null, $callableResolverProphecy->reveal()); $middlewareDispatcher->addDeferred(MockMiddlewareWithoutConstructor::class); $request = $this->createServerRequest('/'); $middlewareDispatcher->handle($request); $this->assertSame(1, $handler->getCalledCount()); } public static function deferredCallableProvider(): array { return [ [MockMiddlewareSlimCallable::class . ':custom', new MockMiddlewareSlimCallable()], ['MiddlewareInstance', new MockMiddlewareWithoutConstructor()], ['NamedFunction', __NAMESPACE__ . '\testProcessRequest'], ['Callable', function (ServerRequestInterface $request, RequestHandlerInterface $handler) { return $handler->handle($request); }], ['MiddlewareInterfaceNotImplemented', 'MiddlewareInterfaceNotImplemented'] ]; } /** * @dataProvider deferredCallableProvider * * @param string $callable * @param callable|MiddlewareInterface */ #[\PHPUnit\Framework\Attributes\DataProvider('deferredCallableProvider')] public function testDeferredResolvedCallableWithContainerAndNonAdvancedCallableResolverUnableToResolveCallable( $callable, $result ): void { if ($callable === 'MiddlewareInterfaceNotImplemented') { $this->expectException(RuntimeException::class); $this->expectExceptionMessage('Middleware MiddlewareInterfaceNotImplemented is not resolvable'); } $callableResolverProphecy = $this->prophesize(CallableResolverInterface::class); $callableResolverProphecy ->resolve($callable) ->willThrow(RuntimeException::class) ->shouldBeCalledOnce(); $containerProphecy = $this->prophesize(ContainerInterface::class); $containerProphecy ->has(Argument::any()) ->willReturn(true) ->shouldBeCalledOnce(); $containerProphecy ->get(Argument::any()) ->willReturn($result) ->shouldBeCalledOnce(); $handler = new MockRequestHandler(); $middlewareDispatcher = $this->createMiddlewareDispatcher( $handler, $containerProphecy->reveal(), $callableResolverProphecy->reveal() ); $middlewareDispatcher->addDeferred($callable); $request = $this->createServerRequest('/'); $middlewareDispatcher->handle($request); $this->assertSame(1, $handler->getCalledCount()); } public function testDeferredResolvedSlimCallable(): void { $handler = new MockRequestHandler(); $middlewareDispatcher = $this->createMiddlewareDispatcher($handler, null); $middlewareDispatcher->addDeferred(MockMiddlewareSlimCallable::class . ':custom'); $request = $this->createServerRequest('/'); $middlewareDispatcher->handle($request); $this->assertSame(1, $handler->getCalledCount()); } public function testDeferredResolvedClosureIsBoundToContainer(): void { $containerProphecy = $this->prophesize(ContainerInterface::class); $self = $this; $callable = function ( ServerRequestInterface $request, RequestHandlerInterface $handler ) use ($self) { $self->assertInstanceOf(ContainerInterface::class, $this); return $handler->handle($request); }; $containerProphecy->has('callable')->willReturn(true); $containerProphecy->get('callable')->willReturn($callable); $handler = new MockRequestHandler(); $middlewareDispatcher = $this->createMiddlewareDispatcher($handler, $containerProphecy->reveal()); $middlewareDispatcher->addDeferred('callable'); $request = $this->createServerRequest('/'); $middlewareDispatcher->handle($request); } public function testAddCallableBindsClosureToContainer(): void { $containerProphecy = $this->prophesize(ContainerInterface::class); $self = $this; $callable = function ( ServerRequestInterface $request, RequestHandlerInterface $handler ) use ( $self, $containerProphecy ) { $self->assertSame($containerProphecy->reveal(), $this); return $handler->handle($request); }; $handler = new MockRequestHandler(); $middlewareDispatcher = $this->createMiddlewareDispatcher($handler, $containerProphecy->reveal()); $middlewareDispatcher->addCallable($callable); $request = $this->createServerRequest('/'); $middlewareDispatcher->handle($request); } public function testResolvableReturnsInstantiatedObject(): void { MockMiddlewareWithoutConstructor::$CalledCount = 0; $handler = new MockRequestHandler(); $middlewareDispatcher = $this->createMiddlewareDispatcher($handler, null); $middlewareDispatcher->addDeferred(MockMiddlewareWithoutConstructor::class); $request = $this->createServerRequest('/'); $middlewareDispatcher->handle($request); $this->assertSame(1, MockMiddlewareWithoutConstructor::$CalledCount); $this->assertSame(1, $handler->getCalledCount()); } public function testResolveThrowsExceptionWhenResolvableDoesNotImplementMiddlewareInterface(): void { $this->expectException(RuntimeException::class); $this->expectExceptionMessage('MiddlewareInterfaceNotImplemented is not resolvable'); $containerProphecy = $this->prophesize(ContainerInterface::class); $containerProphecy ->has('MiddlewareInterfaceNotImplemented') ->willReturn(true) ->shouldBeCalledOnce(); $containerProphecy ->get('MiddlewareInterfaceNotImplemented') ->willReturn(new stdClass()) ->shouldBeCalledOnce(); $handler = new MockRequestHandler(); $middlewareDispatcher = $this->createMiddlewareDispatcher($handler, $containerProphecy->reveal()); $middlewareDispatcher->addDeferred('MiddlewareInterfaceNotImplemented'); $request = $this->createServerRequest('/'); $middlewareDispatcher->handle($request); } public function testResolveThrowsExceptionWithoutContainerAndUnresolvableClass(): void { $this->expectException(RuntimeException::class); $this->expectExceptionMessageMatches('/(Middleware|Callable) Unresolvable::class does not exist/'); $handler = new MockRequestHandler(); $middlewareDispatcher = $this->createMiddlewareDispatcher($handler, null); $middlewareDispatcher->addDeferred('Unresolvable::class'); $request = $this->createServerRequest('/'); $middlewareDispatcher->handle($request); } public function testResolveThrowsExceptionWithoutContainerNonAdvancedCallableResolverAndUnresolvableClass(): void { $this->expectException(RuntimeException::class); $this->expectExceptionMessageMatches('/(Middleware|Callable) Unresolvable::class does not exist/'); $unresolvable = 'Unresolvable::class'; $callableResolverProphecy = $this->prophesize(CallableResolverInterface::class); $callableResolverProphecy ->resolve($unresolvable) ->willThrow(RuntimeException::class) ->shouldBeCalledOnce(); $handler = new MockRequestHandler(); $middlewareDispatcher = $this->createMiddlewareDispatcher($handler, null, $callableResolverProphecy->reveal()); $middlewareDispatcher->addDeferred($unresolvable); $request = $this->createServerRequest('/'); $middlewareDispatcher->handle($request); } public function testExecutesKernelWithEmptyMiddlewareStack(): void { $requestProphecy = $this->prophesize(ServerRequestInterface::class); $responseProphecy = $this->prophesize(ResponseInterface::class); $kernelProphecy = $this->prophesize(RequestHandlerInterface::class); $kernelProphecy->handle(Argument::type(ServerRequestInterface::class))->willReturn($responseProphecy->reveal()); /** @var RequestHandlerInterface $kernel */ $kernel = $kernelProphecy->reveal(); $dispatcher = $this->createMiddlewareDispatcher($kernel, null); $response = $dispatcher->handle($requestProphecy->reveal()); $kernelProphecy->handle(Argument::type(ServerRequestInterface::class))->shouldHaveBeenCalled(); $this->assertSame($responseProphecy->reveal(), $response); } public function testExecutesMiddlewareLastInFirstOut(): void { $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->getHeader(Argument::type('string'))->willReturn([]); $requestProphecy->withAddedHeader(Argument::type('string'), Argument::type('string'))->will(function ($args) { $headers = $this->reveal()->getHeader($args[0]); $headers[] = $args[1]; $this->getHeader($args[0])->willReturn($headers); $this->hasHeader($args[0])->willReturn(true); return $this; }); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy->getHeader(Argument::type('string'))->willReturn([]); $responseProphecy->withHeader(Argument::type('string'), Argument::type('array'))->will(function ($args) { $this->getHeader($args[0])->willReturn($args[1]); $this->hasHeader($args[0])->willReturn(true); return $this; }); $responseProphecy->withAddedHeader(Argument::type('string'), Argument::type('string'))->will(function ($args) { $headers = $this->reveal()->getHeader($args[0]); $headers[] = $args[1]; $this->getHeader($args[0])->willReturn($headers); $this->hasHeader($args[0])->willReturn(true); return $this; }); $responseProphecy->withStatus(Argument::type('int'))->will(function ($args) { $this->getStatusCode()->willReturn($args[0]); return $this; }); $kernelProphecy = $this->prophesize(RequestHandlerInterface::class); $kernelProphecy->handle(Argument::type(ServerRequestInterface::class)) ->will(function ($args) use ($responseProphecy): ResponseInterface { $request = $args[0]; return $responseProphecy->reveal() ->withStatus(204) ->withHeader('X-SEQ-PRE-REQ-HANDLER', $request->getHeader('X-SEQ-PRE-REQ-HANDLER')); }); $middleware0Prophecy = $this->prophesize(MiddlewareInterface::class); $middleware0Prophecy ->process( Argument::type(ServerRequestInterface::class), Argument::type(RequestHandlerInterface::class) ) ->will(function ($args): ResponseInterface { return $args[1]->handle($args[0]->withAddedHeader('X-SEQ-PRE-REQ-HANDLER', '0')) ->withAddedHeader('X-SEQ-POST-REQ-HANDLER', '0'); }); $middleware1Prophecy = $this->prophesize(MiddlewareInterface::class); $middleware1Prophecy ->process( Argument::type(ServerRequestInterface::class), Argument::type(RequestHandlerInterface::class) ) ->will(function ($args): ResponseInterface { return $args[1]->handle($args[0]->withAddedHeader('X-SEQ-PRE-REQ-HANDLER', '1')) ->withAddedHeader('X-SEQ-POST-REQ-HANDLER', '1'); }); MockSequenceMiddleware::$id = '2'; $middleware3Prophecy = $this->prophesize(MiddlewareInterface::class); $middleware3Prophecy ->process( Argument::type(ServerRequestInterface::class), Argument::type(RequestHandlerInterface::class) ) ->will(function ($args): ResponseInterface { return $args[1]->handle($args[0]->withAddedHeader('X-SEQ-PRE-REQ-HANDLER', '3')) ->withAddedHeader('X-SEQ-POST-REQ-HANDLER', '3'); }); /** @var RequestHandlerInterface $kernel */ $kernel = $kernelProphecy->reveal(); $dispatcher = $this->createMiddlewareDispatcher($kernel, null); $dispatcher->add($middleware0Prophecy->reveal()); $dispatcher->addMiddleware($middleware1Prophecy->reveal()); $dispatcher->addDeferred(MockSequenceMiddleware::class); $dispatcher->add($middleware3Prophecy->reveal()); $response = $dispatcher->handle($requestProphecy->reveal()); $this->assertSame(['3', '2', '1', '0'], $response->getHeader('X-SEQ-PRE-REQ-HANDLER')); $this->assertSame(['0', '1', '2', '3'], $response->getHeader('X-SEQ-POST-REQ-HANDLER')); $this->assertSame(204, $response->getStatusCode()); } public function testDoesNotInstantiateDeferredMiddlewareInCaseOfAnEarlyReturningOuterMiddleware(): void { $kernelProphecy = $this->prophesize(RequestHandlerInterface::class); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $responseProphecy = $this->prophesize(ResponseInterface::class); $middlewareProphecy = $this->prophesize(MiddlewareInterface::class); $middlewareProphecy->process(Argument::cetera())->willReturn($responseProphecy->reveal()); MockSequenceMiddleware::$hasBeenInstantiated = false; /** @var RequestHandlerInterface $kernel */ $kernel = $kernelProphecy->reveal(); $dispatcher = $this->createMiddlewareDispatcher($kernel, null); $dispatcher->addDeferred(MockSequenceMiddleware::class); $dispatcher->addMiddleware($middlewareProphecy->reveal()); $response = $dispatcher->handle($requestProphecy->reveal()); $this->assertFalse(MockSequenceMiddleware::$hasBeenInstantiated); $this->assertSame($responseProphecy->reveal(), $response); $kernelProphecy->handle(Argument::type(ServerRequestInterface::class))->shouldNotHaveBeenCalled(); } public function testThrowsExceptionForDeferredNonMiddlewareInterfaceClasses(): void { $this->expectException(RuntimeException::class); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $kernelProphecy = $this->prophesize(RequestHandlerInterface::class); /** @var RequestHandlerInterface $kernel */ $kernel = $kernelProphecy->reveal(); $dispatcher = $this->createMiddlewareDispatcher($kernel, null); $dispatcher->addDeferred(stdClass::class); $dispatcher->handle($requestProphecy->reveal()); $kernelProphecy->handle(Argument::type(ServerRequestInterface::class))->shouldNotHaveBeenCalled(); } public function testCanBeExecutedMultipleTimes(): void { $requestProphecy = $this->prophesize(ServerRequestInterface::class); $responseProphecy = $this->prophesize(ResponseInterface::class); $kernelProphecy = $this->prophesize(RequestHandlerInterface::class); $middlewareProphecy = $this->prophesize(MiddlewareInterface::class); $middlewareProphecy->process(Argument::cetera())->willReturn($responseProphecy->reveal()); /** @var RequestHandlerInterface $kernel */ $kernel = $kernelProphecy->reveal(); $dispatcher = $this->createMiddlewareDispatcher($kernel, null); $dispatcher->add($middlewareProphecy->reveal()); $response1 = $dispatcher->handle($requestProphecy->reveal()); $response2 = $dispatcher->handle($requestProphecy->reveal()); $this->assertSame($responseProphecy->reveal(), $response1); $this->assertSame($responseProphecy->reveal(), $response2); $kernelProphecy->handle(Argument::type(ServerRequestInterface::class))->shouldNotHaveBeenCalled(); } public function testCanBeReExecutedRecursivelyDuringDispatch(): void { $requestProphecy = $this->prophesize(ServerRequestInterface::class); $responseProphecy = $this->prophesize(ResponseInterface::class); $kernelProphecy = $this->prophesize(RequestHandlerInterface::class); $requestProphecy->hasHeader('X-NESTED')->willReturn(false); $requestProphecy->withAddedHeader('X-NESTED', '1')->will(function () { $this->hasHeader('X-NESTED')->willReturn(true); return $this; }); $responseProphecy->getHeader(Argument::type('string'))->willReturn([]); $responseProphecy->withAddedHeader(Argument::type('string'), Argument::type('string'))->will(function ($args) { $headers = $this->reveal()->getHeader($args[0]); $headers[] = $args[1]; $this->getHeader($args[0])->willReturn($headers); $this->hasHeader($args[0])->willReturn(true); return $this; }); /** @var RequestHandlerInterface $kernel */ $kernel = $kernelProphecy->reveal(); $dispatcher = $this->createMiddlewareDispatcher($kernel, null); $middlewareProphecy = $this->prophesize(MiddlewareInterface::class); $middlewareProphecy ->process( Argument::type(ServerRequestInterface::class), Argument::type(RequestHandlerInterface::class) ) ->will(function ($args) use ($dispatcher, $responseProphecy): ResponseInterface { $request = $args[0]; if ($request->hasHeader('X-NESTED')) { return $responseProphecy->reveal()->withAddedHeader('X-TRACE', 'nested'); } $response = $dispatcher->handle($request->withAddedHeader('X-NESTED', '1')); return $response->withAddedHeader('X-TRACE', 'outer'); }); $dispatcher->add($middlewareProphecy->reveal()); $response = $dispatcher->handle($requestProphecy->reveal()); $this->assertSame(['nested', 'outer'], $response->getHeader('X-TRACE')); } public function testFetchesMiddlewareFromContainer(): void { $kernelProphecy = $this->prophesize(RequestHandlerInterface::class); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $responseProphecy = $this->prophesize(ResponseInterface::class); $middlewareProphecy = $this->prophesize(MiddlewareInterface::class); $middlewareProphecy->process(Argument::cetera())->willReturn($responseProphecy->reveal()); $containerProphecy = $this->prophesize(ContainerInterface::class); $containerProphecy->has('somemiddlewarename')->willReturn(true); $containerProphecy->get('somemiddlewarename')->willReturn($middlewareProphecy->reveal()); /** @var ContainerInterface $container */ $container = $containerProphecy->reveal(); /** @var RequestHandlerInterface $kernel */ $kernel = $kernelProphecy->reveal(); $dispatcher = $this->createMiddlewareDispatcher($kernel, $container); $dispatcher->addDeferred('somemiddlewarename'); $response = $dispatcher->handle($requestProphecy->reveal()); $this->assertSame($responseProphecy->reveal(), $response); $kernelProphecy->handle(Argument::type(ServerRequestInterface::class))->shouldNotHaveBeenCalled(); } public function testMiddlewareGetsInstantiatedWithContainer(): void { $kernelProphecy = $this->prophesize(RequestHandlerInterface::class); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $containerProphecy = $this->prophesize(ContainerInterface::class); $containerProphecy->has(MockMiddlewareWithConstructor::class)->willReturn(false); /** @var ContainerInterface $container */ $container = $containerProphecy->reveal(); /** @var RequestHandlerInterface $kernel */ $kernel = $kernelProphecy->reveal(); $dispatcher = $this->createMiddlewareDispatcher($kernel, $container); $dispatcher->addDeferred(MockMiddlewareWithConstructor::class); $dispatcher->handle($requestProphecy->reveal()); $this->assertSame($containerProphecy->reveal(), MockMiddlewareWithConstructor::$container); } } ================================================ FILE: tests/Mocks/CallableTest.php ================================================ createResponse(); } } ================================================ FILE: tests/Mocks/InvocationStrategyTest.php ================================================ getResponseFactory(); $response = $responseFactory ->createResponse() ->withHeader('Content-Type', 'text/plain'); $calledCount = static::$CalledCount; $response->getBody()->write("{$calledCount}"); return $response; } } ================================================ FILE: tests/Mocks/MockAction.php ================================================ $arguments[2]]); $arguments[1]->getBody()->write($contents); return $response; } } ================================================ FILE: tests/Mocks/MockCustomException.php ================================================ handle($request); } } ================================================ FILE: tests/Mocks/MockMiddlewareWithConstructor.php ================================================ prophesize(ResponseInterface::class); return $responseProphecy->reveal(); } } ================================================ FILE: tests/Mocks/MockMiddlewareWithoutConstructor.php ================================================ getAttribute('appendToOutput'); if ($appendToOutput !== null) { $appendToOutput('Hello World'); } static::$CalledCount++; return $handler->handle($request); } } ================================================ FILE: tests/Mocks/MockMiddlewareWithoutInterface.php ================================================ getResponseFactory(); $this->calledCount += 1; return $responseFactory->createResponse(); } /** * @return int */ public function getCalledCount(): int { return $this->calledCount; } } ================================================ FILE: tests/Mocks/MockSequenceMiddleware.php ================================================ withAddedHeader('X-SEQ-PRE-REQ-HANDLER', static::$id); $response = $handler->handle($request); return $response->withAddedHeader('X-SEQ-POST-REQ-HANDLER', static::$id); } } ================================================ FILE: tests/Mocks/MockStream.php ================================================ [ 'r' => true, 'w+' => true, 'r+' => true, 'x+' => true, 'c+' => true, 'rb' => true, 'w+b' => true, 'r+b' => true, 'x+b' => true, 'c+b' => true, 'rt' => true, 'w+t' => true, 'r+t' => true, 'x+t' => true, 'c+t' => true, 'a+' => true, ], 'write' => [ 'w' => true, 'w+' => true, 'rw' => true, 'r+' => true, 'x+' => true, 'c+' => true, 'wb' => true, 'w+b' => true, 'r+b' => true, 'x+b' => true, 'c+b' => true, 'w+t' => true, 'r+t' => true, 'x+t' => true, 'c+t' => true, 'a' => true, 'a+' => true, ], ]; /** * MockStream constructor. * @param string|resource $body */ public function __construct($body = '') { if (is_string($body)) { $resource = fopen('php://temp', 'rw+'); fwrite($resource, $body); $body = $resource; } if ('resource' === gettype($body)) { $this->stream = $body; $meta = stream_get_meta_data($this->stream); $this->seekable = $meta['seekable']; $this->readable = isset(self::$readWriteHash['read'][$meta['mode']]); $this->writable = isset(self::$readWriteHash['write'][$meta['mode']]); $this->uri = $this->getMetadata('uri'); } else { throw new InvalidArgumentException( 'First argument to Stream::create() must be a string, resource or StreamInterface.' ); } } /** * Closes the stream when the destructed. */ public function __destruct() { $this->close(); } public function __toString(): string { try { if ($this->isSeekable()) { $this->seek(0); } return $this->getContents(); } catch (Exception $e) { return ''; } } public function close(): void { if (isset($this->stream)) { if (is_resource($this->stream)) { fclose($this->stream); } $this->detach(); } } public function detach() { if (!isset($this->stream)) { return null; } $result = $this->stream; unset($this->stream); $this->size = $this->uri = null; $this->readable = $this->writable = $this->seekable = false; return $result; } public function getSize(): ?int { if (null !== $this->size) { return $this->size; } if (!isset($this->stream)) { return null; } // Clear the stat cache if the stream has a URI if ($this->uri) { clearstatcache(true, $this->uri); } $stats = fstat($this->stream); if (isset($stats['size'])) { $this->size = $stats['size']; return $this->size; } return null; } public function tell(): int { if (false === $result = ftell($this->stream)) { throw new RuntimeException('Unable to determine stream position'); } return $result; } public function eof(): bool { return !$this->stream || feof($this->stream); } public function isSeekable(): bool { return $this->seekable; } public function seek($offset, $whence = SEEK_SET): void { if (!$this->seekable) { throw new RuntimeException('Stream is not seekable'); } if (fseek($this->stream, $offset, $whence) === -1) { throw new RuntimeException( 'Unable to seek to stream position ' . $offset . ' with whence ' . var_export($whence, true) ); } } public function rewind(): void { $this->seek(0); } public function isWritable(): bool { return $this->writable; } public function write($string): int { if (!$this->writable) { throw new RuntimeException('Cannot write to a non-writable stream'); } // We can't know the size after writing anything $this->size = null; if (false === $result = fwrite($this->stream, $string)) { throw new RuntimeException('Unable to write to stream'); } return $result; } public function isReadable(): bool { return $this->readable; } public function read($length): string { if (!$this->readable) { throw new RuntimeException('Cannot read from non-readable stream'); } return fread($this->stream, $length); } public function getContents(): string { if (!isset($this->stream)) { throw new RuntimeException('Unable to read stream contents'); } if (false === $contents = stream_get_contents($this->stream)) { throw new RuntimeException('Unable to read stream contents'); } return $contents; } public function getMetadata($key = null) { if (!isset($this->stream)) { return $key ? null : []; } if (null === $key) { return stream_get_meta_data($this->stream); } $meta = stream_get_meta_data($this->stream); return $meta[$key] ?? null; } } ================================================ FILE: tests/Mocks/RequestHandlerTest.php ================================================ getResponseFactory(); $response = $responseFactory ->createResponse() ->withHeader('Content-Type', 'text/plain'); $calledCount = static::$CalledCount; $response->getBody()->write("{$calledCount}"); return $response; } public function custom(ServerRequestInterface $request): ResponseInterface { $psr7ObjectProvider = new PSR7ObjectProvider(); $responseFactory = $psr7ObjectProvider->getResponseFactory(); return $responseFactory->createResponse(); } } ================================================ FILE: tests/Mocks/SlowPokeStream.php ================================================ amountToRead = self::SIZE; } public function __toString(): string { $content = ''; while (!$this->eof()) { $content .= $this->read(self::CHUNK_SIZE); } return $content; } public function close(): void { } public function detach() { throw new Exception('not implemented'); } public function eof(): bool { return $this->amountToRead === 0; } public function getContents(): string { throw new Exception('not implemented'); } public function getMetadata($key = null) { throw new Exception('not implemented'); } public function getSize(): ?int { return null; } public function isReadable(): bool { return true; } public function isSeekable(): bool { return false; } public function isWritable(): bool { return false; } public function read($length): string { usleep(1); $size = min($this->amountToRead, self::CHUNK_SIZE, $length); $this->amountToRead -= $size; return str_repeat('.', $size); } public function rewind(): void { throw new Exception('not implemented'); } public function seek($offset, $whence = SEEK_SET): void { throw new Exception('not implemented'); } public function tell(): int { throw new Exception('not implemented'); } public function write($string): int { return strlen($string); } } ================================================ FILE: tests/Mocks/SmallChunksStream.php ================================================ amountToRead = self::SIZE; } public function __toString(): string { return str_repeat('.', self::SIZE); } public function close(): void { } public function detach() { throw new Exception('not implemented'); } public function eof(): bool { return $this->amountToRead === 0; } public function getContents(): string { throw new Exception('not implemented'); } public function getMetadata($key = null) { throw new Exception('not implemented'); } public function getSize(): ?int { return self::SIZE; } public function isReadable(): bool { return true; } public function isSeekable(): bool { return false; } public function isWritable(): bool { return false; } public function read($length): string { $size = min($this->amountToRead, self::CHUNK_SIZE, $length); $this->amountToRead -= $size; return str_repeat('.', min($length, $size)); } public function rewind(): void { throw new Exception('not implemented'); } public function seek($offset, $whence = SEEK_SET): void { throw new Exception('not implemented'); } public function tell(): int { throw new Exception('not implemented'); } public function write($string): int { return strlen($string); } } ================================================ FILE: tests/Providers/PSR7ObjectProvider.php ================================================ 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'HTTP_ACCEPT_CHARSET' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.3', 'HTTP_ACCEPT_LANGUAGE' => 'en-US,en;q=0.8', 'HTTP_HOST' => 'localhost', 'HTTP_USER_AGENT' => 'Slim Framework', 'QUERY_STRING' => '', 'REMOTE_ADDR' => '127.0.0.1', 'REQUEST_METHOD' => $method, 'REQUEST_TIME' => time(), 'REQUEST_TIME_FLOAT' => microtime(true), 'REQUEST_URI' => '', 'SCRIPT_NAME' => '/index.php', 'SERVER_NAME' => 'localhost', 'SERVER_PORT' => 80, 'SERVER_PROTOCOL' => 'HTTP/1.1', ], $data); return $this ->getServerRequestFactory() ->createServerRequest($method, $uri, $headers); } /** * @return ServerRequestFactoryInterface */ public function getServerRequestFactory(): ServerRequestFactoryInterface { return new Psr17Factory(); } /** * @param int $statusCode * @param string $reasonPhrase * @return ResponseInterface */ public function createResponse(int $statusCode = 200, string $reasonPhrase = ''): ResponseInterface { return $this ->getResponseFactory() ->createResponse($statusCode, $reasonPhrase); } /** * @return ResponseFactoryInterface */ public function getResponseFactory(): ResponseFactoryInterface { return new Psr17Factory(); } /** * @param string $contents * @return StreamInterface */ public function createStream(string $contents = ''): StreamInterface { return $this ->getStreamFactory() ->createStream($contents); } /** * @return StreamFactoryInterface */ public function getStreamFactory(): StreamFactoryInterface { return new Psr17Factory(); } } ================================================ FILE: tests/Providers/PSR7ObjectProviderInterface.php ================================================ createResponse(); $response->getBody()->write('Hello'); $responseEmitter = new ResponseEmitter(); $responseEmitter->emit($response); $this->expectOutputString('Hello'); } public function testRespondWithPaddedStreamFilterOutput(): void { $availableFilter = stream_get_filters(); $filterName = 'string.rot13'; $unfilterName = 'string.rot13'; $specificFilterName = 'string.rot13'; $specificUnfilterName = 'string.rot13'; if (in_array($filterName, $availableFilter) && in_array($unfilterName, $availableFilter)) { $key = base64_decode('xxxxxxxxxxxxxxxx'); $iv = base64_decode('Z6wNDk9LogWI4HYlRu0mng=='); $data = 'Hello'; $length = strlen($data); $stream = fopen('php://temp', 'r+'); $filter = stream_filter_append($stream, $specificFilterName, STREAM_FILTER_WRITE, [ 'key' => $key, 'iv' => $iv ]); fwrite($stream, $data); rewind($stream); stream_filter_remove($filter); stream_filter_append($stream, $specificUnfilterName, STREAM_FILTER_READ, [ 'key' => $key, 'iv' => $iv ]); $body = $this->getStreamFactory()->createStreamFromResource($stream); $response = $this ->createResponse() ->withHeader('Content-Length', $length) ->withBody($body); $responseEmitter = new ResponseEmitter(); $responseEmitter->emit($response); $this->expectOutputString('Hello'); } else { $this->assertTrue(true); } } public function testRespondIndeterminateLength(): void { $stream = fopen('php://temp', 'r+'); fwrite($stream, 'Hello'); rewind($stream); $body = $this ->getMockBuilder(MockStream::class) ->setConstructorArgs([$stream]) ->onlyMethods(['getSize']) ->getMock(); $body->method('getSize')->willReturn(null); $response = $this->createResponse()->withBody($body); $responseEmitter = new ResponseEmitter(); $responseEmitter->emit($response); $this->expectOutputString('Hello'); } public function testResponseWithStreamReadYieldingLessBytesThanAsked(): void { $body = new SmallChunksStream(); $response = $this->createResponse()->withBody($body); $responseEmitter = new ResponseEmitter($body::CHUNK_SIZE * 2); $responseEmitter->emit($response); $this->expectOutputString(str_repeat('.', $body->getSize())); } public function testResponseReplacesPreviouslySetHeaders(): void { $response = $this ->createResponse(200, 'OK') ->withHeader('X-Foo', 'baz1') ->withAddedHeader('X-Foo', 'baz2'); $responseEmitter = new ResponseEmitter(); $responseEmitter->emit($response); $expectedStack = [ ['header' => 'X-Foo: baz1', 'replace' => true, 'status_code' => null], ['header' => 'X-Foo: baz2', 'replace' => false, 'status_code' => null], ['header' => 'HTTP/1.1 200 OK', 'replace' => true, 'status_code' => 200], ]; $this->assertSame($expectedStack, HeaderStack::stack()); } public function testResponseDoesNotReplacePreviouslySetSetCookieHeaders(): void { $response = $this ->createResponse(200, 'OK') ->withHeader('set-cOOkie', 'foo=bar') ->withAddedHeader('Set-Cookie', 'bar=baz'); $responseEmitter = new ResponseEmitter(); $responseEmitter->emit($response); $expectedStack = [ ['header' => 'set-cOOkie: foo=bar', 'replace' => false, 'status_code' => null], ['header' => 'set-cOOkie: bar=baz', 'replace' => false, 'status_code' => null], ['header' => 'HTTP/1.1 200 OK', 'replace' => true, 'status_code' => 200], ]; $this->assertSame($expectedStack, HeaderStack::stack()); } public function testIsResponseEmptyWithNonEmptyBodyAndTriggeringStatusCode(): void { $body = $this->createStream('Hello'); $response = $this ->createResponse(204) ->withBody($body); $responseEmitter = new ResponseEmitter(); $this->assertTrue($responseEmitter->isResponseEmpty($response)); } public function testIsResponseEmptyDoesNotReadAllDataFromNonEmptySeekableResponse(): void { $body = $this->createStream('Hello'); $response = $this ->createResponse(200) ->withBody($body); $responseEmitter = new ResponseEmitter(); $responseEmitter->isResponseEmpty($response); $this->assertTrue($body->isSeekable()); $this->assertFalse($body->eof()); } public function testIsResponseEmptyDoesNotDrainNonSeekableResponseWithContent(): void { $resource = popen('echo 12', 'r'); $body = $this->getStreamFactory()->createStreamFromResource($resource); $response = $this->createResponse(200)->withBody($body); $responseEmitter = new ResponseEmitter(); $responseEmitter->isResponseEmpty($response); $this->assertFalse($body->isSeekable()); $this->assertSame('12', trim((string) $body)); } public function testAvoidReadFromSlowStreamAccordingToStatus(): void { $body = new SlowPokeStream(); $response = $this ->createResponse(204, 'No content') ->withBody($body); $responseEmitter = new ResponseEmitter(); $responseEmitter->emit($response); $this->assertFalse($body->eof()); $this->expectOutputString(''); } public function testIsResponseEmptyWithEmptyBody(): void { $response = $this->createResponse(200); $responseEmitter = new ResponseEmitter(); $this->assertTrue($responseEmitter->isResponseEmpty($response)); } public function testIsResponseEmptyWithZeroAsBody(): void { $body = $this->createStream('0'); $response = $this ->createResponse(200) ->withBody($body); $responseEmitter = new ResponseEmitter(); $this->assertFalse($responseEmitter->isResponseEmpty($response)); } public function testWillHandleInvalidConnectionStatusWithADeterminateBody(): void { $body = $this->getStreamFactory()->createStreamFromResource(fopen('php://temp', 'r+')); $body->write('Hello!' . "\n"); $body->write('Hello!' . "\n"); // Tell connection_status() to fail. $GLOBALS['connection_status_return'] = CONNECTION_ABORTED; $response = $this ->createResponse() ->withHeader('Content-Length', $body->getSize()) ->withBody($body); $responseEmitter = new ResponseEmitter(); $responseEmitter->emit($response); $this->expectOutputString("Hello!\nHello!\n"); // Tell connection_status() to pass. unset($GLOBALS['connection_status_return']); } public function testWillHandleInvalidConnectionStatusWithAnIndeterminateBody(): void { $body = $this->getStreamFactory()->createStreamFromResource(fopen('php://input', 'r+')); // Tell connection_status() to fail. $GLOBALS['connection_status_return'] = CONNECTION_TIMEOUT; $response = $this ->createResponse() ->withBody($body); $responseEmitter = new ResponseEmitter(); $mirror = new ReflectionClass(ResponseEmitter::class); $emitBodyMethod = $mirror->getMethod('emitBody'); $this->setAccessible($emitBodyMethod); $emitBodyMethod->invoke($responseEmitter, $response); $this->expectOutputString(""); // Tell connection_status() to pass. unset($GLOBALS['connection_status_return']); } } ================================================ FILE: tests/Routing/DispatcherTest.php ================================================ prophesize(CallableResolverInterface::class); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $routeCollector = new RouteCollector($responseFactoryProphecy->reveal(), $callableResolverProphecy->reveal()); $dispatcher = new Dispatcher($routeCollector); $method = new ReflectionMethod(Dispatcher::class, 'createDispatcher'); $this->setAccessible($method); $this->assertInstanceOf(FastRouteDispatcher::class, $method->invoke($dispatcher)); } /** * Test cached routes file is created & that it holds our routes. */ public function testRouteCacheFileCanBeDispatched() { $callableResolverProphecy = $this->prophesize(CallableResolverInterface::class); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $routeCollector = new RouteCollector($responseFactoryProphecy->reveal(), $callableResolverProphecy->reveal()); $dispatcher = new Dispatcher($routeCollector); $route = $routeCollector->map(['GET'], '/', function () { }); $route->setName('foo'); $cacheFile = __DIR__ . '/' . uniqid((string) microtime(true)); $routeCollector->setCacheFile($cacheFile); $method = new ReflectionMethod(Dispatcher::class, 'createDispatcher'); $this->setAccessible($method); $method->invoke($dispatcher); $this->assertFileExists($cacheFile, 'cache file was not created'); $routeCollector2 = new RouteCollector($responseFactoryProphecy->reveal(), $callableResolverProphecy->reveal()); $routeCollector2->setCacheFile($cacheFile); $dispatcher2 = new Dispatcher($routeCollector2); $method = new ReflectionMethod(Dispatcher::class, 'createDispatcher'); $this->setAccessible($method); $method->invoke($dispatcher2); /** @var RoutingResults $result */ $result = $dispatcher2->dispatch('GET', '/'); $this->assertSame(FastRouteDispatcher::FOUND, $result->getRouteStatus()); unlink($cacheFile); } /** * Calling createDispatcher as second time should give you back the same * dispatcher as when you called it the first time. */ public function testCreateDispatcherReturnsSameDispatcherASecondTime() { $callableResolverProphecy = $this->prophesize(CallableResolverInterface::class); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $routeCollector = new RouteCollector($responseFactoryProphecy->reveal(), $callableResolverProphecy->reveal()); $dispatcher = new Dispatcher($routeCollector); $method = new ReflectionMethod(Dispatcher::class, 'createDispatcher'); $this->setAccessible($method); $fastRouteDispatcher = $method->invoke($dispatcher); $fastRouteDispatcher2 = $method->invoke($dispatcher); $this->assertSame($fastRouteDispatcher, $fastRouteDispatcher2); } public function testGetAllowedMethods() { $methods = ['GET', 'POST', 'PUT']; $uri = '/'; $callableResolverProphecy = $this->prophesize(CallableResolverInterface::class); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $routeCollector = new RouteCollector($responseFactoryProphecy->reveal(), $callableResolverProphecy->reveal()); $routeCollector->map($methods, $uri, function () { }); $dispatcher = new Dispatcher($routeCollector); $results = $dispatcher->getAllowedMethods('/'); $this->assertSame($methods, $results); } public function testDispatch() { $methods = ['GET', 'POST']; $uri = '/hello/{name}'; $callableResolverProphecy = $this->prophesize(CallableResolverInterface::class); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $callable = function () { }; $routeCollector = new RouteCollector($responseFactoryProphecy->reveal(), $callableResolverProphecy->reveal()); $route = $routeCollector->map($methods, $uri, $callable); $dispatcher = new Dispatcher($routeCollector); $results = $dispatcher->dispatch('GET', '/hello/Foo%20Bar'); $this->assertSame(RoutingResults::FOUND, $results->getRouteStatus()); $this->assertSame('GET', $results->getMethod()); $this->assertSame('/hello/Foo%20Bar', $results->getUri()); $this->assertSame($route->getIdentifier(), $results->getRouteIdentifier()); $this->assertSame(['name' => 'Foo Bar'], $results->getRouteArguments()); $this->assertSame(['name' => 'Foo%20Bar'], $results->getRouteArguments(false)); $this->assertSame($methods, $results->getAllowedMethods()); $this->assertSame($dispatcher, $results->getDispatcher()); } } ================================================ FILE: tests/Routing/FastRouteDispatcherTest.php ================================================ generateDispatcherOptions()); $results = $dispatcher->dispatch($method, $uri); $this->assertSame($dispatcher::FOUND, $results[0]); $this->assertSame($handler, $results[1]); $this->assertSame($argDict, $results[2]); } /** * Set appropriate options for the specific Dispatcher class we're testing */ private function generateDispatcherOptions() { return [ 'dataGenerator' => $this->getDataGeneratorClass(), 'dispatcher' => $this->getDispatcherClass() ]; } protected function getDataGeneratorClass() { return GroupCountBased::class; } protected function getDispatcherClass() { return FastRouteDispatcher::class; } /** * @dataProvider provideNotFoundDispatchCases */ #[\PHPUnit\Framework\Attributes\DataProvider('provideNotFoundDispatchCases')] public function testNotFoundDispatches($method, $uri, $callback) { /** @var FastRouteDispatcher $dispatcher */ $dispatcher = simpleDispatcher($callback, $this->generateDispatcherOptions()); $results = $dispatcher->dispatch($method, $uri); $this->assertSame($dispatcher::NOT_FOUND, $results[0]); } /** * @dataProvider provideMethodNotAllowedDispatchCases * @param $method * @param $uri * @param $callback * @param $allowedMethods */ #[\PHPUnit\Framework\Attributes\DataProvider('provideMethodNotAllowedDispatchCases')] public function testMethodNotAllowedDispatches($method, $uri, $callback, $allowedMethods) { /** @var FastRouteDispatcher $dispatcher */ $dispatcher = simpleDispatcher($callback, $this->generateDispatcherOptions()); $results = $dispatcher->dispatch($method, $uri); $this->assertSame($dispatcher::METHOD_NOT_ALLOWED, $results[0]); } /** * @dataProvider provideMethodNotAllowedDispatchCases * @param $method * @param $uri * @param $callback * @param $allowedMethods */ #[\PHPUnit\Framework\Attributes\DataProvider('provideMethodNotAllowedDispatchCases')] public function testGetAllowedMethods($method, $uri, $callback, $allowedMethods) { /** @var FastRouteDispatcher $dispatcher */ $dispatcher = simpleDispatcher($callback, $this->generateDispatcherOptions()); $results = $dispatcher->getAllowedMethods($uri); $this->assertSame($results, $allowedMethods); } public function testDuplicateVariableNameError() { $this->expectException(BadRouteException::class); $this->expectExceptionMessage('Cannot use the same placeholder "test" twice'); simpleDispatcher(function (RouteCollector $r) { $r->addRoute('GET', '/foo/{test}/{test:\d+}', 'handler0'); }, $this->generateDispatcherOptions()); } public function testDuplicateVariableRoute() { $this->expectException(BadRouteException::class); $this->expectExceptionMessage('Cannot register two routes matching "/user/([^/]+)" for method "GET"'); simpleDispatcher(function (RouteCollector $r) { $r->addRoute('GET', '/user/{id}', 'handler0'); // oops, forgot \d+ restriction ;) $r->addRoute('GET', '/user/{name}', 'handler1'); }, $this->generateDispatcherOptions()); } public function testDuplicateStaticRoute() { $this->expectException(BadRouteException::class); $this->expectExceptionMessage('Cannot register two routes matching "/user" for method "GET"'); simpleDispatcher(function (RouteCollector $r) { $r->addRoute('GET', '/user', 'handler0'); $r->addRoute('GET', '/user', 'handler1'); }, $this->generateDispatcherOptions()); } /** * @codingStandardsIgnoreStart * @codingStandardsIgnoreEnd */ public function testShadowedStaticRoute() { $this->expectException(BadRouteException::class); $this->expectExceptionMessage('Static route "/user/nikic" is shadowed by previously defined variable route' . ' "/user/([^/]+)" for method "GET"'); simpleDispatcher(function (RouteCollector $r) { $r->addRoute('GET', '/user/{name}', 'handler0'); $r->addRoute('GET', '/user/nikic', 'handler1'); }, $this->generateDispatcherOptions()); } public function testCapturing() { $this->expectException(BadRouteException::class); $this->expectExceptionMessage('Regex "(en|de)" for parameter "lang" contains a capturing group'); simpleDispatcher(function (RouteCollector $r) { $r->addRoute('GET', '/{lang:(en|de)}', 'handler0'); }, $this->generateDispatcherOptions()); } public static function provideFoundDispatchCases() { $cases = []; // 0 --------------------------------------------------------------------------------------> $callback = function (RouteCollector $r) { $r->addRoute('GET', '/resource/123/456', 'handler0'); }; $method = 'GET'; $uri = '/resource/123/456'; $handler = 'handler0'; $argDict = []; $cases[] = [$method, $uri, $callback, $handler, $argDict]; // 1 --------------------------------------------------------------------------------------> $callback = function (RouteCollector $r) { $r->addRoute('GET', '/handler0', 'handler0'); $r->addRoute('GET', '/handler1', 'handler1'); $r->addRoute('GET', '/handler2', 'handler2'); }; $method = 'GET'; $uri = '/handler2'; $handler = 'handler2'; $argDict = []; $cases[] = [$method, $uri, $callback, $handler, $argDict]; // 2 --------------------------------------------------------------------------------------> $callback = function (RouteCollector $r) { $r->addRoute('GET', '/user/{name}/{id:[0-9]+}', 'handler0'); $r->addRoute('GET', '/user/{id:[0-9]+}', 'handler1'); $r->addRoute('GET', '/user/{name}', 'handler2'); }; $method = 'GET'; $uri = '/user/rdlowrey'; $handler = 'handler2'; $argDict = ['name' => 'rdlowrey']; $cases[] = [$method, $uri, $callback, $handler, $argDict]; // 3 --------------------------------------------------------------------------------------> // reuse $callback from #2 $method = 'GET'; $uri = '/user/12345'; $handler = 'handler1'; $argDict = ['id' => '12345']; $cases[] = [$method, $uri, $callback, $handler, $argDict]; // 4 --------------------------------------------------------------------------------------> // reuse $callback from #3 $method = 'GET'; $uri = '/user/NaN'; $handler = 'handler2'; $argDict = ['name' => 'NaN']; $cases[] = [$method, $uri, $callback, $handler, $argDict]; // 5 --------------------------------------------------------------------------------------> // reuse $callback from #4 $method = 'GET'; $uri = '/user/rdlowrey/12345'; $handler = 'handler0'; $argDict = ['name' => 'rdlowrey', 'id' => '12345']; $cases[] = [$method, $uri, $callback, $handler, $argDict]; // 6 --------------------------------------------------------------------------------------> $callback = function (RouteCollector $r) { $r->addRoute('GET', '/user/{id:[0-9]+}', 'handler0'); $r->addRoute('GET', '/user/12345/extension', 'handler1'); $r->addRoute('GET', '/user/{id:[0-9]+}.{extension}', 'handler2'); }; $method = 'GET'; $uri = '/user/12345.svg'; $handler = 'handler2'; $argDict = ['id' => '12345', 'extension' => 'svg']; $cases[] = [$method, $uri, $callback, $handler, $argDict]; // 7 ----- Test GET method fallback on HEAD route miss ------------------------------------> $callback = function (RouteCollector $r) { $r->addRoute('GET', '/user/{name}', 'handler0'); $r->addRoute('GET', '/user/{name}/{id:[0-9]+}', 'handler1'); $r->addRoute('GET', '/static0', 'handler2'); $r->addRoute('GET', '/static1', 'handler3'); $r->addRoute('HEAD', '/static1', 'handler4'); }; $method = 'HEAD'; $uri = '/user/rdlowrey'; $handler = 'handler0'; $argDict = ['name' => 'rdlowrey']; $cases[] = [$method, $uri, $callback, $handler, $argDict]; // 8 ----- Test GET method fallback on HEAD route miss ------------------------------------> // reuse $callback from #7 $method = 'HEAD'; $uri = '/user/rdlowrey/1234'; $handler = 'handler1'; $argDict = ['name' => 'rdlowrey', 'id' => '1234']; $cases[] = [$method, $uri, $callback, $handler, $argDict]; // 9 ----- Test GET method fallback on HEAD route miss ------------------------------------> // reuse $callback from #8 $method = 'HEAD'; $uri = '/static0'; $handler = 'handler2'; $argDict = []; $cases[] = [$method, $uri, $callback, $handler, $argDict]; // 10 ---- Test existing HEAD route used if available (no fallback) -----------------------> // reuse $callback from #9 $method = 'HEAD'; $uri = '/static1'; $handler = 'handler4'; $argDict = []; $cases[] = [$method, $uri, $callback, $handler, $argDict]; // 11 ---- More specified routes are not shadowed by less specific of another method ------> $callback = function (RouteCollector $r) { $r->addRoute('GET', '/user/{name}', 'handler0'); $r->addRoute('POST', '/user/{name:[a-z]+}', 'handler1'); }; $method = 'POST'; $uri = '/user/rdlowrey'; $handler = 'handler1'; $argDict = ['name' => 'rdlowrey']; $cases[] = [$method, $uri, $callback, $handler, $argDict]; // 12 ---- Handler of more specific routes is used, if it occurs first --------------------> $callback = function (RouteCollector $r) { $r->addRoute('GET', '/user/{name}', 'handler0'); $r->addRoute('POST', '/user/{name:[a-z]+}', 'handler1'); $r->addRoute('POST', '/user/{name}', 'handler2'); }; $method = 'POST'; $uri = '/user/rdlowrey'; $handler = 'handler1'; $argDict = ['name' => 'rdlowrey']; $cases[] = [$method, $uri, $callback, $handler, $argDict]; // 13 ---- Route with constant suffix -----------------------------------------------------> $callback = function (RouteCollector $r) { $r->addRoute('GET', '/user/{name}', 'handler0'); $r->addRoute('GET', '/user/{name}/edit', 'handler1'); }; $method = 'GET'; $uri = '/user/rdlowrey/edit'; $handler = 'handler1'; $argDict = ['name' => 'rdlowrey']; $cases[] = [$method, $uri, $callback, $handler, $argDict]; // 14 ---- Handle multiple methods with the same handler ----------------------------------> $callback = function (RouteCollector $r) { $r->addRoute(['GET', 'POST'], '/user', 'handlerGetPost'); $r->addRoute(['DELETE'], '/user', 'handlerDelete'); $r->addRoute([], '/user', 'handlerNone'); }; $argDict = []; $cases[] = ['GET', '/user', $callback, 'handlerGetPost', $argDict]; $cases[] = ['POST', '/user', $callback, 'handlerGetPost', $argDict]; $cases[] = ['DELETE', '/user', $callback, 'handlerDelete', $argDict]; // 17 ---- $callback = function (RouteCollector $r) { $r->addRoute('POST', '/user.json', 'handler0'); $r->addRoute('GET', '/{entity}.json', 'handler1'); }; $cases[] = ['GET', '/user.json', $callback, 'handler1', ['entity' => 'user']]; // 18 ---- $callback = function (RouteCollector $r) { $r->addRoute('GET', '', 'handler0'); }; $cases[] = ['GET', '', $callback, 'handler0', []]; // 19 ---- $callback = function (RouteCollector $r) { $r->addRoute('HEAD', '/a/{foo}', 'handler0'); $r->addRoute('GET', '/b/{foo}', 'handler1'); }; $cases[] = ['HEAD', '/b/bar', $callback, 'handler1', ['foo' => 'bar']]; // 20 ---- $callback = function (RouteCollector $r) { $r->addRoute('HEAD', '/a', 'handler0'); $r->addRoute('GET', '/b', 'handler1'); }; $cases[] = ['HEAD', '/b', $callback, 'handler1', []]; // 21 ---- $callback = function (RouteCollector $r) { $r->addRoute('GET', '/foo', 'handler0'); $r->addRoute('HEAD', '/{bar}', 'handler1'); }; $cases[] = ['HEAD', '/foo', $callback, 'handler1', ['bar' => 'foo']]; // 22 ---- $callback = function (RouteCollector $r) { $r->addRoute('*', '/user', 'handler0'); $r->addRoute('*', '/{user}', 'handler1'); $r->addRoute('GET', '/user', 'handler2'); }; $cases[] = ['GET', '/user', $callback, 'handler2', []]; // 23 ---- $callback = function (RouteCollector $r) { $r->addRoute('*', '/user', 'handler0'); $r->addRoute('GET', '/user', 'handler1'); }; $cases[] = ['POST', '/user', $callback, 'handler0', []]; // 24 ---- $cases[] = ['HEAD', '/user', $callback, 'handler1', []]; // 25 ---- $callback = function (RouteCollector $r) { $r->addRoute('GET', '/{bar}', 'handler0'); $r->addRoute('*', '/foo', 'handler1'); }; $cases[] = ['GET', '/foo', $callback, 'handler0', ['bar' => 'foo']]; // 26 ---- $callback = function (RouteCollector $r) { $r->addRoute('GET', '/user', 'handler0'); $r->addRoute('*', '/{foo:.*}', 'handler1'); }; $cases[] = ['POST', '/bar', $callback, 'handler1', ['foo' => 'bar']]; // 27 --- International characters $callback = function (RouteCollector $r) { $r->addRoute('GET', '/новости/{name}', 'handler0'); }; $cases[] = ['GET', '/новости/rdlowrey', $callback, 'handler0', ['name' => 'rdlowrey']]; // x --------------------------------------------------------------------------------------> return $cases; } public static function provideNotFoundDispatchCases() { $cases = []; // 0 --------------------------------------------------------------------------------------> $callback = function (RouteCollector $r) { $r->addRoute('GET', '/resource/123/456', 'handler0'); }; $method = 'GET'; $uri = '/not-found'; $cases[] = [$method, $uri, $callback]; // 1 --------------------------------------------------------------------------------------> // reuse callback from #0 $method = 'POST'; $uri = '/not-found'; $cases[] = [$method, $uri, $callback]; // 2 --------------------------------------------------------------------------------------> // reuse callback from #1 $method = 'PUT'; $uri = '/not-found'; $cases[] = [$method, $uri, $callback]; // 3 --------------------------------------------------------------------------------------> $callback = function (RouteCollector $r) { $r->addRoute('GET', '/handler0', 'handler0'); $r->addRoute('GET', '/handler1', 'handler1'); $r->addRoute('GET', '/handler2', 'handler2'); }; $method = 'GET'; $uri = '/not-found'; $cases[] = [$method, $uri, $callback]; // 4 --------------------------------------------------------------------------------------> $callback = function (RouteCollector $r) { $r->addRoute('GET', '/user/{name}/{id:[0-9]+}', 'handler0'); $r->addRoute('GET', '/user/{id:[0-9]+}', 'handler1'); $r->addRoute('GET', '/user/{name}', 'handler2'); }; $method = 'GET'; $uri = '/not-found'; $cases[] = [$method, $uri, $callback]; // 5 --------------------------------------------------------------------------------------> // reuse callback from #4 $method = 'GET'; $uri = '/user/rdlowrey/12345/not-found'; $cases[] = [$method, $uri, $callback]; // 6 --------------------------------------------------------------------------------------> // reuse callback from #5 $method = 'HEAD'; $cases[] = [$method, $uri, $callback]; // x --------------------------------------------------------------------------------------> return $cases; } public static function provideMethodNotAllowedDispatchCases() { $cases = []; // 0 --------------------------------------------------------------------------------------> $callback = function (RouteCollector $r) { $r->addRoute('GET', '/resource/123/456', 'handler0'); }; $method = 'POST'; $uri = '/resource/123/456'; $allowedMethods = ['GET']; $cases[] = [$method, $uri, $callback, $allowedMethods]; // 1 --------------------------------------------------------------------------------------> $callback = function (RouteCollector $r) { $r->addRoute('GET', '/resource/123/456', 'handler0'); $r->addRoute('POST', '/resource/123/456', 'handler1'); $r->addRoute('PUT', '/resource/123/456', 'handler2'); $r->addRoute('*', '/', 'handler3'); }; $method = 'DELETE'; $uri = '/resource/123/456'; $allowedMethods = ['GET', 'POST', 'PUT']; $cases[] = [$method, $uri, $callback, $allowedMethods]; // 2 --------------------------------------------------------------------------------------> $callback = function (RouteCollector $r) { $r->addRoute('GET', '/user/{name}/{id:[0-9]+}', 'handler0'); $r->addRoute('POST', '/user/{name}/{id:[0-9]+}', 'handler1'); $r->addRoute('PUT', '/user/{name}/{id:[0-9]+}', 'handler2'); $r->addRoute('PATCH', '/user/{name}/{id:[0-9]+}', 'handler3'); }; $method = 'DELETE'; $uri = '/user/rdlowrey/42'; $allowedMethods = ['GET', 'POST', 'PUT', 'PATCH']; $cases[] = [$method, $uri, $callback, $allowedMethods]; // 3 --------------------------------------------------------------------------------------> $callback = function (RouteCollector $r) { $r->addRoute('POST', '/user/{name}', 'handler1'); $r->addRoute('PUT', '/user/{name:[a-z]+}', 'handler2'); $r->addRoute('PATCH', '/user/{name:[a-z]+}', 'handler3'); }; $method = 'GET'; $uri = '/user/rdlowrey'; $allowedMethods = ['POST', 'PUT', 'PATCH']; $cases[] = [$method, $uri, $callback, $allowedMethods]; // 4 --------------------------------------------------------------------------------------> $callback = function (RouteCollector $r) { $r->addRoute(['GET', 'POST'], '/user', 'handlerGetPost'); $r->addRoute(['DELETE'], '/user', 'handlerDelete'); $r->addRoute([], '/user', 'handlerNone'); }; $cases[] = ['PUT', '/user', $callback, ['GET', 'POST', 'DELETE']]; // 5 $callback = function (RouteCollector $r) { $r->addRoute('POST', '/user.json', 'handler0'); $r->addRoute('GET', '/{entity}.json', 'handler1'); }; $cases[] = ['PUT', '/user.json', $callback, ['POST', 'GET']]; // x --------------------------------------------------------------------------------------> return $cases; } } ================================================ FILE: tests/Routing/RouteCollectorProxyTest.php ================================================ prophesize(ResponseFactoryInterface::class); $callableResolverProphecy = $this->prophesize(CallableResolverInterface::class); $routeCollectorProxy = new RouteCollectorProxy( $responseFactoryProphecy->reveal(), $callableResolverProphecy->reveal() ); $this->assertSame( $responseFactoryProphecy->reveal(), $routeCollectorProxy->getResponseFactory() ); } public function testGetCallableResolver() { $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $callableResolverProphecy = $this->prophesize(CallableResolverInterface::class); $routeCollectorProxy = new RouteCollectorProxy( $responseFactoryProphecy->reveal(), $callableResolverProphecy->reveal() ); $this->assertSame( $callableResolverProphecy->reveal(), $routeCollectorProxy->getCallableResolver() ); } public function testGetContainerReturnsInjectedInstance() { $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $callableResolverProphecy = $this->prophesize(CallableResolverInterface::class); $containerProphecy = $this->prophesize(ContainerInterface::class); $routeCollectorProxy = new RouteCollectorProxy( $responseFactoryProphecy->reveal(), $callableResolverProphecy->reveal(), $containerProphecy->reveal() ); $this->assertSame( $containerProphecy->reveal(), $routeCollectorProxy->getContainer() ); } public function testGetRouteCollectorReturnsInjectedInstance() { $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $callableResolverProphecy = $this->prophesize(CallableResolverInterface::class); $containerProphecy = $this->prophesize(ContainerInterface::class); $routeCollectorProphecy = $this->prophesize(RouteCollectorInterface::class); $routeCollectorProxy = new RouteCollectorProxy( $responseFactoryProphecy->reveal(), $callableResolverProphecy->reveal(), $containerProphecy->reveal(), $routeCollectorProphecy->reveal() ); $this->assertSame( $routeCollectorProphecy->reveal(), $routeCollectorProxy->getRouteCollector() ); } public function testGetSetBasePath() { $basePath = '/base/path'; $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $callableResolverProphecy = $this->prophesize(CallableResolverInterface::class); $containerProphecy = $this->prophesize(ContainerInterface::class); $routeCollectorProxy = new RouteCollectorProxy( $responseFactoryProphecy->reveal(), $callableResolverProphecy->reveal(), $containerProphecy->reveal() ); $routeCollectorProxy->setBasePath($basePath); $this->assertSame($basePath, $routeCollectorProxy->getBasePath()); $newBasePath = '/new/base/path'; $routeCollectorProxy->setBasePath('/new/base/path'); $this->assertSame($newBasePath, $routeCollectorProxy->getBasePath()); } public function testGet() { $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $callableResolverProphecy = $this->prophesize(CallableResolverInterface::class); $pattern = '/'; $callable = function () { }; $routeProphecy = $this->prophesize(RouteInterface::class); $routeProphecy ->getPattern() ->willReturn($pattern) ->shouldBeCalledOnce(); $routeCollectorProphecy = $this->prophesize(RouteCollectorInterface::class); $routeCollectorProphecy ->map(['GET'], $pattern, Argument::is($callable)) ->willReturn($routeProphecy->reveal()) ->shouldBeCalledOnce(); $routeCollectorProxy = new RouteCollectorProxy( $responseFactoryProphecy->reveal(), $callableResolverProphecy->reveal(), null, $routeCollectorProphecy->reveal() ); $route = $routeCollectorProxy->get($pattern, $callable); $this->assertSame($pattern, $route->getPattern()); } public function testPost() { $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $callableResolverProphecy = $this->prophesize(CallableResolverInterface::class); $pattern = '/'; $callable = function () { }; $routeProphecy = $this->prophesize(RouteInterface::class); $routeProphecy ->getPattern() ->willReturn($pattern) ->shouldBeCalledOnce(); $routeCollectorProphecy = $this->prophesize(RouteCollectorInterface::class); $routeCollectorProphecy ->map(['POST'], $pattern, Argument::is($callable)) ->willReturn($routeProphecy->reveal()) ->shouldBeCalledOnce(); $routeCollectorProxy = new RouteCollectorProxy( $responseFactoryProphecy->reveal(), $callableResolverProphecy->reveal(), null, $routeCollectorProphecy->reveal() ); $route = $routeCollectorProxy->post($pattern, $callable); $this->assertSame($pattern, $route->getPattern()); } public function testPut() { $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $callableResolverProphecy = $this->prophesize(CallableResolverInterface::class); $pattern = '/'; $callable = function () { }; $routeProphecy = $this->prophesize(RouteInterface::class); $routeProphecy ->getPattern() ->willReturn($pattern) ->shouldBeCalledOnce(); $routeCollectorProphecy = $this->prophesize(RouteCollectorInterface::class); $routeCollectorProphecy ->map(['PUT'], $pattern, Argument::is($callable)) ->willReturn($routeProphecy->reveal()) ->shouldBeCalledOnce(); $routeCollectorProxy = new RouteCollectorProxy( $responseFactoryProphecy->reveal(), $callableResolverProphecy->reveal(), null, $routeCollectorProphecy->reveal() ); $route = $routeCollectorProxy->put($pattern, $callable); $this->assertSame($pattern, $route->getPattern()); } public function testPatch() { $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $callableResolverProphecy = $this->prophesize(CallableResolverInterface::class); $pattern = '/'; $callable = function () { }; $routeProphecy = $this->prophesize(RouteInterface::class); $routeProphecy ->getPattern() ->willReturn($pattern) ->shouldBeCalledOnce(); $routeCollectorProphecy = $this->prophesize(RouteCollectorInterface::class); $routeCollectorProphecy ->map(['PATCH'], $pattern, Argument::is($callable)) ->willReturn($routeProphecy->reveal()) ->shouldBeCalledOnce(); $routeCollectorProxy = new RouteCollectorProxy( $responseFactoryProphecy->reveal(), $callableResolverProphecy->reveal(), null, $routeCollectorProphecy->reveal() ); $route = $routeCollectorProxy->patch($pattern, $callable); $this->assertSame($pattern, $route->getPattern()); } public function testDelete() { $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $callableResolverProphecy = $this->prophesize(CallableResolverInterface::class); $pattern = '/'; $callable = function () { }; $routeProphecy = $this->prophesize(RouteInterface::class); $routeProphecy ->getPattern() ->willReturn($pattern) ->shouldBeCalledOnce(); $routeCollectorProphecy = $this->prophesize(RouteCollectorInterface::class); $routeCollectorProphecy ->map(['DELETE'], $pattern, Argument::is($callable)) ->willReturn($routeProphecy->reveal()) ->shouldBeCalledOnce(); $routeCollectorProxy = new RouteCollectorProxy( $responseFactoryProphecy->reveal(), $callableResolverProphecy->reveal(), null, $routeCollectorProphecy->reveal() ); $route = $routeCollectorProxy->delete($pattern, $callable); $this->assertSame($pattern, $route->getPattern()); } public function testOptions() { $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $callableResolverProphecy = $this->prophesize(CallableResolverInterface::class); $pattern = '/'; $callable = function () { }; $routeProphecy = $this->prophesize(RouteInterface::class); $routeProphecy ->getPattern() ->willReturn($pattern) ->shouldBeCalledOnce(); $routeCollectorProphecy = $this->prophesize(RouteCollectorInterface::class); $routeCollectorProphecy ->map(['OPTIONS'], $pattern, Argument::is($callable)) ->willReturn($routeProphecy->reveal()) ->shouldBeCalledOnce(); $routeCollectorProxy = new RouteCollectorProxy( $responseFactoryProphecy->reveal(), $callableResolverProphecy->reveal(), null, $routeCollectorProphecy->reveal() ); $route = $routeCollectorProxy->options($pattern, $callable); $this->assertSame($pattern, $route->getPattern()); } public function testAny() { $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $callableResolverProphecy = $this->prophesize(CallableResolverInterface::class); $pattern = '/'; $callable = function () { }; $routeProphecy = $this->prophesize(RouteInterface::class); $routeProphecy ->getPattern() ->willReturn($pattern) ->shouldBeCalledOnce(); $routeCollectorProphecy = $this->prophesize(RouteCollectorInterface::class); $routeCollectorProphecy ->map(['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'], $pattern, Argument::is($callable)) ->willReturn($routeProphecy->reveal()) ->shouldBeCalledOnce(); $routeCollectorProxy = new RouteCollectorProxy( $responseFactoryProphecy->reveal(), $callableResolverProphecy->reveal(), null, $routeCollectorProphecy->reveal() ); $route = $routeCollectorProxy->any($pattern, $callable); $this->assertSame($pattern, $route->getPattern()); } public function testMap() { $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $callableResolverProphecy = $this->prophesize(CallableResolverInterface::class); $pattern = '/'; $methods = ['GET', 'POST']; $callable = function () { }; $routeProphecy = $this->prophesize(RouteInterface::class); $routeProphecy ->getPattern() ->willReturn($pattern) ->shouldBeCalledOnce(); $routeCollectorProphecy = $this->prophesize(RouteCollectorInterface::class); $routeCollectorProphecy ->map($methods, $pattern, Argument::is($callable)) ->willReturn($routeProphecy->reveal()) ->shouldBeCalledOnce(); $routeCollectorProxy = new RouteCollectorProxy( $responseFactoryProphecy->reveal(), $callableResolverProphecy->reveal(), null, $routeCollectorProphecy->reveal() ); $route = $routeCollectorProxy->map($methods, $pattern, $callable); $this->assertSame($pattern, $route->getPattern()); } public function testRedirect() { $containerProphecy = $this->prophesize(ContainerInterface::class); $callableResolver = new CallableResolver($containerProphecy->reveal()); $from = '/from'; $to = '/to'; $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy ->withHeader('Location', $to) ->willReturn($responseProphecy->reveal()) ->shouldBeCalledOnce(); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy ->createResponse() ->will(function () use ($responseProphecy) { $this ->createResponse(302) ->willReturn($responseProphecy) ->shouldBeCalledOnce(); return $responseProphecy->reveal(); }) ->shouldBeCalledOnce(); $routeCollector = new RouteCollector( $responseFactoryProphecy->reveal(), $callableResolver ); $routeCollectorProxy = new RouteCollectorProxy( $responseFactoryProphecy->reveal(), $callableResolver, $containerProphecy->reveal(), $routeCollector ); $route = $routeCollectorProxy->redirect($from, $to); $request = $this->createServerRequest('/'); $response = $route->run($request); $this->assertSame($responseProphecy->reveal(), $response); } public function testGroup() { $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $callableResolverProphecy = $this->prophesize(CallableResolverInterface::class); $pattern = '/group'; $callable = function () { }; $routeGroupProphecy = $this->prophesize(RouteGroupInterface::class); $routeCollectorProphecy = $this->prophesize(RouteCollectorInterface::class); $routeCollectorProphecy ->group($pattern, Argument::is($callable)) ->willReturn($routeGroupProphecy->reveal()) ->shouldBeCalledOnce(); $routeCollectorProxy = new RouteCollectorProxy( $responseFactoryProphecy->reveal(), $callableResolverProphecy->reveal(), null, $routeCollectorProphecy->reveal() ); $routeCollectorProxy->group($pattern, $callable); } } ================================================ FILE: tests/Routing/RouteCollectorTest.php ================================================ cacheFile && file_exists($this->cacheFile)) { unlink($this->cacheFile); } } public function testGetSetBasePath() { $basePath = '/app'; $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $callableResolverProphecy = $this->prophesize(CallableResolverInterface::class); $routeCollector = new RouteCollector($responseFactoryProphecy->reveal(), $callableResolverProphecy->reveal()); $routeCollector->setBasePath($basePath); $this->assertSame($basePath, $routeCollector->getBasePath()); } public function testMap() { $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $callableResolverProphecy = $this->prophesize(CallableResolverInterface::class); $routeCollector = new RouteCollector($responseFactoryProphecy->reveal(), $callableResolverProphecy->reveal()); $route = $routeCollector->map(['GET'], '/', function () { }); $routes = $routeCollector->getRoutes(); $this->assertSame($route, $routes[$route->getIdentifier()]); } public function testMapPrependsGroupPattern() { $self = $this; $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $callable = function (RouteCollectorProxy $proxy) use ($self) { $route = $proxy->get('/test', function () { }); $self->assertSame('/prefix/test', $route->getPattern()); }; $callableResolverProphecy = $this->prophesize(CallableResolverInterface::class); $callableResolverProphecy ->resolve(Argument::is($callable)) ->willReturn($callable) ->shouldBeCalledOnce(); $routeCollector = new RouteCollector($responseFactoryProphecy->reveal(), $callableResolverProphecy->reveal()); $routeCollector->group('/prefix', $callable); } public function testGetRouteInvocationStrategy() { $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $callableResolverProphecy = $this->prophesize(CallableResolverInterface::class); $containerProphecy = $this->prophesize(ContainerInterface::class); $invocationStrategyProphecy = $this->prophesize(InvocationStrategyInterface::class); $routeCollector = new RouteCollector( $responseFactoryProphecy->reveal(), $callableResolverProphecy->reveal(), $containerProphecy->reveal(), $invocationStrategyProphecy->reveal() ); $this->assertSame($invocationStrategyProphecy->reveal(), $routeCollector->getDefaultInvocationStrategy()); } public function testRemoveNamedRoute() { $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $callableResolverProphecy = $this->prophesize(CallableResolverInterface::class); $routeCollector = new RouteCollector($responseFactoryProphecy->reveal(), $callableResolverProphecy->reveal()); $routeCollector->setBasePath('/base/path'); $route = $routeCollector->map(['GET'], '/test', function () { }); $route->setName('test'); $routes = $routeCollector->getRoutes(); $this->assertCount(1, $routes); $routeCollector->removeNamedRoute('test'); $routes = $routeCollector->getRoutes(); $this->assertCount(0, $routes); } public function testRemoveNamedRouteWithARouteThatDoesNotExist() { $this->expectException(RuntimeException::class); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $callableResolverProphecy = $this->prophesize(CallableResolverInterface::class); $routeCollector = new RouteCollector($responseFactoryProphecy->reveal(), $callableResolverProphecy->reveal()); $routeCollector->removeNamedRoute('missing'); } public function testLookupRouteThrowsExceptionIfRouteNotFound() { $this->expectException(RuntimeException::class); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $callableResolverProphecy = $this->prophesize(CallableResolverInterface::class); $routeCollector = new RouteCollector($responseFactoryProphecy->reveal(), $callableResolverProphecy->reveal()); $routeCollector->lookupRoute('missing'); } /** * Test cache file exists but is not writable */ public function testCacheFileExistsAndIsNotReadable() { $this->cacheFile = __DIR__ . '/non-readable.cache'; file_put_contents($this->cacheFile, ''); $this->expectException(RuntimeException::class); $this->expectExceptionMessage(sprintf('Route collector cache file `%s` is not readable', $this->cacheFile)); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $callableResolverProphecy = $this->prophesize(CallableResolverInterface::class); $routeCollector = new RouteCollector($responseFactoryProphecy->reveal(), $callableResolverProphecy->reveal()); $routeCollector->setCacheFile($this->cacheFile); } /** * Test cache file does not exist and directory is not writable */ public function testCacheFileDoesNotExistsAndDirectoryIsNotWritable() { $cacheFile = __DIR__ . '/non-writable-directory/router.cache'; $this->expectException(RuntimeException::class); $this->expectExceptionMessage(sprintf( 'Route collector cache file directory `%s` is not writable', dirname($cacheFile) )); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $callableResolverProphecy = $this->prophesize(CallableResolverInterface::class); $routeCollector = new RouteCollector($responseFactoryProphecy->reveal(), $callableResolverProphecy->reveal()); $routeCollector->setCacheFile($cacheFile); } public function testSetCacheFileViaConstructor() { $cacheFile = __DIR__ . '/router.cache'; $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $callableResolverProphecy = $this->prophesize(CallableResolverInterface::class); $routeCollector = new RouteCollector( $responseFactoryProphecy->reveal(), $callableResolverProphecy->reveal(), null, null, null, $cacheFile ); $this->assertSame($cacheFile, $routeCollector->getCacheFile()); } } ================================================ FILE: tests/Routing/RouteContextTest.php ================================================ createMock(RouteInterface::class); $routeParser = $this->createMock(RouteParserInterface::class); $routingResults = $this->createMock(RoutingResults::class); $serverRequest = $this->createServerRequest('/') ->withAttribute(RouteContext::BASE_PATH, '') ->withAttribute(RouteContext::ROUTE, $route) ->withAttribute(RouteContext::ROUTE_PARSER, $routeParser) ->withAttribute(RouteContext::ROUTING_RESULTS, $routingResults); $routeContext = RouteContext::fromRequest($serverRequest); $this->assertSame($route, $routeContext->getRoute()); $this->assertSame($routeParser, $routeContext->getRouteParser()); $this->assertSame($routingResults, $routeContext->getRoutingResults()); $this->assertSame('', $routeContext->getBasePath()); } public function testCanCreateInstanceWithoutRoute(): void { $serverRequest = $this->createServerRequestWithRouteAttributes(); // Route attribute is not required $serverRequest = $serverRequest->withoutAttribute(RouteContext::ROUTE); $routeContext = RouteContext::fromRequest($serverRequest); $this->assertNull($routeContext->getRoute()); $this->assertNotNull($routeContext->getRouteParser()); $this->assertNotNull($routeContext->getRoutingResults()); $this->assertNotNull($routeContext->getBasePath()); } public function testCanCreateInstanceWithoutBasePathAndThrowExceptionIfGetBasePathIsCalled(): void { $serverRequest = $this->createServerRequestWithRouteAttributes(); // Route attribute is not required $serverRequest = $serverRequest->withoutAttribute(RouteContext::BASE_PATH); $routeContext = RouteContext::fromRequest($serverRequest); $this->assertNotNull($routeContext->getRoute()); $this->assertNotNull($routeContext->getRouteParser()); $this->assertNotNull($routeContext->getRoutingResults()); $this->expectException(RuntimeException::class); $routeContext->getBasePath(); } public static function requiredRouteContextRequestAttributes(): array { return [ [RouteContext::ROUTE_PARSER], [RouteContext::ROUTING_RESULTS], ]; } /** * @dataProvider requiredRouteContextRequestAttributes * @param string $attribute */ #[\PHPUnit\Framework\Attributes\DataProvider('requiredRouteContextRequestAttributes')] public function testCannotCreateInstanceIfRequestIsMissingAttributes(string $attribute): void { $this->expectException(RuntimeException::class); $serverRequest = $this->createServerRequestWithRouteAttributes()->withoutAttribute($attribute); RouteContext::fromRequest($serverRequest); } private function createServerRequestWithRouteAttributes(): ServerRequestInterface { $route = $this->createMock(RouteInterface::class); $routeParser = $this->createMock(RouteParserInterface::class); $routingResults = $this->createMock(RoutingResults::class); return $this->createServerRequest('/') ->withAttribute(RouteContext::BASE_PATH, '') ->withAttribute(RouteContext::ROUTE, $route) ->withAttribute(RouteContext::ROUTE_PARSER, $routeParser) ->withAttribute(RouteContext::ROUTING_RESULTS, $routingResults); } } ================================================ FILE: tests/Routing/RouteParserTest.php ================================================ [ true, '/{first}/{second}', ['first' => 'hello', 'second' => 'world'], [], '/app/hello/world', ], 'without base path' => [ false, '/{first}/{second}', ['first' => 'hello', 'second' => 'world'], [], '/hello/world', ], 'with query parameters' => [ false, '/{first}/{second}', ['first' => 'hello', 'second' => 'world'], ['a' => 'b', 'c' => 'd'], '/hello/world?a=b&c=d', ], 'with query parameters containing array with string keys' => [ false, '/{first}/{second}', ['first' => 'hello', 'second' => 'world'], ['a' => ['k' => '1', 'f' => 'x'], 'b', 'c' => 'd'], '/hello/world?a%5Bk%5D=1&a%5Bf%5D=x&0=b&c=d', ], 'with query parameters containing array with numeric keys' => [ false, '/{first}/{second}', ['first' => 'hello', 'second' => 'world'], ['a' => ['b', 'x', 'y'], 'c' => 'd'], '/hello/world?a%5B0%5D=b&a%5B1%5D=x&a%5B2%5D=y&c=d', ], 'with argument without optional parameter' => [ false, '/archive/{year}[/{month:[\d:{2}]}[/d/{day}]]', ['year' => '2015'], [], '/archive/2015', ], 'with argument and optional parameter' => [ false, '/archive/{year}[/{month:[\d:{2}]}[/d/{day}]]', ['year' => '2015', 'month' => '07'], [], '/archive/2015/07', ], 'with argument and optional parameters' => [ false, '/archive/{year}[/{month:[\d:{2}]}[/d/{day}]]', ['year' => '2015', 'month' => '07', 'day' => '19'], [], '/archive/2015/07/d/19', ], ]; } public function testRelativePathForWithNoBasePath() { $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $callableResolverProphecy = $this->prophesize(CallableResolverInterface::class); $routeCollector = new RouteCollector($responseFactoryProphecy->reveal(), $callableResolverProphecy->reveal()); $route = $routeCollector->map(['GET'], '/{first}/{second}', function () { }); $route->setName('test'); $routeParser = $routeCollector->getRouteParser(); $results = $routeParser->relativeUrlFor('test', ['first' => 'hello', 'second' => 'world']); $this->assertSame('/hello/world', $results); } public function testBasePathIsIgnoreInRelativePathFor() { $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $callableResolverProphecy = $this->prophesize(CallableResolverInterface::class); $routeCollector = new RouteCollector($responseFactoryProphecy->reveal(), $callableResolverProphecy->reveal()); $routeCollector->setBasePath('/app'); $route = $routeCollector->map(['GET'], '/{first}/{second}', function () { }); $route->setName('test'); $routeParser = $routeCollector->getRouteParser(); $results = $routeParser->relativeUrlFor('test', ['first' => 'hello', 'second' => 'world']); $this->assertSame('/hello/world', $results); } /** * @dataProvider urlForCases * @param $withBasePath * @param $pattern * @param $arguments * @param $queryParams * @param $expectedResult */ #[\PHPUnit\Framework\Attributes\DataProvider('urlForCases')] public function testUrlForWithBasePath($withBasePath, $pattern, $arguments, $queryParams, $expectedResult) { $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $callableResolverProphecy = $this->prophesize(CallableResolverInterface::class); $routeCollector = new RouteCollector($responseFactoryProphecy->reveal(), $callableResolverProphecy->reveal()); if ($withBasePath) { $routeCollector->setBasePath('/app'); } $route = $routeCollector->map(['GET'], $pattern, function () { }); $route->setName('test'); $routeParser = $routeCollector->getRouteParser(); $results = $routeParser->urlFor('test', $arguments, $queryParams); $this->assertSame($expectedResult, $results); } public function testUrlForWithMissingSegmentData() { $this->expectException(InvalidArgumentException::class); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $callableResolverProphecy = $this->prophesize(CallableResolverInterface::class); $routeCollector = new RouteCollector($responseFactoryProphecy->reveal(), $callableResolverProphecy->reveal()); $route = $routeCollector->map(['GET'], '/{first}/{last}', function () { }); $route->setName('test'); $routeParser = $routeCollector->getRouteParser(); $routeParser->urlFor('test', ['last' => 'world']); } public function testUrlForRouteThatDoesNotExist() { $this->expectException(RuntimeException::class); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $callableResolverProphecy = $this->prophesize(CallableResolverInterface::class); $routeCollector = new RouteCollector($responseFactoryProphecy->reveal(), $callableResolverProphecy->reveal()); $routeParser = $routeCollector->getRouteParser(); $routeParser->urlFor('test'); } public function testFullUrlFor() { $uriProphecy = $this->prophesize(UriInterface::class); $uriProphecy ->getScheme() ->willReturn('http') ->shouldBeCalledOnce(); $uriProphecy ->getAuthority() ->willReturn('example.com:8080') ->shouldBeCalledOnce(); $routeProphecy = $this->prophesize(RouteInterface::class); $routeProphecy ->getPattern() ->willReturn('/{token}') ->shouldBeCalledOnce(); $routeCollectorProphecy = $this->prophesize(RouteCollectorInterface::class); $routeCollectorProphecy ->getBasePath() ->willReturn('/app') ->shouldBeCalledOnce(); $routeCollectorProphecy ->getNamedRoute('test') ->willReturn($routeProphecy->reveal()) ->shouldBeCalledOnce(); $routeParser = new RouteParser($routeCollectorProphecy->reveal()); $result = $routeParser->fullUrlFor($uriProphecy->reveal(), 'test', ['token' => '123']); $expectedResult = 'http://example.com:8080/app/123'; $this->assertSame($expectedResult, $result); } } ================================================ FILE: tests/Routing/RouteResolverTest.php ================================================ prophesize(RouteCollectorInterface::class); $routingResultsProphecy = $this->prophesize(RoutingResults::class); $dispatcherProphecy = $this->prophesize(DispatcherInterface::class); $dispatcherProphecy ->dispatch(Argument::type('string'), Argument::type('string')) ->will(function ($args) use ($routingResultsProphecy, $expectedUri) { if ($args[1] !== $expectedUri) { throw new Error(sprintf( "URI transformation failed.\n Received: '%s'\n Expected: '%s'", $args[1], $expectedUri )); } return $routingResultsProphecy->reveal(); }) ->shouldBeCalledOnce(); $routeResolver = new RouteResolver( $routeCollectorProphecy->reveal(), $dispatcherProphecy->reveal() ); $routeResolver->computeRoutingResults($uri, $method); } public function testResolveRoute() { $identifier = 'test'; $routeProphecy = $this->prophesize(RouteInterface::class); $dispatcherProphecy = $this->prophesize(DispatcherInterface::class); $routeCollectorProphecy = $this->prophesize(RouteCollectorInterface::class); $routeCollectorProphecy ->lookupRoute($identifier) ->willReturn($routeProphecy->reveal()) ->shouldBeCalledOnce(); $routeResolver = new RouteResolver( $routeCollectorProphecy->reveal(), $dispatcherProphecy->reveal() ); $routeResolver->resolveRoute($identifier); } } ================================================ FILE: tests/Routing/RouteRunnerTest.php ================================================ getAttribute(RouteContext::ROUTE_PARSER); $this->assertInstanceOf(RouteParser::class, $routeParser); $routingResults = $request->getAttribute(RouteContext::ROUTING_RESULTS); $this->assertInstanceOf(RoutingResults::class, $routingResults); return $response; })->bindTo($this); $callableResolver = $this->getCallableResolver(); $responseFactory = $this->getResponseFactory(); $routeCollector = new RouteCollector($responseFactory, $callableResolver); $routeCollector->map(['GET'], '/hello/{name}', $handler); $routeParser = new RouteParser($routeCollector); $routeResolver = new RouteResolver($routeCollector); $request = $this->createServerRequest('https://example.com:443/hello/foo', 'GET'); $dispatcher = new RouteRunner($routeResolver, $routeParser); $middlewareDispatcher = new MiddlewareDispatcher($dispatcher, $callableResolver); $middlewareDispatcher->handle($request); } } ================================================ FILE: tests/Routing/RouteTest.php ================================================ prophesize(CallableResolverInterface::class); $callableResolverProphecy ->resolve(Argument::is($callable)) ->willReturn($callable); $callableResolverProphecy ->resolve(MockMiddlewareWithoutConstructor::class) ->will(function ($args) { return [new MockMiddlewareWithoutConstructor(), 'process']; }); $streamProphecy = $this->prophesize(StreamInterface::class); $value = ''; $streamProphecy ->write(Argument::type('string')) ->will(function ($args) use ($value) { $value .= $args[0]; $this->__toString()->willReturn($value); return strlen($value); }); $streamProphecy ->__toString() ->willReturn($value); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy ->getBody() ->willReturn($streamProphecy->reveal()); $responseProphecy ->withStatus(Argument::type('integer')) ->will(function ($args) { $this->getStatusCode()->willReturn($args[0]); return $this->reveal(); }); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy ->createResponse() ->willReturn($responseProphecy->reveal()); $methods = is_string($methods) ? [$methods] : $methods; return new Route( $methods, $pattern, $callable, $responseFactoryProphecy->reveal(), $callableResolverProphecy->reveal() ); } public function testConstructor() { $methods = ['GET', 'POST']; $pattern = '/hello/{name}'; $callable = function ($request, $response, $args) { return $response; }; $route = $this->createRoute($methods, $pattern, $callable); $this->assertSame($methods, $route->getMethods()); $this->assertSame($pattern, $route->getPattern()); $this->assertSame($callable, $route->getCallable()); } public function testGetMethodsReturnsArrayWhenConstructedWithString() { $route = $this->createRoute(); $this->assertSame(['GET'], $route->getMethods()); } public function testGetMethods() { $methods = ['GET', 'POST']; $route = $this->createRoute($methods); $this->assertSame($methods, $route->getMethods()); } public function testGetPattern() { $route = $this->createRoute(); $this->assertSame('/', $route->getPattern()); } public function testGetCallable() { $route = $this->createRoute(); $this->assertTrue(is_callable($route->getCallable())); } public function testGetCallableResolver() { $callable = function (ServerRequestInterface $request, ResponseInterface $response) { return $response; }; $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $callableResolverProphecy = $this->prophesize(CallableResolverInterface::class); $route = new Route( ['GET'], '/', $callable, $responseFactoryProphecy->reveal(), $callableResolverProphecy->reveal() ); $this->assertSame($callableResolverProphecy->reveal(), $route->getCallableResolver()); } public function testGetInvocationStrategy() { $callable = function (ServerRequestInterface $request, ResponseInterface $response) { return $response; }; $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $callableResolverProphecy = $this->prophesize(CallableResolverInterface::class); $containerProphecy = $this->prophesize(ContainerInterface::class); $strategyProphecy = $this->prophesize(InvocationStrategyInterface::class); $route = new Route( ['GET'], '/', $callable, $responseFactoryProphecy->reveal(), $callableResolverProphecy->reveal(), $containerProphecy->reveal(), $strategyProphecy->reveal() ); $this->assertSame($strategyProphecy->reveal(), $route->getInvocationStrategy()); } public function testSetInvocationStrategy() { $callable = function (ServerRequestInterface $request, ResponseInterface $response) { return $response; }; $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $callableResolverProphecy = $this->prophesize(CallableResolverInterface::class); $strategyProphecy = $this->prophesize(InvocationStrategyInterface::class); $route = new Route( ['GET'], '/', $callable, $responseFactoryProphecy->reveal(), $callableResolverProphecy->reveal() ); $route->setInvocationStrategy($strategyProphecy->reveal()); $this->assertSame($strategyProphecy->reveal(), $route->getInvocationStrategy()); } public function testGetGroups() { $callable = function (ServerRequestInterface $request, ResponseInterface $response) { return $response; }; $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $callableResolverProphecy = $this->prophesize(CallableResolverInterface::class); $strategyProphecy = $this->prophesize(InvocationStrategyInterface::class); $routeCollectorProxyProphecy = $this->prophesize(RouteCollectorProxyInterface::class); $routeGroup = new RouteGroup( '/group', $callable, $callableResolverProphecy->reveal(), $routeCollectorProxyProphecy->reveal() ); $groups = [$routeGroup]; $route = new Route( ['GET'], '/', $callable, $responseFactoryProphecy->reveal(), $callableResolverProphecy->reveal(), null, $strategyProphecy->reveal(), $groups ); $this->assertSame($groups, $route->getGroups()); } public function testArgumentSetting() { $route = $this->createRoute(); $route->setArguments(['foo' => 'FOO', 'bar' => 'BAR']); $this->assertSame($route->getArguments(), ['foo' => 'FOO', 'bar' => 'BAR']); $route->setArgument('bar', 'bar'); $this->assertSame($route->getArguments(), ['foo' => 'FOO', 'bar' => 'bar']); $route->setArgument('baz', 'BAZ'); $this->assertSame($route->getArguments(), ['foo' => 'FOO', 'bar' => 'bar', 'baz' => 'BAZ']); $route->setArguments(['a' => 'b']); $this->assertSame($route->getArguments(), ['a' => 'b']); $this->assertSame($route->getArgument('a', 'default'), 'b'); $this->assertSame($route->getArgument('b', 'default'), 'default'); } public function testAddMiddleware() { $route = $this->createRoute(); $called = 0; $mw = function (ServerRequestInterface $request, RequestHandlerInterface $handler) use (&$called) { $called++; return $handler->handle($request); }; $route->add($mw); $request = $this->createServerRequest('/'); $route->run($request); $this->assertSame($called, 1); } public function testAddMiddlewareOnGroup() { $callable = function (ServerRequestInterface $request, ResponseInterface $response) { return $response; }; $callableResolverProphecy = $this->prophesize(CallableResolverInterface::class); $callableResolverProphecy ->resolve(Argument::is($callable)) ->willReturn($callable) ->shouldBeCalledOnce(); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy ->createResponse() ->willReturn($responseProphecy->reveal()) ->shouldBeCalledOnce(); $routeCollectorProxyProphecy = $this->prophesize(RouteCollectorProxyInterface::class); $strategy = new RequestResponse(); $called = 0; $mw = function (ServerRequestInterface $request, RequestHandlerInterface $handler) use (&$called) { $called++; return $handler->handle($request); }; $routeGroup = new RouteGroup( '/group', $callable, $callableResolverProphecy->reveal(), $routeCollectorProxyProphecy->reveal() ); $routeGroup->add($mw); $groups = [$routeGroup]; $route = new Route( ['GET'], '/', $callable, $responseFactoryProphecy->reveal(), $callableResolverProphecy->reveal(), null, $strategy, $groups ); $request = $this->createServerRequest('/'); $route->run($request); $this->assertSame($called, 1); } public function testAddClosureMiddleware() { $route = $this->createRoute(); $called = 0; $route->add(function (ServerRequestInterface $request, RequestHandlerInterface $handler) use (&$called) { $called++; return $handler->handle($request); }); $request = $this->createServerRequest('/'); $route->run($request); $this->assertSame($called, 1); } public function testAddMiddlewareUsingDeferredResolution() { $route = $this->createRoute(); $route->add(MockMiddlewareWithoutConstructor::class); $output = ''; $appendToOutput = function (string $value) use (&$output) { $output .= $value; }; $request = $this->createServerRequest('/')->withAttribute('appendToOutput', $appendToOutput); $route->run($request); $this->assertSame('Hello World', $output); } public function testAddMiddlewareAsStringNotImplementingInterfaceThrowsException() { $this->expectException(RuntimeException::class); $this->expectExceptionMessage( 'A middleware must be an object/class name referencing an implementation of ' . 'MiddlewareInterface or a callable with a matching signature.' ); $route = $this->createRoute(); $route->add(new MockMiddlewareWithoutInterface()); } public function testIdentifier() { $route = $this->createRoute(); $this->assertSame('route0', $route->getIdentifier()); } public function testSetName() { $route = $this->createRoute(); $this->assertSame($route, $route->setName('foo')); $this->assertSame('foo', $route->getName()); } public function testControllerMethodAsStringResolvesWithoutContainer() { $callableResolver = new CallableResolver(); $responseFactory = $this->getResponseFactory(); $deferred = $callableResolver->resolve('\Slim\Tests\Mocks\CallableTest:toCall'); $route = new Route(['GET'], '/', $deferred, $responseFactory, $callableResolver); CallableTest::$CalledCount = 0; $request = $this->createServerRequest('/'); $response = $route->run($request); $this->assertInstanceOf(ResponseInterface::class, $response); $this->assertSame(1, CallableTest::$CalledCount); } public function testControllerMethodAsStringResolvesWithContainer() { $self = $this; $responseProphecy = $this->prophesize(ResponseInterface::class); $callableResolverProphecy = $this->prophesize(CallableResolverInterface::class); $callable = 'callable'; $callableResolverProphecy ->resolve(Argument::is($callable)) ->willReturn(function ( ServerRequestInterface $request, ResponseInterface $response ) use ( $self, $responseProphecy ) { $self->assertSame($responseProphecy->reveal(), $response); return $response; }) ->shouldBeCalledOnce(); $deferred = $callableResolverProphecy->reveal()->resolve($callable); $callableResolverProphecy ->resolve(Argument::is($deferred)) ->willReturn($deferred) ->shouldBeCalledOnce(); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy ->createResponse() ->willReturn($responseProphecy->reveal()); $route = new Route( ['GET'], '/', $deferred, $responseFactoryProphecy->reveal(), $callableResolverProphecy->reveal() ); $request = $this->createServerRequest('/'); $response = $route->run($request); $this->assertInstanceOf(ResponseInterface::class, $response); } /** * Ensure that the response returned by a route callable is the response * object that is returned by __invoke(). */ public function testProcessWhenReturningAResponse() { $callable = function (ServerRequestInterface $request, ResponseInterface $response) { $response->getBody()->write('foo'); return $response; }; $route = $this->createRoute(['GET'], '/', $callable); $request = $this->createServerRequest('/'); $response = $route->run($request); $this->assertSame('foo', (string) $response->getBody()); } /** * Ensure that anything echo'd in a route callable is, by default, NOT * added to the response object body. */ public function testRouteCallableDoesNotAppendEchoedOutput() { $callable = function (ServerRequestInterface $request, ResponseInterface $response) { echo "foo"; return $response->withStatus(201); }; $route = $this->createRoute(['GET'], '/', $callable); $request = $this->createServerRequest('/'); // We capture output buffer here only to clean test CLI output ob_start(); $response = $route->run($request); ob_end_clean(); // Output buffer is ignored without optional middleware $this->assertSame('', (string) $response->getBody()); $this->assertSame(201, $response->getStatusCode()); } /** * Ensure that if a string is returned by a route callable, then it is * added to the response object that is returned by __invoke(). */ public function testRouteCallableAppendsCorrectOutputToResponse() { $callable = function (ServerRequestInterface $request, ResponseInterface $response) { $response->getBody()->write('foo'); return $response; }; $route = $this->createRoute(['GET'], '/', $callable); $request = $this->createServerRequest('/'); $response = $route->run($request); $this->assertSame('foo', (string) $response->getBody()); } public function testInvokeWithException() { $this->expectException(Exception::class); $callable = function (ServerRequestInterface $request, ResponseInterface $response) { throw new Exception(); }; $route = $this->createRoute(['GET'], '/', $callable); $request = $this->createServerRequest('/'); $route->run($request); } /** * Ensure that `foundHandler` is called on actual callable */ public function testInvokeDeferredCallableWithNoContainer() { $responseProphecy = $this->prophesize(ResponseInterface::class); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy ->createResponse() ->willReturn($responseProphecy->reveal()) ->shouldBeCalledOnce(); $callableResolver = new CallableResolver(); $invocationStrategy = new InvocationStrategyTest(); $deferred = '\Slim\Tests\Mocks\CallableTest:toCall'; $route = new Route( ['GET'], '/', $deferred, $responseFactoryProphecy->reveal(), $callableResolver, null, $invocationStrategy ); $request = $this->createServerRequest('/'); $response = $route->run($request); $this->assertInstanceOf(ResponseInterface::class, $response); $this->assertEquals([new CallableTest(), 'toCall'], InvocationStrategyTest::$LastCalledFor); } /** * Ensure that `foundHandler` is called on actual callable */ public function testInvokeDeferredCallableWithContainer() { $responseProphecy = $this->prophesize(ResponseInterface::class); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy ->createResponse() ->willReturn($responseProphecy->reveal()) ->shouldBeCalledOnce(); $containerProphecy = $this->prophesize(ContainerInterface::class); $containerProphecy->has('\Slim\Tests\Mocks\CallableTest')->willReturn(true); $containerProphecy->get('\Slim\Tests\Mocks\CallableTest')->willReturn(new CallableTest()); $callableResolver = new CallableResolver($containerProphecy->reveal()); $strategy = new InvocationStrategyTest(); $deferred = '\Slim\Tests\Mocks\CallableTest:toCall'; $route = new Route( ['GET'], '/', $deferred, $responseFactoryProphecy->reveal(), $callableResolver, $containerProphecy->reveal(), $strategy ); $request = $this->createServerRequest('/'); $response = $route->run($request); $this->assertInstanceOf(ResponseInterface::class, $response); $this->assertEquals([new CallableTest(), 'toCall'], InvocationStrategyTest::$LastCalledFor); } public function testInvokeUsesRequestHandlerStrategyForRequestHandlers() { $responseProphecy = $this->prophesize(ResponseInterface::class); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy ->createResponse() ->willReturn($responseProphecy->reveal()) ->shouldBeCalledOnce(); $containerProphecy = $this->prophesize(ContainerInterface::class); $containerProphecy->has(RequestHandlerTest::class)->willReturn(true); $containerProphecy->get(RequestHandlerTest::class)->willReturn(new RequestHandlerTest()); $callableResolver = new CallableResolver($containerProphecy->reveal()); $deferred = RequestHandlerTest::class; $route = new Route( ['GET'], '/', $deferred, $responseFactoryProphecy->reveal(), $callableResolver, $containerProphecy->reveal() ); $request = $this->createServerRequest('/', 'GET'); $route->run($request); /** @var InvocationStrategyInterface $strategy */ $strategy = $containerProphecy ->reveal() ->get(RequestHandlerTest::class)::$strategy; $this->assertSame(RequestHandler::class, $strategy); } public function testInvokeUsesUserSetStrategyForRequestHandlers() { $responseProphecy = $this->prophesize(ResponseInterface::class); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy ->createResponse() ->willReturn($responseProphecy->reveal()) ->shouldBeCalledOnce(); $containerProphecy = $this->prophesize(ContainerInterface::class); $containerProphecy->has(RequestHandlerTest::class)->willReturn(true); $containerProphecy->get(RequestHandlerTest::class)->willReturn(new RequestHandlerTest()); $callableResolver = new CallableResolver($containerProphecy->reveal()); $deferred = RequestHandlerTest::class; $route = new Route( ['GET'], '/', $deferred, $responseFactoryProphecy->reveal(), $callableResolver, $containerProphecy->reveal() ); $strategy = new MockCustomRequestHandlerInvocationStrategy(); $route->setInvocationStrategy($strategy); $request = $this->createServerRequest('/', 'GET'); $route->run($request); $this->assertSame(1, $strategy::$CalledCount); } public function testRequestHandlerStrategyAppendsRouteArgumentsAsAttributesToRequest() { $self = $this; $responseProphecy = $this->prophesize(ResponseInterface::class); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy ->createResponse() ->willReturn($responseProphecy->reveal()) ->shouldBeCalledOnce(); $containerProphecy = $this->prophesize(ContainerInterface::class); $containerProphecy->has(RequestHandlerTest::class)->willReturn(true); $containerProphecy->get(RequestHandlerTest::class)->willReturn(new RequestHandlerTest()); $callableResolver = new CallableResolver($containerProphecy->reveal()); $deferred = RequestHandlerTest::class; $route = new Route( ['GET'], '/', $deferred, $responseFactoryProphecy->reveal(), $callableResolver, $containerProphecy->reveal() ); $strategy = new RequestHandler(true); $route->setInvocationStrategy($strategy); $route->setArguments(['id' => 1]); $requestProphecy = $this->prophesize(ServerRequestInterface::class); $requestProphecy->withAttribute(Argument::type('string'), Argument::any())->will(function ($args) use ($self) { $name = $args[0]; $value = $args[1]; $self->assertSame('id', $name); $self->assertSame(1, $value); return $this; })->shouldBeCalledOnce(); $route->run($requestProphecy->reveal()); } /** * Ensure that the pattern can be dynamically changed */ public function testPatternCanBeChanged() { $route = $this->createRoute(); $route->setPattern('/hola/{nombre}'); $this->assertSame('/hola/{nombre}', $route->getPattern()); } /** * Ensure that the callable can be changed */ public function testChangingCallableWithNoContainer() { $responseProphecy = $this->prophesize(ResponseInterface::class); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy ->createResponse() ->willReturn($responseProphecy->reveal()) ->shouldBeCalledOnce(); $callableResolver = new CallableResolver(); $strategy = new InvocationStrategyTest(); $deferred = 'NonExistent:toCall'; $route = new Route( ['GET'], '/', $deferred, $responseFactoryProphecy->reveal(), $callableResolver, null, $strategy ); $route->setCallable('\Slim\Tests\Mocks\CallableTest:toCall'); //Then we fix it here. $request = $this->createServerRequest('/'); $response = $route->run($request); $this->assertInstanceOf(ResponseInterface::class, $response); $this->assertEquals([new CallableTest(), 'toCall'], InvocationStrategyTest::$LastCalledFor); } /** * Ensure that the callable can be changed */ public function testChangingCallableWithContainer() { $responseProphecy = $this->prophesize(ResponseInterface::class); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy ->createResponse() ->willReturn($responseProphecy->reveal()) ->shouldBeCalledOnce(); $containerProphecy = $this->prophesize(ContainerInterface::class); $containerProphecy->has('CallableTest2')->willReturn(true); $containerProphecy->get('CallableTest2')->willReturn(new CallableTest()); $callableResolver = new CallableResolver($containerProphecy->reveal()); $strategy = new InvocationStrategyTest(); $deferred = 'NonExistent:toCall'; $route = new Route( ['GET'], '/', $deferred, $responseFactoryProphecy->reveal(), $callableResolver, $containerProphecy->reveal(), $strategy ); $route->setCallable('CallableTest2:toCall'); //Then we fix it here. $request = $this->createServerRequest('/'); $response = $route->run($request); $this->assertInstanceOf(ResponseInterface::class, $response); $this->assertSame( [$containerProphecy->reveal()->get('CallableTest2'), 'toCall'], InvocationStrategyTest::$LastCalledFor ); } public function testRouteCallableIsResolvedUsingContainerWhenCallableResolverIsPresent() { $streamProphecy = $this->prophesize(StreamInterface::class); $value = ''; $streamProphecy ->write(Argument::type('string')) ->will(function ($args) use ($value) { $value .= $args[0]; $this->__toString()->willReturn($value); return strlen($value); }); $streamProphecy ->__toString() ->willReturn($value); $responseProphecy = $this->prophesize(ResponseInterface::class); $responseProphecy ->getBody() ->willReturn($streamProphecy->reveal()) ->shouldBeCalledTimes(2); $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); $responseFactoryProphecy ->createResponse() ->willReturn($responseProphecy->reveal()) ->shouldBeCalledOnce(); $containerProphecy = $this->prophesize(ContainerInterface::class); $containerProphecy->has('CallableTest3')->willReturn(true); $containerProphecy->get('CallableTest3')->willReturn(new CallableTest()); $containerProphecy->has('ClosureMiddleware')->willReturn(true); $containerProphecy->get('ClosureMiddleware')->willReturn(function () use ($responseFactoryProphecy) { $response = $responseFactoryProphecy->reveal()->createResponse(); $response->getBody()->write('Hello'); return $response; }); $callableResolver = new CallableResolver($containerProphecy->reveal()); $strategy = new InvocationStrategyTest(); $deferred = 'CallableTest3'; $route = new Route( ['GET'], '/', $deferred, $responseFactoryProphecy->reveal(), $callableResolver, $containerProphecy->reveal(), $strategy ); $route->add('ClosureMiddleware'); $request = $this->createServerRequest('/'); $response = $route->run($request); $this->assertSame('Hello', (string) $response->getBody()); } } ================================================ FILE: tests/TestCase.php ================================================ getServerRequestFactory(); } /** * @return ResponseFactoryInterface */ protected function getResponseFactory(): ResponseFactoryInterface { $psr7ObjectProvider = new PSR7ObjectProvider(); return $psr7ObjectProvider->getResponseFactory(); } /** * @return StreamFactoryInterface */ protected function getStreamFactory(): StreamFactoryInterface { $psr7ObjectProvider = new PSR7ObjectProvider(); return $psr7ObjectProvider->getStreamFactory(); } /** * @param ContainerInterface|null $container * * @return CallableResolverInterface */ protected function getCallableResolver(?ContainerInterface $container = null): CallableResolverInterface { return new CallableResolver($container); } /** * @param RequestHandlerInterface $requestHandler * @param ContainerInterface|null $container * @param CallableResolverInterface|null $callableResolver * * @return MiddlewareDispatcher */ protected function createMiddlewareDispatcher( RequestHandlerInterface $requestHandler, ?ContainerInterface $container = null, ?CallableResolverInterface $callableResolver = null ): MiddlewareDispatcher { return new MiddlewareDispatcher( $requestHandler, $callableResolver ?? $this->getCallableResolver($container), $container ); } /** * @param string $uri * @param string $method * @param array $data * @return ServerRequestInterface */ protected function createServerRequest( string $uri, string $method = 'GET', array $data = [] ): ServerRequestInterface { $psr7ObjectProvider = new PSR7ObjectProvider(); return $psr7ObjectProvider->createServerRequest($uri, $method, $data); } /** * @param int $statusCode * @param string $reasonPhrase * @return ResponseInterface */ protected function createResponse(int $statusCode = 200, string $reasonPhrase = ''): ResponseInterface { $psr7ObjectProvider = new PSR7ObjectProvider(); return $psr7ObjectProvider->createResponse($statusCode, $reasonPhrase); } /** * @param string $contents * @return StreamInterface */ protected function createStream(string $contents = ''): StreamInterface { $psr7ObjectProvider = new PSR7ObjectProvider(); return $psr7ObjectProvider->createStream($contents); } /** * @param ReflectionProperty|ReflectionMethod $ref * @return void */ protected function setAccessible($ref, bool $accessible = true): void { if (PHP_VERSION_ID < 80100) { $ref->setAccessible($accessible); } } } ================================================ FILE: tests/bootstrap.php ================================================ [ 'connection_status' => function (): int { if (isset($GLOBALS['connection_status_return'])) { return $GLOBALS['connection_status_return']; } return connection_status(); }, 'header' => function (string $string, bool $replace = true, ?int $statusCode = null): void { HeaderStack::push( [ 'header' => $string, 'replace' => $replace, 'status_code' => $statusCode, ] ); }, 'headers_sent' => function (): bool { return false; } ], RouteCollector::class => [ 'is_readable' => function (string $file): bool { return stripos($file, 'non-readable.cache') === false; }, 'is_writable' => function (string $path): bool { return stripos($path, 'non-writable-directory') === false; } ] ]);