Add tests for ticket validity and event date formatting
- Create TicketValiditySchemaTest to verify database schema for ticket validity. - Update CatalogModelsTest to include tests for event date attributes and selection options. - Introduce EventDateTextFormatterTest for formatting event dates in Spanish. - Refactor EventModelsTest to include validity time relationships. - Add SaleDetailResourceTest to ensure correct serialization of purchase items. - Enhance TicketTest with validity time checks and status management. - Implement ValidityTimeResourceTest to validate resource output for different validity types. - Add ValidityTimeTest to verify casting and validity checks for validity time types.
This commit is contained in:
112
app/Domains/Ticket/Models/ValidityTime.php
Normal file
112
app/Domains/Ticket/Models/ValidityTime.php
Normal file
@@ -0,0 +1,112 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\Ticket\Models;
|
||||
|
||||
use App\Domains\Catalog\Models\AttributeOption;
|
||||
use App\Domains\Catalog\Models\CatalogItem;
|
||||
use App\Domains\Event\Models\EventDate;
|
||||
use App\Domains\Ticket\Enums\ValidityTimeType;
|
||||
use Carbon\CarbonImmutable;
|
||||
use Carbon\CarbonInterface;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||
|
||||
#[Fillable([
|
||||
'type',
|
||||
'start_time',
|
||||
'end_time',
|
||||
'fixed_starts_at',
|
||||
'fixed_expires_at',
|
||||
])]
|
||||
class ValidityTime extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'type' => ValidityTimeType::class,
|
||||
'fixed_starts_at' => 'datetime',
|
||||
'fixed_expires_at' => 'datetime',
|
||||
];
|
||||
}
|
||||
|
||||
/** @return HasMany<CatalogItem, $this> */
|
||||
public function catalogItems(): HasMany
|
||||
{
|
||||
return $this->hasMany(CatalogItem::class);
|
||||
}
|
||||
|
||||
/** @return HasMany<AttributeOption, $this> */
|
||||
public function attributeOptions(): HasMany
|
||||
{
|
||||
return $this->hasMany(AttributeOption::class);
|
||||
}
|
||||
|
||||
/** @return HasOne<EventDate, $this> */
|
||||
public function eventDate(): HasOne
|
||||
{
|
||||
return $this->hasOne(EventDate::class);
|
||||
}
|
||||
|
||||
/** @return BelongsToMany<TicketValidityGroup, $this> */
|
||||
public function ticketValidityGroups(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(
|
||||
TicketValidityGroup::class,
|
||||
'ticket_validity_group_times',
|
||||
'validity_time_id',
|
||||
'ticket_validity_group_id',
|
||||
);
|
||||
}
|
||||
|
||||
public function startsAt(
|
||||
?CarbonInterface $at = null,
|
||||
): ?CarbonInterface {
|
||||
if ($this->type === ValidityTimeType::FixedWindow) {
|
||||
return $this->fixed_starts_at;
|
||||
}
|
||||
|
||||
return $this->atCurrentDate($this->start_time, $at);
|
||||
}
|
||||
|
||||
public function expiresAt(
|
||||
?CarbonInterface $at = null,
|
||||
): ?CarbonInterface {
|
||||
if ($this->type === ValidityTimeType::FixedWindow) {
|
||||
return $this->fixed_expires_at;
|
||||
}
|
||||
|
||||
return $this->atCurrentDate($this->end_time, $at);
|
||||
}
|
||||
|
||||
public function isValid(
|
||||
?CarbonInterface $at = null,
|
||||
): bool {
|
||||
$at ??= now();
|
||||
$startsAt = $this->startsAt($at);
|
||||
$expiresAt = $this->expiresAt($at);
|
||||
|
||||
return ($startsAt === null || $startsAt->lessThanOrEqualTo($at))
|
||||
&& ($expiresAt === null || $expiresAt->greaterThan($at));
|
||||
}
|
||||
|
||||
private function atCurrentDate(
|
||||
?string $time,
|
||||
?CarbonInterface $at,
|
||||
): ?CarbonInterface {
|
||||
if ($time === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$at ??= now();
|
||||
$localDate = CarbonImmutable::instance($at)
|
||||
->format('Y-m-d');
|
||||
|
||||
return CarbonImmutable::parse($localDate.' '.$time);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user