Some checks failed
CI / Coding Standards (push) Failing after 2m31s
CI / PHPStan (push) Failing after 50s
CI / Tests (PHP 8.1) (push) Successful in 50s
CI / Tests (PHP 8.2) (push) Successful in 48s
CI / Tests (PHP 8.3) (push) Successful in 40s
CI / No Debug Code (push) Successful in 2s
- Custom DB tables for availability slots and lesson bookings - Instructor (wp-admin) and student (front-end) roles with custom capabilities - REST API under us-scheduler/v1 for availability CRUD and booking - [us_booking] and [us_student_login] shortcodes for student front end - PHPUnit + Brain\Monkey unit test suite (29 tests) - Gitea Actions CI: lint, PHPStan, tests on PHP 8.1/8.2/8.3, no-debug check - Feature docs under docs/features/ Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
127 lines
3.7 KiB
PHP
127 lines
3.7 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Unsupervised\Schedular\Tests\Unit\Data;
|
|
|
|
use Brain\Monkey\Functions;
|
|
use Mockery;
|
|
use Unsupervised\Schedular\Data\BookingRepository;
|
|
use Unsupervised\Schedular\Model\Lesson;
|
|
use Unsupervised\Schedular\Tests\Unit\TestCase;
|
|
|
|
class BookingRepositoryTest extends TestCase
|
|
{
|
|
private \wpdb $db;
|
|
private BookingRepository $repo;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
$this->db = Mockery::mock(\wpdb::class);
|
|
$this->db->prefix = 'wp_';
|
|
$this->repo = new BookingRepository($this->db);
|
|
}
|
|
|
|
public function testInsertCallsWpdbInsertAndReturnsId(): void
|
|
{
|
|
Functions\expect('current_time')->with('mysql')->andReturn('2026-04-01 12:00:00');
|
|
|
|
$this->db->shouldReceive('insert')
|
|
->once()
|
|
->with(
|
|
'wp_us_lessons',
|
|
Mockery::on(static function (array $data): bool {
|
|
return $data['slot_id'] === 10
|
|
&& $data['student_id'] === 5
|
|
&& $data['status'] === Lesson::STATUS_PENDING;
|
|
}),
|
|
['%d', '%d', '%d', '%s', '%s', '%s']
|
|
);
|
|
|
|
$this->db->insert_id = 77;
|
|
|
|
$lesson = new Lesson(slotId: 10, studentId: 5, instructorId: 3);
|
|
$result = $this->repo->insert($lesson);
|
|
|
|
self::assertSame(77, $result);
|
|
}
|
|
|
|
public function testFindByIdReturnsNullWhenNotFound(): void
|
|
{
|
|
$this->db->shouldReceive('prepare')->andReturn('SELECT ...');
|
|
$this->db->shouldReceive('get_row')->andReturn(null);
|
|
|
|
self::assertNull($this->repo->findById(99));
|
|
}
|
|
|
|
public function testFindByIdReturnsLesson(): void
|
|
{
|
|
$row = (object) [
|
|
'id' => '15',
|
|
'slot_id' => '10',
|
|
'student_id' => '5',
|
|
'instructor_id' => '3',
|
|
'status' => 'pending',
|
|
'notes' => null,
|
|
];
|
|
|
|
$this->db->shouldReceive('prepare')->andReturn('SELECT ...');
|
|
$this->db->shouldReceive('get_row')->andReturn($row);
|
|
|
|
$lesson = $this->repo->findById(15);
|
|
|
|
self::assertInstanceOf(Lesson::class, $lesson);
|
|
self::assertSame(15, $lesson->id);
|
|
}
|
|
|
|
public function testUpdateStatusReturnsFalseForInvalidStatus(): void
|
|
{
|
|
$result = $this->repo->updateStatus(1, 'invalid');
|
|
self::assertFalse($result);
|
|
}
|
|
|
|
public function testUpdateStatusCallsWpdbUpdate(): void
|
|
{
|
|
$this->db->shouldReceive('update')
|
|
->once()
|
|
->with(
|
|
'wp_us_lessons',
|
|
['status' => Lesson::STATUS_CONFIRMED],
|
|
['id' => 1],
|
|
['%s'],
|
|
['%d']
|
|
)
|
|
->andReturn(1);
|
|
|
|
self::assertTrue($this->repo->updateStatus(1, Lesson::STATUS_CONFIRMED));
|
|
}
|
|
|
|
public function testUpdateStatusReturnsFalseWhenDbFails(): void
|
|
{
|
|
$this->db->shouldReceive('update')->andReturn(0);
|
|
|
|
self::assertFalse($this->repo->updateStatus(1, Lesson::STATUS_CONFIRMED));
|
|
}
|
|
|
|
public function testFindByStudentReturnsLessons(): void
|
|
{
|
|
$row = (object) [
|
|
'id' => '1',
|
|
'slot_id' => '2',
|
|
'student_id' => '5',
|
|
'instructor_id' => '3',
|
|
'status' => 'pending',
|
|
'notes' => null,
|
|
];
|
|
|
|
$this->db->shouldReceive('prepare')->andReturn('SELECT ...');
|
|
$this->db->shouldReceive('get_results')->andReturn([$row]);
|
|
|
|
$lessons = $this->repo->findByStudent(5);
|
|
|
|
self::assertCount(1, $lessons);
|
|
self::assertInstanceOf(Lesson::class, $lessons[0]);
|
|
}
|
|
}
|