diff --git a/.gitattributes b/.gitattributes index 61e9c5f..9e9519b 100644 --- a/.gitattributes +++ b/.gitattributes @@ -2,10 +2,18 @@ # https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html # Ignore all test and documentation with "export-ignore". +/.github export-ignore /.gitattributes export-ignore /.gitignore export-ignore /phpunit.xml.dist export-ignore +/art export-ignore +/docs export-ignore /tests export-ignore /.editorconfig export-ignore -/.php_cs.dist export-ignore -/.github export-ignore +/.php_cs.dist.php export-ignore +/psalm.xml export-ignore +/psalm.xml.dist export-ignore +/testbench.yaml export-ignore +/UPGRADING.md export-ignore +/phpstan.neon.dist export-ignore +/phpstan-baseline.neon export-ignore diff --git a/.github/ISSUE_TEMPLATE/bug.yml b/.github/ISSUE_TEMPLATE/bug.yml deleted file mode 100644 index 52f37d5..0000000 --- a/.github/ISSUE_TEMPLATE/bug.yml +++ /dev/null @@ -1,52 +0,0 @@ -name: Bug Report -description: Report an Issue or Bug with the Package -title: "[Bug]: " -labels: ["bug"] -body: - - type: markdown - attributes: - value: | - We're sorry to hear you have a problem. Can you help us solve it by providing the following details. - - type: textarea - id: what-happened - attributes: - label: What happened? - description: What did you expect to happen? - placeholder: I cannot currently do X thing because when I do, it breaks X thing. - validations: - required: true - - - type: input - id: package-version - attributes: - label: Package Version - description: What version of our Package are you running? Please be as specific as possible - placeholder: 2.0.0 - validations: - required: true - - type: input - id: php-version - attributes: - label: PHP Version - description: What version of PHP are you running? Please be as specific as possible - placeholder: 8.2.0 - validations: - required: true - - type: input - id: laravel-version - attributes: - label: Laravel Version - description: What version of Laravel are you running? Please be as specific as possible - placeholder: 9.0.0 - validations: - required: true - - type: dropdown - id: operating-systems - attributes: - label: Which operating systems does with happen with? - description: You may select more than one. - multiple: true - options: - - macOS - - Windows - - Linux diff --git a/.github/ISSUE_TEMPLATE/bug_report.yml b/.github/ISSUE_TEMPLATE/bug_report.yml index 7546b77..bc5e177 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.yml +++ b/.github/ISSUE_TEMPLATE/bug_report.yml @@ -1,18 +1,18 @@ name: Bug Report description: Report an Issue or Bug with the Package title: "[Bug]: " -labels: ["bug"] +labels: [ "bug" ] body: - type: markdown attributes: - value: | - We're sorry to hear you have a problem. Can you help us solve it by providing the following details. + value: "| +We're sorry to hear you have a problem. Can you help us solve it by providing the following details." - type: textarea id: what-happened attributes: label: What happened? description: What did you expect to happen? - placeholder: I cannot currently do X thing because when I do, it breaks X thing. + placeholder: "I cannot currently do X thing because when I do, it breaks X thing." validations: required: true - type: input @@ -20,7 +20,8 @@ body: attributes: label: Package Version description: What version of our Package are you running? Please be as specific as possible - placeholder: 1.0.0 + placeholder: "12.0" + value: "12.0" validations: required: true - type: input @@ -28,7 +29,8 @@ body: attributes: label: PHP Version description: What version of PHP are you running? Please be as specific as possible - placeholder: 8.3.0 + placeholder: "8.4.0" + value: "8.4.0" validations: required: true - type: input @@ -36,7 +38,8 @@ body: attributes: label: Laravel Version description: What version of Laravel are you running? Please be as specific as possible - placeholder: 11.0.0 + placeholder: "12.0.0" + value: "12.0.0" validations: required: true - type: dropdown diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..0bc378d --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,12 @@ +# Please see the documentation for all configuration options: +# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates + +version: 2 +updates: + + - package-ecosystem: "github-actions" + directory: "/" + schedule: + interval: "weekly" + labels: + - "dependencies" diff --git a/.github/workflows/dependabot-auto-merge.yml b/.github/workflows/dependabot-auto-merge.yml new file mode 100644 index 0000000..2df1631 --- /dev/null +++ b/.github/workflows/dependabot-auto-merge.yml @@ -0,0 +1,32 @@ +name: dependabot-auto-merge +on: pull_request_target + +permissions: + pull-requests: write + contents: write + +jobs: + dependabot: + runs-on: ubuntu-latest + if: ${{ github.actor == 'dependabot[bot]' }} + steps: + + - name: Dependabot metadata + id: metadata + uses: dependabot/fetch-metadata@v2.3.0 + with: + github-token: "${{ secrets.GITHUB_TOKEN }}" + + - name: Auto-merge Dependabot PRs for semver-minor updates + if: ${{steps.metadata.outputs.update-type == 'version-update:semver-minor'}} + run: gh pr merge --auto --merge "$PR_URL" + env: + PR_URL: ${{github.event.pull_request.html_url}} + GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} + + - name: Auto-merge Dependabot PRs for semver-patch updates + if: ${{steps.metadata.outputs.update-type == 'version-update:semver-patch'}} + run: gh pr merge --auto --merge "$PR_URL" + env: + PR_URL: ${{github.event.pull_request.html_url}} + GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} diff --git a/.github/workflows/dependency-review.yml b/.github/workflows/dependency-review.yml new file mode 100644 index 0000000..0d4a013 --- /dev/null +++ b/.github/workflows/dependency-review.yml @@ -0,0 +1,20 @@ +# Dependency Review Action +# +# This Action will scan dependency manifest files that change as part of a Pull Request, surfacing known-vulnerable versions of the packages declared or updated in the PR. Once installed, if the workflow run is marked as required, PRs introducing known-vulnerable packages will be blocked from merging. +# +# Source repository: https://github.com/actions/dependency-review-action +# Public documentation: https://docs.github.com/en/code-security/supply-chain-security/understanding-your-software-supply-chain/about-dependency-review#dependency-review-enforcement +name: 'Dependency Review' +on: [pull_request] + +permissions: + contents: read + +jobs: + dependency-review: + runs-on: ubuntu-latest + steps: + - name: 'Checkout Repository' + uses: actions/checkout@v4 + - name: 'Dependency Review' + uses: actions/dependency-review-action@v4 diff --git a/.github/workflows/fix-php-code-style-issues.yml b/.github/workflows/fix-php-code-style-issues.yml index 7f61fce..ef2fadf 100644 --- a/.github/workflows/fix-php-code-style-issues.yml +++ b/.github/workflows/fix-php-code-style-issues.yml @@ -13,7 +13,7 @@ jobs: ref: ${{ github.head_ref }} - name: Fix PHP code style issues - uses: aglipanci/laravel-pint-action@2.3.1 + uses: aglipanci/laravel-pint-action@2.5 - name: Commit changes uses: stefanzweifel/git-auto-commit-action@v5 diff --git a/.github/workflows/phpstan.yml b/.github/workflows/phpstan.yml new file mode 100644 index 0000000..f381a0b --- /dev/null +++ b/.github/workflows/phpstan.yml @@ -0,0 +1,22 @@ +name: PHPStan + +on: [push] + +jobs: + phpstan: + name: phpstan + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: '8.4' + coverage: none + + - name: Install composer dependencies + uses: ramsey/composer-install@v3 + + - name: Run PHPStan + run: ./vendor/bin/phpstan --error-format=github diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..afa28ff --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,23 @@ +name: release + +on: + pull_request: + types: + - closed + branches: + - main +jobs: + release: + name: Release + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: '0' + - name: Bump version and push tag + uses: anothrNick/github-tag-action@master + env: + GITHUB_TOKEN: ${{ secrets.MY_GITHUB_TOKEN }} + WITH_V: true + RELEASE_BRANCHES: main + DEFAULT_BUMP: minor diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index be5239a..c4d9e45 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -2,42 +2,34 @@ name: run-tests on: push: - branches: - - main + branches: [ main ] pull_request: - branches: - - main + branches: [ main ] jobs: test: runs-on: ${{ matrix.os }} - strategy: fail-fast: true max-parallel: 1 matrix: - os: [ubuntu-latest, windows-latest] - php: [8.2, 8.3] - laravel: ['11.*', '12.*'] - stability: [prefer-lowest, prefer-stable] - include: - - laravel: 11.* - testbench: 9.* - - laravel: 12.* - testbench: 10.* + os: [ ubuntu-latest ] + php: [ 8.2, 8.3, 8.4 ] + laravel: [ 12.* ] + stability: [ prefer-lowest, prefer-stable ] name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }} steps: - name: Checkout code - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Setup PHP uses: shivammathur/setup-php@v2 with: php-version: ${{ matrix.php }} extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo - coverage: xdebug + coverage: none - name: Setup problem matchers run: | @@ -46,19 +38,18 @@ jobs: - name: Install dependencies run: | - composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --dev --no-interaction --no-update - composer update --${{ matrix.stability }} --prefer-dist --no-interaction --no-suggest + composer require "laravel/framework:${{ matrix.laravel }}" --no-interaction --no-update + composer update --${{ matrix.stability }} --prefer-dist --no-interaction - - name: Set phpunit.xml + - name: Execute tests run: cp phpunit.xml.dist phpunit.xml - name: Execute tests - run: vendor/bin/phpunit + run: vendor/bin/pest - - name: Store test reports - uses: actions/upload-artifact@v2 + - name: Store Log Artifacts + if: failure() + uses: actions/upload-artifact@v4 with: - name: Store report - retention-days: 1 - path: | - ./reports + name: Store report artifacts + path: ./vendor/orchestra/testbench-core/laravel/storage/logs diff --git a/.gitignore b/.gitignore index 7514ae6..91b23b2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,12 +1,12 @@ -.DS_Store .idea .phpunit.result.cache .phpunit.cache -build composer.lock coverage -docs +phpunit.xml +phpstan.neon +testbench.yaml vendor -.php_cs.cache -.php-cs-fixer.cache -coverage.xml +node_modules +.phpactor.json +build diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php deleted file mode 100644 index c55db8a..0000000 --- a/.php-cs-fixer.dist.php +++ /dev/null @@ -1,36 +0,0 @@ -in([ - __DIR__ . '/src', - __DIR__ . '/tests', - ]) - ->name('*.php') - ->notName('*.blade.php') - ->ignoreDotFiles(true) - ->ignoreVCS(true); - -return (new PhpCsFixer\Config()) - ->setRules([ - '@PSR2' => true, - 'array_syntax' => ['syntax' => 'short'], - 'ordered_imports' => ['sort_algorithm' => 'alpha'], - 'no_unused_imports' => true, - 'not_operator_with_successor_space' => false, - 'trailing_comma_in_multiline' => true, - 'phpdoc_scalar' => true, - 'unary_operator_spaces' => true, - 'binary_operator_spaces' => true, - 'blank_line_before_statement' => [ - 'statements' => ['break', 'continue', 'declare', 'return', 'throw', 'try'], - ], - 'phpdoc_single_line_var_spacing' => true, - 'phpdoc_var_without_name' => true, - 'method_argument_space' => [ - 'on_multiline' => 'ensure_fully_multiline', - 'keep_multiple_spaces_after_comma' => true, - ], - 'single_trait_insert_per_statement' => true, - 'single_quote' => true, - ]) - ->setFinder($finder); diff --git a/README.md b/README.md index 8949e2c..7cca505 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,16 @@ [![Latest Version on Packagist](https://img.shields.io/packagist/v/codebar-ag/laravel-prerender.svg?style=flat-square)](https://packagist.org/packages/codebar-ag/laravel-prerender) +[![GitHub-Tests](https://github.com/codebar-ag/laravel-prerender/actions/workflows/run-tests.yml/badge.svg?branch=main)](https://github.com/codebar-ag/laravel-prerender/actions/workflows/run-tests.yml) +[![GitHub Code Style](https://github.com/codebar-ag/laravel-prerender/actions/workflows/fix-php-code-style-issues.yml/badge.svg?branch=main)](https://github.com/codebar-ag/laravel-prerender/actions/workflows/fix-php-code-style-issues.yml) [![Total Downloads](https://img.shields.io/packagist/dt/codebar-ag/laravel-prerender.svg?style=flat-square)](https://packagist.org/packages/codebar-ag/laravel-prerender) -[![run-tests](https://github.com/codebar-ag/laravel-prerender/actions/workflows/run-tests.yml/badge.svg)](https://github.com/codebar-ag/laravel-prerender/actions/workflows/run-tests.yml) -[![Check & fix styling](https://github.com/codebar-ag/laravel-prerender/actions/workflows/php-cs-fixer.yml/badge.svg)](https://github.com/codebar-ag/laravel-prerender/actions/workflows/php-cs-fixer.yml) This package was developed to give you a quick start to integrate with the Prerender.io service in your Laravel application. ## πŸ™‡ Credits -This package is a clone from [jeroennoten/Laravel-Prerender](https://github.com/jeroennoten/Laravel-Prerender) +This package is a clone from [jeroennoten/laravel-prerender](https://github.com/jeroennoten/Laravel-Prerender) with [jeroennoten](https://github.com/jeroennoten) as the original author. [CasperLaiTW](https://github.com/CasperLaiTW) provided Laravel 6,7 & 8 compatibility by an unmerged (14th September 2020) Pull-Request. @@ -32,7 +32,8 @@ using Prerender.io since the response always goes through your server. | Package | PHP | Laravel | Prerender.io access | |:-------:|:-----------:|:-------:|:-------------------:| -| main | ^8.2 - ^8.3 | 11.x | βœ… | +| v12.0.0 | ^8.2 - ^8.4 | 12.x | βœ… | +| v11.0.0 | ^8.2 - ^8.3 | 11.x | βœ… | | v3.3.0 | ^8.1 - ^8.3 | 10.x | βœ… | | v3.2.0 | ^8.0 - ^8.2 | 9.x | βœ… | | v3.1.1 | ^7.3 - ^8.1 | 8.x | βœ… | diff --git a/composer.json b/composer.json index 0ccd10f..da0acb2 100644 --- a/composer.json +++ b/composer.json @@ -10,10 +10,14 @@ "license": "MIT", "authors": [ { - "name": "Sebastian Fix", - "email": "sebastian.fix@codebar.ch", + "name": "Sebastian BΓΌrgin-Fix", + "email": "sebastian.buergin@buergin.ch", "homepage": "https://www.codebar.ch", - "role": "Developer" + "role": "Sofware-Engineer" + }, + { + "name": "Rhys Lees", + "role": "Software-Engineer" }, { "name": "Casper Lai", @@ -28,15 +32,20 @@ } ], "require": { - "php": "^8.2|^8.3", + "php": "8.2.*|8.3.*|8.4.*", "guzzlehttp/guzzle": "^7.8", - "illuminate/support": "^11.0|^12.0", + "illuminate/contracts": "^12.0", "symfony/psr-http-message-bridge": "^7.0" }, "require-dev": { - "friendsofphp/php-cs-fixer": "^3.50", - "orchestra/testbench": "^9.0|^10.0", - "phpunit/phpunit": "^10.5|^11.5.3" + "laravel/pint": "^1.21", + "larastan/larastan": "^v3.1", + "orchestra/testbench": "^10.0", + "pestphp/pest": "^3.7", + "phpstan/extension-installer": "^1.4", + "phpstan/phpstan-deprecation-rules": "^2.0", + "phpstan/phpstan-phpunit": "^2.0", + "spatie/laravel-ray": "^1.39" }, "autoload": { "psr-4": { @@ -56,7 +65,8 @@ "config": { "sort-packages": true, "allow-plugins": { - "pestphp/pest-plugin": true + "pestphp/pest-plugin": true, + "phpstan/extension-installer": true } }, "extra": { diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon new file mode 100644 index 0000000..e69de29 diff --git a/phpstan.neon.dist b/phpstan.neon.dist new file mode 100644 index 0000000..facf7fa --- /dev/null +++ b/phpstan.neon.dist @@ -0,0 +1,13 @@ + +includes: + - phpstan-baseline.neon + +parameters: + level: 5 + paths: + - src + - config + tmpDir: build/phpstan + checkOctaneCompatibility: true + checkModelProperties: true + noEnvCallsOutsideOfConfig: false diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..d41aea3 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,30 @@ + + + + + tests + + + + + + + + + + + + + + + + + + ./src + + + diff --git a/phpunit.xml.dist b/phpunit.xml.dist index eedb770..d41aea3 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,23 +1,30 @@ - - - - tests - - - - - - - - - - - - - - - ./src - - + + + + tests + + + + + + + + + + + + + + + + + + ./src + + diff --git a/src/PrerenderMiddleware.php b/src/PrerenderMiddleware.php index 69dcf66..df40c6d 100644 --- a/src/PrerenderMiddleware.php +++ b/src/PrerenderMiddleware.php @@ -79,6 +79,7 @@ public function __construct(Guzzle $client) { $this->returnSoftHttpCodes = config('prerender.prerender_soft_http_codes'); + // @phpstan-ignore-next-line $guzzleConfig = $client->getConfig(); $guzzleConfig['timeout'] = config('prerender.timeout'); diff --git a/tests/Feature/PrerenderMiddlewareTest.php b/tests/Feature/PrerenderMiddlewareTest.php new file mode 100644 index 0000000..4e5c886 --- /dev/null +++ b/tests/Feature/PrerenderMiddlewareTest.php @@ -0,0 +1,92 @@ +get('/test-middleware') + ->assertHeader('prerender.io-mock', true) + ->assertSuccessful(); +}); + +test('it should not prerender page when user agent does not in list', function () { + $this->get('/test-middleware') + ->assertHeaderMissing('prerender.io-mock') + ->assertSee('GET - Success'); +}); + +test('it should prerender page with escaped fragment in query string', function () { + $this->get('/test-middleware?_escaped_fragment_') + ->assertHeader('prerender.io-mock', true) + ->assertSuccessful(); +}); + +test('it should prerender when user agent is part of crawler user agents', function () { + $this->get('/test-middleware', ['User-Agent' => 'Googlebot/2.1 (+http://www.google.com/bot.html)']) + ->assertHeader('prerender.io-mock', true) + ->assertSuccessful(); +}); + +test('it should prerender page with url in whitelist', function () { + config()->set('prerender.whitelist', ['/test-middleware*']); + + $this->get('/test-middleware?_escaped_fragment_') + ->assertHeader('prerender.io-mock', true) + ->assertSuccessful(); +}); + +test('it should not prerender page in blacklist', function () { + config()->set('prerender.blacklist', ['/test-middleware*']); + + $this->get('/test-middleware?_escaped_fragment_') + ->assertSuccessful() + ->assertHeaderMissing('prerender.io-mock') + ->assertSee('GET - Success'); +}); + +test('it should not prerender page on non-get request', function () { + allowSymfonyUserAgent(); + + $this->post('/test-middleware') + ->assertSuccessful() + ->assertSee('Success'); +}); + +test('it should not prerender page when missing user agent', function () { + $this->get('/test-middleware', ['User-Agent' => null]) + ->assertHeaderMissing('prerender.io-mock') + ->assertSee('GET - Success'); +}); + +test('it should not prerender page if request times out', function () { + $this->app->bind(\GuzzleHttp\Client::class, fn () => createMockTimeoutClient()); + + allowSymfonyUserAgent(); + + $this->get('/test-middleware') + ->assertHeaderMissing('prerender.io-mock') + ->assertSee('GET - Success'); +}); + +test('it does not send query strings to prerender by default', function () { + $this->app->bind(\GuzzleHttp\Client::class, fn () => createMockUrlTrackingClient()); + + allowSymfonyUserAgent(); + + $this->get('/test-middleware?withQueryParam=true') + ->assertHeader('prerender.io-mock', true) + ->assertSuccessful() + ->assertSee(urlencode('/test-middleware')) + ->assertDontSee('withQueryParam'); +}); + +test('it sends full query string to prerender', function () { + $this->app->bind(\GuzzleHttp\Client::class, fn () => createMockUrlTrackingClient()); + + allowSymfonyUserAgent(); + allowQueryParams(); + + $this->get('/test-middleware?withQueryParam=true') + ->assertHeader('prerender.io-mock', true) + ->assertSuccessful() + ->assertSee(urlencode('/test-middleware?withQueryParam=true')); +}); diff --git a/tests/Pest.php b/tests/Pest.php new file mode 100644 index 0000000..2c2ec01 --- /dev/null +++ b/tests/Pest.php @@ -0,0 +1,59 @@ +in(__DIR__); + +function createMockPrerenderClient(): Client +{ + $mock = new MockHandler([ + new Response(200, ['prerender.io-mock' => 'true'], 'Mocked Prerender Response'), + ]); + + $stack = HandlerStack::create($mock); + + return new Client(['handler' => $stack]); +} + +function createMockTimeoutClient(): Client +{ + $mock = new MockHandler([ + new ConnectException('Could not connect', new Request('GET', 'test')), + ]); + + $stack = HandlerStack::create($mock); + + return new Client(['handler' => $stack]); +} + +function createMockUrlTrackingClient(): Client +{ + $mock = new MockHandler([ + fn (RequestInterface $request) => new Response( + 200, + ['prerender.io-mock' => 'true'], + (string) $request->getUri() + ), + ]); + + $stack = HandlerStack::create($mock); + + return new Client(['handler' => $stack]); +} + +function allowSymfonyUserAgent() +{ + config()->set('prerender.crawler_user_agents', ['symfony']); +} + +function allowQueryParams() +{ + config()->set('prerender.full_url', true); +} diff --git a/tests/PrerenderMiddlewareTest.php b/tests/PrerenderMiddlewareTest.php deleted file mode 100644 index 9ae6196..0000000 --- a/tests/PrerenderMiddlewareTest.php +++ /dev/null @@ -1,138 +0,0 @@ -allowSymfonyUserAgent(); - - $this->get('/test-middleware') - ->assertHeader('prerender.io-mock', true) - ->assertSuccessful(); - } - - /** @test */ - public function it_should_not_prerender_page_when_user_agent_does_not_in_list() - { - $this->get('/test-middleware') - ->assertSuccessful() - ->assertHeaderMissing('prerender.io-mock') - ->assertSee('GET - Success'); - } - - /** @test */ - public function it_should_prerender_page_with_escaped_fragment_in_query_string() - { - $this->get('/test-middleware?_escaped_fragment_') - ->assertHeader('prerender.io-mock', true) - ->assertSuccessful(); - } - - /** @test */ - public function it_should_prerender_when_user_agent_is_part_of_crawler_user_agents() - { - $this->get('/test-middleware', ['User-Agent' => 'Googlebot/2.1 (+http://www.google.com/bot.html)']) - ->assertHeader('prerender.io-mock', true) - ->assertSuccessful(); - } - - /** @test */ - public function it_should_prerender_page_with_url_in_whitelist() - { - config()->set('prerender.whitelist', ['/test-middleware*']); - - $this->get('/test-middleware?_escaped_fragment_') - ->assertHeader('prerender.io-mock', true) - ->assertSuccessful(); - } - - /** @test */ - public function is_should_not_prerender_page_in_blacklist() - { - config()->set('prerender.blacklist', ['/test-middleware*']); - - $this->get('/test-middleware?_escaped_fragment_') - ->assertSuccessful() - ->assertHeaderMissing('prerender.io-mock') - ->assertSee('GET - Success'); - } - - /** @test */ - public function it_should_not_prerender_page_on_non_get_request() - { - $this->allowSymfonyUserAgent(); - - $this->post('/test-middleware') - ->assertSuccessful() - ->assertSee('Success'); - } - - /** @test */ - public function it_should_not_prerender_page_when_missing_user_agent() - { - $this->get('/test-middleware', ['User-Agent' => null]) - ->assertHeaderMissing('prerender.io-mock') - ->assertSee('GET - Success'); - } - - /** @test */ - public function it_should_not_prerender_page_if_request_times_out() - { - $this->app->bind(Client::class, function () { - return $this->createMockTimeoutClient(); - }); - - $this->allowSymfonyUserAgent(); - - $this->get('/test-middleware') - ->assertHeaderMissing('prerender.io-mock') - ->assertSee('GET - Success'); - } - - /** @test */ - public function it_does_not_send_query_strings_to_prerender_by_default() - { - $this->app->bind(Client::class, function () { - return $this->createMockUrlTrackingClient(); - }); - - $this->allowSymfonyUserAgent(); - - $this->get('/test-middleware?withQueryParam=true') - ->assertHeader('prerender.io-mock', true) - ->assertSuccessful() - ->assertSee(urlencode('/test-middleware')) - ->assertDontSee('withQueryParam'); - } - - /** @test */ - public function it_sends_full_query_string_to_prerender() - { - $this->app->bind(Client::class, function () { - return $this->createMockUrlTrackingClient(); - }); - - $this->allowSymfonyUserAgent(); - $this->allowQueryParams(); - - $this->get('/test-middleware?withQueryParam=true') - ->assertHeader('prerender.io-mock', true) - ->assertSuccessful() - ->assertSee(urlencode('/test-middleware?withQueryParam=true')); - } - - private function allowSymfonyUserAgent() - { - config()->set('prerender.crawler_user_agents', ['symfony']); - } - - private function allowQueryParams() - { - config()->set('prerender.full_url', true); - } -} diff --git a/tests/TestCase.php b/tests/TestCase.php index ff281b0..1cf6277 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -5,28 +5,12 @@ use CodebarAg\LaravelPrerender\LaravelPrerenderServiceProvider; use CodebarAg\LaravelPrerender\PrerenderMiddleware; use GuzzleHttp\Client; -use GuzzleHttp\Exception\ConnectException; -use GuzzleHttp\Handler\MockHandler; -use GuzzleHttp\HandlerStack; -use GuzzleHttp\Psr7\Request; -use GuzzleHttp\Psr7\Response; -use Illuminate\Foundation\Application; use Illuminate\Foundation\Http\Kernel; use Illuminate\Support\Facades\Route; -use Psr\Http\Message\RequestInterface; +use Orchestra\Testbench\TestCase as Orchestra; -class TestCase extends \Orchestra\Testbench\TestCase +class TestCase extends Orchestra { - protected function setUp(): void - { - parent::setUp(); - - $this->setupRoutes(); - } - - /** - * @param Application $app - */ protected function getPackageProviders($app): array { return [ @@ -34,60 +18,32 @@ protected function getPackageProviders($app): array ]; } - /** - * @param Application $app - */ - protected function getEnvironmentSetUp($app): void - { - $app->make(Kernel::class)->prependMiddleware(PrerenderMiddleware::class); - - // mock guzzle client - $app->bind(Client::class, function () { - $mock = new MockHandler([ - new Response(200, ['prerender.io-mock' => true]), - ]); - $stack = HandlerStack::create($mock); - - return new Client(['handler' => $stack]); - }); - } - - protected function createMockTimeoutClient(): Client + public function getEnvironmentSetUp($app): void { - $mock = new MockHandler([ - new ConnectException('Could not connect', new Request('GET', 'test')), + $app['config']->set('database.default', 'sqlite'); + $app['config']->set('database.connections.sqlite', [ + 'driver' => 'sqlite', + 'database' => ':memory:', + 'prefix' => '', ]); - $stack = HandlerStack::create($mock); - - return new Client(['handler' => $stack]); - } - - protected function createMockUrlTrackingClient(): Client - { - $mock = new MockHandler([ - function (RequestInterface $request) { - return new Response( - 200, - ['prerender.io-mock' => true], - (string) $request->getUri() - ); - }, - ]); + $app->make(Kernel::class)->prependMiddleware(PrerenderMiddleware::class); - $stack = HandlerStack::create($mock); + $this->setupRoutes(); - return new Client(['handler' => $stack]); + $app->bind(Client::class, function () { + return createMockPrerenderClient(); + }); } protected function setupRoutes(): void { Route::get('test-middleware', function () { - return 'GET - Success'; + return response('GET - Success')->header('Content-Type', 'text/plain'); }); Route::post('test-middleware', function () { - return 'Success'; + return response('Success')->header('Content-Type', 'text/plain'); }); } }