93 lines
3 KiB
PHP
93 lines
3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Unit\Domain\Models\ValueObjects\Coupon;
|
|
|
|
use Domain\Models\ValueObjects\Coupon\CouponProcessingCode;
|
|
use Shared\Enums\Country;
|
|
|
|
covers(CouponProcessingCode::class);
|
|
|
|
describe('coupon processing code', function (): void {
|
|
$value = 'ABC1234567890';
|
|
|
|
it('creates a valid processing code', function () use ($value): void {
|
|
// GIVEN / WHEN
|
|
$code = new CouponProcessingCode($value, Country::FRANCE);
|
|
|
|
// THEN
|
|
expect($code->value)->toBe($value)
|
|
->and($code->availableIn)->toBe(Country::FRANCE);
|
|
});
|
|
|
|
it('accepts lowercase and uppercase letters and digits', function (): void {
|
|
// GIVEN / WHEN
|
|
$code = new CouponProcessingCode('aBc1234567xYz', Country::FRANCE);
|
|
|
|
// THEN
|
|
expect($code->value)->toBe('aBc1234567xYz');
|
|
});
|
|
|
|
it('rejects an invalid value', function (string $invalidValue): void {
|
|
// GIVEN / WHEN
|
|
$action = fn (): CouponProcessingCode => new CouponProcessingCode($invalidValue, Country::FRANCE);
|
|
|
|
// THEN
|
|
expect($action)->toThrow(
|
|
\DomainException::class,
|
|
'Processing Code must be exactly 13 alphanumeric characters, e.g. ABC1234567890.',
|
|
);
|
|
})->with([
|
|
'empty' => '',
|
|
'too short' => 'ABC123456789',
|
|
'too long' => 'ABC12345678901',
|
|
'special char' => 'ABC123456789!',
|
|
'with space' => 'ABC 123456789',
|
|
'trailing break' => "ABC1234567890\n",
|
|
]);
|
|
|
|
it('is equal to another processing code with the same value', function () use ($value): void {
|
|
// GIVEN
|
|
$code = new CouponProcessingCode($value, Country::FRANCE);
|
|
$other = new CouponProcessingCode($value, Country::FRANCE);
|
|
|
|
// WHEN / THEN
|
|
expect($code->equals($other))->toBeTrue();
|
|
});
|
|
|
|
it('ignores the country when comparing', function () use ($value): void {
|
|
// GIVEN
|
|
$france = new CouponProcessingCode($value, Country::FRANCE);
|
|
$benelux = new CouponProcessingCode($value, Country::BENELUX);
|
|
|
|
// WHEN / THEN
|
|
expect($france->equals($benelux))->toBeTrue();
|
|
});
|
|
|
|
it('is not equal to a processing code with a different value', function () use ($value): void {
|
|
// GIVEN
|
|
$code = new CouponProcessingCode($value, Country::FRANCE);
|
|
$other = new CouponProcessingCode('XYZ0987654321', Country::FRANCE);
|
|
|
|
// WHEN / THEN
|
|
expect($code->equals($other))->toBeFalse();
|
|
});
|
|
|
|
it('is not equal to a value that is not a processing code', function () use ($value): void {
|
|
// GIVEN
|
|
$code = new CouponProcessingCode($value, Country::FRANCE);
|
|
|
|
// WHEN / THEN
|
|
expect($code->equals($value))->toBeFalse()
|
|
->and($code->equals(null))->toBeFalse();
|
|
});
|
|
|
|
it('is converted to its value as a string', function () use ($value): void {
|
|
// GIVEN
|
|
$code = new CouponProcessingCode($value, Country::FRANCE);
|
|
|
|
// WHEN / THEN
|
|
expect((string) $code)->toBe($value);
|
|
});
|
|
});
|