Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import AdditionalAssertions to base test case when generating Pest tests #733

Conversation

nicodevs
Copy link
Contributor

@nicodevs nicodevs commented Jan 6, 2025

This PR modifies the strategy for importing AdditionalAssertions into Pest tests, changing from importing it in every test to importing it once in test/TestCase.php (from which Pest tests extend). The original idea was not to alter files beyond those generated, but doing so avoids boilerplate and streamlines the codebase. Check #726 (comment)

What changed

If validation statements are detected, PestTestGenerator checks if the file test/TestCase.php exists. If it does, it verifies whether AdditionalAssertions is imported. If not, the following modifications are made:

<?php

namespace Tests;

use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
+use JMac\Testing\Traits\AdditionalAssertions;

abstract class TestCase extends BaseTestCase
{
+    use AdditionalAssertions;
}

A test has been added to validate this behavior and check that these alterations only happen once.

The use JMac\Testing\Traits\AdditionalAssertions; and pest()->use(AdditionalAssertions::class); lines are no longer added to the individual test files. The test fixtures have been updated to reflect this change.

Note

The only caveat with this strategy is that the regex might require updating if the TestCase content changes in future Laravel versions.

How to test locally

Install:

# Create Laravel project
composer create-project laravel/laravel test
cd test
# Install Blueprint
composer require -W --dev laravel-shift/blueprint
# Install Pest
composer remove phpunit/phpunit
composer require pestphp/pest --dev --with-all-dependencies
composer require pestphp/pest-plugin-faker --dev
composer require pestphp/pest-plugin-laravel --dev
./vendor/bin/pest --init
# Install Laravel Test Assertions
composer require --dev jasonmccreary/laravel-test-assertions

Point the Blueprint package to this fork and branch.

Then, initialize:

php artisan blueprint:new --config

Comment out PHPUnit and uncomment Pest:

// 'test' => \Blueprint\Generators\PhpUnitTestGenerator::class,
'test' => \Blueprint\Generators\PestTestGenerator::class,

Paste this in the draft.yaml:

models:
  Post:
    title: string
controllers:
  Post:
    resource: web

Generate:

php artisan blueprint:build

Blueprint must say test base case was updated:

Updated:
- routes/web.php
- tests/TestCase.php

Go to tests/Pest.php and use RefreshDatabase:

->use(Illuminate\Foundation\Testing\RefreshDatabase::class)

Then run the generated tests:

./vendor/bin/pest

Tests should run without errors.

   PASS  Tests\Feature\Http\Controllers\PostControllerTest
  ✓ index displays view                                                                                                  0.12s
  ✓ create displays view                                                                                                 0.01s
  ✓ store uses form request validation                                                                                   0.01s
  ✓ store saves and redirects                                                                                            0.02s
  ✓ show displays view                                                                                                   0.01s
  ✓ edit displays view                                                                                                   0.01s
  ✓ update uses form request validation                                                                                  0.01s
  ✓ update redirects                                                                                                     0.01s
  ✓ destroy deletes and redirects                                                                                        0.01s

@nicodevs nicodevs mentioned this pull request Jan 6, 2025
@jasonmccreary jasonmccreary merged commit d7a8e20 into laravel-shift:master Jan 6, 2025
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants