db = Mockery::mock(\wpdb::class); $this->db->prefix = 'wp_'; $this->repo = new PolicyRepository($this->db); } public function testInsertReturnsId(): void { Functions\expect('current_time')->with('mysql')->andReturn('2026-06-01 12:00:00'); $this->db->shouldReceive('insert') ->once() ->with( 'wp_us_policies', Mockery::on(static fn (array $d): bool => $d['title'] === 'Cancellation' && $d['slug'] === 'cancellation' && $d['current_version_id'] === null), ['%s', '%s', '%d', '%s'] ); $this->db->insert_id = 7; self::assertSame(7, $this->repo->insert(new Policy('Cancellation', 'cancellation'))); } public function testUpdateCurrentVersion(): void { $this->db->shouldReceive('update') ->once() ->with('wp_us_policies', ['current_version_id' => 9], ['id' => 7], ['%d'], ['%d']) ->andReturn(1); self::assertTrue($this->repo->updateCurrentVersion(7, 9)); } public function testFindBySlugReturnsPolicy(): void { $this->db->shouldReceive('prepare')->andReturn('SELECT ...'); $this->db->shouldReceive('get_row')->andReturn((object) [ 'id' => '7', 'title' => 'Cancellation', 'slug' => 'cancellation', 'current_version_id' => null, ]); $policy = $this->repo->findBySlug('cancellation'); self::assertInstanceOf(Policy::class, $policy); self::assertSame('cancellation', $policy->slug); } public function testFindBySlugReturnsNullWhenMissing(): void { $this->db->shouldReceive('prepare')->andReturn('SELECT ...'); $this->db->shouldReceive('get_row')->andReturn(null); self::assertNull($this->repo->findBySlug('nope')); } public function testFindAllMapsRows(): void { $this->db->shouldReceive('get_results')->andReturn([ (object) ['id' => '1', 'title' => 'A', 'slug' => 'a', 'current_version_id' => null], ]); $all = $this->repo->findAll(); self::assertCount(1, $all); self::assertInstanceOf(Policy::class, $all[0]); } public function testDeleteCallsWpdb(): void { $this->db->shouldReceive('delete')->once()->with('wp_us_policies', ['id' => 3], ['%d'])->andReturn(1); self::assertTrue($this->repo->delete(3)); } }