Skip to content

Commit

Permalink
Fix Pest generated tests (#726)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicodevs authored Jan 3, 2025
1 parent 6e9221b commit d49ef4e
Show file tree
Hide file tree
Showing 15 changed files with 67 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/Generators/PestTestGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,7 +25,7 @@

class PestTestGenerator extends AbstractClassGenerator implements Generator
{
use HandlesImports, HandlesTraits;
use HandlesImports;

const TESTS_VIEW = 1;

Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);";
}
}
3 changes: 3 additions & 0 deletions tests/fixtures/tests/pest/api-shorthand-validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
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;
use function Pest\Laravel\get;
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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'));

Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/tests/pest/certificate-pascal-case-example.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
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;
use function Pest\Laravel\get;
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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
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;
use function Pest\Laravel\get;
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();

Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/tests/pest/date-formats.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@

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;
use function Pest\Laravel\get;
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();

Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/tests/pest/full-crud-example.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
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;
use function Pest\Laravel\get;
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();

Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/tests/pest/models-with-custom-namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
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;
use function Pest\Laravel\get;
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();

Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/tests/pest/readme-example-notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/tests/pest/readme-example.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/tests/pest/reference-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/tests/pest/respond-statements.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/tests/pest/routes-with-pluralized-names.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
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;
use function Pest\Laravel\get;
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();

Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/tests/pest/routes-with-singular-names.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
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;
use function Pest\Laravel\get;
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();

Expand Down

0 comments on commit d49ef4e

Please sign in to comment.