Skip to content

Commit e446a41

Browse files
authored
Determine correct model for context (#477)
1 parent d93983d commit e446a41

8 files changed

+268
-11
lines changed

src/Generators/TestGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ protected function buildTestCases(Controller $controller)
248248

249249
$setup['data'][] = $faker;
250250
$request_data[$data] = '$' . $variable_name;
251-
} else {
251+
} elseif (! is_null($local_model)) {
252252
foreach ($local_model->columns() as $local_column) {
253253
if ($local_column->name() === 'id') {
254254
continue;

src/Tree.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function modelForContext(string $context)
4646
}
4747

4848
$matches = array_filter(array_keys($this->models), function ($key) use ($context) {
49-
return Str::endsWith($key, '/' . Str::studly($context));
49+
return Str::endsWith(Str::afterLast(Str::afterLast($key, '\\'), '/'), [Str::studly($context), Str::studly(Str::plural($context))]);
5050
});
5151

5252
if (count($matches) === 1) {

tests/Feature/Generators/TestGeneratorTest.php

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,19 +87,25 @@ public function output_generates_test_for_controller_tree_l8($definition, $path,
8787
$this->filesystem->expects('stub')
8888
->with('test.case.stub')
8989
->andReturn($this->stub('test.case.stub'));
90-
$dirname = dirname($path);
91-
$this->filesystem->expects('exists')
92-
->with($dirname)
93-
->andReturnFalse();
94-
$this->filesystem->expects('makeDirectory')
95-
->with($dirname, 0755, true);
96-
$this->filesystem->expects('put')
90+
91+
$paths = collect($path)->combine($test)->toArray();
92+
foreach ($paths as $path => $test) {
93+
$dirname = dirname($path);
94+
95+
$this->filesystem->expects('exists')
96+
->with($dirname)
97+
->andReturnFalse();
98+
99+
$this->filesystem->expects('makeDirectory')
100+
->with($dirname, 0755, true);
101+
102+
$this->filesystem->expects('put')
97103
->with($path, $this->fixture($test));
104+
}
98105

99106
$tokens = $this->blueprint->parse($this->fixture($definition));
100107
$tree = $this->blueprint->analyze($tokens);
101-
102-
$this->assertEquals(['created' => [$path]], $this->subject->output($tree));
108+
$this->assertEquals(['created' => array_keys($paths)], $this->subject->output($tree));
103109
}
104110

105111
/**
@@ -362,6 +368,17 @@ public function laravel8ControllerTreeDataProvider()
362368
['drafts/respond-statements.yaml', 'tests/Feature/Http/Controllers/Api/PostControllerTest.php', 'tests/respond-statements-laravel8.php'],
363369
['drafts/full-crud-example.yaml', 'tests/Feature/Http/Controllers/PostControllerTest.php', 'tests/full-crud-example-laravel8.php'],
364370
['drafts/model-reference-validate.yaml', 'tests/Feature/Http/Controllers/CertificateControllerTest.php', 'tests/api-shorthand-validation-laravel8.php'],
371+
['drafts/call-to-a-member-function-columns-on-null.yaml', [
372+
'tests/Feature/Http/Controllers/SubscriptionControllerTest.php',
373+
'tests/Feature/Http/Controllers/TelegramControllerTest.php',
374+
'tests/Feature/Http/Controllers/PaymentControllerTest.php',
375+
'tests/Feature/Http/Controllers/Api/PaymentControllerTest.php'
376+
],[
377+
'tests/call-to-a-member-function-columns-on-null-SubscriptionControllerTest-laravel8.php',
378+
'tests/call-to-a-member-function-columns-on-null-TelegramControllerTest-laravel8.php',
379+
'tests/call-to-a-member-function-columns-on-null-PaymentControllerTest-laravel8.php',
380+
'tests/call-to-a-member-function-columns-on-null-Api-PaymentControllerTest-laravel8.php',
381+
]],
365382
];
366383
}
367384
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
models:
2+
Models\Subscription:
3+
source: string:400
4+
start_date: date
5+
end_date: date
6+
user_id: id foreign:users.id
7+
payment_id: id foreign:payments.id
8+
softDeletes: true
9+
timestamps: true
10+
11+
Models\Payment:
12+
status: string
13+
amount: decimal:8,3
14+
user_id: id foreign:users.id
15+
softDeletes: true
16+
timestamps: true
17+
18+
controllers:
19+
Subscription:
20+
resource: index,show
21+
22+
Telegram:
23+
resource: all
24+
25+
Payment:
26+
create:
27+
render: payment.create
28+
store:
29+
validate: status,amount,user_id
30+
save: payment
31+
fire: NewPayment with:payment
32+
send: PaymentCreated to:payment.user with:payment
33+
flash: message
34+
redirect: payment.create
35+
36+
Api\Payment:
37+
store:
38+
validate: payment
39+
save: payment
40+
respond: 204
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace Tests\Feature\Http\Controllers\Api;
4+
5+
use App\Events\NewPayment;
6+
use App\Mail\PaymentCreated;
7+
use App\Payment;
8+
use App\User;
9+
use Illuminate\Foundation\Testing\RefreshDatabase;
10+
use Illuminate\Foundation\Testing\WithFaker;
11+
use Illuminate\Support\Facades\Event;
12+
use Illuminate\Support\Facades\Mail;
13+
use JMac\Testing\Traits\AdditionalAssertions;
14+
use Tests\TestCase;
15+
16+
/**
17+
* @see \App\Http\Controllers\Api\PaymentController
18+
*/
19+
class PaymentControllerTest extends TestCase
20+
{
21+
use AdditionalAssertions, RefreshDatabase, WithFaker;
22+
23+
/**
24+
* @test
25+
*/
26+
public function store_uses_form_request_validation()
27+
{
28+
$this->assertActionUsesFormRequest(
29+
\App\Http\Controllers\Api\PaymentController::class,
30+
'store',
31+
\App\Http\Requests\Api\PaymentStoreRequest::class
32+
);
33+
}
34+
35+
/**
36+
* @test
37+
*/
38+
public function store_saves_and_responds_with()
39+
{
40+
$status = $this->faker->word;
41+
$amount = $this->faker->randomFloat(/** decimal_attributes **/);
42+
$user = User::factory()->create();
43+
44+
$response = $this->post(route('payment.store'), [
45+
'status' => $status,
46+
'amount' => $amount,
47+
'user_id' => $user->id,
48+
]);
49+
50+
$payments = Payment::query()
51+
->where('status', $status)
52+
->where('amount', $amount)
53+
->where('user_id', $user->id)
54+
->get();
55+
$this->assertCount(1, $payments);
56+
$payment = $payments->first();
57+
58+
$response->assertNoContent();
59+
}
60+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
namespace Tests\Feature\Http\Controllers;
4+
5+
use App\Events\NewPayment;
6+
use App\Mail\PaymentCreated;
7+
use App\Payment;
8+
use App\User;
9+
use Illuminate\Foundation\Testing\RefreshDatabase;
10+
use Illuminate\Foundation\Testing\WithFaker;
11+
use Illuminate\Support\Facades\Event;
12+
use Illuminate\Support\Facades\Mail;
13+
use JMac\Testing\Traits\AdditionalAssertions;
14+
use Tests\TestCase;
15+
16+
/**
17+
* @see \App\Http\Controllers\PaymentController
18+
*/
19+
class PaymentControllerTest extends TestCase
20+
{
21+
use AdditionalAssertions, RefreshDatabase, WithFaker;
22+
23+
/**
24+
* @test
25+
*/
26+
public function create_displays_view()
27+
{
28+
$response = $this->get(route('payment.create'));
29+
30+
$response->assertOk();
31+
$response->assertViewIs('payment.create');
32+
}
33+
34+
35+
/**
36+
* @test
37+
*/
38+
public function store_uses_form_request_validation()
39+
{
40+
$this->assertActionUsesFormRequest(
41+
\App\Http\Controllers\PaymentController::class,
42+
'store',
43+
\App\Http\Requests\PaymentStoreRequest::class
44+
);
45+
}
46+
47+
/**
48+
* @test
49+
*/
50+
public function store_saves_and_redirects()
51+
{
52+
$status = $this->faker->word;
53+
$amount = $this->faker->randomFloat(/** decimal_attributes **/);
54+
$user = User::factory()->create();
55+
56+
Event::fake();
57+
Mail::fake();
58+
59+
$response = $this->post(route('payment.store'), [
60+
'status' => $status,
61+
'amount' => $amount,
62+
'user_id' => $user->id,
63+
]);
64+
65+
$payments = Payment::query()
66+
->where('status', $status)
67+
->where('amount', $amount)
68+
->where('user_id', $user->id)
69+
->get();
70+
$this->assertCount(1, $payments);
71+
$payment = $payments->first();
72+
73+
$response->assertRedirect(route('payment.create'));
74+
$response->assertSessionHas('message', $message);
75+
76+
Event::assertDispatched(NewPayment::class, function ($event) use ($payment) {
77+
return $event->payment->is($payment);
78+
});
79+
Mail::assertSent(PaymentCreated::class, function ($mail) use ($payment) {
80+
return $mail->hasTo($payment->user) && $mail->payment->is($payment);
81+
});
82+
}
83+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Tests\Feature\Http\Controllers;
4+
5+
use App\Subscription;
6+
use Illuminate\Foundation\Testing\RefreshDatabase;
7+
use Tests\TestCase;
8+
9+
/**
10+
* @see \App\Http\Controllers\SubscriptionController
11+
*/
12+
class SubscriptionControllerTest extends TestCase
13+
{
14+
use RefreshDatabase;
15+
16+
/**
17+
* @test
18+
*/
19+
public function index_displays_view()
20+
{
21+
$subscriptions = Subscription::factory()->count(3)->create();
22+
23+
$response = $this->get(route('subscription.index'));
24+
25+
$response->assertOk();
26+
$response->assertViewIs('subscription.index');
27+
$response->assertViewHas('subscriptions');
28+
}
29+
30+
31+
/**
32+
* @test
33+
*/
34+
public function show_displays_view()
35+
{
36+
$subscription = Subscription::factory()->create();
37+
38+
$response = $this->get(route('subscription.show', $subscription));
39+
40+
$response->assertOk();
41+
$response->assertViewIs('subscription.show');
42+
$response->assertViewHas('subscription');
43+
}
44+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Tests\Feature\Http\Controllers;
4+
5+
use Tests\TestCase;
6+
7+
/**
8+
* @see \App\Http\Controllers\TelegramController
9+
*/
10+
class TelegramControllerTest extends TestCase
11+
{
12+
13+
}

0 commit comments

Comments
 (0)