Skip to content

Commit

Permalink
Add tests for Coupon and CouponHistory models and improve validations.
Browse files Browse the repository at this point in the history
Signed-off-by: Sam Poyigi <[email protected]>
  • Loading branch information
sampoyigi committed Nov 3, 2024
1 parent f21832e commit 17d4824
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 131 deletions.
22 changes: 22 additions & 0 deletions src/Database/Factories/CouponHistoryFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Igniter\Coupons\Database\Factories;

use Igniter\Flame\Database\Factories\Factory;

class CouponHistoryFactory extends Factory
{
protected $model = \Igniter\Coupons\Models\CouponHistory::class;

public function definition(): array
{
return [
'coupon_id' => $this->faker->randomDigitNotNull,
'order_id' => $this->faker->randomDigitNotNull,
'customer_id' => $this->faker->randomDigitNotNull,
'min_total' => $this->faker->randomFloat(2, 1, 100),
'amount' => $this->faker->randomFloat(2, 1, 100),
'status' => $this->faker->boolean,
];
}
}
3 changes: 1 addition & 2 deletions src/Http/Requests/CouponRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Igniter\Coupons\Http\Requests;

use Igniter\System\Classes\FormRequest;
use Illuminate\Validation\Rule;

class CouponRequest extends FormRequest
{
Expand Down Expand Up @@ -37,7 +36,7 @@ public function rules()
{
return [
'name' => ['required', 'between:2,128'],
'code' => ['required', 'min:2', Rule::unique('igniter_coupons')->ignore($this->getRecordId(), 'coupon_id')],
'code' => ['required', 'min:2', 'unique:igniter_coupons,code,'.$this->getRecordId().',coupon_id'],
'type' => ['required', 'string', 'size:1', 'in:P,F'],
'discount' => ['required', 'numeric', 'min:0'],
'min_total' => ['numeric'],
Expand Down
2 changes: 2 additions & 0 deletions src/Models/CouponHistory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Igniter\Coupons\Models;

use Igniter\Flame\Database\Factories\HasFactory;
use Igniter\Flame\Database\Model;
use Igniter\System\Models\Concerns\Switchable;
use Igniter\User\Models\Concerns\HasCustomer;
Expand All @@ -12,6 +13,7 @@
class CouponHistory extends Model
{
use HasCustomer;
use HasFactory;
use Switchable;

/**
Expand Down
94 changes: 94 additions & 0 deletions tests/Http/Requests/CouponRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace Igniter\Coupons\Tests\Http\Requests;

use Igniter\Coupons\Http\Requests\CouponRequest;

it('returns correct attribute labels', function() {
$request = new CouponRequest();

$attributes = $request->attributes();

expect($attributes)->toHaveKey('name', lang('admin::lang.label_name'))
->and($attributes)->toHaveKey('code', lang('igniter.coupons::default.label_code'))
->and($attributes)->toHaveKey('type', lang('admin::lang.label_type'))
->and($attributes)->toHaveKey('discount', lang('igniter.coupons::default.label_discount'))
->and($attributes)->toHaveKey('min_total', lang('igniter.coupons::default.label_min_total'))
->and($attributes)->toHaveKey('redemptions', lang('igniter.coupons::default.label_redemption'))
->and($attributes)->toHaveKey('customer_redemptions', lang('igniter.coupons::default.label_customer_redemption'))
->and($attributes)->toHaveKey('description', lang('admin::lang.label_description'))
->and($attributes)->toHaveKey('validity', lang('igniter.coupons::default.label_validity'))
->and($attributes)->toHaveKey('fixed_date', lang('igniter.coupons::default.label_fixed_date'))
->and($attributes)->toHaveKey('fixed_from_time', lang('igniter.coupons::default.label_fixed_from_time'))
->and($attributes)->toHaveKey('fixed_to_time', lang('igniter.coupons::default.label_fixed_to_time'))
->and($attributes)->toHaveKey('period_start_date', lang('igniter.coupons::default.label_period_start_date'))
->and($attributes)->toHaveKey('period_end_date', lang('igniter.coupons::default.label_period_end_date'))
->and($attributes)->toHaveKey('recurring_every', lang('igniter.coupons::default.label_recurring_every'))
->and($attributes)->toHaveKey('recurring_from_time', lang('igniter.coupons::default.label_recurring_from_time'))
->and($attributes)->toHaveKey('recurring_to_time', lang('igniter.coupons::default.label_recurring_to_time'))
->and($attributes)->toHaveKey('order_restriction.*', lang('igniter.coupons::default.label_order_restriction'))
->and($attributes)->toHaveKey('status', lang('admin::lang.label_status'))
->and($attributes)->toHaveKey('locations.*', lang('admin::lang.column_location'));
});

it('returns correct validation rules', function() {
$request = new CouponRequest();

$request->setRouteResolver(fn() => new class
{
public function parameter($key, $default = null)
{
return 'coupon';
}
});

$rules = $request->rules();

expect($rules)->toHaveKey('name')
->and($rules)->toHaveKey('code')
->and($rules)->toHaveKey('type')
->and($rules)->toHaveKey('discount')
->and($rules)->toHaveKey('min_total')
->and($rules)->toHaveKey('redemptions')
->and($rules)->toHaveKey('customer_redemptions')
->and($rules)->toHaveKey('customers')
->and($rules)->toHaveKey('customer_groups')
->and($rules)->toHaveKey('customers.*')
->and($rules)->toHaveKey('customer_groups.*')
->and($rules)->toHaveKey('description')
->and($rules)->toHaveKey('validity')
->and($rules)->toHaveKey('fixed_date')
->and($rules)->toHaveKey('fixed_from_time')
->and($rules)->toHaveKey('fixed_to_time')
->and($rules)->toHaveKey('period_start_date')
->and($rules)->toHaveKey('period_end_date')
->and($rules)->toHaveKey('recurring_every')
->and($rules)->toHaveKey('recurring_from_time')
->and($rules)->toHaveKey('recurring_to_time')
->and($rules)->toHaveKey('order_restriction.*')
->and($rules)->toHaveKey('status')
->and($rules)->toHaveKey('locations.*')
->and($rules['name'])->toContain('required', 'between:2,128')
->and($rules['code'])->toContain('required', 'min:2', 'unique:igniter_coupons,code,coupon,coupon_id')
->and($rules['type'])->toContain('required', 'string', 'size:1', 'in:P,F')
->and($rules['discount'])->toContain('required', 'numeric', 'min:0')
->and($rules['min_total'])->toContain('numeric')
->and($rules['redemptions'])->toContain('required', 'integer')
->and($rules['customer_redemptions'])->toContain('required', 'integer')
->and($rules['customers'])->toContain('nullable', 'array')
->and($rules['customer_groups'])->toContain('nullable', 'array')
->and($rules['description'])->toContain('max:1028')
->and($rules['validity'])->toContain('required', 'in:forever,fixed,period,recurring')
->and($rules['fixed_date'])->toContain('nullable', 'required_if:validity,fixed', 'date')
->and($rules['fixed_from_time'])->toContain('nullable', 'required_if:validity,fixed', 'date_format:H:i')
->and($rules['fixed_to_time'])->toContain('nullable', 'required_if:validity,fixed', 'date_format:H:i')
->and($rules['period_start_date'])->toContain('nullable', 'required_if:validity,period', 'date')
->and($rules['period_end_date'])->toContain('nullable', 'required_if:validity,period', 'date')
->and($rules['recurring_every'])->toContain('nullable', 'required_if:validity,recurring')
->and($rules['recurring_from_time'])->toContain('nullable', 'required_if:validity,recurring', 'date_format:H:i')
->and($rules['recurring_to_time'])->toContain('nullable', 'required_if:validity,recurring', 'date_format:H:i')
->and($rules['order_restriction.*'])->toContain('nullable', 'string')
->and($rules['status'])->toContain('boolean')
->and($rules['auto_apply'])->toContain('boolean')
->and($rules['locations.*'])->toContain('integer');
});
129 changes: 0 additions & 129 deletions tests/Http/Requests/CouponTest.php

This file was deleted.

16 changes: 16 additions & 0 deletions tests/Models/CouponHistoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Igniter\Cart\Tests\Models;

use Igniter\Coupons\Models\CouponHistory;
use Igniter\User\Models\Customer;

it('gets customer name attribute correctly', function() {
$customer = Customer::factory()->create(['first_name' => 'Jane', 'last_name' => 'Doe']);
$couponHistory = CouponHistory::factory()->create([
'customer_id' => $customer->getKey(),
]);

expect($couponHistory->customer_name)->toBe('Jane Doe');
});

2 changes: 2 additions & 0 deletions tests/Models/CouponTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace Igniter\Cart\Tests\Models;

use Igniter\Coupons\Models\Coupon;
use Igniter\Coupons\Models\CouponHistory;
use Igniter\Coupons\Models\Scopes\CouponScope;
Expand Down

0 comments on commit 17d4824

Please sign in to comment.