From 735097d722f395ef08ed8fdaecd302c628298bcb Mon Sep 17 00:00:00 2001 From: AVMG20 Date: Mon, 12 Jul 2021 13:44:26 +0200 Subject: [PATCH] tested voucher controller --- .../Controllers/Admin/VoucherController.php | 12 +- database/factories/VoucherFactory.php | 7 +- tests/Feature/TestVouchersController.php | 318 ++++++++++++++++++ 3 files changed, 330 insertions(+), 7 deletions(-) create mode 100644 tests/Feature/TestVouchersController.php diff --git a/app/Http/Controllers/Admin/VoucherController.php b/app/Http/Controllers/Admin/VoucherController.php index d90b22c7..e6f24255 100644 --- a/app/Http/Controllers/Admin/VoucherController.php +++ b/app/Http/Controllers/Admin/VoucherController.php @@ -45,10 +45,10 @@ class VoucherController extends Controller { $request->validate([ 'memo' => 'nullable|string|max:191', - 'code' => 'required|string|alpha_dash|max:36', - 'uses' => 'required|numeric|max:2147483647', + 'code' => 'required|string|alpha_dash|max:36|min:4', + 'uses' => 'required|numeric|max:2147483647|min:1', 'credits' => 'required|numeric|between:0,99999999', - 'expires_at' => ['nullable','date_format:d-m-Y','after:today',"before:1000 years"], + 'expires_at' => ['nullable','date_format:d-m-Y','after:today',"before:10 years"], ]); Voucher::create($request->except('_token')); @@ -91,10 +91,10 @@ class VoucherController extends Controller { $request->validate([ 'memo' => 'nullable|string|max:191', - 'code' => 'required|string|alpha_dash|max:36', - 'uses' => 'required|numeric|max:2147483647', + 'code' => 'required|string|alpha_dash|max:36|min:4', + 'uses' => 'required|numeric|max:2147483647|min:1', 'credits' => 'required|numeric|between:0,99999999', - 'expires_at' => ['nullable','date_format:d-m-Y','after:today',"before:1000 years"], + 'expires_at' => ['nullable','date_format:d-m-Y','after:today',"before:10 years"], ]); $voucher->update($request->except('_token')); diff --git a/database/factories/VoucherFactory.php b/database/factories/VoucherFactory.php index 4f4a2f2b..9a381296 100644 --- a/database/factories/VoucherFactory.php +++ b/database/factories/VoucherFactory.php @@ -4,6 +4,7 @@ namespace Database\Factories; use App\Models\voucher; use Illuminate\Database\Eloquent\Factories\Factory; +use Illuminate\Support\Str; class VoucherFactory extends Factory { @@ -22,8 +23,12 @@ class VoucherFactory extends Factory public function definition() { return [ + 'memo' => $this->faker->word(), + 'code' => Str::random(36), 'credits' => $this->faker->numberBetween(100, 1000), - 'expires_at' => $this->faker->dateTimeBetween(now(), '+30 days') + 'uses' => $this->faker->numberBetween(1, 1000), + 'expires_at' => now()->addDays($this->faker->numberBetween(1, 90))->format('d-m-Y') ]; + } } diff --git a/tests/Feature/TestVouchersController.php b/tests/Feature/TestVouchersController.php new file mode 100644 index 00000000..30188071 --- /dev/null +++ b/tests/Feature/TestVouchersController.php @@ -0,0 +1,318 @@ +create([ + 'id' => 1 + ]); + + $response = $this->actingAs(User::factory()->create([ + 'role' => 'admin', + 'pterodactyl_id' => '1' + ]))->{$method}($route); + + $response->assertStatus($expectedStatus); + } + + /** + * @dataProvider VoucherDataProvider + * @param array $dataSet + * @param int $expectedCount + * @param bool $assertValidationErrors + */ + function test_creating_vouchers(array $dataSet, int $expectedCount, bool $assertValidationErrors) + { + $response = $this->actingAs($this->getTestUser())->post(route('admin.vouchers.store'), $dataSet); + + if ($assertValidationErrors) $response->assertSessionHasErrors(); + else $response->assertSessionHasNoErrors(); + + $response->assertRedirect(); + $this->assertDatabaseCount('vouchers', $expectedCount); + } + + /** + * @return User + */ + private function getTestUser(): User + { + return User::factory()->create([ + 'role' => 'admin', + 'pterodactyl_id' => '1' + ]); + } + + /** + * @dataProvider VoucherDataProvider + * @param array $dataSet + * @param int $expectedCount + * @param bool $assertValidationErrors + */ + function test_updating_voucher(array $dataSet, int $expectedCount, bool $assertValidationErrors) + { + $voucher = Voucher::factory()->create([ + 'id' => 1 + ]); + + $response = $this->actingAs($this->getTestUser())->patch(route('admin.vouchers.update', $voucher->id), $dataSet); + + if ($assertValidationErrors) $response->assertSessionHasErrors(); + else $response->assertSessionHasNoErrors(); + + $response->assertRedirect(); + $this->assertDatabaseCount('vouchers', 1); + } + + /** + * + */ + function test_deleting_vouchers() + { + $voucher = Voucher::factory()->create([ + 'id' => 1 + ]); + + $response = $this->actingAs($this->getTestUser())->delete(route('admin.vouchers.update', $voucher->id)); + + $response->assertRedirect(); + $this->assertDatabaseCount('vouchers', 0); + } + + /** + * @return array + */ + function VoucherDataProvider(): array + { + return [ + 'Valid dataset 1' => [ + 'dataSet' => [ + "memo" => "TESTING", + "code" => Str::random(20), + "credits" => 500, + "uses" => 500, + "expires_at" => now()->addDay()->format('d-m-Y'), + ], + 'expectedCount' => 1, + 'assertValidationErrors' => false + ], + 'Valid dataset 2' => [ + 'dataSet' => [ + "code" => Str::random(36), + "credits" => 500, + "uses" => 500, + ], + 'expectedCount' => 1, + 'assertValidationErrors' => false + ], + 'Valid dataset 3' => [ + 'dataSet' => [ + "memo" => "TESTING", + "code" => Str::random(4), + "credits" => 1000000, + "uses" => 1, + "expires_at" => now()->addYears(6)->format('d-m-Y'), + ], + 'expectedCount' => 1, + 'assertValidationErrors' => false + ], + 'Invalid dataset (memo to long)' => [ + 'dataSet' => [ + "memo" => Str::random(250), + "code" => Str::random(20), + "credits" => 500, + "uses" => 500, + "expires_at" => now()->addDay()->format('d-m-Y'), + ], + 'expectedCount' => 0, + 'assertValidationErrors' => true + ], + 'Invalid dataset (code to short)' => [ + 'dataSet' => [ + "memo" => Str::random(250), + "code" => Str::random(1), + "credits" => 500, + "uses" => 500, + "expires_at" => now()->addDay()->format('d-m-Y'), + ], + 'expectedCount' => 0, + 'assertValidationErrors' => true + ], + 'Invalid dataset (code missing)' => [ + 'dataSet' => [ + "memo" => Str::random(250), + "credits" => 500, + "uses" => 500, + "expires_at" => now()->addDay()->format('d-m-Y'), + ], + 'expectedCount' => 0, + 'assertValidationErrors' => true + ], + 'Invalid dataset (code to long)' => [ + 'dataSet' => [ + "memo" => Str::random(250), + "code" => Str::random(60), + "credits" => 500, + "uses" => 500, + "expires_at" => now()->addDay()->format('d-m-Y'), + ], + 'expectedCount' => 0, + 'assertValidationErrors' => true + ], + 'Invalid dataset (credits missing)' => [ + 'dataSet' => [ + "memo" => Str::random(250), + "code" => Str::random(1), + "uses" => 500, + "expires_at" => now()->addDay()->format('d-m-Y'), + ], + 'expectedCount' => 0, + 'assertValidationErrors' => true + ], + 'Invalid dataset (0 credits)' => [ + 'dataSet' => [ + "memo" => Str::random(250), + "code" => Str::random(1), + "credits" => 0, + "uses" => 500, + "expires_at" => now()->addDay()->format('d-m-Y'), + ], + 'expectedCount' => 0, + 'assertValidationErrors' => true + ], + 'Invalid dataset (to many credits)' => [ + 'dataSet' => [ + "memo" => Str::random(250), + "code" => Str::random(1), + "credits" => 99999999999, + "uses" => 500, + "expires_at" => now()->addDay()->format('d-m-Y'), + ], + 'expectedCount' => 0, + 'assertValidationErrors' => true + ], + 'Invalid dataset (uses missing)' => [ + 'dataSet' => [ + "memo" => Str::random(250), + "code" => Str::random(1), + "credits" => 99999999999, + "expires_at" => now()->addDay()->format('d-m-Y'), + ], + 'expectedCount' => 0, + 'assertValidationErrors' => true + ], + 'Invalid dataset (0 uses)' => [ + 'dataSet' => [ + "memo" => Str::random(250), + "code" => Str::random(1), + "credits" => 99999999999, + "uses" => 0, + "expires_at" => now()->addDay()->format('d-m-Y'), + ], + 'expectedCount' => 0, + 'assertValidationErrors' => true + ], + 'Invalid dataset (expires_at today)' => [ + 'dataSet' => [ + "memo" => Str::random(250), + "code" => Str::random(1), + "credits" => 99999999999, + "uses" => 500, + "expires_at" => now()->format('d-m-Y'), + ], + 'expectedCount' => 0, + 'assertValidationErrors' => true + ], + 'Invalid dataset (expires_at earlier)' => [ + 'dataSet' => [ + "memo" => Str::random(250), + "code" => Str::random(1), + "credits" => 99999999999, + "uses" => 500, + "expires_at" => now()->subDays(5)->format('d-m-Y'), + ], + 'expectedCount' => 0, + 'assertValidationErrors' => true + ], + 'Invalid dataset (expires_at to far)' => [ + 'dataSet' => [ + "memo" => Str::random(250), + "code" => Str::random(1), + "credits" => 99999999999, + "uses" => 500, + "expires_at" => now()->addYears(100)->format('d-m-Y'), + ], + 'expectedCount' => 0, + 'assertValidationErrors' => true + ], + 'Invalid dataset (expires_at invalid format 1)' => [ + 'dataSet' => [ + "memo" => Str::random(250), + "code" => Str::random(1), + "credits" => 99999999999, + "uses" => 500, + "expires_at" => now()->addYears(100)->format('Y-m-d'), + ], + 'expectedCount' => 0, + 'assertValidationErrors' => true + ], + 'Invalid dataset (expires_at invalid value)' => [ + 'dataSet' => [ + "memo" => Str::random(250), + "code" => Str::random(1), + "credits" => 99999999999, + "uses" => 500, + "expires_at" => Str::random(20), + ], + 'expectedCount' => 0, + 'assertValidationErrors' => true + ], + ]; + + } + + /** + * @return array[] + */ + public function accessibleRoutesDataProvider(): array + { + return [ + 'index page' => [ + 'method' => 'get', + 'route' => '/admin/vouchers', + 'expectedStatus' => 200 + ], + 'Create page' => [ + 'method' => 'get', + 'route' => '/admin/vouchers/create', + 'expectedStatus' => 200 + ], + 'Edit page' => [ + 'method' => 'get', + 'route' => '/admin/vouchers/1/edit', + 'expectedStatus' => 200 + ], + ]; + } +}