db = Mockery::mock(\wpdb::class); $this->db->prefix = 'wp_'; $this->repo = new AnswerRepository($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_question_answers', Mockery::on(static function (array $data): bool { return $data['question_id'] === 3 && $data['registration_type'] === Answer::REG_LESSON && $data['registration_id'] === 12 && $data['student_id'] === 5 && $data['answer_value'] === 'Beginner'; }), ['%d', '%s', '%d', '%d', '%s', '%s'] ); $this->db->insert_id = 77; $answer = new Answer(3, Answer::REG_LESSON, 12, 5, 'Beginner'); self::assertSame(77, $this->repo->insert($answer)); } public function testInsertManyReturnsAllIds(): void { Functions\when('current_time')->justReturn('2026-04-01 12:00:00'); $ids = [101, 102]; $this->db->shouldReceive('insert') ->twice() ->andReturnUsing(function () use (&$ids): void { $this->db->insert_id = array_shift($ids); }); $answers = [ new Answer(3, Answer::REG_LESSON, 12, 5, 'A'), new Answer(4, Answer::REG_LESSON, 12, 5, 'B'), ]; $result = $this->repo->insertMany($answers); self::assertSame([101, 102], $result); } public function testFindByRegistrationPreparesQueryAndMaps(): void { $row = (object) [ 'id' => '1', 'question_id' => '3', 'registration_type' => Answer::REG_LESSON, 'registration_id' => '12', 'student_id' => '5', 'answer_value' => 'Beginner', ]; $this->db->shouldReceive('prepare') ->once() ->with( Mockery::pattern('/registration_type = %s AND registration_id = %d/'), Answer::REG_LESSON, 12 ) ->andReturn('SELECT ...'); $this->db->shouldReceive('get_results')->andReturn([$row]); $answers = $this->repo->findByRegistration(Answer::REG_LESSON, 12); self::assertCount(1, $answers); self::assertInstanceOf(Answer::class, $answers[0]); self::assertSame(3, $answers[0]->questionId); } }