diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml new file mode 100644 index 0000000..5cda85c --- /dev/null +++ b/.github/workflows/coverage.yml @@ -0,0 +1,97 @@ +name: Coverage + +on: + # Run testing on all push and pull requests that have committed changes in PHP files + push: + paths: + - '**/*.php' + pull_request: + paths: + - '**/*.php' + # Make it possible to run the workflow manually + workflow_dispatch: + +permissions: + contents: read + +jobs: + coverage: + + runs-on: ubuntu-latest + + name: Code coverage report + + steps: + + #- name: Configure operating system + # run: sudo apt-get update && sudo apt-get install -y locales locales-all + + - name: Checkout code + uses: actions/checkout@v6.0.2 + with: + ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }} + + - name: Setup Node.js + uses: actions/setup-node@v4 + with: + node-version: '20' + cache: 'npm' + + - name: Cache Playwright browsers + id: playwright-cache + uses: actions/cache@v3 + with: + path: ~/.cache/ms-playwright + key: playwright-${{ runner.os }}-latest + restore-keys: | + playwright-${{ runner.os }}-latest + playwright-${{ runner.os }}- + + - name: Install and update Playwright + run: | + if [ ! -d ~/.cache/ms-playwright ]; then + npm install playwright@latest + npx playwright install --with-deps + fi + + - name: Validate composer.json and composer.lock + run: composer validate --strict + + - name: Cache Composer packages + id: composer-cache + uses: actions/cache@v5.0.5 + with: + path: vendor + key: coverage-${{ hashFiles('**/composer.lock') }} + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: '8.3' + coverage: xdebug + extensions: mbstring, gd, intl, pcntl + + - name: Install dependencies + run: composer update --prefer-dist --no-progress --prefer-stable + + - name: Setup testbench environment + run: | + cp workbench/.env.example workbench/.env + sed -i 's/APP_KEY=/APP_KEY=base64:ZQvPGC7uVADkjOgtGIIuCI8u3\/Pzu+VaRObIbHsgjCc=/' workbench/.env + sed -i 's/APP_ENV=local/APP_ENV=testing/' workbench/.env + grep "APP_KEY=base64:" workbench/.env + npm install + php vendor/bin/testbench vendor:publish --tag='filament-shield-config' + php vendor/bin/testbench filament:assets + php vendor/bin/testbench package:sync-skeleton + + - name: Run test suite with coverage + run: vendor/bin/pest --coverage-clover ./coverage.xml + + - name: Upload coverage reports to Codecov + uses: codecov/codecov-action@v6.0.0 + env: + CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} + with: + files: ./coverage.xml + verbose: true diff --git a/.github/workflows/test-runner.yml b/.github/workflows/test-runner.yml index b46b505..96f687b 100644 --- a/.github/workflows/test-runner.yml +++ b/.github/workflows/test-runner.yml @@ -19,7 +19,7 @@ jobs: runs-on: ubuntu-latest - # Define the matrix of different PHP, Laravel, and testbench versions + # Define the matrix of different PHP, Laravel, testbench versions, and shards strategy: # Fail the whole workflow if one of the jobs fails fail-fast: true @@ -27,6 +27,7 @@ jobs: php: [ 8.3, 8.4, 8.5 ] laravel: [ 11.*, 12.* ] dependency-version: [ prefer-stable ] + shard: [ 1, 2, 3, 4 ] include: # Laravel 12 uses Orchestra Testbench 10 - laravel: 12.* @@ -34,7 +35,7 @@ jobs: # Laravel 11 uses Orchestra Testbench 9 - laravel: 11.* testbench: 9.* - name: PHP ${{ matrix.php }} / L${{ matrix.laravel }} / ${{ matrix.dependency-version }} + name: PHP ${{ matrix.php }} / L${{ matrix.laravel }} / ${{ matrix.dependency-version }} / Shard ${{ matrix.shard }}/4 steps: @@ -46,6 +47,29 @@ jobs: with: ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }} + - name: Setup Node.js + uses: actions/setup-node@v4 + with: + node-version: '20' + cache: 'npm' + + - name: Cache Playwright browsers + id: playwright-cache + uses: actions/cache@v3 + with: + path: ~/.cache/ms-playwright + key: playwright-${{ runner.os }}-latest + restore-keys: | + playwright-${{ runner.os }}-latest + playwright-${{ runner.os }}- + + - name: Install and update Playwright + run: | + if [ ! -d ~/.cache/ms-playwright ]; then + npm install playwright@latest + npx playwright install --with-deps + fi + - name: Validate composer.json and composer.lock run: composer validate --strict @@ -60,23 +84,25 @@ jobs: uses: shivammathur/setup-php@v2 with: php-version: ${{ matrix.php }} - coverage: xdebug - # extensions: mbstring, gd, intl + coverage: none + extensions: mbstring, gd, intl, pcntl - name: Install dependencies run: | - composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update - composer update --${{ matrix.dependency-version }} --prefer-dist --no-progress --no-interaction + composer require "laravel/framework:${{ matrix.laravel }}" --no-interaction --no-update + composer require "orchestra/testbench:${{ matrix.testbench }}" --dev --no-interaction --no-update + composer update --${{ matrix.dependency-version }} --prefer-dist --no-progress --no-interaction --no-scripts + + - name: Setup testbench environment + run: | + cp workbench/.env.example workbench/.env + sed -i 's/APP_KEY=/APP_KEY=base64:ZQvPGC7uVADkjOgtGIIuCI8u3\/Pzu+VaRObIbHsgjCc=/' workbench/.env + sed -i 's/APP_ENV=local/APP_ENV=testing/' workbench/.env + grep "APP_KEY=base64:" workbench/.env + npm install + php vendor/bin/testbench vendor:publish --tag='filament-shield-config' + php vendor/bin/testbench filament:assets + php vendor/bin/testbench package:sync-skeleton - name: Run test suite - run: composer test -- --coverage-clover ./coverage.xml - - - name: Upload coverage reports to Codecov - # Make sure the Codecov action is only executed once - if: matrix.php == '8.3' && matrix.laravel == '12.*' && matrix.dependency-version == 'prefer-stable' - uses: codecov/codecov-action@v6.0.0 - env: - CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} - with: - files: ./coverage.xml - verbose: true + run: vendor/bin/pest --shard=${{ matrix.shard }}/4 --parallel diff --git a/composer.json b/composer.json index 0fe9e09..a605532 100644 --- a/composer.json +++ b/composer.json @@ -53,8 +53,10 @@ "require-dev": { "laravel/pint": "^1.21", "orchestra/testbench": "^9.9|^10.0", - "pestphp/pest": "^3.7", - "pestphp/pest-plugin-livewire": "^3.0" + "pestphp/pest": "^4.0", + "pestphp/pest-plugin-browser": "^4.0", + "pestphp/pest-plugin-laravel": "^4.0", + "pestphp/pest-plugin-livewire": "^4.0" }, "scripts": { "post-autoload-dump": [ @@ -64,7 +66,7 @@ "post-install-cmd": "@setup", "post-update-cmd": "@setup", "format": "vendor/bin/pint", - "test": "vendor/bin/testbench package:test", + "test": "vendor/bin/pest --parallel", "clear": "@php vendor/bin/testbench package:purge-skeleton --ansi", "prepare": "@php vendor/bin/testbench package:discover --ansi", "build": "@php vendor/bin/testbench workbench:build --ansi", diff --git a/package-lock.json b/package-lock.json index 41e8dad..78a277b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4,6 +4,9 @@ "requires": true, "packages": { "": { + "dependencies": { + "playwright": "^1.59.1" + }, "devDependencies": { "@commitlint/cli": "^19.7.1", "@commitlint/config-conventional": "^19.7.1", @@ -303,13 +306,13 @@ } }, "node_modules/@types/node": { - "version": "25.5.0", - "resolved": "https://registry.npmjs.org/@types/node/-/node-25.5.0.tgz", - "integrity": "sha512-jp2P3tQMSxWugkCUKLRPVUpGaL5MVFwF8RDuSRztfwgN1wmqJeMSbKlnEtQqU8UrhTmzEmZdu2I6v2dpp7XIxw==", + "version": "25.6.0", + "resolved": "https://registry.npmjs.org/@types/node/-/node-25.6.0.tgz", + "integrity": "sha512-+qIYRKdNYJwY3vRCZMdJbPLJAtGjQBudzZzdzwQYkEPQd+PJGixUL5QfvCLDaULoLv+RhT3LDkwEfKaAkgSmNQ==", "dev": true, "license": "MIT", "dependencies": { - "undici-types": "~7.18.0" + "undici-types": "~7.19.0" } }, "node_modules/ajv": { @@ -511,13 +514,13 @@ } }, "node_modules/cosmiconfig-typescript-loader": { - "version": "6.2.0", - "resolved": "https://registry.npmjs.org/cosmiconfig-typescript-loader/-/cosmiconfig-typescript-loader-6.2.0.tgz", - "integrity": "sha512-GEN39v7TgdxgIoNcdkRE3uiAzQt3UXLyHbRHD6YoL048XAeOomyxaP+Hh/+2C6C2wYjxJ2onhJcsQp+L4YEkVQ==", + "version": "6.3.0", + "resolved": "https://registry.npmjs.org/cosmiconfig-typescript-loader/-/cosmiconfig-typescript-loader-6.3.0.tgz", + "integrity": "sha512-Akr82WH1Wfqatyiqpj8HDkO2o2KmJRu1FhKfSNJP3K4IdXwHfEyL7MOb62i1AGQVLtIQM+iCE9CGOtrfhR+mmA==", "dev": true, "license": "MIT", "dependencies": { - "jiti": "^2.6.1" + "jiti": "2.6.1" }, "engines": { "node": ">=v18" @@ -633,6 +636,20 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/fsevents": { + "version": "2.3.2", + "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz", + "integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==", + "hasInstallScript": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": "^8.16.0 || ^10.6.0 || >=11.0.0" + } + }, "node_modules/get-caller-file": { "version": "2.0.5", "resolved": "https://registry.npmjs.org/get-caller-file/-/get-caller-file-2.0.5.tgz", @@ -1043,6 +1060,36 @@ "dev": true, "license": "ISC" }, + "node_modules/playwright": { + "version": "1.59.1", + "resolved": "https://registry.npmjs.org/playwright/-/playwright-1.59.1.tgz", + "integrity": "sha512-C8oWjPR3F81yljW9o5OxcWzfh6avkVwDD2VYdwIGqTkl+OGFISgypqzfu7dOe4QNLL2aqcWBmI3PMtLIK233lw==", + "license": "Apache-2.0", + "dependencies": { + "playwright-core": "1.59.1" + }, + "bin": { + "playwright": "cli.js" + }, + "engines": { + "node": ">=18" + }, + "optionalDependencies": { + "fsevents": "2.3.2" + } + }, + "node_modules/playwright-core": { + "version": "1.59.1", + "resolved": "https://registry.npmjs.org/playwright-core/-/playwright-core-1.59.1.tgz", + "integrity": "sha512-HBV/RJg81z5BiiZ9yPzIiClYV/QMsDCKUyogwH9p3MCP6IYjUFu/MActgYAvK0oWyV9NlwM3GLBjADyWgydVyg==", + "license": "Apache-2.0", + "bin": { + "playwright-core": "cli.js" + }, + "engines": { + "node": ">=18" + } + }, "node_modules/require-directory": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz", @@ -1145,9 +1192,9 @@ "license": "MIT" }, "node_modules/tinyexec": { - "version": "1.0.4", - "resolved": "https://registry.npmjs.org/tinyexec/-/tinyexec-1.0.4.tgz", - "integrity": "sha512-u9r3uZC0bdpGOXtlxUIdwf9pkmvhqJdrVCH9fapQtgy/OeTTMZ1nqH7agtvEfmGui6e1XxjcdrlxvxJvc3sMqw==", + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/tinyexec/-/tinyexec-1.1.1.tgz", + "integrity": "sha512-VKS/ZaQhhkKFMANmAOhhXVoIfBXblQxGX1myCQ2faQrfmobMftXeJPcZGp0gS07ocvGJWDLZGyOZDadDBqYIJg==", "dev": true, "license": "MIT", "engines": { @@ -1155,9 +1202,9 @@ } }, "node_modules/typescript": { - "version": "6.0.2", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-6.0.2.tgz", - "integrity": "sha512-bGdAIrZ0wiGDo5l8c++HWtbaNCWTS4UTv7RaTH/ThVIgjkveJt83m74bBHMJkuCbslY8ixgLBVZJIOiQlQTjfQ==", + "version": "6.0.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-6.0.3.tgz", + "integrity": "sha512-y2TvuxSZPDyQakkFRPZHKFm+KKVqIisdg9/CZwm9ftvKXLP8NRWj38/ODjNbr43SsoXqNuAisEf1GdCxqWcdBw==", "dev": true, "license": "Apache-2.0", "peer": true, @@ -1170,9 +1217,9 @@ } }, "node_modules/undici-types": { - "version": "7.18.2", - "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-7.18.2.tgz", - "integrity": "sha512-AsuCzffGHJybSaRrmr5eHr81mwJU3kjw6M+uprWvCXiNeN9SOGwQ3Jn8jb8m3Z6izVgknn1R0FTCEAP2QrLY/w==", + "version": "7.19.2", + "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-7.19.2.tgz", + "integrity": "sha512-qYVnV5OEm2AW8cJMCpdV20CDyaN3g0AjDlOGf1OW4iaDEx8MwdtChUp4zu4H0VP3nDRF/8RKWH+IPp9uW0YGZg==", "dev": true, "license": "MIT" }, diff --git a/package.json b/package.json index fc63ee2..128faf8 100644 --- a/package.json +++ b/package.json @@ -8,5 +8,8 @@ "@commitlint/cli": "^19.7.1", "@commitlint/config-conventional": "^19.7.1", "husky": "^9.1.7" + }, + "dependencies": { + "playwright": "^1.59.1" } } diff --git a/tests/Browser/CountryResourceBrowserTest.php b/tests/Browser/CountryResourceBrowserTest.php new file mode 100644 index 0000000..ef760fa --- /dev/null +++ b/tests/Browser/CountryResourceBrowserTest.php @@ -0,0 +1,48 @@ +setUpSuperAdmin(); +}); + +test('can browse country resource page', function () { + $region = Region::factory()->create(['name' => 'Test Region']); + $country = Country::factory()->create([ + 'name' => 'Test Country', + 'id' => 'TC', + 'region_id' => $region->id, + ]); + + $this->visit(CountryResource::getUrl()) + ->assertSee('Countries') + ->assertSee('Test Country') + ->assertSee('TC') + ->assertSee('Test Region'); +}); + +test('can interact with country resource table', function () { + $region = Region::factory()->create(['name' => 'Browser Test Region']); + $country = Country::factory()->create([ + 'name' => 'Browser Test Country', + 'id' => 'BTC', + 'region_id' => $region->id, + ]); + + $this->visit(CountryResource::getUrl()) + ->assertSee('Browser Test Country') + ->assertSee('BTC') + ->assertSee('Browser Test Region') + ->assertSee('Countries'); +}); + +test('country resource page loads without JavaScript errors', function () { + $this->visit(CountryResource::getUrl()) + ->assertSee('Countries') + ->assertDontSee('Uncaught') + ->assertDontSee('ReferenceError') + ->assertDontSee('TypeError') + ->assertDontSee('SyntaxError'); +}); diff --git a/tests/Browser/VisualRegressionTest.php b/tests/Browser/VisualRegressionTest.php new file mode 100644 index 0000000..be670d4 --- /dev/null +++ b/tests/Browser/VisualRegressionTest.php @@ -0,0 +1,93 @@ +setUpSuperAdmin(); +}); + +describe('Visual Regression Tests', function () { + test('country resource page visual regression', function () { + $region = Region::factory()->create(['name' => 'Visual Test Region']); + $country = Country::factory()->create([ + 'name' => 'Visual Test Country', + 'id' => 'VT', + 'a3_id' => 'VTC', + 'num_code' => '999', + 'flag' => '🏳️', + 'region_id' => $region->id, + ]); + + $this->visit(CountryResource::getUrl()) + ->assertSee('Countries') + ->assertSee('Visual Test Country') + ->wait(1) + ->assertScreenshotMatches(); + }); + + test('currency resource page visual regression', function () { + $this->visit(CurrencyResource::getUrl()) + ->assertSee('Currencies') + ->wait(1) + ->assertScreenshotMatches(); + }); + + test('post resource page visual regression', function () { + $this->visit(PostResource::getUrl()) + ->assertSee('Posts') + ->wait(1) + ->assertScreenshotMatches(); + }); + + test('region resource page visual regression', function () { + $this->visit(RegionResource::getUrl()) + ->assertSee('Regions') + ->wait(1) + ->assertScreenshotMatches(); + }); + + test('country resource page with data visual regression', function () { + $region1 = Region::factory()->create(['name' => 'Europe']); + $region2 = Region::factory()->create(['name' => 'Asia']); + + Country::factory()->create([ + 'name' => 'Germany', + 'id' => 'DE', + 'a3_id' => 'DEU', + 'num_code' => '276', + 'flag' => 'πŸ‡©πŸ‡ͺ', + 'region_id' => $region1->id, + ]); + + Country::factory()->create([ + 'name' => 'Japan', + 'id' => 'JP', + 'a3_id' => 'JPN', + 'num_code' => '392', + 'flag' => 'πŸ‡―πŸ‡΅', + 'region_id' => $region2->id, + ]); + + Country::factory()->create([ + 'name' => 'United States', + 'id' => 'US', + 'a3_id' => 'USA', + 'num_code' => '840', + 'flag' => 'πŸ‡ΊπŸ‡Έ', + 'region_id' => $region1->id, + ]); + + $this->visit(CountryResource::getUrl()) + ->assertSee('Countries') + ->assertSee('Germany') + ->assertSee('Japan') + ->assertSee('United States') + ->wait(1) + ->assertScreenshotMatches(); + }); +}); diff --git a/tests/Feature/SmokeTest.php b/tests/Feature/SmokeTest.php new file mode 100644 index 0000000..ed23abc --- /dev/null +++ b/tests/Feature/SmokeTest.php @@ -0,0 +1,93 @@ +setUpSuperAdmin(); +}); + +describe('Smoke Tests', function () { + test('all resource URLs are accessible', function () { + $resources = [ + CountryResource::class, + CurrencyResource::class, + PostResource::class, + RegionResource::class, + ]; + + foreach ($resources as $resource) { + /** @noinspection PhpUndefinedMethodInspection */ + $this->get($resource::getUrl()) + ->assertSuccessful() + ->assertSee('Filament'); + } + }); + + test('country resource URLs are accessible', function () { + $this->get(CountryResource::getUrl()) + ->assertSuccessful() + ->assertSee('Countries') + ->assertSee('Filament'); + }); + + test('currency resource URLs are accessible', function () { + $this->get(CurrencyResource::getUrl()) + ->assertSuccessful() + ->assertSee('Currencies') + ->assertSee('Filament'); + }); + + test('post resource URLs are accessible', function () { + $this->get(PostResource::getUrl()) + ->assertSuccessful() + ->assertSee('Posts') + ->assertSee('Filament'); + }); + + test('region resource URLs are accessible', function () { + $this->get(RegionResource::getUrl()) + ->assertSuccessful() + ->assertSee('Regions') + ->assertSee('Filament'); + }); + + test('all resource URLs return valid HTML', function () { + $resources = [ + CountryResource::class, + CurrencyResource::class, + PostResource::class, + RegionResource::class, + ]; + + foreach ($resources as $resource) { + /** @noinspection PhpUndefinedMethodInspection */ + $response = $this->get($resource::getUrl()); + + $response->assertSuccessful(); + $response->assertHeader('content-type', 'text/html; charset=utf-8'); + $response->assertSee('', false); + } + }); + + test('all resource URLs have no JavaScript errors', function () { + $resources = [ + CountryResource::class, + CurrencyResource::class, + PostResource::class, + RegionResource::class, + ]; + + foreach ($resources as $resource) { + /** @noinspection PhpUndefinedMethodInspection */ + $response = $this->get($resource::getUrl()); + + $response->assertSuccessful(); + $response->assertDontSee('Uncaught'); + $response->assertDontSee('ReferenceError'); + $response->assertDontSee('TypeError'); + } + }); +}); diff --git a/tests/TestCase.php b/tests/TestCase.php index 4270cf7..f2f51c4 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -42,7 +42,10 @@ protected function migrate(): self */ protected function setUpSuperAdmin(): self { - $this->superAdmin = User::factory()->create(); + $this->superAdmin = User::factory()->create([ + 'name' => 'Test Super Admin', + 'email' => 'test@example.com', + ]); // Assign super admin role and give all permissions $superAdminRole = Role::where('name', 'super_admin')->first();