diff --git a/src/Generators/TestGenerator.php b/src/Generators/TestGenerator.php index c64274ee..de561cbb 100644 --- a/src/Generators/TestGenerator.php +++ b/src/Generators/TestGenerator.php @@ -248,7 +248,7 @@ protected function buildTestCases(Controller $controller) $setup['data'][] = $faker; $request_data[$data] = '$' . $variable_name; - } else { + } elseif (! is_null($local_model)) { foreach ($local_model->columns() as $local_column) { if ($local_column->name() === 'id') { continue; diff --git a/src/Tree.php b/src/Tree.php index 1ca9ecce..90b354d1 100644 --- a/src/Tree.php +++ b/src/Tree.php @@ -46,7 +46,7 @@ public function modelForContext(string $context) } $matches = array_filter(array_keys($this->models), function ($key) use ($context) { - return Str::endsWith($key, '/' . Str::studly($context)); + return Str::endsWith(Str::afterLast(Str::afterLast($key, '\\'), '/'), [Str::studly($context), Str::studly(Str::plural($context))]); }); if (count($matches) === 1) { diff --git a/tests/Feature/Generators/TestGeneratorTest.php b/tests/Feature/Generators/TestGeneratorTest.php index 0d82f985..12bbe298 100644 --- a/tests/Feature/Generators/TestGeneratorTest.php +++ b/tests/Feature/Generators/TestGeneratorTest.php @@ -87,19 +87,25 @@ public function output_generates_test_for_controller_tree_l8($definition, $path, $this->filesystem->expects('stub') ->with('test.case.stub') ->andReturn($this->stub('test.case.stub')); - $dirname = dirname($path); - $this->filesystem->expects('exists') - ->with($dirname) - ->andReturnFalse(); - $this->filesystem->expects('makeDirectory') - ->with($dirname, 0755, true); - $this->filesystem->expects('put') + + $paths = collect($path)->combine($test)->toArray(); + foreach ($paths as $path => $test) { + $dirname = dirname($path); + + $this->filesystem->expects('exists') + ->with($dirname) + ->andReturnFalse(); + + $this->filesystem->expects('makeDirectory') + ->with($dirname, 0755, true); + + $this->filesystem->expects('put') ->with($path, $this->fixture($test)); + } $tokens = $this->blueprint->parse($this->fixture($definition)); $tree = $this->blueprint->analyze($tokens); - - $this->assertEquals(['created' => [$path]], $this->subject->output($tree)); + $this->assertEquals(['created' => array_keys($paths)], $this->subject->output($tree)); } /** @@ -362,6 +368,17 @@ public function laravel8ControllerTreeDataProvider() ['drafts/respond-statements.yaml', 'tests/Feature/Http/Controllers/Api/PostControllerTest.php', 'tests/respond-statements-laravel8.php'], ['drafts/full-crud-example.yaml', 'tests/Feature/Http/Controllers/PostControllerTest.php', 'tests/full-crud-example-laravel8.php'], ['drafts/model-reference-validate.yaml', 'tests/Feature/Http/Controllers/CertificateControllerTest.php', 'tests/api-shorthand-validation-laravel8.php'], + ['drafts/call-to-a-member-function-columns-on-null.yaml', [ + 'tests/Feature/Http/Controllers/SubscriptionControllerTest.php', + 'tests/Feature/Http/Controllers/TelegramControllerTest.php', + 'tests/Feature/Http/Controllers/PaymentControllerTest.php', + 'tests/Feature/Http/Controllers/Api/PaymentControllerTest.php' + ],[ + 'tests/call-to-a-member-function-columns-on-null-SubscriptionControllerTest-laravel8.php', + 'tests/call-to-a-member-function-columns-on-null-TelegramControllerTest-laravel8.php', + 'tests/call-to-a-member-function-columns-on-null-PaymentControllerTest-laravel8.php', + 'tests/call-to-a-member-function-columns-on-null-Api-PaymentControllerTest-laravel8.php', + ]], ]; } } diff --git a/tests/fixtures/drafts/call-to-a-member-function-columns-on-null.yaml b/tests/fixtures/drafts/call-to-a-member-function-columns-on-null.yaml new file mode 100644 index 00000000..0686da63 --- /dev/null +++ b/tests/fixtures/drafts/call-to-a-member-function-columns-on-null.yaml @@ -0,0 +1,40 @@ +models: + Models\Subscription: + source: string:400 + start_date: date + end_date: date + user_id: id foreign:users.id + payment_id: id foreign:payments.id + softDeletes: true + timestamps: true + + Models\Payment: + status: string + amount: decimal:8,3 + user_id: id foreign:users.id + softDeletes: true + timestamps: true + +controllers: + Subscription: + resource: index,show + + Telegram: + resource: all + + Payment: + create: + render: payment.create + store: + validate: status,amount,user_id + save: payment + fire: NewPayment with:payment + send: PaymentCreated to:payment.user with:payment + flash: message + redirect: payment.create + + Api\Payment: + store: + validate: payment + save: payment + respond: 204 \ No newline at end of file diff --git a/tests/fixtures/tests/call-to-a-member-function-columns-on-null-Api-PaymentControllerTest-laravel8.php b/tests/fixtures/tests/call-to-a-member-function-columns-on-null-Api-PaymentControllerTest-laravel8.php new file mode 100644 index 00000000..f717474b --- /dev/null +++ b/tests/fixtures/tests/call-to-a-member-function-columns-on-null-Api-PaymentControllerTest-laravel8.php @@ -0,0 +1,60 @@ +assertActionUsesFormRequest( + \App\Http\Controllers\Api\PaymentController::class, + 'store', + \App\Http\Requests\Api\PaymentStoreRequest::class + ); + } + + /** + * @test + */ + public function store_saves_and_responds_with() + { + $status = $this->faker->word; + $amount = $this->faker->randomFloat(/** decimal_attributes **/); + $user = User::factory()->create(); + + $response = $this->post(route('payment.store'), [ + 'status' => $status, + 'amount' => $amount, + 'user_id' => $user->id, + ]); + + $payments = Payment::query() + ->where('status', $status) + ->where('amount', $amount) + ->where('user_id', $user->id) + ->get(); + $this->assertCount(1, $payments); + $payment = $payments->first(); + + $response->assertNoContent(); + } +} diff --git a/tests/fixtures/tests/call-to-a-member-function-columns-on-null-PaymentControllerTest-laravel8.php b/tests/fixtures/tests/call-to-a-member-function-columns-on-null-PaymentControllerTest-laravel8.php new file mode 100644 index 00000000..69ad2ba4 --- /dev/null +++ b/tests/fixtures/tests/call-to-a-member-function-columns-on-null-PaymentControllerTest-laravel8.php @@ -0,0 +1,83 @@ +get(route('payment.create')); + + $response->assertOk(); + $response->assertViewIs('payment.create'); + } + + + /** + * @test + */ + public function store_uses_form_request_validation() + { + $this->assertActionUsesFormRequest( + \App\Http\Controllers\PaymentController::class, + 'store', + \App\Http\Requests\PaymentStoreRequest::class + ); + } + + /** + * @test + */ + public function store_saves_and_redirects() + { + $status = $this->faker->word; + $amount = $this->faker->randomFloat(/** decimal_attributes **/); + $user = User::factory()->create(); + + Event::fake(); + Mail::fake(); + + $response = $this->post(route('payment.store'), [ + 'status' => $status, + 'amount' => $amount, + 'user_id' => $user->id, + ]); + + $payments = Payment::query() + ->where('status', $status) + ->where('amount', $amount) + ->where('user_id', $user->id) + ->get(); + $this->assertCount(1, $payments); + $payment = $payments->first(); + + $response->assertRedirect(route('payment.create')); + $response->assertSessionHas('message', $message); + + Event::assertDispatched(NewPayment::class, function ($event) use ($payment) { + return $event->payment->is($payment); + }); + Mail::assertSent(PaymentCreated::class, function ($mail) use ($payment) { + return $mail->hasTo($payment->user) && $mail->payment->is($payment); + }); + } +} diff --git a/tests/fixtures/tests/call-to-a-member-function-columns-on-null-SubscriptionControllerTest-laravel8.php b/tests/fixtures/tests/call-to-a-member-function-columns-on-null-SubscriptionControllerTest-laravel8.php new file mode 100644 index 00000000..d81a4bc8 --- /dev/null +++ b/tests/fixtures/tests/call-to-a-member-function-columns-on-null-SubscriptionControllerTest-laravel8.php @@ -0,0 +1,44 @@ +count(3)->create(); + + $response = $this->get(route('subscription.index')); + + $response->assertOk(); + $response->assertViewIs('subscription.index'); + $response->assertViewHas('subscriptions'); + } + + + /** + * @test + */ + public function show_displays_view() + { + $subscription = Subscription::factory()->create(); + + $response = $this->get(route('subscription.show', $subscription)); + + $response->assertOk(); + $response->assertViewIs('subscription.show'); + $response->assertViewHas('subscription'); + } +} diff --git a/tests/fixtures/tests/call-to-a-member-function-columns-on-null-TelegramControllerTest-laravel8.php b/tests/fixtures/tests/call-to-a-member-function-columns-on-null-TelegramControllerTest-laravel8.php new file mode 100644 index 00000000..ecd7765d --- /dev/null +++ b/tests/fixtures/tests/call-to-a-member-function-columns-on-null-TelegramControllerTest-laravel8.php @@ -0,0 +1,13 @@ +