db = Mockery::mock(\wpdb::class); $this->db->prefix = 'wp_'; $this->repo = new AcceptanceRepository($this->db); } public function testInsertReturnsId(): void { Functions\expect('current_time')->with('mysql')->andReturn('2026-06-02 09:00:00'); $this->db->shouldReceive('insert') ->once() ->with( 'wp_us_policy_acceptances', Mockery::on(static function (array $d): bool { return $d['policy_version_id'] === 9 && $d['student_id'] === 5 && $d['registration_type'] === PolicyAcceptance::REG_LESSON && $d['registration_id'] === 12 && $d['ip_address'] === '203.0.113.7'; }), ['%d', '%d', '%s', '%d', '%s', '%s'] ); $this->db->insert_id = 1; $acceptance = new PolicyAcceptance(9, 5, PolicyAcceptance::REG_LESSON, 12, '203.0.113.7'); self::assertSame(1, $this->repo->insert($acceptance)); } public function testInsertManyReturnsAllIds(): void { Functions\when('current_time')->justReturn('2026-06-02 09:00:00'); $ids = [11, 12]; $this->db->shouldReceive('insert') ->twice() ->andReturnUsing(function () use (&$ids): void { $this->db->insert_id = array_shift($ids); }); $result = $this->repo->insertMany([ new PolicyAcceptance(9, 5, PolicyAcceptance::REG_LESSON, 12), new PolicyAcceptance(10, 5, PolicyAcceptance::REG_LESSON, 12), ]); self::assertSame([11, 12], $result); } public function testFindByRegistrationMapsRows(): void { $this->db->shouldReceive('prepare') ->once() ->with(Mockery::pattern('/registration_type = %s AND registration_id = %d/'), PolicyAcceptance::REG_LESSON, 12) ->andReturn('SELECT ...'); $this->db->shouldReceive('get_results')->andReturn([ (object) [ 'id' => '1', 'policy_version_id' => '9', 'student_id' => '5', 'registration_type' => PolicyAcceptance::REG_LESSON, 'registration_id' => '12', 'accepted_at' => '2026-06-02 09:00:00', 'ip_address' => null, ], ]); $rows = $this->repo->findByRegistration(PolicyAcceptance::REG_LESSON, 12); self::assertCount(1, $rows); self::assertInstanceOf(PolicyAcceptance::class, $rows[0]); } }