diff --git a/includes/internal/quiz-submission/answer/repositories/class-aggregate-answer-repository.php b/includes/internal/quiz-submission/answer/repositories/class-aggregate-answer-repository.php index a4d38f0764..7e8d7d525d 100644 --- a/includes/internal/quiz-submission/answer/repositories/class-aggregate-answer-repository.php +++ b/includes/internal/quiz-submission/answer/repositories/class-aggregate-answer-repository.php @@ -89,10 +89,8 @@ public function create( Submission $submission, int $question_id, string $value $answer = $this->comments_based_repository->create( $submission, $question_id, $value ); if ( $this->use_tables ) { - $tables_based_submission = $this->get_tables_based_submission( $submission ); - if ( $tables_based_submission ) { - $this->tables_based_repository->create( $tables_based_submission, $question_id, $value ); - } + $tables_based_submission = $this->get_or_create_tables_based_submission( $submission ); + $this->tables_based_repository->create( $tables_based_submission, $question_id, $value ); } return $answer; @@ -122,21 +120,23 @@ public function delete_all( Submission $submission ): void { $this->comments_based_repository->delete_all( $submission ); if ( $this->use_tables ) { - $tables_based_submission = $this->get_tables_based_submission( $submission ); - if ( $tables_based_submission ) { - $this->tables_based_repository->delete_all( $tables_based_submission ); - } + $tables_based_submission = $this->get_or_create_tables_based_submission( $submission ); + $this->tables_based_repository->delete_all( $tables_based_submission ); } } /** - * Get the tables based submission for a given submission. + * Get the tables based submission for a given submission or create if not exists. * * @param Submission $submission The submission. * - * @return Submission|null The tables based submission or null if it does not exist. + * @return Submission The tables based submission or null if it does not exist. */ - private function get_tables_based_submission( Submission $submission ): ?Submission { - return $this->tables_based_submission_repository->get( $submission->get_quiz_id(), $submission->get_user_id() ); + private function get_or_create_tables_based_submission( Submission $submission ): Submission { + return $this->tables_based_submission_repository->get_or_create( + $submission->get_quiz_id(), + $submission->get_user_id(), + $submission->get_final_grade() + ); } } diff --git a/includes/internal/quiz-submission/grade/repositories/class-aggregate-grade-repository.php b/includes/internal/quiz-submission/grade/repositories/class-aggregate-grade-repository.php index 0a452e19e1..e72ac1b712 100644 --- a/includes/internal/quiz-submission/grade/repositories/class-aggregate-grade-repository.php +++ b/includes/internal/quiz-submission/grade/repositories/class-aggregate-grade-repository.php @@ -9,10 +9,10 @@ use DateTimeImmutable; use Sensei\Internal\Quiz_Submission\Answer\Models\Answer; +use Sensei\Internal\Quiz_Submission\Answer\Repositories\Comments_Based_Answer_Repository; use Sensei\Internal\Quiz_Submission\Answer\Repositories\Tables_Based_Answer_Repository; use Sensei\Internal\Quiz_Submission\Grade\Models\Grade; use Sensei\Internal\Quiz_Submission\Submission\Models\Submission; -use Sensei\Internal\Quiz_Submission\Submission\Repositories\Comments_Based_Submission_Repository; use Sensei\Internal\Quiz_Submission\Submission\Repositories\Tables_Based_Submission_Repository; if ( ! defined( 'ABSPATH' ) ) { @@ -56,6 +56,14 @@ class Aggregate_Grade_Repository implements Grade_Repository_Interface { */ private $tables_based_answer_repository; + + /** + * Comments based answer repository. + * + * @var Comments_Based_Answer_Repository + */ + private $comments_based_answer_repository; + /** * The flag if the tables based implementation is available for use. * @@ -72,6 +80,7 @@ class Aggregate_Grade_Repository implements Grade_Repository_Interface { * @param Tables_Based_Grade_Repository $tables_based_repository Tables based quiz answer repository implementation. * @param Tables_Based_Submission_Repository $tables_based_submission_repository Tables based quiz submission repository implementation. * @param Tables_Based_Answer_Repository $tables_based_answer_repository Tables based quiz answer repository implementation. + * @param Comments_Based_Answer_Repository $comments_based_answer_repository Comments based quiz answer repository implementation. * @param bool $use_tables The flag if the tables based implementation is available for use. */ public function __construct( @@ -79,12 +88,14 @@ public function __construct( Tables_Based_Grade_Repository $tables_based_repository, Tables_Based_Submission_Repository $tables_based_submission_repository, Tables_Based_Answer_Repository $tables_based_answer_repository, + Comments_Based_Answer_Repository $comments_based_answer_repository, bool $use_tables ) { $this->comments_based_repository = $comments_based_repository; $this->tables_based_repository = $tables_based_repository; $this->tables_based_submission_repository = $tables_based_submission_repository; $this->tables_based_answer_repository = $tables_based_answer_repository; + $this->comments_based_answer_repository = $comments_based_answer_repository; $this->use_tables = $use_tables; } @@ -105,25 +116,52 @@ public function create( Submission $submission, int $answer_id, int $question_id $grade = $this->comments_based_repository->create( $submission, $answer_id, $question_id, $points, $feedback ); if ( $this->use_tables ) { - $tables_based_submission = $this->tables_based_submission_repository->get( $submission->get_quiz_id(), $submission->get_user_id() ); - if ( $tables_based_submission ) { - $answers = $this->tables_based_answer_repository->get_all( $tables_based_submission->get_id() ); - $filtered = array_filter( - $answers, - function( Answer $answer ) use ( $question_id ) { - return $answer->get_question_id() === $question_id; - } - ); - if ( count( $filtered ) === 1 ) { - $answer = array_shift( $filtered ); - $this->tables_based_repository->create( $tables_based_submission, $answer->get_id(), $question_id, $points, $feedback ); - } + $tables_based_submission = $this->get_or_create_tables_based_submission( $submission ); + + $answers = $this->get_or_create_tables_based_answers( $submission, $tables_based_submission ); + $answer = $answers[ $question_id ] ?? null; + + if ( $answer ) { + $this->tables_based_repository->create( $tables_based_submission, $answer->get_id(), $question_id, $points, $feedback ); } } return $grade; } + /** + * Get or create all answers for the table based submission. + * + * @param Submission $comments_based_submission The comments based submission. + * @param Submission $tables_based_submission The tables based submission. + * @return Answer[] The answers. + */ + public function get_or_create_tables_based_answers( $comments_based_submission, $tables_based_submission ): array { + $comments_based_answers = $this->comments_based_answer_repository->get_all( $comments_based_submission->get_id() ); + $tables_based_answers = $this->tables_based_answer_repository->get_all( $tables_based_submission->get_id() ); + $result = array(); + foreach ( $comments_based_answers as $comments_based_answer ) { + $filtered = array_filter( + $tables_based_answers, + function( Answer $answer ) use ( $comments_based_answer ) { + return $answer->get_question_id() === $comments_based_answer->get_question_id(); + } + ); + if ( count( $filtered ) === 1 ) { + $answer = array_shift( $filtered ); + $result[ $answer->get_question_id() ] = $answer; + } else { + $result[ $comments_based_answer->get_question_id() ] = $this->tables_based_answer_repository->create( + $tables_based_submission, + $comments_based_answer->get_question_id(), + $comments_based_answer->get_value() + ); + } + } + + return $result; + } + /** * Get all grades for a quiz submission. * @@ -149,41 +187,86 @@ public function save_many( Submission $submission, array $grades ): void { $this->comments_based_repository->save_many( $submission, $grades ); if ( $this->use_tables ) { - $grades_to_save = []; - $tables_based_submission = $this->tables_based_submission_repository->get( $submission->get_quiz_id(), $submission->get_user_id() ); - if ( $tables_based_submission ) { - $tables_based_grades = $this->tables_based_repository->get_all( $tables_based_submission->get_id() ); - foreach ( $grades as $grade ) { - $filtered = array_filter( - $tables_based_grades, - function( Grade $tables_based_grade ) use ( $grade ) { - return $tables_based_grade->get_question_id() === $grade->get_question_id(); - } - ); - if ( count( $filtered ) !== 1 ) { - continue; - } - $tables_based_grade = array_shift( $filtered ); - - $created_at = new DateTimeImmutable( '@' . $grade->get_created_at()->getTimestamp() ); - $updated_at = new DateTimeImmutable( '@' . $grade->get_updated_at()->getTimestamp() ); - - $grades_to_save[] = new Grade( - $tables_based_grade->get_id(), - $tables_based_grade->get_answer_id(), - $tables_based_grade->get_question_id(), - $grade->get_points(), - $grade->get_feedback(), - $created_at, - $updated_at - ); + $tables_based_submission = $this->get_or_create_tables_based_submission( $submission ); + $tables_based_grades = $this->get_or_create_tables_based_grades_for_save( + $submission, + $tables_based_submission, + $grades + ); + + $grades_to_save = []; + foreach ( $grades as $grade ) { + $tables_based_grade = $tables_based_grades[ $grade->get_question_id() ] ?? null; + if ( null === $tables_based_grade ) { + continue; } - $this->tables_based_repository->save_many( $tables_based_submission, $grades_to_save ); + $created_at = new DateTimeImmutable( '@' . $grade->get_created_at()->getTimestamp() ); + $updated_at = new DateTimeImmutable( '@' . $grade->get_updated_at()->getTimestamp() ); + + $grades_to_save[] = new Grade( + $tables_based_grade->get_id(), + $tables_based_grade->get_answer_id(), + $tables_based_grade->get_question_id(), + $grade->get_points(), + $grade->get_feedback(), + $created_at, + $updated_at + ); } + + $this->tables_based_repository->save_many( $tables_based_submission, $grades_to_save ); } } + /** + * Get or create all grades for the table based submission. + * + * @param Submission $comments_based_submission The comments based submission. + * @param Submission $tables_based_submission The tables based submission. + * @param Grade[] $comments_based_grades The comments based grades. + * @return Grade[] The tables based grades. + */ + private function get_or_create_tables_based_grades_for_save( + Submission $comments_based_submission, + Submission $tables_based_submission, + array $comments_based_grades + ): array { + $tables_based_answers = $this->get_or_create_tables_based_answers( + $comments_based_submission, + $tables_based_submission + ); + $tables_based_grades = $this->tables_based_repository->get_all( $tables_based_submission->get_id() ); + $result = array(); + foreach ( $comments_based_grades as $comments_based_grade ) { + $filtered = array_filter( + $tables_based_grades, + function( Grade $grade ) use ( $comments_based_grade ) { + return $grade->get_question_id() === $comments_based_grade->get_question_id(); + } + ); + if ( count( $filtered ) === 1 ) { + $grade = array_shift( $filtered ); + $result[ $grade->get_question_id() ] = $grade; + } else { + $answer = $tables_based_answers[ $comments_based_grade->get_question_id() ] ?? null; + if ( ! $answer ) { + continue; + } + + $result[ $comments_based_grade->get_question_id() ] = $this->tables_based_repository->create( + $tables_based_submission, + $answer->get_id(), + $comments_based_grade->get_question_id(), + $comments_based_grade->get_points(), + $comments_based_grade->get_feedback() + ); + } + } + + return $result; + } + /** * Delete all grades for a submission. * @@ -195,10 +278,23 @@ public function delete_all( Submission $submission ): void { $this->comments_based_repository->delete_all( $submission ); if ( $this->use_tables ) { - $tables_based_submission = $this->tables_based_submission_repository->get( $submission->get_quiz_id(), $submission->get_user_id() ); - if ( $tables_based_submission ) { - $this->tables_based_repository->delete_all( $tables_based_submission ); - } + $tables_based_submission = $this->get_or_create_tables_based_submission( $submission ); + $this->tables_based_repository->delete_all( $tables_based_submission ); } } + + /** + * Get the tables based submission for a given submission or create if not exists. + * + * @param Submission $submission The submission. + * + * @return Submission The tables based submission. + */ + private function get_or_create_tables_based_submission( Submission $submission ): Submission { + return $this->tables_based_submission_repository->get_or_create( + $submission->get_quiz_id(), + $submission->get_user_id(), + $submission->get_final_grade() + ); + } } diff --git a/includes/internal/quiz-submission/grade/repositories/class-grade-repository-factory.php b/includes/internal/quiz-submission/grade/repositories/class-grade-repository-factory.php index 7158ce073c..e5f88a4bf8 100644 --- a/includes/internal/quiz-submission/grade/repositories/class-grade-repository-factory.php +++ b/includes/internal/quiz-submission/grade/repositories/class-grade-repository-factory.php @@ -7,6 +7,7 @@ namespace Sensei\Internal\Quiz_Submission\Grade\Repositories; +use Sensei\Internal\Quiz_Submission\Answer\Repositories\Comments_Based_Answer_Repository; use Sensei\Internal\Quiz_Submission\Answer\Repositories\Tables_Based_Answer_Repository; use Sensei\Internal\Quiz_Submission\Submission\Repositories\Tables_Based_Submission_Repository; @@ -56,6 +57,7 @@ public function create(): Grade_Repository_Interface { new Tables_Based_Grade_Repository( $wpdb ), new Tables_Based_Submission_Repository( $wpdb ), new Tables_Based_Answer_Repository( $wpdb ), + new Comments_Based_Answer_Repository(), $this->use_tables ); } diff --git a/includes/internal/quiz-submission/submission/repositories/class-aggregate-submission-repository.php b/includes/internal/quiz-submission/submission/repositories/class-aggregate-submission-repository.php index 32bc8ffc8f..ee4da2d75b 100644 --- a/includes/internal/quiz-submission/submission/repositories/class-aggregate-submission-repository.php +++ b/includes/internal/quiz-submission/submission/repositories/class-aggregate-submission-repository.php @@ -91,13 +91,7 @@ public function create( int $quiz_id, int $user_id, float $final_grade = null ): * @return Submission The quiz submission. */ public function get_or_create( int $quiz_id, int $user_id, float $final_grade = null ): Submission { - $submission = $this->get( $quiz_id, $user_id ); - - if ( $submission ) { - return $submission; - } - - return $this->create( $quiz_id, $user_id, $final_grade ); + return $this->comments_based_repository->get_or_create( $quiz_id, $user_id, $final_grade ); } /** @@ -138,24 +132,26 @@ public function save( Submission $submission ): void { $this->comments_based_repository->save( $submission ); if ( $this->use_tables ) { - $tables_based_submission = $this->tables_based_repository->get( $submission->get_quiz_id(), $submission->get_user_id() ); - - if ( $tables_based_submission ) { - // Make sure the dates are in UTC. - $created_at = new DateTimeImmutable( '@' . $submission->get_created_at()->getTimestamp() ); - $updated_at = new DateTimeImmutable( '@' . $submission->get_updated_at()->getTimestamp() ); - - $submission_to_save = new Submission( - $tables_based_submission->get_id(), - $submission->get_quiz_id(), - $submission->get_user_id(), - $submission->get_final_grade(), - $created_at, - $updated_at - ); - - $this->tables_based_repository->save( $submission_to_save ); - } + $tables_based_submission = $this->tables_based_repository->get_or_create( + $submission->get_quiz_id(), + $submission->get_user_id(), + $submission->get_final_grade() + ); + + // Make sure the dates are in UTC. + $created_at = new DateTimeImmutable( '@' . $submission->get_created_at()->getTimestamp() ); + $updated_at = new DateTimeImmutable( '@' . $submission->get_updated_at()->getTimestamp() ); + + $submission_to_save = new Submission( + $tables_based_submission->get_id(), + $submission->get_quiz_id(), + $submission->get_user_id(), + $submission->get_final_grade(), + $created_at, + $updated_at + ); + + $this->tables_based_repository->save( $submission_to_save ); } } diff --git a/includes/internal/student-progress/course-progress/repositories/class-aggregate-course-progress-repository.php b/includes/internal/student-progress/course-progress/repositories/class-aggregate-course-progress-repository.php index 99f416feb7..512830aae0 100644 --- a/includes/internal/student-progress/course-progress/repositories/class-aggregate-course-progress-repository.php +++ b/includes/internal/student-progress/course-progress/repositories/class-aggregate-course-progress-repository.php @@ -113,28 +113,34 @@ public function save( Course_Progress $course_progress ): void { $this->comments_based_repository->save( $course_progress ); if ( $this->use_tables ) { $tables_based_progress = $this->tables_based_repository->get( $course_progress->get_course_id(), $course_progress->get_user_id() ); - if ( $tables_based_progress ) { - $started_at = null; - if ( $course_progress->get_started_at() ) { - $started_at = new \DateTimeImmutable( '@' . $course_progress->get_started_at()->getTimestamp() ); - } - $completed_at = null; - if ( $course_progress->get_completed_at() ) { - $completed_at = new \DateTimeImmutable( '@' . $course_progress->get_completed_at()->getTimestamp() ); - } - - $progress_to_save = new Course_Progress( - $tables_based_progress->get_id(), - $tables_based_progress->get_course_id(), - $tables_based_progress->get_user_id(), - $course_progress->get_status(), - $started_at, - $completed_at, - $tables_based_progress->get_created_at(), - $tables_based_progress->get_updated_at() + if ( ! $tables_based_progress ) { + $tables_based_progress = $this->tables_based_repository->create( + $course_progress->get_course_id(), + $course_progress->get_user_id() ); - $this->tables_based_repository->save( $progress_to_save ); } + + $started_at = null; + if ( $course_progress->get_started_at() ) { + $started_at = new \DateTimeImmutable( '@' . $course_progress->get_started_at()->getTimestamp() ); + } + + $completed_at = null; + if ( $course_progress->get_completed_at() ) { + $completed_at = new \DateTimeImmutable( '@' . $course_progress->get_completed_at()->getTimestamp() ); + } + + $progress_to_save = new Course_Progress( + $tables_based_progress->get_id(), + $tables_based_progress->get_course_id(), + $tables_based_progress->get_user_id(), + $course_progress->get_status(), + $started_at, + $completed_at, + $tables_based_progress->get_created_at(), + $tables_based_progress->get_updated_at() + ); + $this->tables_based_repository->save( $progress_to_save ); } } diff --git a/includes/internal/student-progress/lesson-progress/repositories/class-aggregate-lesson-progress-repository.php b/includes/internal/student-progress/lesson-progress/repositories/class-aggregate-lesson-progress-repository.php index 2baac0fc6a..6363d57698 100644 --- a/includes/internal/student-progress/lesson-progress/repositories/class-aggregate-lesson-progress-repository.php +++ b/includes/internal/student-progress/lesson-progress/repositories/class-aggregate-lesson-progress-repository.php @@ -113,28 +113,34 @@ public function save( Lesson_Progress $lesson_progress ): void { $this->comments_based_repository->save( $lesson_progress ); if ( $this->use_tables ) { $tables_based_progress = $this->tables_based_repository->get( $lesson_progress->get_lesson_id(), $lesson_progress->get_user_id() ); - if ( $tables_based_progress ) { - $started_at = null; - if ( $lesson_progress->get_started_at() ) { - $started_at = new \DateTimeImmutable( '@' . $lesson_progress->get_started_at()->getTimestamp() ); - } - $completed_at = null; - if ( $lesson_progress->get_completed_at() ) { - $completed_at = new \DateTimeImmutable( '@' . $lesson_progress->get_completed_at()->getTimestamp() ); - } - - $progress_to_save = new Lesson_Progress( - $tables_based_progress->get_id(), - $tables_based_progress->get_lesson_id(), - $tables_based_progress->get_user_id(), - $lesson_progress->get_status(), - $started_at, - $completed_at, - $tables_based_progress->get_created_at(), - $tables_based_progress->get_updated_at() + if ( ! $tables_based_progress ) { + $tables_based_progress = $this->tables_based_repository->create( + $lesson_progress->get_lesson_id(), + $lesson_progress->get_user_id() ); - $this->tables_based_repository->save( $progress_to_save ); } + + $started_at = null; + if ( $lesson_progress->get_started_at() ) { + $started_at = new \DateTimeImmutable( '@' . $lesson_progress->get_started_at()->getTimestamp() ); + } + + $completed_at = null; + if ( $lesson_progress->get_completed_at() ) { + $completed_at = new \DateTimeImmutable( '@' . $lesson_progress->get_completed_at()->getTimestamp() ); + } + + $progress_to_save = new Lesson_Progress( + $tables_based_progress->get_id(), + $tables_based_progress->get_lesson_id(), + $tables_based_progress->get_user_id(), + $lesson_progress->get_status(), + $started_at, + $completed_at, + $tables_based_progress->get_created_at(), + $tables_based_progress->get_updated_at() + ); + $this->tables_based_repository->save( $progress_to_save ); } } diff --git a/includes/internal/student-progress/quiz-progress/repositories/class-aggregate-quiz-progress-repository.php b/includes/internal/student-progress/quiz-progress/repositories/class-aggregate-quiz-progress-repository.php index 63a552aa00..3f92b93ec5 100644 --- a/includes/internal/student-progress/quiz-progress/repositories/class-aggregate-quiz-progress-repository.php +++ b/includes/internal/student-progress/quiz-progress/repositories/class-aggregate-quiz-progress-repository.php @@ -113,28 +113,34 @@ public function save( Quiz_Progress $quiz_progress ): void { $this->comments_based_repository->save( $quiz_progress ); if ( $this->use_tables ) { $tables_based_progress = $this->tables_based_repository->get( $quiz_progress->get_quiz_id(), $quiz_progress->get_user_id() ); - if ( $tables_based_progress ) { - $started_at = null; - if ( $quiz_progress->get_started_at() ) { - $started_at = new \DateTimeImmutable( '@' . $quiz_progress->get_started_at()->getTimestamp() ); - } - $completed_at = null; - if ( $quiz_progress->get_completed_at() ) { - $completed_at = new \DateTimeImmutable( '@' . $quiz_progress->get_completed_at()->getTimestamp() ); - } - - $progress_to_save = new Quiz_Progress( - $tables_based_progress->get_id(), - $tables_based_progress->get_quiz_id(), - $tables_based_progress->get_user_id(), - $quiz_progress->get_status(), - $started_at, - $completed_at, - $tables_based_progress->get_created_at(), - $tables_based_progress->get_updated_at() + if ( ! $tables_based_progress ) { + $tables_based_progress = $this->tables_based_repository->create( + $quiz_progress->get_quiz_id(), + $quiz_progress->get_user_id() ); - $this->tables_based_repository->save( $progress_to_save ); } + + $started_at = null; + if ( $quiz_progress->get_started_at() ) { + $started_at = new \DateTimeImmutable( '@' . $quiz_progress->get_started_at()->getTimestamp() ); + } + + $completed_at = null; + if ( $quiz_progress->get_completed_at() ) { + $completed_at = new \DateTimeImmutable( '@' . $quiz_progress->get_completed_at()->getTimestamp() ); + } + + $progress_to_save = new Quiz_Progress( + $tables_based_progress->get_id(), + $tables_based_progress->get_quiz_id(), + $tables_based_progress->get_user_id(), + $quiz_progress->get_status(), + $started_at, + $completed_at, + $tables_based_progress->get_created_at(), + $tables_based_progress->get_updated_at() + ); + $this->tables_based_repository->save( $progress_to_save ); } } diff --git a/tests/unit-tests/internal/migration/test-class-migration-tool.php b/tests/unit-tests/internal/migration/test-class-migration-tool.php index 0d17ce6b86..b0ff4ff532 100644 --- a/tests/unit-tests/internal/migration/test-class-migration-tool.php +++ b/tests/unit-tests/internal/migration/test-class-migration-tool.php @@ -8,7 +8,7 @@ /** * Class Migration_Tool_Test * - * @covers \SenseiTest\Internal\Migration\Migration_Tool + * @covers \Sensei\Internal\Student_Progress\Tools\Migration_Tool */ class Migration_Tool_Test extends \WP_UnitTestCase { public function testInit_Always_AddsFilter(): void { diff --git a/tests/unit-tests/internal/quiz-submission/answer/repositories/test-class-aggregate-answer-repository.php b/tests/unit-tests/internal/quiz-submission/answer/repositories/test-class-aggregate-answer-repository.php index 80657cf34d..7ffca2e21a 100644 --- a/tests/unit-tests/internal/quiz-submission/answer/repositories/test-class-aggregate-answer-repository.php +++ b/tests/unit-tests/internal/quiz-submission/answer/repositories/test-class-aggregate-answer-repository.php @@ -22,14 +22,18 @@ public function testCreate_UseTablesOn_CallsTablesBasedRepository(): void { $submission = $this->createMock( Submission::class ); $submission->method( 'get_quiz_id' )->willReturn( 1 ); $submission->method( 'get_user_id' )->willReturn( 2 ); - $tables_based_submission = $this->createMock( Submission::class ); - $comments_based = $this->createMock( Comments_Based_Answer_Repository::class ); - $tables_based = $this->createMock( Tables_Based_Answer_Repository::class ); + $submission->method( 'get_final_grade' )->willReturn( 3.0 ); + + $tables_based_submission = $this->createMock( Submission::class ); + $comments_based = $this->createMock( Comments_Based_Answer_Repository::class ); + $tables_based = $this->createMock( Tables_Based_Answer_Repository::class ); + $tables_based_submission_repository = $this->createMock( Tables_Based_Submission_Repository::class ); $tables_based_submission_repository - ->method( 'get' ) - ->with( 1, 2 ) + ->method( 'get_or_create' ) + ->with( 1, 2, 3.0 ) ->willReturn( $tables_based_submission ); + $repository = new Aggregate_Answer_Repository( $comments_based, $tables_based, @@ -70,15 +74,16 @@ public function testCreate_UseTablesOn_CallsCommentsBasedRepository(): void { public function testCreate_UseTablesOff_DoesntCallTablesBasedRepository(): void { /* Arrange. */ - $submission = $this->createMock( Submission::class ); - $comments_based = $this->createMock( Comments_Based_Answer_Repository::class ); - $tables_based = $this->createMock( Tables_Based_Answer_Repository::class ); + $submission = $this->createMock( Submission::class ); + $comments_based = $this->createMock( Comments_Based_Answer_Repository::class ); + $tables_based = $this->createMock( Tables_Based_Answer_Repository::class ); + $tables_based_submission_repository = $this->createMock( Tables_Based_Submission_Repository::class ); $repository = new Aggregate_Answer_Repository( $comments_based, $tables_based, $tables_based_submission_repository, - true + false ); /* Expect & Act. */ @@ -142,7 +147,7 @@ public function testDeleteAll_UseTablesOff_DoesntCallTablesBasedRepository(): vo $comments_based, $tables_based, $tables_based_submission_repository, - true + false ); /* Expect & Act. */ @@ -158,14 +163,18 @@ public function testDeleteAll_UseTablesOn_CallsTablesBasedRepository(): void { $submission = $this->createMock( Submission::class ); $submission->method( 'get_quiz_id' )->willReturn( 1 ); $submission->method( 'get_user_id' )->willReturn( 2 ); - $tables_based_submission = $this->createMock( Submission::class ); - $comments_based = $this->createMock( Comments_Based_Answer_Repository::class ); - $tables_based = $this->createMock( Tables_Based_Answer_Repository::class ); + $submission->method( 'get_final_grade' )->willReturn( 3.0 ); + + $tables_based_submission = $this->createMock( Submission::class ); + $comments_based = $this->createMock( Comments_Based_Answer_Repository::class ); + $tables_based = $this->createMock( Tables_Based_Answer_Repository::class ); + $tables_based_submission_repository = $this->createMock( Tables_Based_Submission_Repository::class ); $tables_based_submission_repository - ->method( 'get' ) - ->with( 1, 2 ) + ->method( 'get_or_create' ) + ->with( 1, 2.0 ) ->willReturn( $tables_based_submission ); + $repository = new Aggregate_Answer_Repository( $comments_based, $tables_based, diff --git a/tests/unit-tests/internal/quiz-submission/grade/repositories/test-class-aggregate-grade-repository.php b/tests/unit-tests/internal/quiz-submission/grade/repositories/test-class-aggregate-grade-repository.php index db757b9765..48975a1607 100644 --- a/tests/unit-tests/internal/quiz-submission/grade/repositories/test-class-aggregate-grade-repository.php +++ b/tests/unit-tests/internal/quiz-submission/grade/repositories/test-class-aggregate-grade-repository.php @@ -2,7 +2,10 @@ namespace SenseiTest\Internal\Quiz_Submission\Grade\Repositories; +use DateTime; use DateTimeImmutable; +use Sensei\Internal\Quiz_Submission\Answer\Models\Answer; +use Sensei\Internal\Quiz_Submission\Answer\Repositories\Comments_Based_Answer_Repository; use Sensei\Internal\Quiz_Submission\Answer\Repositories\Tables_Based_Answer_Repository; use Sensei\Internal\Quiz_Submission\Grade\Models\Grade; use Sensei\Internal\Quiz_Submission\Grade\Repositories\Aggregate_Grade_Repository; @@ -24,12 +27,14 @@ public function testCreate_Always_UsesCommentsBasedRepository(): void { $tables_based_repository = $this->createMock( Tables_Based_Grade_Repository::class ); $tables_based_submission_repository = $this->createMock( Tables_Based_Submission_Repository::class ); $tables_based_answer_repository = $this->createMock( Tables_Based_Answer_Repository::class ); + $comments_based_answer_repository = $this->createMock( Comments_Based_Answer_Repository::class ); $repository = new Aggregate_Grade_Repository( $comments_based_repository, $tables_based_repository, $tables_based_submission_repository, $tables_based_answer_repository, + $comments_based_answer_repository, true ); @@ -48,12 +53,14 @@ public function testCreate_UseTablesSetToFalse_DoesntUseCommentsBasedRepository( $tables_based_repository = $this->createMock( Tables_Based_Grade_Repository::class ); $tables_based_submission_repository = $this->createMock( Tables_Based_Submission_Repository::class ); $tables_based_answer_repository = $this->createMock( Tables_Based_Answer_Repository::class ); + $comments_based_answer_repository = $this->createMock( Comments_Based_Answer_Repository::class ); $repository = new Aggregate_Grade_Repository( $comments_based_repository, $tables_based_repository, $tables_based_submission_repository, $tables_based_answer_repository, + $comments_based_answer_repository, true ); @@ -70,12 +77,14 @@ public function testGetAll_Always_UsesCommentsBasedRepository(): void { $tables_based_repository = $this->createMock( Tables_Based_Grade_Repository::class ); $tables_based_submission_repository = $this->createMock( Tables_Based_Submission_Repository::class ); $tables_based_answer_repository = $this->createMock( Tables_Based_Answer_Repository::class ); + $comments_based_answer_repository = $this->createMock( Comments_Based_Answer_Repository::class ); $repository = new Aggregate_Grade_Repository( $comments_based_repository, $tables_based_repository, $tables_based_submission_repository, $tables_based_answer_repository, + $comments_based_answer_repository, true ); @@ -93,12 +102,14 @@ public function testGetAll_Always_DoesntUseTablesBasedRepository(): void { $tables_based_repository = $this->createMock( Tables_Based_Grade_Repository::class ); $tables_based_submission_repository = $this->createMock( Tables_Based_Submission_Repository::class ); $tables_based_answer_repository = $this->createMock( Tables_Based_Answer_Repository::class ); + $comments_based_answer_repository = $this->createMock( Comments_Based_Answer_Repository::class ); $repository = new Aggregate_Grade_Repository( $comments_based_repository, $tables_based_repository, $tables_based_submission_repository, $tables_based_answer_repository, + $comments_based_answer_repository, true ); @@ -116,12 +127,14 @@ public function testDeleteAll_Always_UsesCommentsBasedRepository(): void { $tables_based_repository = $this->createMock( Tables_Based_Grade_Repository::class ); $tables_based_submission_repository = $this->createMock( Tables_Based_Submission_Repository::class ); $tables_based_answer_repository = $this->createMock( Tables_Based_Answer_Repository::class ); + $comments_based_answer_repository = $this->createMock( Comments_Based_Answer_Repository::class ); $repository = new Aggregate_Grade_Repository( $comments_based_repository, $tables_based_repository, $tables_based_submission_repository, $tables_based_answer_repository, + $comments_based_answer_repository, true ); @@ -140,12 +153,14 @@ public function testDeleteAll_UseTablesSetToFalse_DoesntUseTablesBasedRepository $tables_based_repository = $this->createMock( Tables_Based_Grade_Repository::class ); $tables_based_submission_repository = $this->createMock( Tables_Based_Submission_Repository::class ); $tables_based_answer_repository = $this->createMock( Tables_Based_Answer_Repository::class ); + $comments_based_answer_repository = $this->createMock( Comments_Based_Answer_Repository::class ); $repository = new Aggregate_Grade_Repository( $comments_based_repository, $tables_based_repository, $tables_based_submission_repository, $tables_based_answer_repository, + $comments_based_answer_repository, false ); @@ -161,6 +176,7 @@ public function testDeleteAll_UseTablesSetToTrue_UsesTablesBasedRepository(): vo $submission = $this->createMock( Submission::class ); $submission->method( 'get_quiz_id' )->willReturn( 1 ); $submission->method( 'get_user_id' )->willReturn( 2 ); + $submission->method( 'get_final_grade' )->willReturn( 3.0 ); $tables_based_submission = $this->createMock( Submission::class ); @@ -169,17 +185,19 @@ public function testDeleteAll_UseTablesSetToTrue_UsesTablesBasedRepository(): vo $tables_based_submission_repository = $this->createMock( Tables_Based_Submission_Repository::class ); $tables_based_submission_repository - ->method( 'get' ) + ->method( 'get_or_create' ) ->with( 1, 2 ) ->willReturn( $tables_based_submission ); - $tables_based_answer_repository = $this->createMock( Tables_Based_Answer_Repository::class ); + $tables_based_answer_repository = $this->createMock( Tables_Based_Answer_Repository::class ); + $comments_based_answer_repository = $this->createMock( Comments_Based_Answer_Repository::class ); $repository = new Aggregate_Grade_Repository( $comments_based_repository, $tables_based_repository, $tables_based_submission_repository, $tables_based_answer_repository, + $comments_based_answer_repository, true ); @@ -198,6 +216,7 @@ public function testSaveMany_Always_UsesCommentsBasedRepository(): void { $tables_based_repository = $this->createMock( Tables_Based_Grade_Repository::class ); $tables_based_submission_repository = $this->createMock( Tables_Based_Submission_Repository::class ); $tables_based_answer_repository = $this->createMock( Tables_Based_Answer_Repository::class ); + $comments_based_answer_repository = $this->createMock( Comments_Based_Answer_Repository::class ); $grades = [ $this->createMock( Grade::class ) ]; $repository = new Aggregate_Grade_Repository( @@ -205,6 +224,7 @@ public function testSaveMany_Always_UsesCommentsBasedRepository(): void { $tables_based_repository, $tables_based_submission_repository, $tables_based_answer_repository, + $comments_based_answer_repository, true ); @@ -219,13 +239,14 @@ public function testSaveMany_Always_UsesCommentsBasedRepository(): void { $repository->save_many( $submission, $grades ); } - public function testSaveMany_UseTablesSetToFalse_DoesntUseCommentsBasedRepository(): void { + public function testSaveMany_UseTablesSetToFalse_DoesntUseTablesBasedRepository(): void { /* Arrange */ $submission = $this->createMock( Submission::class ); $comments_based_repository = $this->createMock( Comments_Based_Grade_Repository::class ); $tables_based_repository = $this->createMock( Tables_Based_Grade_Repository::class ); $tables_based_submission_repository = $this->createMock( Tables_Based_Submission_Repository::class ); $tables_based_answer_repository = $this->createMock( Tables_Based_Answer_Repository::class ); + $comments_based_answer_repository = $this->createMock( Comments_Based_Answer_Repository::class ); $grades = [ $this->createMock( Grade::class ) ]; $repository = new Aggregate_Grade_Repository( @@ -233,7 +254,8 @@ public function testSaveMany_UseTablesSetToFalse_DoesntUseCommentsBasedRepositor $tables_based_repository, $tables_based_submission_repository, $tables_based_answer_repository, - true + $comments_based_answer_repository, + false ); /* Expect & Act */ @@ -243,11 +265,12 @@ public function testSaveMany_UseTablesSetToFalse_DoesntUseCommentsBasedRepositor $repository->save_many( $submission, $grades ); } - public function testSaveMany_UseTablesSetToTrue_UsesCommentsBasedRepository(): void { + public function testSaveMany_UseTablesSetToTrue_UsesTablesBasedRepository(): void { /* Arrange */ $submission = $this->createMock( Submission::class ); $submission->method( 'get_quiz_id' )->willReturn( 5 ); $submission->method( 'get_user_id' )->willReturn( 6 ); + $submission->method( 'get_final_grade' )->willReturn( 7.0 ); $comments_based_repository = $this->createMock( Comments_Based_Grade_Repository::class ); @@ -265,17 +288,19 @@ public function testSaveMany_UseTablesSetToTrue_UsesCommentsBasedRepository(): v $tables_based_submission_repository = $this->createMock( Tables_Based_Submission_Repository::class ); $tables_based_submission_repository - ->method( 'get' ) - ->with( 5, 6 ) + ->method( 'get_or_create' ) + ->with( 5, 6, 7.0 ) ->willReturn( $tables_based_submission ); - $tables_based_answer_repository = $this->createMock( Tables_Based_Answer_Repository::class ); + $tables_based_answer_repository = $this->createMock( Tables_Based_Answer_Repository::class ); + $comments_based_answer_repository = $this->createMock( Comments_Based_Answer_Repository::class ); $repository = new Aggregate_Grade_Repository( $comments_based_repository, $tables_based_repository, $tables_based_submission_repository, $tables_based_answer_repository, + $comments_based_answer_repository, true ); @@ -296,4 +321,69 @@ function ( array $grades ) { ); $repository->save_many( $submission, $grades ); } + + public function testSaveMany_UseTablesSetToTrueAndTablesBasedGradesNotFound_CreatesTablesBasedGrades(): void { + /* Arrange */ + $submission = $this->createMock( Submission::class ); + $submission->method( 'get_id' )->willReturn( 4 ); + $submission->method( 'get_quiz_id' )->willReturn( 5 ); + $submission->method( 'get_user_id' )->willReturn( 6 ); + $submission->method( 'get_final_grade' )->willReturn( 7.0 ); + + $comments_based_repository = $this->createMock( Comments_Based_Grade_Repository::class ); + + $existing_grade = new Grade( 1, 2, 3, 4, 'feedback', new DateTimeImmutable(), new DateTimeImmutable() ); + $tables_based_repository = $this->createMock( Tables_Based_Grade_Repository::class ); + $tables_based_repository + ->method( 'get_all' ) + ->with( 8 ) + ->willReturn( [] ); + + $grades = [ new Grade( 1, 2, 3, 4, 'feedback2', new DateTimeImmutable(), new DateTimeImmutable() ) ]; + + $tables_based_submission = $this->createMock( Submission::class ); + $tables_based_submission->method( 'get_id' )->willReturn( 8 ); + + $tables_based_submission_repository = $this->createMock( Tables_Based_Submission_Repository::class ); + $tables_based_submission_repository + ->method( 'get_or_create' ) + ->with( 5, 6, 7.0 ) + ->willReturn( $tables_based_submission ); + + $tables_based_answer = new Answer( 2, 8, 3, '4', new DateTime( '@5' ), new DateTime( '@6' ) ); + $tables_based_answer_repository = $this->createMock( Tables_Based_Answer_Repository::class ); + $tables_based_answer_repository + ->method( 'get_all' ) + ->with( 8 ) + ->willReturn( [ $tables_based_answer ] ); + + $comments_based_answer = new Answer( 2, 8, 3, '4', new DateTime( '@5' ), new DateTime( '@6' ) ); + $comments_based_answer_repository = $this->createMock( Comments_Based_Answer_Repository::class ); + $comments_based_answer_repository + ->method( 'get_all' ) + ->with( 4 ) + ->willReturn( [ $comments_based_answer ] ); + + $repository = new Aggregate_Grade_Repository( + $comments_based_repository, + $tables_based_repository, + $tables_based_submission_repository, + $tables_based_answer_repository, + $comments_based_answer_repository, + true + ); + + /* Expect & Act */ + $tables_based_repository + ->expects( $this->once() ) + ->method( 'create' ) + ->with( + $this->identicalTo( $tables_based_submission ), + 2, + 3, + 4, + 'feedback2' + ); + $repository->save_many( $submission, $grades ); + } } diff --git a/tests/unit-tests/internal/quiz-submission/grade/repositories/test-class-grade-repository-factory.php b/tests/unit-tests/internal/quiz-submission/grade/repositories/test-class-grade-repository-factory.php index d0d4e91a92..d108ef7ba7 100644 --- a/tests/unit-tests/internal/quiz-submission/grade/repositories/test-class-grade-repository-factory.php +++ b/tests/unit-tests/internal/quiz-submission/grade/repositories/test-class-grade-repository-factory.php @@ -22,5 +22,4 @@ public function testCreate_WhenCalled_ReturnsGradeRepository(): void { /* Assert. */ self::assertInstanceOf( Aggregate_Grade_Repository::class, $actual ); } - } diff --git a/tests/unit-tests/internal/quiz-submission/submission/repositories/test-class-aggregate-submission-repository.php b/tests/unit-tests/internal/quiz-submission/submission/repositories/test-class-aggregate-submission-repository.php index 1dbd9d3dde..2eaee77f20 100644 --- a/tests/unit-tests/internal/quiz-submission/submission/repositories/test-class-aggregate-submission-repository.php +++ b/tests/unit-tests/internal/quiz-submission/submission/repositories/test-class-aggregate-submission-repository.php @@ -74,43 +74,31 @@ public function testCreate_UseTablesOff_CallsCommentsBasedRepository(): void { $repository->create( 1, 2, 12.34 ); } - public function testGetOrCreate_WhenHasSubmission_DoesntCallCreate(): void { + public function testGetOrCreate_Always_CallsGetOrCreateOnCommentsBasedRepository(): void { /* Arrange. */ - $repository = $this->getMockBuilder( Aggregate_Submission_Repository::class ) - ->disableOriginalConstructor() - ->setMethods( [ 'get', 'create' ] ) - ->getMock(); + $comments_based = $this->createMock( Comments_Based_Submission_Repository::class ); + $tables_based = $this->createMock( Tables_Based_Submission_Repository::class ); + $repository = new Aggregate_Submission_Repository( $comments_based, $tables_based, true ); /* Expect & Act. */ - $repository + $comments_based ->expects( $this->once() ) - ->method( 'get' ) + ->method( 'get_or_create' ) + ->with( 1, 2, 12.34 ) ->willReturn( $this->createMock( Submission::class ) ); - - $repository - ->expects( $this->never() ) - ->method( 'create' ); - $repository->get_or_create( 1, 2, 12.34 ); } - public function testGetOrCreate_WhenHasNoSubmission_CallsCreate(): void { + public function testGetOrCreate_Never_CallsGetOrCreateOnTablesBasedRepository(): void { /* Arrange. */ $comments_based = $this->createMock( Comments_Based_Submission_Repository::class ); $tables_based = $this->createMock( Tables_Based_Submission_Repository::class ); $repository = new Aggregate_Submission_Repository( $comments_based, $tables_based, true ); /* Expect & Act. */ - $comments_based - ->expects( $this->once() ) - ->method( 'create' ) - ->with( 1, 2, 12.34 ); - $tables_based - ->expects( $this->once() ) - ->method( 'create' ) - ->with( 1, 2, 12.34 ); - + ->expects( $this->never() ) + ->method( 'get_or_create' ); $repository->get_or_create( 1, 2, 12.34 ); } @@ -203,22 +191,20 @@ function ( Submission $submission_to_save ) use ( $submission, $found_submission $repository->save( $submission ); } - public function testSave_UseTablesOnAndSubmissionNotFound_DoesntCallTablesBasedRepository(): void { + public function testSave_UseTablesOnAndSubmissionNotFound_CreatesTablesBasedSubmission(): void { /* Arrange. */ $submission = $this->create_submission(); $comments_based = $this->createMock( Comments_Based_Submission_Repository::class ); $tables_based = $this->createMock( Tables_Based_Submission_Repository::class ); - $tables_based - ->method( 'get' ) - ->with( 2, 3 ) - ->willReturn( null ); $repository = new Aggregate_Submission_Repository( $comments_based, $tables_based, true ); /* Expect & Act. */ $tables_based - ->expects( $this->never() ) - ->method( 'save' ); + ->expects( $this->once() ) + ->method( 'get_or_create' ) + ->with( 2, 3, 12.34 ) + ->willReturn( $this->create_submission() ); $repository->save( $submission ); } diff --git a/tests/unit-tests/internal/student-progress/course-progress/repositories/test-class-aggregate-course-progress-repository.php b/tests/unit-tests/internal/student-progress/course-progress/repositories/test-class-aggregate-course-progress-repository.php index 523e966d8e..3f694a495e 100644 --- a/tests/unit-tests/internal/student-progress/course-progress/repositories/test-class-aggregate-course-progress-repository.php +++ b/tests/unit-tests/internal/student-progress/course-progress/repositories/test-class-aggregate-course-progress-repository.php @@ -91,18 +91,12 @@ public function testHas_Always_CallsCommentsBasedRepository(): void { $repository->has( 1, 2 ); } - /** - * Test that the repository will always use comments based repository while saving. - * - * @param bool $use_tables - * @dataProvider providerSave_Always_CallsCommentsBasedRepository - */ - public function testSave_Always_CallsCommentsBasedRepository( bool $use_tables ): void { + public function testSave_WhenDidntUseTables_CallsCommentsBasedRepository(): void { /* Arrange. */ $progress = $this->create_course_progress(); $comments_based = $this->createMock( Comments_Based_Course_Progress_Repository::class ); $tables_based = $this->createMock( Tables_Based_Course_Progress_Repository::class ); - $repository = new Aggregate_Course_Progress_Repository( $comments_based, $tables_based, $use_tables ); + $repository = new Aggregate_Course_Progress_Repository( $comments_based, $tables_based, false ); /* Expect & Act. */ $comments_based @@ -112,11 +106,22 @@ public function testSave_Always_CallsCommentsBasedRepository( bool $use_tables ) $repository->save( $progress ); } - public function providerSave_Always_CallsCommentsBasedRepository(): array { - return [ - 'uses tables' => [ true ], - 'does not use tables' => [ false ], - ]; + public function testSave_WhenTablesUsed_CallsCommentsBasedRepository(): void { + /* Arrange. */ + $progress = $this->create_course_progress(); + $comments_based = $this->createMock( Comments_Based_Course_Progress_Repository::class ); + + $tables_based = $this->createMock( Tables_Based_Course_Progress_Repository::class ); + $tables_based->method( 'get' )->willReturn( $progress ); + + $repository = new Aggregate_Course_Progress_Repository( $comments_based, $tables_based, true ); + + /* Expect & Act. */ + $comments_based + ->expects( $this->once() ) + ->method( 'save' ) + ->with( $progress ); + $repository->save( $progress ); } public function testSave_UseTablesOnAndProgressFound_CallsTablesBasedRepository(): void { @@ -178,9 +183,10 @@ function ( Course_Progress $progress_to_save ) { $repository->save( $progress ); } - public function testSave_UseTablesOnAndProgressNotFound_DoesntCallTablesBasedRepository(): void { + public function testSave_UseTablesOnAndProgressNotFound_CreatesTablesBasedProgress(): void { /* Arrange. */ - $progress = $this->create_course_progress(); + $progress = $this->create_course_progress(); + $created_progress = $this->create_course_progress(); $comments_based = $this->createMock( Comments_Based_Course_Progress_Repository::class ); $tables_based = $this->createMock( Tables_Based_Course_Progress_Repository::class ); @@ -193,8 +199,10 @@ public function testSave_UseTablesOnAndProgressNotFound_DoesntCallTablesBasedRep /* Expect & Act. */ $tables_based - ->expects( $this->never() ) - ->method( 'save' ); + ->expects( $this->once() ) + ->method( 'create' ) + ->with( 2, 3 ) + ->willReturn( $created_progress ); $repository->save( $progress ); } diff --git a/tests/unit-tests/internal/student-progress/lesson-progress/repositories/test-class-aggregate-lesson-progress-repository.php b/tests/unit-tests/internal/student-progress/lesson-progress/repositories/test-class-aggregate-lesson-progress-repository.php index e66be6cfd8..0aff53aa50 100644 --- a/tests/unit-tests/internal/student-progress/lesson-progress/repositories/test-class-aggregate-lesson-progress-repository.php +++ b/tests/unit-tests/internal/student-progress/lesson-progress/repositories/test-class-aggregate-lesson-progress-repository.php @@ -96,18 +96,12 @@ public function testHas_Always_CallsCommentsBasedRepository(): void { $repository->has( 1, 2 ); } - /** - * Test that the repository will always use comments based repository while saving. - * - * @param bool $use_tables - * @dataProvider providerSave_Always_CallsCommentsBasedRepository - */ - public function testSave_Always_CallsCommentsBasedRepository( bool $use_tables ): void { + public function testSave_WhenDidntUseTables_CallsCommentsBasedRepository(): void { /* Arrange. */ $progress = $this->create_lesson_progress(); $comments_based = $this->createMock( Comments_Based_Lesson_Progress_Repository::class ); $tables_based = $this->createMock( Tables_Based_Lesson_Progress_Repository::class ); - $repository = new Aggregate_Lesson_Progress_Repository( $comments_based, $tables_based, $use_tables ); + $repository = new Aggregate_Lesson_Progress_Repository( $comments_based, $tables_based, false ); /* Expect & Act. */ $comments_based @@ -117,11 +111,24 @@ public function testSave_Always_CallsCommentsBasedRepository( bool $use_tables ) $repository->save( $progress ); } - public function providerSave_Always_CallsCommentsBasedRepository(): array { - return [ - 'uses tables' => [ true ], - 'does not use tables' => [ false ], - ]; + public function testSave_WhenUsedTables_CallsCommentsBasedRepository(): void { + /* Arrange. */ + $progress = $this->create_lesson_progress(); + $comments_based = $this->createMock( Comments_Based_Lesson_Progress_Repository::class ); + + $tables_based = $this->createMock( Tables_Based_Lesson_Progress_Repository::class ); + $tables_based + ->method( 'get' ) + ->willReturn( $progress ); + + $repository = new Aggregate_Lesson_Progress_Repository( $comments_based, $tables_based, true ); + + /* Expect & Act. */ + $comments_based + ->expects( $this->once() ) + ->method( 'save' ) + ->with( $progress ); + $repository->save( $progress ); } public function testSave_UseTablesOnAndProgressFound_CallsTablesBasedRepository(): void { @@ -191,9 +198,10 @@ function ( Lesson_Progress $progress_to_save ) { ); $repository->save( $progress ); } - public function testSave_UseTablesOnAndProgressNotFound_DoesntCallTablesBasedRepository(): void { + public function testSave_UseTablesOnAndProgressNotFound_CreatesTablesBasedProgress(): void { /* Arrange. */ - $progress = $this->create_lesson_progress(); + $progress = $this->create_lesson_progress(); + $created_progress = $this->create_lesson_progress(); $comments_based = $this->createMock( Comments_Based_Lesson_Progress_Repository::class ); $tables_based = $this->createMock( Tables_Based_Lesson_Progress_Repository::class ); @@ -206,8 +214,10 @@ public function testSave_UseTablesOnAndProgressNotFound_DoesntCallTablesBasedRep /* Expect & Act. */ $tables_based - ->expects( $this->never() ) - ->method( 'save' ); + ->expects( $this->once() ) + ->method( 'create' ) + ->with( 2, 3 ) + ->willReturn( $created_progress ); $repository->save( $progress ); } diff --git a/tests/unit-tests/internal/student-progress/quiz-progress/repositories/test-class-aggregate-quiz-progress-repository.php b/tests/unit-tests/internal/student-progress/quiz-progress/repositories/test-class-aggregate-quiz-progress-repository.php index d64b6733a3..d019293f1e 100644 --- a/tests/unit-tests/internal/student-progress/quiz-progress/repositories/test-class-aggregate-quiz-progress-repository.php +++ b/tests/unit-tests/internal/student-progress/quiz-progress/repositories/test-class-aggregate-quiz-progress-repository.php @@ -96,18 +96,12 @@ public function testHas_Always_CallsCommentsBasedRepository(): void { $repository->has( 1, 2 ); } - /** - * Test that the repository will always use comments based repository while saving. - * - * @param bool $use_tables - * @dataProvider providerSave_Always_CallsCommentsBasedRepository - */ - public function testSave_Always_CallsCommentsBasedRepository( bool $use_tables ): void { + public function testSave_WhenDidnUseTables_CallsCommentsBasedRepository(): void { /* Arrange. */ $progress = $this->create_quiz_progress(); $comments_based = $this->createMock( Comments_Based_Quiz_Progress_Repository::class ); $tables_based = $this->createMock( Tables_Based_Quiz_Progress_Repository::class ); - $repository = new Aggregate_Quiz_Progress_Repository( $comments_based, $tables_based, $use_tables ); + $repository = new Aggregate_Quiz_Progress_Repository( $comments_based, $tables_based, false ); /* Expect & Act. */ $comments_based @@ -117,11 +111,22 @@ public function testSave_Always_CallsCommentsBasedRepository( bool $use_tables ) $repository->save( $progress ); } - public function providerSave_Always_CallsCommentsBasedRepository(): array { - return [ - 'uses tables' => [ true ], - 'does not use tables' => [ false ], - ]; + public function testSave_WhenUsedTables_CallsCommentsBasedRepository(): void { + /* Arrange. */ + $progress = $this->create_quiz_progress(); + $comments_based = $this->createMock( Comments_Based_Quiz_Progress_Repository::class ); + + $tables_based = $this->createMock( Tables_Based_Quiz_Progress_Repository::class ); + $tables_based->method( 'get' )->willReturn( $progress ); + + $repository = new Aggregate_Quiz_Progress_Repository( $comments_based, $tables_based, true ); + + /* Expect & Act. */ + $comments_based + ->expects( $this->once() ) + ->method( 'save' ) + ->with( $progress ); + $repository->save( $progress ); } public function testSave_UseTablesOnAndProgressFound_CallsTablesBasedRepository(): void { @@ -192,9 +197,10 @@ function ( Quiz_Progress $progress_to_save ) { $repository->save( $progress ); } - public function testSave_UseTablesOnAndProgressNotFound_DoesntCallTablesBasedRepository(): void { + public function testSave_UseTablesOnAndProgressNotFound_CaertesQuizProgress(): void { /* Arrange. */ - $progress = $this->create_quiz_progress(); + $progress = $this->create_quiz_progress(); + $created_progress = $this->create_quiz_progress(); $comments_based = $this->createMock( Comments_Based_Quiz_Progress_Repository::class ); $tables_based = $this->createMock( Tables_Based_Quiz_Progress_Repository::class ); @@ -207,8 +213,10 @@ public function testSave_UseTablesOnAndProgressNotFound_DoesntCallTablesBasedRep /* Expect & Act. */ $tables_based - ->expects( $this->never() ) - ->method( 'save' ); + ->expects( $this->once() ) + ->method( 'create' ) + ->with( 2, 3 ) + ->willReturn( $created_progress ); $repository->save( $progress ); } diff --git a/tests/unit-tests/test-class-quiz.php b/tests/unit-tests/test-class-quiz.php index efe8fd3b93..cbb118a14d 100644 --- a/tests/unit-tests/test-class-quiz.php +++ b/tests/unit-tests/test-class-quiz.php @@ -1804,7 +1804,9 @@ public function testResetUserLessonData_WhenCourseAlreadyStarted_DoesNotResetThe /* Act. */ Sensei()->course_progress_repository = $course_progress_repository_mock; + ob_start(); Sensei()->quiz->reset_user_lesson_data( $lesson_id, $user_id ); + ob_end_clean(); Sensei()->course_progress_repository = $_course_progress_repository; // Reset. }