From d49ef4e714bca2d22fe824d6e2170440590341bf Mon Sep 17 00:00:00 2001 From: Nico Devs Date: Thu, 2 Jan 2025 22:29:54 -0300 Subject: [PATCH] Fix Pest generated tests (#726) --- src/Generators/PestTestGenerator.php | 27 +++++++++++++++++-- .../tests/pest/api-shorthand-validation.php | 3 +++ ...umns-on-null-Api-PaymentControllerTest.php | 3 +++ ...-columns-on-null-PaymentControllerTest.php | 3 +++ .../pest/certificate-pascal-case-example.php | 3 +++ .../certificate-type-pascal-case-example.php | 3 +++ tests/fixtures/tests/pest/date-formats.php | 3 +++ .../fixtures/tests/pest/full-crud-example.php | 3 +++ .../pest/models-with-custom-namespace.php | 3 +++ .../pest/readme-example-notification.php | 3 +++ tests/fixtures/tests/pest/readme-example.php | 3 +++ tests/fixtures/tests/pest/reference-cache.php | 3 +++ .../tests/pest/respond-statements.php | 3 +++ .../pest/routes-with-pluralized-names.php | 3 +++ .../tests/pest/routes-with-singular-names.php | 3 +++ 15 files changed, 67 insertions(+), 2 deletions(-) diff --git a/src/Generators/PestTestGenerator.php b/src/Generators/PestTestGenerator.php index 2e1ec9e1..d9a8e93a 100644 --- a/src/Generators/PestTestGenerator.php +++ b/src/Generators/PestTestGenerator.php @@ -4,7 +4,6 @@ use Blueprint\Blueprint; use Blueprint\Concerns\HandlesImports; -use Blueprint\Concerns\HandlesTraits; use Blueprint\Contracts\Generator; use Blueprint\Contracts\Model as BlueprintModel; use Blueprint\Models\Column; @@ -26,7 +25,7 @@ class PestTestGenerator extends AbstractClassGenerator implements Generator { - use HandlesImports, HandlesTraits; + use HandlesImports; const TESTS_VIEW = 1; @@ -42,6 +41,8 @@ class PestTestGenerator extends AbstractClassGenerator implements Generator protected array $types = ['controllers', 'tests']; + protected array $traits = []; + public function output(Tree $tree): array { $this->tree = $tree; @@ -204,6 +205,9 @@ protected function buildTestCases(Controller $controller): string $class = $this->buildFormRequestName($controller, $name); $test_case = $this->buildFormRequestTestCase($controller->fullyQualifiedClassName(), $name, config('blueprint.namespace') . '\\Http\\Requests\\' . $class) . PHP_EOL . PHP_EOL . $test_case; + $this->addImport($controller, 'JMac\\Testing\\Traits\AdditionalAssertions'); + $this->addTrait($controller, 'AdditionalAssertions'); + if ($statement->data()) { foreach ($statement->data() as $data) { [$qualifier, $column] = $this->splitField($data); @@ -653,4 +657,23 @@ private function buildLines($lines): string { return str_pad(' ', 4) . implode(PHP_EOL . str_pad(' ', 4), $lines); } + + protected function addTrait(BlueprintModel $model, $trait): void + { + $this->traits[$model->name()][] = $trait; + } + + protected function buildTraits(BlueprintModel $model): string + { + if (empty($this->traits[$model->name()])) { + return ''; + } + + $traits = collect($this->traits[$model->name()]) + ->unique() + ->sort() + ->implode('::class)->use('); + + return "pest()->use({$traits}::class);"; + } } diff --git a/tests/fixtures/tests/pest/api-shorthand-validation.php b/tests/fixtures/tests/pest/api-shorthand-validation.php index e9795605..953dea4a 100644 --- a/tests/fixtures/tests/pest/api-shorthand-validation.php +++ b/tests/fixtures/tests/pest/api-shorthand-validation.php @@ -5,6 +5,7 @@ use App\Models\Certificate; use App\Models\CertificateType; use Illuminate\Support\Carbon; +use JMac\Testing\Traits\AdditionalAssertions; use function Pest\Faker\fake; use function Pest\Laravel\assertModelMissing; use function Pest\Laravel\delete; @@ -12,6 +13,8 @@ use function Pest\Laravel\post; use function Pest\Laravel\put; +pest()->use(AdditionalAssertions::class); + test('index behaves as expected', function (): void { $certificates = Certificate::factory()->count(3)->create(); diff --git a/tests/fixtures/tests/pest/call-to-a-member-function-columns-on-null-Api-PaymentControllerTest.php b/tests/fixtures/tests/pest/call-to-a-member-function-columns-on-null-Api-PaymentControllerTest.php index 885f8450..c53e33da 100644 --- a/tests/fixtures/tests/pest/call-to-a-member-function-columns-on-null-Api-PaymentControllerTest.php +++ b/tests/fixtures/tests/pest/call-to-a-member-function-columns-on-null-Api-PaymentControllerTest.php @@ -8,10 +8,13 @@ use App\Models\User; use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Mail; +use JMac\Testing\Traits\AdditionalAssertions; use function Pest\Faker\fake; use function Pest\Laravel\get; use function Pest\Laravel\post; +pest()->use(AdditionalAssertions::class); + test('store uses form request validation') ->assertActionUsesFormRequest( \App\Http\Controllers\Api\PaymentController::class, diff --git a/tests/fixtures/tests/pest/call-to-a-member-function-columns-on-null-PaymentControllerTest.php b/tests/fixtures/tests/pest/call-to-a-member-function-columns-on-null-PaymentControllerTest.php index 8374413b..3a6cf3fb 100644 --- a/tests/fixtures/tests/pest/call-to-a-member-function-columns-on-null-PaymentControllerTest.php +++ b/tests/fixtures/tests/pest/call-to-a-member-function-columns-on-null-PaymentControllerTest.php @@ -8,10 +8,13 @@ use App\Models\User; use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Mail; +use JMac\Testing\Traits\AdditionalAssertions; use function Pest\Faker\fake; use function Pest\Laravel\get; use function Pest\Laravel\post; +pest()->use(AdditionalAssertions::class); + test('create displays view', function (): void { $response = get(route('payments.create')); diff --git a/tests/fixtures/tests/pest/certificate-pascal-case-example.php b/tests/fixtures/tests/pest/certificate-pascal-case-example.php index e9795605..953dea4a 100644 --- a/tests/fixtures/tests/pest/certificate-pascal-case-example.php +++ b/tests/fixtures/tests/pest/certificate-pascal-case-example.php @@ -5,6 +5,7 @@ use App\Models\Certificate; use App\Models\CertificateType; use Illuminate\Support\Carbon; +use JMac\Testing\Traits\AdditionalAssertions; use function Pest\Faker\fake; use function Pest\Laravel\assertModelMissing; use function Pest\Laravel\delete; @@ -12,6 +13,8 @@ use function Pest\Laravel\post; use function Pest\Laravel\put; +pest()->use(AdditionalAssertions::class); + test('index behaves as expected', function (): void { $certificates = Certificate::factory()->count(3)->create(); diff --git a/tests/fixtures/tests/pest/certificate-type-pascal-case-example.php b/tests/fixtures/tests/pest/certificate-type-pascal-case-example.php index 9493f89c..e606251f 100644 --- a/tests/fixtures/tests/pest/certificate-type-pascal-case-example.php +++ b/tests/fixtures/tests/pest/certificate-type-pascal-case-example.php @@ -3,6 +3,7 @@ namespace Tests\Feature\Http\Controllers; use App\Models\CertificateType; +use JMac\Testing\Traits\AdditionalAssertions; use function Pest\Faker\fake; use function Pest\Laravel\assertModelMissing; use function Pest\Laravel\delete; @@ -10,6 +11,8 @@ use function Pest\Laravel\post; use function Pest\Laravel\put; +pest()->use(AdditionalAssertions::class); + test('index behaves as expected', function (): void { $certificateTypes = CertificateType::factory()->count(3)->create(); diff --git a/tests/fixtures/tests/pest/date-formats.php b/tests/fixtures/tests/pest/date-formats.php index 47def130..6d3f8a17 100644 --- a/tests/fixtures/tests/pest/date-formats.php +++ b/tests/fixtures/tests/pest/date-formats.php @@ -4,6 +4,7 @@ use App\Models\Date; use Illuminate\Support\Carbon; +use JMac\Testing\Traits\AdditionalAssertions; use function Pest\Faker\fake; use function Pest\Laravel\assertModelMissing; use function Pest\Laravel\delete; @@ -11,6 +12,8 @@ use function Pest\Laravel\post; use function Pest\Laravel\put; +pest()->use(AdditionalAssertions::class); + test('index displays view', function (): void { $dates = Date::factory()->count(3)->create(); diff --git a/tests/fixtures/tests/pest/full-crud-example.php b/tests/fixtures/tests/pest/full-crud-example.php index c60263a7..ba8636dd 100644 --- a/tests/fixtures/tests/pest/full-crud-example.php +++ b/tests/fixtures/tests/pest/full-crud-example.php @@ -3,6 +3,7 @@ namespace Tests\Feature\Http\Controllers; use App\Models\Post; +use JMac\Testing\Traits\AdditionalAssertions; use function Pest\Faker\fake; use function Pest\Laravel\assertModelMissing; use function Pest\Laravel\delete; @@ -10,6 +11,8 @@ use function Pest\Laravel\post; use function Pest\Laravel\put; +pest()->use(AdditionalAssertions::class); + test('index displays view', function (): void { $posts = Post::factory()->count(3)->create(); diff --git a/tests/fixtures/tests/pest/models-with-custom-namespace.php b/tests/fixtures/tests/pest/models-with-custom-namespace.php index 75ac9091..d3a241ae 100644 --- a/tests/fixtures/tests/pest/models-with-custom-namespace.php +++ b/tests/fixtures/tests/pest/models-with-custom-namespace.php @@ -3,6 +3,7 @@ namespace Tests\Feature\Http\Controllers; use App\Models\Category; +use JMac\Testing\Traits\AdditionalAssertions; use function Pest\Faker\fake; use function Pest\Laravel\assertSoftDeleted; use function Pest\Laravel\delete; @@ -10,6 +11,8 @@ use function Pest\Laravel\post; use function Pest\Laravel\put; +pest()->use(AdditionalAssertions::class); + test('index behaves as expected', function (): void { $categories = Category::factory()->count(3)->create(); diff --git a/tests/fixtures/tests/pest/readme-example-notification.php b/tests/fixtures/tests/pest/readme-example-notification.php index 861bcd4e..4d47e4b9 100644 --- a/tests/fixtures/tests/pest/readme-example-notification.php +++ b/tests/fixtures/tests/pest/readme-example-notification.php @@ -9,10 +9,13 @@ use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Notification; use Illuminate\Support\Facades\Queue; +use JMac\Testing\Traits\AdditionalAssertions; use function Pest\Faker\fake; use function Pest\Laravel\get; use function Pest\Laravel\post; +pest()->use(AdditionalAssertions::class); + test('index displays view', function (): void { $posts = Post::factory()->count(3)->create(); diff --git a/tests/fixtures/tests/pest/readme-example.php b/tests/fixtures/tests/pest/readme-example.php index ac2e9b7d..501c3ed2 100644 --- a/tests/fixtures/tests/pest/readme-example.php +++ b/tests/fixtures/tests/pest/readme-example.php @@ -10,10 +10,13 @@ use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Mail; use Illuminate\Support\Facades\Queue; +use JMac\Testing\Traits\AdditionalAssertions; use function Pest\Faker\fake; use function Pest\Laravel\get; use function Pest\Laravel\post; +pest()->use(AdditionalAssertions::class); + test('index displays view', function (): void { $posts = Post::factory()->count(3)->create(); diff --git a/tests/fixtures/tests/pest/reference-cache.php b/tests/fixtures/tests/pest/reference-cache.php index 86fb7e92..71edc136 100644 --- a/tests/fixtures/tests/pest/reference-cache.php +++ b/tests/fixtures/tests/pest/reference-cache.php @@ -3,9 +3,12 @@ namespace Tests\Feature\Http\Controllers; use App\Models\User; +use JMac\Testing\Traits\AdditionalAssertions; use function Pest\Faker\fake; use function Pest\Laravel\post; +pest()->use(AdditionalAssertions::class); + test('store uses form request validation') ->assertActionUsesFormRequest( \App\Http\Controllers\UserController::class, diff --git a/tests/fixtures/tests/pest/respond-statements.php b/tests/fixtures/tests/pest/respond-statements.php index 835bd13d..ec6d0805 100644 --- a/tests/fixtures/tests/pest/respond-statements.php +++ b/tests/fixtures/tests/pest/respond-statements.php @@ -3,10 +3,13 @@ namespace Tests\Feature\Http\Controllers\Api; use App\Models\Post; +use JMac\Testing\Traits\AdditionalAssertions; use function Pest\Faker\fake; use function Pest\Laravel\get; use function Pest\Laravel\post; +pest()->use(AdditionalAssertions::class); + test('index responds with', function (): void { $posts = Post::factory()->count(3)->create(); diff --git a/tests/fixtures/tests/pest/routes-with-pluralized-names.php b/tests/fixtures/tests/pest/routes-with-pluralized-names.php index 75ac9091..d3a241ae 100644 --- a/tests/fixtures/tests/pest/routes-with-pluralized-names.php +++ b/tests/fixtures/tests/pest/routes-with-pluralized-names.php @@ -3,6 +3,7 @@ namespace Tests\Feature\Http\Controllers; use App\Models\Category; +use JMac\Testing\Traits\AdditionalAssertions; use function Pest\Faker\fake; use function Pest\Laravel\assertSoftDeleted; use function Pest\Laravel\delete; @@ -10,6 +11,8 @@ use function Pest\Laravel\post; use function Pest\Laravel\put; +pest()->use(AdditionalAssertions::class); + test('index behaves as expected', function (): void { $categories = Category::factory()->count(3)->create(); diff --git a/tests/fixtures/tests/pest/routes-with-singular-names.php b/tests/fixtures/tests/pest/routes-with-singular-names.php index 32ba02a4..57b97d7d 100644 --- a/tests/fixtures/tests/pest/routes-with-singular-names.php +++ b/tests/fixtures/tests/pest/routes-with-singular-names.php @@ -3,6 +3,7 @@ namespace Tests\Feature\Http\Controllers; use App\Models\Category; +use JMac\Testing\Traits\AdditionalAssertions; use function Pest\Faker\fake; use function Pest\Laravel\assertSoftDeleted; use function Pest\Laravel\delete; @@ -10,6 +11,8 @@ use function Pest\Laravel\post; use function Pest\Laravel\put; +pest()->use(AdditionalAssertions::class); + test('index behaves as expected', function (): void { $categories = Category::factory()->count(3)->create();