tested voucher controller

This commit is contained in:
AVMG20 2021-07-12 13:44:26 +02:00
parent cfc61bebda
commit 735097d722
3 changed files with 330 additions and 7 deletions

View file

@ -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'));

View file

@ -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')
];
}
}

View file

@ -0,0 +1,318 @@
<?php
namespace Tests\Feature;
use App\Models\User;
use App\Models\Voucher;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Str;
use Tests\TestCase;
/**
* Class TestUsefulLinksController
* @package Tests\Feature
*/
class TestVouchersController extends TestCase
{
use DatabaseTransactions;
/**
* @dataProvider accessibleRoutesDataProvider
* @param string $method
* @param string $route
* @param int $expectedStatus
*/
function test_accessible_routes(string $method, string $route, int $expectedStatus)
{
Voucher::factory()->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
],
];
}
}