db = Mockery::mock(\wpdb::class); $this->db->prefix = 'wp_'; $this->repo = new PolicyVersionRepository($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_policy_versions', Mockery::on(static fn (array $d): bool => $d['policy_id'] === 4 && $d['version_number'] === 1 && $d['status'] === PolicyVersion::STATUS_DRAFT), ['%d', '%d', '%s', '%s', '%s', '%s'] ); $this->db->insert_id = 9; self::assertSame(9, $this->repo->insert(new PolicyVersion(4, 1, '
x
'))); } public function testUpdateStatusWithPublishedAt(): void { $this->db->shouldReceive('update') ->once() ->with( 'wp_us_policy_versions', ['status' => PolicyVersion::STATUS_PUBLISHED, 'published_at' => '2026-06-01 12:00:00'], ['id' => 9], ['%s', '%s'], ['%d'] ) ->andReturn(1); self::assertTrue($this->repo->updateStatus(9, PolicyVersion::STATUS_PUBLISHED, '2026-06-01 12:00:00')); } public function testUpdateBody(): void { $this->db->shouldReceive('update') ->once() ->with('wp_us_policy_versions', ['body' => 'new
'], ['id' => 9], ['%s'], ['%d']) ->andReturn(1); self::assertTrue($this->repo->updateBody(9, 'new
')); } public function testMaxVersionNumberReturnsZeroWhenNone(): void { $this->db->shouldReceive('prepare')->andReturn('SELECT ...'); $this->db->shouldReceive('get_var')->andReturn(null); self::assertSame(0, $this->repo->maxVersionNumber(4)); } public function testMaxVersionNumberCastsResult(): void { $this->db->shouldReceive('prepare')->andReturn('SELECT ...'); $this->db->shouldReceive('get_var')->andReturn('3'); self::assertSame(3, $this->repo->maxVersionNumber(4)); } public function testFindByPolicyMapsRows(): void { $this->db->shouldReceive('prepare')->andReturn('SELECT ...'); $this->db->shouldReceive('get_results')->andReturn([ (object) [ 'id' => '9', 'policy_id' => '4', 'version_number' => '1', 'body' => null, 'status' => PolicyVersion::STATUS_DRAFT, 'published_at' => null, ], ]); $versions = $this->repo->findByPolicy(4); self::assertCount(1, $versions); self::assertInstanceOf(PolicyVersion::class, $versions[0]); } }