From fbe83866175cbeffd0a6f4ddac19c7b746ee0400 Mon Sep 17 00:00:00 2001 From: Nathanael Esayeas Date: Tue, 18 May 2021 16:58:49 -0400 Subject: [PATCH 1/5] Add draft file Signed-off-by: Nathanael Esayeas --- ...-to-a-member-function-columns-on-null.yaml | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 tests/fixtures/drafts/call-to-a-member-function-columns-on-null.yaml 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 From 44df68313e06e5abae177e0d7cee03cb329aa9a4 Mon Sep 17 00:00:00 2001 From: Nathanael Esayeas Date: Tue, 18 May 2021 17:01:04 -0400 Subject: [PATCH 2/5] Use the correct model Signed-off-by: Nathanael Esayeas --- src/Generators/TestGenerator.php | 2 +- src/Tree.php | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Generators/TestGenerator.php b/src/Generators/TestGenerator.php index c64274ee..df023110 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 { + }else if (! 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..64cd8cfb 100644 --- a/src/Tree.php +++ b/src/Tree.php @@ -46,7 +46,11 @@ 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) { From 26886d9a7b958c05f126f5a6ab792a443904a883 Mon Sep 17 00:00:00 2001 From: Nathanael Esayeas Date: Tue, 18 May 2021 17:01:44 -0400 Subject: [PATCH 3/5] Add tests and fixtures Signed-off-by: Nathanael Esayeas --- .../Feature/Generators/TestGeneratorTest.php | 35 ++++++-- ...ull-Api-PaymentControllerTest-laravel8.php | 60 ++++++++++++++ ...on-null-PaymentControllerTest-laravel8.php | 83 +++++++++++++++++++ ...ll-SubscriptionControllerTest-laravel8.php | 44 ++++++++++ ...n-null-TelegramControllerTest-laravel8.php | 13 +++ 5 files changed, 226 insertions(+), 9 deletions(-) create mode 100644 tests/fixtures/tests/call-to-a-member-function-columns-on-null-Api-PaymentControllerTest-laravel8.php create mode 100644 tests/fixtures/tests/call-to-a-member-function-columns-on-null-PaymentControllerTest-laravel8.php create mode 100644 tests/fixtures/tests/call-to-a-member-function-columns-on-null-SubscriptionControllerTest-laravel8.php create mode 100644 tests/fixtures/tests/call-to-a-member-function-columns-on-null-TelegramControllerTest-laravel8.php diff --git a/tests/Feature/Generators/TestGeneratorTest.php b/tests/Feature/Generators/TestGeneratorTest.php index 0d82f985..a8ee5e28 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/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 @@ + Date: Tue, 18 May 2021 17:10:58 -0400 Subject: [PATCH 4/5] Fix StyleCI Issues Signed-off-by: Nathanael Esayeas --- src/Generators/TestGenerator.php | 2 +- src/Tree.php | 8 +++++--- tests/Feature/Generators/TestGeneratorTest.php | 2 +- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/Generators/TestGenerator.php b/src/Generators/TestGenerator.php index df023110..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 if (! is_null($local_model)){ + } 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 64cd8cfb..ff7cc96a 100644 --- a/src/Tree.php +++ b/src/Tree.php @@ -47,10 +47,12 @@ public function modelForContext(string $context) $matches = array_filter(array_keys($this->models), function ($key) use ($context) { return Str::endsWith( - Str::afterLast(Str::afterLast($key, '\\'), '/'),[ - Str::studly($context), + 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 a8ee5e28..12bbe298 100644 --- a/tests/Feature/Generators/TestGeneratorTest.php +++ b/tests/Feature/Generators/TestGeneratorTest.php @@ -89,7 +89,7 @@ public function output_generates_test_for_controller_tree_l8($definition, $path, ->andReturn($this->stub('test.case.stub')); $paths = collect($path)->combine($test)->toArray(); - foreach($paths as $path => $test){ + foreach ($paths as $path => $test) { $dirname = dirname($path); $this->filesystem->expects('exists') From dc36089228e685cc697d61acdff2bb2ed0ec6a67 Mon Sep 17 00:00:00 2001 From: Jason McCreary Date: Wed, 19 May 2021 16:26:49 -0400 Subject: [PATCH 5/5] Formatting --- src/Tree.php | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/Tree.php b/src/Tree.php index ff7cc96a..90b354d1 100644 --- a/src/Tree.php +++ b/src/Tree.php @@ -46,13 +46,7 @@ public function modelForContext(string $context) } $matches = array_filter(array_keys($this->models), function ($key) use ($context) { - return Str::endsWith( - Str::afterLast(Str::afterLast($key, '\\'), '/'), - [ - Str::studly($context), - Str::studly(Str::plural($context)), - ] - ); + return Str::endsWith(Str::afterLast(Str::afterLast($key, '\\'), '/'), [Str::studly($context), Str::studly(Str::plural($context))]); }); if (count($matches) === 1) {