Skip to content

Commit bb11d59

Browse files
authored
Fix for variables without context (#553)
1 parent ec1bdde commit bb11d59

File tree

4 files changed

+61
-4
lines changed

4 files changed

+61
-4
lines changed

src/Generators/TestGenerator.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ protected function buildTestCases(Controller $controller)
125125
$assertion .= ', function ($notification)';
126126

127127
foreach ($statement->data() as $data) {
128-
if (Str::studly(Str::singular($data)) === $context) {
128+
if (Str::studly(Str::singular($data)) === $context || ! Str::contains($data, '.')) {
129129
$variables[] .= '$' . $data;
130130
$conditions[] .= sprintf('$notification->%s->is($%s)', $data, $data);
131131
} else {
@@ -166,7 +166,7 @@ protected function buildTestCases(Controller $controller)
166166
}
167167

168168
foreach ($statement->data() as $data) {
169-
if (Str::studly(Str::singular($data)) === $context) {
169+
if (Str::studly(Str::singular($data)) === $context || ! Str::contains($data, '.')) {
170170
$variables[] .= '$' . $data;
171171
$conditions[] .= sprintf('$mail->%s->is($%s)', $data, $data);
172172
} else {
@@ -262,7 +262,7 @@ protected function buildTestCases(Controller $controller)
262262
$assertion .= ', function ($job)';
263263

264264
foreach ($statement->data() as $data) {
265-
if (Str::studly(Str::singular($data)) === $context) {
265+
if (Str::studly(Str::singular($data)) === $context || ! Str::contains($data, '.')) {
266266
$variables[] .= '$' . $data;
267267
$conditions[] .= sprintf('$job->%s->is($%s)', $data, $data);
268268
} else {
@@ -305,7 +305,7 @@ protected function buildTestCases(Controller $controller)
305305
$assertion .= ', function ($event)';
306306

307307
foreach ($statement->data() as $data) {
308-
if (Str::studly(Str::singular($data)) === $context) {
308+
if (Str::studly(Str::singular($data)) === $context || ! Str::contains($data, '.')) {
309309
$variables[] .= '$' . $data;
310310
$conditions[] .= sprintf('$event->%s->is($%s)', $data, $data);
311311
} else {

tests/Feature/Generators/TestGeneratorTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ public function controllerTreeDataProvider()
220220
['drafts/respond-statements.yaml', 'tests/Feature/Http/Controllers/Api/PostControllerTest.php', 'tests/respond-statements.php'],
221221
['drafts/full-crud-example.yaml', 'tests/Feature/Http/Controllers/PostControllerTest.php', 'tests/full-crud-example.php'],
222222
['drafts/model-reference-validate.yaml', 'tests/Feature/Http/Controllers/CertificateControllerTest.php', 'tests/api-shorthand-validation.php'],
223+
['drafts/controllers-only-no-context.yaml', 'tests/Feature/Http/Controllers/ReportControllerTest.php', 'tests/controllers-only-no-context.php'],
223224
['drafts/call-to-a-member-function-columns-on-null.yaml', [
224225
'tests/Feature/Http/Controllers/SubscriptionControllerTest.php',
225226
'tests/Feature/Http/Controllers/TelegramControllerTest.php',
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
controllers:
2+
Report:
3+
invokable:
4+
dispatch: GenerateReport with:event
5+
fire: ExportReport with:event
6+
notify: auth.user ReportGenerated with:event
7+
send: SendReport with:event
8+
render: report
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Tests\Feature\Http\Controllers;
4+
5+
use App\Events\ExportReport;
6+
use App\Jobs\GenerateReport;
7+
use App\Mail\SendReport;
8+
use App\Notification\ReportGenerated;
9+
use Illuminate\Support\Facades\Event;
10+
use Illuminate\Support\Facades\Mail;
11+
use Illuminate\Support\Facades\Notification;
12+
use Illuminate\Support\Facades\Queue;
13+
use Tests\TestCase;
14+
15+
/**
16+
* @see \App\Http\Controllers\ReportController
17+
*/
18+
class ReportControllerTest extends TestCase
19+
{
20+
/**
21+
* @test
22+
*/
23+
public function __invoke_displays_view()
24+
{
25+
Queue::fake();
26+
Event::fake();
27+
Notification::fake();
28+
Mail::fake();
29+
30+
$response = $this->get(route('report.__invoke'));
31+
32+
$response->assertOk();
33+
$response->assertViewIs('report');
34+
35+
Queue::assertPushed(GenerateReport::class, function ($job) use ($event) {
36+
return $job->event->is($event);
37+
});
38+
Event::assertDispatched(ExportReport::class, function ($event) use ($event) {
39+
return $event->event->is($event);
40+
});
41+
Notification::assertSentTo($auth->user, ReportGenerated::class, function ($notification) use ($event) {
42+
return $notification->event->is($event);
43+
});
44+
Mail::assertSent(SendReport::class, function ($mail) use ($event) {
45+
return $mail->event->is($event);
46+
});
47+
}
48+
}

0 commit comments

Comments
 (0)