Skip to content

Commit d8fc2c3

Browse files
committed
Strengthen assertions
1 parent 17308ff commit d8fc2c3

15 files changed

+49
-29
lines changed

src/Generators/PestTestGenerator.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,13 @@ protected function buildTestCases(Controller $controller): string
375375
$view_assertions[] = sprintf('$response->assertViewIs(\'%s\');', $statement->view());
376376

377377
foreach ($statement->data() as $data) {
378-
// TODO: if data references locally scoped var, strengthen assertion...
379-
$view_assertions[] = sprintf('$response->assertViewHas(\'%s\');', $data);
378+
$assertion = sprintf('$response->assertViewHas(\'%s\'', $data);
379+
if ($this->hasLocalVariable($setup['data'], $data)) {
380+
$assertion .= sprintf(', $%s', $data);
381+
}
382+
383+
$assertion .= ');';
384+
$view_assertions[] = $assertion;
380385
}
381386

382387
array_unshift($assertions['response'], ...$view_assertions);
@@ -708,4 +713,9 @@ private function importAdditionalAssertionsToBaseTest(): void
708713

709714
$this->filesystem->put($fullPath, $updatedContent);
710715
}
716+
717+
private function hasLocalVariable(array $locals, string $name): bool
718+
{
719+
return collect($locals)->contains(fn ($local) => str_starts_with($local, '$' . $name . ' = '));
720+
}
711721
}

src/Generators/PhpUnitTestGenerator.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,8 +371,13 @@ protected function buildTestCases(Controller $controller): string
371371
$view_assertions[] = sprintf('$response->assertViewIs(\'%s\');', $statement->view());
372372

373373
foreach ($statement->data() as $data) {
374-
// TODO: if data references locally scoped var, strengthen assertion...
375-
$view_assertions[] = sprintf('$response->assertViewHas(\'%s\');', $data);
374+
$assertion = sprintf('$response->assertViewHas(\'%s\'', $data);
375+
if ($this->hasLocalVariable($setup['data'], $data)) {
376+
$assertion .= sprintf(', $%s', $data);
377+
}
378+
379+
$assertion .= ');';
380+
$view_assertions[] = $assertion;
376381
}
377382

378383
array_unshift($assertions['response'], ...$view_assertions);
@@ -581,6 +586,11 @@ private function addFakerTrait(Controller $controller): void
581586
$this->addTrait($controller, 'WithFaker');
582587
}
583588

589+
private function hasLocalVariable(array $locals, string $name): bool
590+
{
591+
return collect($locals)->contains(fn ($local) => str_starts_with($local, '$' . $name . ' = '));
592+
}
593+
584594
private function splitField($field): array
585595
{
586596
if (Str::contains($field, '.')) {

tests/fixtures/tests/pest/call-to-a-member-function-columns-on-null-SubscriptionControllerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
$response->assertOk();
1414
$response->assertViewIs('subscription.index');
15-
$response->assertViewHas('subscriptions');
15+
$response->assertViewHas('subscriptions', $subscriptions);
1616
});
1717

1818

@@ -23,5 +23,5 @@
2323

2424
$response->assertOk();
2525
$response->assertViewIs('subscription.show');
26-
$response->assertViewHas('subscription');
26+
$response->assertViewHas('subscription', $subscription);
2727
});

tests/fixtures/tests/pest/crud-show-only.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212

1313
$response->assertOk();
1414
$response->assertViewIs('posts.show');
15-
$response->assertViewHas('post');
15+
$response->assertViewHas('post', $post);
1616
});

tests/fixtures/tests/pest/date-formats.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
$response->assertOk();
2020
$response->assertViewIs('date.index');
21-
$response->assertViewHas('dates');
21+
$response->assertViewHas('dates', $dates);
2222
});
2323

2424

@@ -68,7 +68,7 @@
6868

6969
$response->assertOk();
7070
$response->assertViewIs('date.show');
71-
$response->assertViewHas('date');
71+
$response->assertViewHas('date', $date);
7272
});
7373

7474

@@ -79,7 +79,7 @@
7979

8080
$response->assertOk();
8181
$response->assertViewIs('date.edit');
82-
$response->assertViewHas('date');
82+
$response->assertViewHas('date', $date);
8383
});
8484

8585

tests/fixtures/tests/pest/full-crud-example.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
$response->assertOk();
1919
$response->assertViewIs('posts.index');
20-
$response->assertViewHas('posts');
20+
$response->assertViewHas('posts', $posts);
2121
});
2222

2323

@@ -64,7 +64,7 @@
6464

6565
$response->assertOk();
6666
$response->assertViewIs('posts.show');
67-
$response->assertViewHas('post');
67+
$response->assertViewHas('post', $post);
6868
});
6969

7070

@@ -75,7 +75,7 @@
7575

7676
$response->assertOk();
7777
$response->assertViewIs('posts.edit');
78-
$response->assertViewHas('post');
78+
$response->assertViewHas('post', $post);
7979
});
8080

8181

tests/fixtures/tests/pest/readme-example-notification.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
$response->assertOk();
2222
$response->assertViewIs('post.index');
23-
$response->assertViewHas('posts');
23+
$response->assertViewHas('posts', $posts);
2424
});
2525

2626

tests/fixtures/tests/pest/readme-example.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
$response->assertOk();
2323
$response->assertViewIs('post.index');
24-
$response->assertViewHas('posts');
24+
$response->assertViewHas('posts', $posts);
2525
});
2626

2727

tests/fixtures/tests/phpunit/call-to-a-member-function-columns-on-null-SubscriptionControllerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function index_displays_view(): void
2323

2424
$response->assertOk();
2525
$response->assertViewIs('subscription.index');
26-
$response->assertViewHas('subscriptions');
26+
$response->assertViewHas('subscriptions', $subscriptions);
2727
}
2828

2929

@@ -36,6 +36,6 @@ public function show_displays_view(): void
3636

3737
$response->assertOk();
3838
$response->assertViewIs('subscription.show');
39-
$response->assertViewHas('subscription');
39+
$response->assertViewHas('subscription', $subscription);
4040
}
4141
}

tests/fixtures/tests/phpunit/crud-show-only.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ public function show_displays_view(): void
2020

2121
$response->assertOk();
2222
$response->assertViewIs('posts.show');
23-
$response->assertViewHas('post');
23+
$response->assertViewHas('post', $post);
2424
}
2525
}

0 commit comments

Comments
 (0)