Extend availability (durations, weekly recurrence, calendar); price offerings in dollars
CI / Coding Standards (pull_request) Successful in 50s
CI / PHPStan (pull_request) Successful in 1m2s
CI / Tests (PHP 8.1) (pull_request) Successful in 47s
CI / Tests (PHP 8.2) (pull_request) Successful in 48s
CI / Tests (PHP 8.3) (pull_request) Successful in 46s
CI / No Debug Code (pull_request) Successful in 2s
CI / Build Plugin Zip (pull_request) Has been skipped

Availability (#2):
- us_availability gains offering_id, duration_minutes (default 60), and
  recurrence_group; AvailabilitySlot carries the new fields.
- AvailabilityRepository::createWeeklySeries() generates N weekly rows
  sharing a recurrence_group; findAvailable() filters by offering and
  duration. Date math uses DateTimeImmutable::modify() (the no-debug CI
  regex `dd\(` matches `->add(`).
- REST GET filters by offering_id/duration_minutes; POST accepts
  duration_minutes, offering_id, recurrence (single|weekly) + weeks.
- Admin form adds duration, an offering picker, and one-off/weekly options
  (OfferingRepository wired into AvailabilityController).
- booking.js renders an agenda calendar (slots grouped by day, with
  duration). The richer booking UX lands with the booking-flow work.

Offering price in dollars:
- Switch us_offerings.price_cents (INT) to price DECIMAL(10,2); Offering
  uses float $price. Admin form and REST take dollars.
- Fix a pre-existing misalignment in the Offering insert/update $wpdb
  format arrays (billing_mode/capacity/is_active were mapped to the wrong
  specifiers, which would corrupt values) via a single COLUMN_FORMATS list.

Also bump PHPStan to --memory-limit=1G in the lint script; 128M now
crashes analysis as the codebase has grown.

Refs #2

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-06-05 15:38:58 -03:00
parent 5352fb7d69
commit 19e663d6fa
18 changed files with 398 additions and 112 deletions
@@ -34,19 +34,50 @@ class AvailabilityRepositoryTest extends TestCase
Mockery::on(static function (array $data): bool {
return $data['instructor_id'] === 5
&& $data['start_dt'] === '2026-04-01 09:00:00'
&& $data['duration_minutes'] === 30
&& $data['offering_id'] === 8
&& $data['is_booked'] === 0;
}),
['%d', '%s', '%s', '%d', '%s']
['%d', '%d', '%s', '%s', '%d', '%d', '%d', '%s']
);
$this->db->insert_id = 42;
$slot = new AvailabilitySlot(5, '2026-04-01 09:00:00', '2026-04-01 10:00:00');
$slot = new AvailabilitySlot(5, '2026-04-01 09:00:00', '2026-04-01 10:00:00', 30, 8);
$result = $this->repo->insert($slot);
self::assertSame(42, $result);
}
public function testCreateWeeklySeriesInsertsWeeklyAndSharesGroup(): void
{
Functions\when('current_time')->justReturn('2026-04-07 12:00:00');
$captured = [];
$ids = [10, 11, 12];
$this->db->shouldReceive('insert')
->times(3)
->andReturnUsing(function (string $table, array $data) use (&$captured, &$ids): void {
$captured[] = $data['start_dt'];
$this->db->insert_id = array_shift($ids);
});
// The first row is back-filled with its own id as the recurrence group.
$this->db->shouldReceive('update')
->once()
->with('wp_us_availability', ['recurrence_group' => 10], ['id' => 10], ['%d'], ['%d']);
$first = new AvailabilitySlot(5, '2026-04-07 09:00:00', '2026-04-07 10:00:00', 60);
$result = $this->repo->createWeeklySeries($first, 3);
self::assertSame([10, 11, 12], $result);
self::assertSame(
['2026-04-07 09:00:00', '2026-04-14 09:00:00', '2026-04-21 09:00:00'],
$captured
);
}
public function testFindByIdReturnsNullWhenNotFound(): void
{
$this->db->shouldReceive('prepare')
@@ -65,11 +96,14 @@ class AvailabilityRepositoryTest extends TestCase
public function testFindByIdReturnsSlotWhenFound(): void
{
$row = (object) [
'id' => '10',
'instructor_id' => '5',
'start_dt' => '2026-04-01 09:00:00',
'end_dt' => '2026-04-01 10:00:00',
'is_booked' => '0',
'id' => '10',
'instructor_id' => '5',
'offering_id' => null,
'start_dt' => '2026-04-01 09:00:00',
'end_dt' => '2026-04-01 10:00:00',
'duration_minutes' => '60',
'is_booked' => '0',
'recurrence_group' => null,
];
$this->db->shouldReceive('prepare')->andReturn('SELECT ...');
@@ -128,14 +162,32 @@ class AvailabilityRepositoryTest extends TestCase
$this->repo->findAvailable(instructorId: 3);
}
public function testFindAvailableWithOfferingAndDurationFilters(): void
{
$this->db->shouldReceive('prepare')
->once()
->with(
Mockery::pattern('/offering_id = %d AND duration_minutes = %d/'),
Mockery::on(static fn (array $p): bool => $p === [8, 30])
)
->andReturn('SELECT ...');
$this->db->shouldReceive('get_results')->andReturn([]);
$this->repo->findAvailable(offeringId: 8, durationMinutes: 30);
}
public function testFindByInstructorReturnsSlots(): void
{
$row = (object) [
'id' => '5',
'instructor_id' => '3',
'start_dt' => '2026-04-01 09:00:00',
'end_dt' => '2026-04-01 10:00:00',
'is_booked' => '0',
'id' => '5',
'instructor_id' => '3',
'offering_id' => null,
'start_dt' => '2026-04-01 09:00:00',
'end_dt' => '2026-04-01 10:00:00',
'duration_minutes' => '60',
'is_booked' => '0',
'recurrence_group' => null,
];
$this->db->shouldReceive('prepare')->andReturn('SELECT ...');
@@ -11,58 +11,66 @@ class AvailabilitySlotTest extends TestCase
public function testConstructorAndProperties(): void
{
$slot = new AvailabilitySlot(
instructorId: 5,
startDt: '2026-04-01 09:00:00',
endDt: '2026-04-01 10:00:00',
isBooked: false,
id: 42,
instructorId: 5,
startDt: '2026-04-01 09:00:00',
endDt: '2026-04-01 10:00:00',
durationMinutes: 30,
offeringId: 8,
isBooked: false,
recurrenceGroup: 100,
id: 42,
);
self::assertSame(5, $slot->instructorId);
self::assertSame('2026-04-01 09:00:00', $slot->startDt);
self::assertSame('2026-04-01 10:00:00', $slot->endDt);
self::assertSame(30, $slot->durationMinutes);
self::assertSame(8, $slot->offeringId);
self::assertSame(100, $slot->recurrenceGroup);
self::assertFalse($slot->isBooked);
self::assertSame(42, $slot->id);
}
public function testFromRowMapsCorrectly(): void
public function testDefaults(): void
{
$slot = new AvailabilitySlot(1, '2026-04-01 09:00:00', '2026-04-01 10:00:00');
self::assertSame(60, $slot->durationMinutes);
self::assertNull($slot->offeringId);
self::assertFalse($slot->isBooked);
self::assertNull($slot->recurrenceGroup);
self::assertNull($slot->id);
}
public function testFromRowMapsCorrectlyAndCastsNullables(): void
{
$row = (object) [
'id' => '7',
'instructor_id' => '3',
'start_dt' => '2026-05-10 14:00:00',
'end_dt' => '2026-05-10 15:00:00',
'is_booked' => '1',
'id' => '7',
'instructor_id' => '3',
'offering_id' => null,
'start_dt' => '2026-05-10 14:00:00',
'end_dt' => '2026-05-10 15:00:00',
'duration_minutes' => '60',
'is_booked' => '1',
'recurrence_group' => '7',
];
$slot = AvailabilitySlot::fromRow($row);
self::assertSame(7, $slot->id);
self::assertSame(3, $slot->instructorId);
self::assertNull($slot->offeringId);
self::assertSame(60, $slot->durationMinutes);
self::assertSame(7, $slot->recurrenceGroup);
self::assertTrue($slot->isBooked);
}
public function testToArrayContainsExpectedKeys(): void
{
$slot = new AvailabilitySlot(1, '2026-04-01 09:00:00', '2026-04-01 10:00:00', false, 10);
$slot = new AvailabilitySlot(1, '2026-04-01 09:00:00', '2026-04-01 10:00:00', 30, 8, false, null, 10);
$arr = $slot->toArray();
self::assertArrayHasKey('id', $arr);
self::assertArrayHasKey('instructor_id', $arr);
self::assertArrayHasKey('start_dt', $arr);
self::assertArrayHasKey('end_dt', $arr);
self::assertArrayHasKey('is_booked', $arr);
}
public function testDefaultIsBookedIsFalse(): void
{
$slot = new AvailabilitySlot(1, '2026-04-01 09:00:00', '2026-04-01 10:00:00');
self::assertFalse($slot->isBooked);
}
public function testDefaultIdIsNull(): void
{
$slot = new AvailabilitySlot(1, '2026-04-01 09:00:00', '2026-04-01 10:00:00');
self::assertNull($slot->id);
foreach (['id', 'instructor_id', 'offering_id', 'start_dt', 'end_dt', 'duration_minutes', 'is_booked', 'recurrence_group'] as $key) {
self::assertArrayHasKey($key, $arr);
}
}
}