91 lines
2.9 KiB
PHP
91 lines
2.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Unit\Application\UseCases\Campaign\Update;
|
|
|
|
use Domain\Ports\Repositories\CouponRepository;
|
|
use Domain\Services\Campaign\CampaignAvailabilityChecker;
|
|
use Tests\Fixtures\Campaign\CampaignFactory;
|
|
use Tests\Fixtures\Coupon\CouponFactory;
|
|
|
|
covers(CampaignAvailabilityChecker::class);
|
|
|
|
describe('campaign availability checker', function (): void {
|
|
it('is available with an active coupon', function (): void {
|
|
// GIVEN
|
|
$coupon = CouponFactory::make();
|
|
$campaign = CampaignFactory::hydrated(couponCodes: [$coupon->code]);
|
|
|
|
$repository = \Mockery::mock(CouponRepository::class);
|
|
$repository->shouldReceive('findByCodes')->once()->with($campaign->couponCodes())->andReturn([
|
|
$coupon
|
|
]);
|
|
|
|
$service = new CampaignAvailabilityChecker($repository);
|
|
|
|
// WHEN
|
|
$result = $service->hasActiveCoupon($campaign);
|
|
|
|
// THEN
|
|
expect($result)->toBeTrue();
|
|
});
|
|
|
|
it('is available with one active and one expired coupon', function (): void {
|
|
// GIVEN
|
|
$expiredCoupon = CouponFactory::expired();
|
|
$validCoupon = CouponFactory::make();
|
|
$campaign = CampaignFactory::hydrated(couponCodes: [$expiredCoupon->code, $validCoupon->code]);
|
|
|
|
$repository = \Mockery::mock(CouponRepository::class);
|
|
$repository->shouldReceive('findByCodes')->once()->with($campaign->couponCodes())->andReturn([
|
|
$expiredCoupon,
|
|
$validCoupon
|
|
]);
|
|
|
|
$service = new CampaignAvailabilityChecker($repository);
|
|
|
|
// WHEN
|
|
$result = $service->hasActiveCoupon($campaign);
|
|
|
|
// THEN
|
|
expect($result)->toBeTrue();
|
|
});
|
|
|
|
it('is not available when every coupon is expired or upcoming', function (): void {
|
|
// GIVEN
|
|
$expiredCoupon = CouponFactory::expired();
|
|
$upcomingCoupon = CouponFactory::upcoming();
|
|
$campaign = CampaignFactory::hydrated(couponCodes: [$expiredCoupon->code, $upcomingCoupon->code]);
|
|
|
|
$repository = \Mockery::mock(CouponRepository::class);
|
|
$repository->shouldReceive('findByCodes')->once()->with($campaign->couponCodes())->andReturn([
|
|
$expiredCoupon,
|
|
$upcomingCoupon
|
|
]);
|
|
|
|
$service = new CampaignAvailabilityChecker($repository);
|
|
|
|
// WHEN
|
|
$result = $service->hasActiveCoupon($campaign);
|
|
|
|
// THEN
|
|
expect($result)->toBeFalse();
|
|
});
|
|
|
|
it('is not available when no coupon is found', function (): void {
|
|
// GIVEN
|
|
$campaign = CampaignFactory::hydrated();
|
|
|
|
$repository = \Mockery::mock(CouponRepository::class);
|
|
$repository->shouldReceive('findByCodes')->once()->with($campaign->couponCodes())->andReturn([]);
|
|
|
|
$service = new CampaignAvailabilityChecker($repository);
|
|
|
|
// WHEN
|
|
$result = $service->hasActiveCoupon($campaign);
|
|
|
|
// THEN
|
|
expect($result)->toBeFalse();
|
|
});
|
|
});
|