diff --git a/includes/class-sensei-quiz.php b/includes/class-sensei-quiz.php index 6294ce74e2..956830da92 100755 --- a/includes/class-sensei-quiz.php +++ b/includes/class-sensei-quiz.php @@ -1080,7 +1080,7 @@ public function set_user_grades( $quiz_grades, $lesson_id, $user_id = 0 ) { foreach ( $quiz_grades as $question_id => $points ) { $answer = $answers_map[ $question_id ]; - Sensei()->quiz_grade_repository->create( $submission, $answer->get_id(), $question_id, $points ); + Sensei()->quiz_grade_repository->create( $submission, $answer, $question_id, $points ); } $transient_key = 'quiz_grades_' . $user_id . '_' . $lesson_id; diff --git a/includes/internal/quiz-submission/answer/models/class-answer.php b/includes/internal/quiz-submission/answer/models/class-answer-abstract.php similarity index 94% rename from includes/internal/quiz-submission/answer/models/class-answer.php rename to includes/internal/quiz-submission/answer/models/class-answer-abstract.php index a137af6fa3..0fc52c0d01 100644 --- a/includes/internal/quiz-submission/answer/models/class-answer.php +++ b/includes/internal/quiz-submission/answer/models/class-answer-abstract.php @@ -1,6 +1,6 @@ comments_based_repository->create( $submission, $question_id, $value ); if ( $this->use_tables ) { @@ -103,7 +103,7 @@ public function create( Submission_Interface $submission, int $question_id, stri * * @param int $submission_id The submission ID. * - * @return Answer[] An array of answers. + * @return Answer_Interface[] An array of answers. */ public function get_all( int $submission_id ): array { return $this->comments_based_repository->get_all( $submission_id ); diff --git a/includes/internal/quiz-submission/answer/repositories/class-answer-repository-interface.php b/includes/internal/quiz-submission/answer/repositories/class-answer-repository-interface.php index e374e65626..4eb8d67b93 100644 --- a/includes/internal/quiz-submission/answer/repositories/class-answer-repository-interface.php +++ b/includes/internal/quiz-submission/answer/repositories/class-answer-repository-interface.php @@ -7,7 +7,7 @@ namespace Sensei\Internal\Quiz_Submission\Answer\Repositories; -use Sensei\Internal\Quiz_Submission\Answer\Models\Answer; +use Sensei\Internal\Quiz_Submission\Answer\Models\Answer_Interface; use Sensei\Internal\Quiz_Submission\Submission\Models\Submission_Interface; if ( ! defined( 'ABSPATH' ) ) { @@ -31,9 +31,9 @@ interface Answer_Repository_Interface { * @param int $question_id The question ID. * @param string $value The answer value. * - * @return Answer The answer model. + * @return Answer_Interface The answer model. */ - public function create( Submission_Interface $submission, int $question_id, string $value ): Answer; + public function create( Submission_Interface $submission, int $question_id, string $value ): Answer_Interface; /** * Get all answers for a quiz submission. @@ -42,7 +42,7 @@ public function create( Submission_Interface $submission, int $question_id, stri * * @param int $submission_id The submission ID. * - * @return Answer[] An array of answers. + * @return Answer_Interface[] An array of answers. */ public function get_all( int $submission_id ): array; diff --git a/includes/internal/quiz-submission/answer/repositories/class-comments-based-answer-repository.php b/includes/internal/quiz-submission/answer/repositories/class-comments-based-answer-repository.php index 42d4334f01..29c11e5b18 100644 --- a/includes/internal/quiz-submission/answer/repositories/class-comments-based-answer-repository.php +++ b/includes/internal/quiz-submission/answer/repositories/class-comments-based-answer-repository.php @@ -7,7 +7,8 @@ namespace Sensei\Internal\Quiz_Submission\Answer\Repositories; -use Sensei\Internal\Quiz_Submission\Answer\Models\Answer; +use Sensei\Internal\Quiz_Submission\Answer\Models\Answer_Interface; +use Sensei\Internal\Quiz_Submission\Answer\Models\Comments_Based_Answer; use Sensei\Internal\Quiz_Submission\Submission\Models\Submission_Interface; if ( ! defined( 'ABSPATH' ) ) { @@ -31,9 +32,9 @@ class Comments_Based_Answer_Repository implements Answer_Repository_Interface { * @param int $question_id The question ID. * @param string $value The answer value. * - * @return Answer The answer model. + * @return Answer_Interface The answer model. */ - public function create( Submission_Interface $submission, int $question_id, string $value ): Answer { + public function create( Submission_Interface $submission, int $question_id, string $value ): Answer_Interface { $submission_id = $submission->get_id(); $answers_map = $this->get_answers_map( $submission_id ); $answers_map[ $question_id ] = $value; @@ -44,7 +45,7 @@ public function create( Submission_Interface $submission, int $question_id, stri $created_at = current_datetime(); - return new Answer( 0, $submission_id, $question_id, $value, $created_at, $created_at ); + return new Comments_Based_Answer( $submission_id, $question_id, $value, $created_at, $created_at ); } /** @@ -54,14 +55,14 @@ public function create( Submission_Interface $submission, int $question_id, stri * * @param int $submission_id The submission ID. * - * @return Answer[] An array of answers. + * @return Answer_Interface[] An array of answers. */ public function get_all( int $submission_id ): array { $answers = []; $created_at = current_datetime(); foreach ( $this->get_answers_map( $submission_id ) as $question_id => $value ) { - $answers[] = new Answer( 0, $submission_id, $question_id, $value, $created_at, $created_at ); + $answers[] = new Comments_Based_Answer( $submission_id, $question_id, $value, $created_at, $created_at ); } return $answers; diff --git a/includes/internal/quiz-submission/answer/repositories/class-tables-based-answer-repository.php b/includes/internal/quiz-submission/answer/repositories/class-tables-based-answer-repository.php index 89c8150420..f69c041c67 100644 --- a/includes/internal/quiz-submission/answer/repositories/class-tables-based-answer-repository.php +++ b/includes/internal/quiz-submission/answer/repositories/class-tables-based-answer-repository.php @@ -9,7 +9,8 @@ use DateTimeImmutable; use DateTimeZone; -use Sensei\Internal\Quiz_Submission\Answer\Models\Answer; +use Sensei\Internal\Quiz_Submission\Answer\Models\Answer_Interface; +use Sensei\Internal\Quiz_Submission\Answer\Models\Tables_Based_Answer; use Sensei\Internal\Quiz_Submission\Submission\Models\Submission_Interface; use wpdb; @@ -52,9 +53,9 @@ public function __construct( wpdb $wpdb ) { * @param int $question_id The question ID. * @param string $value The answer value. * - * @return Answer The answer model. + * @return Answer_Interface The answer model. */ - public function create( Submission_Interface $submission, int $question_id, string $value ): Answer { + public function create( Submission_Interface $submission, int $question_id, string $value ): Answer_Interface { $current_datetime = new DateTimeImmutable( 'now', new DateTimeZone( 'UTC' ) ); $date_format = 'Y-m-d H:i:s'; @@ -76,7 +77,7 @@ public function create( Submission_Interface $submission, int $question_id, stri ] ); - return new Answer( + return new Tables_Based_Answer( $this->wpdb->insert_id, $submission->get_id(), $question_id, @@ -93,7 +94,7 @@ public function create( Submission_Interface $submission, int $question_id, stri * * @param int $submission_id The submission ID. * - * @return Answer[] An array of answers. + * @return Answer_Interface[] An array of answers. */ public function get_all( int $submission_id ): array { $query = $this->wpdb->prepare( @@ -105,7 +106,7 @@ public function get_all( int $submission_id ): array { $answers = []; // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Prepared earlier. foreach ( $this->wpdb->get_results( $query ) as $result ) { - $answers[] = new Answer( + $answers[] = new Tables_Based_Answer( $result->id, $result->submission_id, $result->question_id, 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 bd3dc9d1d5..7dbd79b15b 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 @@ -8,7 +8,8 @@ namespace Sensei\Internal\Quiz_Submission\Grade\Repositories; use DateTimeImmutable; -use Sensei\Internal\Quiz_Submission\Answer\Models\Answer; +use Sensei\Internal\Quiz_Submission\Answer\Models\Answer_Interface; +use Sensei\Internal\Quiz_Submission\Answer\Models\Tables_Based_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; @@ -105,24 +106,23 @@ public function __construct( * @internal * * @param Submission_Interface $submission The submission ID. - * @param int $answer_id The answer ID. + * @param Answer_Interface $answer The answer. * @param int $question_id The question ID. * @param int $points The points. * @param string|null $feedback The feedback. * * @return Grade The grade. */ - public function create( Submission_Interface $submission, int $answer_id, int $question_id, int $points, ?string $feedback = null ): Grade { - $grade = $this->comments_based_repository->create( $submission, $answer_id, $question_id, $points, $feedback ); + public function create( Submission_Interface $submission, Answer_Interface $answer, int $question_id, int $points, ?string $feedback = null ): Grade { + $grade = $this->comments_based_repository->create( $submission, $answer, $question_id, $points, $feedback ); if ( $this->use_tables ) { $tables_based_submission = $this->get_or_create_tables_based_submission( $submission ); + $tables_based_answers = $this->get_or_create_tables_based_answers( $submission, $tables_based_submission ); + $tables_based_answer = $tables_based_answers[ $question_id ] ?? null; - $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 ); + if ( $tables_based_answer ) { + $this->tables_based_repository->create( $tables_based_submission, $tables_based_answer, $question_id, $points, $feedback ); } } @@ -134,7 +134,7 @@ public function create( Submission_Interface $submission, int $answer_id, int $q * * @param Submission_Interface $comments_based_submission The comments based submission. * @param Submission_Interface $tables_based_submission The tables based submission. - * @return Answer[] The answers. + * @return Answer_Interface[] The answers. */ public function get_or_create_tables_based_answers( Submission_Interface $comments_based_submission, Submission_Interface $tables_based_submission ): array { $comments_based_answers = $this->comments_based_answer_repository->get_all( $comments_based_submission->get_id() ); @@ -143,7 +143,7 @@ public function get_or_create_tables_based_answers( Submission_Interface $commen foreach ( $comments_based_answers as $comments_based_answer ) { $filtered = array_filter( $tables_based_answers, - function( Answer $answer ) use ( $comments_based_answer ) { + function( Answer_Interface $answer ) use ( $comments_based_answer ) { return $answer->get_question_id() === $comments_based_answer->get_question_id(); } ); @@ -256,7 +256,7 @@ function( Grade $grade ) use ( $comments_based_grade ) { $result[ $comments_based_grade->get_question_id() ] = $this->tables_based_repository->create( $tables_based_submission, - $answer->get_id(), + $answer, $comments_based_grade->get_question_id(), $comments_based_grade->get_points(), $comments_based_grade->get_feedback() diff --git a/includes/internal/quiz-submission/grade/repositories/class-comments-based-grade-repository.php b/includes/internal/quiz-submission/grade/repositories/class-comments-based-grade-repository.php index 142d25d84b..13265e3482 100644 --- a/includes/internal/quiz-submission/grade/repositories/class-comments-based-grade-repository.php +++ b/includes/internal/quiz-submission/grade/repositories/class-comments-based-grade-repository.php @@ -7,6 +7,7 @@ namespace Sensei\Internal\Quiz_Submission\Grade\Repositories; +use Sensei\Internal\Quiz_Submission\Answer\Models\Answer_Interface; use Sensei\Internal\Quiz_Submission\Grade\Models\Grade; use Sensei\Internal\Quiz_Submission\Submission\Models\Submission_Interface; @@ -27,15 +28,15 @@ class Comments_Based_Grade_Repository implements Grade_Repository_Interface { * * @internal * - * @param Submission_Interface $submission The submission ID. - * @param int $answer_id The answer ID. - * @param int $question_id The question ID. - * @param int $points The points. - * @param string|null $feedback The feedback. + * @param Submission_Interface $submission The submission ID. + * @param Answer_Interface $answer The answer. + * @param int $question_id The question ID. + * @param int $points The points. + * @param string|null $feedback The feedback. * * @return Grade The grade. */ - public function create( Submission_Interface $submission, int $answer_id, int $question_id, int $points, string $feedback = null ): Grade { + public function create( Submission_Interface $submission, Answer_Interface $answer, int $question_id, int $points, string $feedback = null ): Grade { $submission_id = $submission->get_id(); $grades_map = get_comment_meta( $submission_id, 'quiz_grades', true ); $grades_map = is_array( $grades_map ) ? $grades_map : []; diff --git a/includes/internal/quiz-submission/grade/repositories/class-grade-repository-interface.php b/includes/internal/quiz-submission/grade/repositories/class-grade-repository-interface.php index 6420e9832b..4a2e0b1171 100644 --- a/includes/internal/quiz-submission/grade/repositories/class-grade-repository-interface.php +++ b/includes/internal/quiz-submission/grade/repositories/class-grade-repository-interface.php @@ -7,6 +7,7 @@ namespace Sensei\Internal\Quiz_Submission\Grade\Repositories; +use Sensei\Internal\Quiz_Submission\Answer\Models\Answer_Interface; use Sensei\Internal\Quiz_Submission\Grade\Models\Grade; use Sensei\Internal\Quiz_Submission\Submission\Models\Submission_Interface; @@ -27,15 +28,15 @@ interface Grade_Repository_Interface { * * @internal * - * @param Submission_Interface $submission The submission. - * @param int $answer_id The answer ID. - * @param int $question_id The question ID. - * @param int $points The points. - * @param string|null $feedback The feedback. + * @param Submission_Interface $submission The submission. + * @param Answer_Interface $answer The answer. + * @param int $question_id The question ID. + * @param int $points The points. + * @param string|null $feedback The feedback. * * @return Grade The grade. */ - public function create( Submission_Interface $submission, int $answer_id, int $question_id, int $points, string $feedback = null ): Grade; + public function create( Submission_Interface $submission, Answer_Interface $answer, int $question_id, int $points, string $feedback = null ): Grade; /** * Get all grades for a quiz submission. diff --git a/includes/internal/quiz-submission/grade/repositories/class-tables-based-grade-repository.php b/includes/internal/quiz-submission/grade/repositories/class-tables-based-grade-repository.php index 8f13504542..eff9b398e1 100644 --- a/includes/internal/quiz-submission/grade/repositories/class-tables-based-grade-repository.php +++ b/includes/internal/quiz-submission/grade/repositories/class-tables-based-grade-repository.php @@ -11,6 +11,7 @@ exit; } +use Sensei\Internal\Quiz_Submission\Answer\Models\Answer_Interface; use Sensei\Internal\Quiz_Submission\Grade\Models\Grade; use Sensei\Internal\Quiz_Submission\Submission\Models\Submission_Interface; use wpdb; @@ -47,21 +48,21 @@ public function __construct( wpdb $wpdb ) { * @internal * * @param Submission_Interface $submission The submission. - * @param int $answer_id The answer ID. + * @param Answer_Interface $answer The answer. * @param int $question_id The question ID. * @param int $points The points. * @param string|null $feedback The feedback. * * @return Grade The grade. */ - public function create( Submission_Interface $submission, int $answer_id, int $question_id, int $points, ?string $feedback = null ): Grade { + public function create( Submission_Interface $submission, Answer_Interface $answer, int $question_id, int $points, ?string $feedback = null ): Grade { $current_date = new \DateTimeImmutable( 'now', new \DateTimeZone( 'UTC' ) ); $date_format = 'Y-m-d H:i:s'; $this->wpdb->insert( $this->get_table_name(), [ - 'answer_id' => $answer_id, + 'answer_id' => $answer->get_id(), 'question_id' => $question_id, 'points' => $points, 'feedback' => $feedback, @@ -80,7 +81,7 @@ public function create( Submission_Interface $submission, int $answer_id, int $q return new Grade( $this->wpdb->insert_id, - $answer_id, + $answer->get_id(), $question_id, $points, $feedback, diff --git a/tests/unit-tests/internal/quiz-submission/answer/models/test-class-comments-based-answer.php b/tests/unit-tests/internal/quiz-submission/answer/models/test-class-comments-based-answer.php new file mode 100644 index 0000000000..de419c775d --- /dev/null +++ b/tests/unit-tests/internal/quiz-submission/answer/models/test-class-comments-based-answer.php @@ -0,0 +1,96 @@ +create_answer(); + + /* Assert. */ + self::expectException( \BadMethodCallException::class ); + + /* Act. */ + $answer->get_id(); + } + + public function testGetSubmissionId_ConstructedWithSubmissionId_ReturnsSameSubmissionId(): void { + /* Arrange. */ + $answer = $this->create_answer(); + + /* Act. */ + $actual = $answer->get_submission_id(); + + /* Assert. */ + self::assertSame( 2, $actual ); + } + + public function testGetQuestionId_ConstructedWithQuestionId_ReturnsSameQuestionId(): void { + /* Arrange. */ + $answer = $this->create_answer(); + + /* Act. */ + $actual = $answer->get_question_id(); + + /* Assert. */ + self::assertSame( 3, $actual ); + } + + public function testGetValue_ConstructedWithValue_ReturnsSameValue(): void { + /* Arrange. */ + $answer = $this->create_answer(); + + /* Act. */ + $actual = $answer->get_value(); + + /* Assert. */ + self::assertSame( 'yes', $actual ); + } + + public function testGetCreatedAt_ConstructedWithCreatedAt_ReturnsSameCreatedAt(): void { + /* Arrange. */ + $answer = $this->create_answer(); + + /* Act. */ + $actual = $answer->get_created_at()->format( 'Y-m-d H:i:s' ); + + /* Assert. */ + self::assertSame( '2020-01-01 00:00:01', $actual ); + } + + public function testGetUpdatedAt_ConstructedWithUpdatedAt_ReturnsSameUpdatedAt(): void { + /* Arrange. */ + $answer = $this->create_answer(); + + /* Act. */ + $actual = $answer->get_updated_at()->format( 'Y-m-d H:i:s' ); + + /* Assert. */ + self::assertSame( '2020-01-01 00:00:02', $actual ); + } + + private function create_answer(): Comments_Based_Answer { + return new Comments_Based_Answer( + 2, + 3, + 'yes', + new \DateTime( '2020-01-01 00:00:01' ), + new \DateTime( '2020-01-01 00:00:02' ) + ); + } +} diff --git a/tests/unit-tests/internal/quiz-submission/answer/models/test-class-answer.php b/tests/unit-tests/internal/quiz-submission/answer/models/test-class-tables-based-answer.php similarity index 72% rename from tests/unit-tests/internal/quiz-submission/answer/models/test-class-answer.php rename to tests/unit-tests/internal/quiz-submission/answer/models/test-class-tables-based-answer.php index 125adef7f4..0b2f1d2a5a 100644 --- a/tests/unit-tests/internal/quiz-submission/answer/models/test-class-answer.php +++ b/tests/unit-tests/internal/quiz-submission/answer/models/test-class-tables-based-answer.php @@ -1,26 +1,26 @@ createAnswer(); + $answer = $this->create_answer(); /* Act. */ $actual = $answer->get_id(); @@ -31,7 +31,7 @@ public function testGetId_ConstructedWithId_ReturnsSameId(): void { public function testGetSubmissionId_ConstructedWithSubmissionId_ReturnsSameSubmissionId(): void { /* Arrange. */ - $answer = $this->createAnswer(); + $answer = $this->create_answer(); /* Act. */ $actual = $answer->get_submission_id(); @@ -42,7 +42,7 @@ public function testGetSubmissionId_ConstructedWithSubmissionId_ReturnsSameSubmi public function testGetQuestionId_ConstructedWithQuestionId_ReturnsSameQuestionId(): void { /* Arrange. */ - $answer = $this->createAnswer(); + $answer = $this->create_answer(); /* Act. */ $actual = $answer->get_question_id(); @@ -53,7 +53,7 @@ public function testGetQuestionId_ConstructedWithQuestionId_ReturnsSameQuestionI public function testGetValue_ConstructedWithValue_ReturnsSameValue(): void { /* Arrange. */ - $answer = $this->createAnswer(); + $answer = $this->create_answer(); /* Act. */ $actual = $answer->get_value(); @@ -64,7 +64,7 @@ public function testGetValue_ConstructedWithValue_ReturnsSameValue(): void { public function testGetCreatedAt_ConstructedWithCreatedAt_ReturnsSameCreatedAt(): void { /* Arrange. */ - $answer = $this->createAnswer(); + $answer = $this->create_answer(); /* Act. */ $actual = $answer->get_created_at()->format( 'Y-m-d H:i:s' ); @@ -75,7 +75,7 @@ public function testGetCreatedAt_ConstructedWithCreatedAt_ReturnsSameCreatedAt() public function testGetUpdatedAt_ConstructedWithUpdatedAt_ReturnsSameUpdatedAt(): void { /* Arrange. */ - $answer = $this->createAnswer(); + $answer = $this->create_answer(); /* Act. */ $actual = $answer->get_updated_at()->format( 'Y-m-d H:i:s' ); @@ -84,8 +84,8 @@ public function testGetUpdatedAt_ConstructedWithUpdatedAt_ReturnsSameUpdatedAt() self::assertSame( '2020-01-01 00:00:02', $actual ); } - private function createAnswer(): Answer { - return new Answer( + private function create_answer(): Tables_Based_Answer { + return new Tables_Based_Answer( 1, 2, 3, diff --git a/tests/unit-tests/internal/quiz-submission/answer/repositories/test-class-comments-based-answer-repository.php b/tests/unit-tests/internal/quiz-submission/answer/repositories/test-class-comments-based-answer-repository.php index fb2e93e9cf..bbd2f4ffad 100644 --- a/tests/unit-tests/internal/quiz-submission/answer/repositories/test-class-comments-based-answer-repository.php +++ b/tests/unit-tests/internal/quiz-submission/answer/repositories/test-class-comments-based-answer-repository.php @@ -2,7 +2,7 @@ namespace SenseiTest\Internal\Quiz_Submission\Answer\Repositories; -use Sensei\Internal\Quiz_Submission\Answer\Models\Answer; +use Sensei\Internal\Quiz_Submission\Answer\Models\Comments_Based_Answer; use Sensei\Internal\Quiz_Submission\Answer\Repositories\Comments_Based_Answer_Repository; use Sensei\Internal\Quiz_Submission\Submission\Models\Comments_Based_Submission; use Sensei_Utils; @@ -141,7 +141,7 @@ public function testDeleteAll_WhenCalled_DeletesAllGrades(): void { ); } - private function export_answer( Answer $answer ): array { + private function export_answer( Comments_Based_Answer $answer ): array { return [ 'question_id' => $answer->get_question_id(), 'value' => $answer->get_value(), diff --git a/tests/unit-tests/internal/quiz-submission/answer/repositories/test-class-tables-based-answer-repository.php b/tests/unit-tests/internal/quiz-submission/answer/repositories/test-class-tables-based-answer-repository.php index 4bc5b01a41..92bc577e80 100644 --- a/tests/unit-tests/internal/quiz-submission/answer/repositories/test-class-tables-based-answer-repository.php +++ b/tests/unit-tests/internal/quiz-submission/answer/repositories/test-class-tables-based-answer-repository.php @@ -3,7 +3,7 @@ namespace SenseiTest\Internal\Quiz_Submission\Answer\Repositories; use DateTimeImmutable; -use Sensei\Internal\Quiz_Submission\Answer\Models\Answer; +use Sensei\Internal\Quiz_Submission\Answer\Models\Tables_Based_Answer; use Sensei\Internal\Quiz_Submission\Answer\Repositories\Tables_Based_Answer_Repository; use Sensei\Internal\Quiz_Submission\Submission\Models\Submission_Interface; use wpdb; @@ -214,7 +214,7 @@ public function testDeleteAll_WhenCalled_DeletesAllFromTheDatabase(): void { $repository->delete_all( $submission ); } - private function export_answer( Answer $answer ): array { + private function export_answer( Tables_Based_Answer $answer ): array { return [ 'id' => $answer->get_id(), 'submission_id' => $answer->get_submission_id(), @@ -223,7 +223,7 @@ private function export_answer( Answer $answer ): array { ]; } - private function export_answer_with_dates( Answer $answer ): array { + private function export_answer_with_dates( Tables_Based_Answer $answer ): array { return array_merge( $this->export_answer( $answer ), [ 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 8a4bfc6295..5c58a1f197 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 @@ -4,7 +4,8 @@ use DateTime; use DateTimeImmutable; -use Sensei\Internal\Quiz_Submission\Answer\Models\Answer; +use Sensei\Internal\Quiz_Submission\Answer\Models\Comments_Based_Answer; +use Sensei\Internal\Quiz_Submission\Answer\Models\Tables_Based_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; @@ -23,6 +24,7 @@ class Aggregate_Grade_Repository_Test extends \WP_UnitTestCase { public function testCreate_Always_UsesCommentsBasedRepository(): void { /* Arrange */ + $answer = $this->createMock( Comments_Based_Answer::class ); $submission = $this->createMock( Comments_Based_Submission::class ); $comments_based_repository = $this->createMock( Comments_Based_Grade_Repository::class ); $tables_based_repository = $this->createMock( Tables_Based_Grade_Repository::class ); @@ -43,12 +45,13 @@ public function testCreate_Always_UsesCommentsBasedRepository(): void { $comments_based_repository ->expects( $this->once() ) ->method( 'create' ) - ->with( $this->identicalTo( $submission ), 2, 3, 4, 'feedback' ); - $repository->create( $submission, 2, 3, 4, 'feedback' ); + ->with( $this->identicalTo( $submission ), $this->identicalTo( $answer ), 3, 4, 'feedback' ); + $repository->create( $submission, $answer, 3, 4, 'feedback' ); } public function testCreate_UseTablesSetToFalse_DoesntUseCommentsBasedRepository(): void { /* Arrange */ + $answer = $this->createMock( Tables_Based_Answer::class ); $submission = $this->createMock( Comments_Based_Submission::class ); $comments_based_repository = $this->createMock( Comments_Based_Grade_Repository::class ); $tables_based_repository = $this->createMock( Tables_Based_Grade_Repository::class ); @@ -69,7 +72,7 @@ public function testCreate_UseTablesSetToFalse_DoesntUseCommentsBasedRepository( $tables_based_repository ->expects( $this->never() ) ->method( 'create' ); - $repository->create( $submission, 2, 3, 4, 'feedback' ); + $repository->create( $submission, $answer, 3, 4, 'feedback' ); } public function testGetAll_Always_UsesCommentsBasedRepository(): void { @@ -351,14 +354,14 @@ public function testSaveMany_UseTablesSetToTrueAndTablesBasedGradesNotFound_Crea ->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 = new Tables_Based_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 = new Comments_Based_Answer( 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' ) @@ -380,7 +383,7 @@ public function testSaveMany_UseTablesSetToTrueAndTablesBasedGradesNotFound_Crea ->method( 'create' ) ->with( $this->identicalTo( $tables_based_submission ), - 2, + $this->identicalTo( $tables_based_answer ), 3, 4, 'feedback2' diff --git a/tests/unit-tests/internal/quiz-submission/grade/repositories/test-class-comments-based-grade-repository.php b/tests/unit-tests/internal/quiz-submission/grade/repositories/test-class-comments-based-grade-repository.php index b99e5dc218..a6bb5afc5d 100644 --- a/tests/unit-tests/internal/quiz-submission/grade/repositories/test-class-comments-based-grade-repository.php +++ b/tests/unit-tests/internal/quiz-submission/grade/repositories/test-class-comments-based-grade-repository.php @@ -2,6 +2,7 @@ namespace SenseiTest\Internal\Quiz_Submission\Grade\Repositories; +use Sensei\Internal\Quiz_Submission\Answer\Models\Answer_Interface; use Sensei\Internal\Quiz_Submission\Grade\Models\Grade; use Sensei\Internal\Quiz_Submission\Grade\Repositories\Comments_Based_Grade_Repository; use Sensei\Internal\Quiz_Submission\Submission\Models\Comments_Based_Submission; @@ -32,9 +33,10 @@ public function testCreate_WhenCalled_ReturnsGrade(): void { $user_id = $this->factory->user->create(); $grade_repository = new Comments_Based_Grade_Repository(); $submission = $this->create_submission( $lesson_id, $user_id ); + $answer = $this->createMock( Answer_Interface::class ); /* Act. */ - $grade = $grade_repository->create( $submission, 0, 1, 22, 'Great!' ); + $grade = $grade_repository->create( $submission, $answer, 1, 22, 'Great!' ); /* Assert. */ $expected = [ @@ -53,10 +55,11 @@ public function testCreate_WhenCalled_SavesTheGrade(): void { $user_id = $this->factory->user->create(); $grade_repository = new Comments_Based_Grade_Repository(); $submission = $this->create_submission( $lesson_id, $user_id ); + $answer = $this->createMock( Answer_Interface::class ); /* Act. */ - $grade_repository->create( $submission, 0, 1, 22, 'Great!' ); - $grade_repository->create( $submission, 0, 2, 33, 'Awesome!' ); + $grade_repository->create( $submission, $answer, 1, 22, 'Great!' ); + $grade_repository->create( $submission, $answer, 2, 33, 'Awesome!' ); /* Assert. */ $this->assertSame( @@ -74,10 +77,11 @@ public function testCreate_WhenCalled_SavesTheGradeFeedback(): void { $user_id = $this->factory->user->create(); $grade_repository = new Comments_Based_Grade_Repository(); $submission = $this->create_submission( $lesson_id, $user_id ); + $answer = $this->createMock( Answer_Interface::class ); /* Act. */ - $grade_repository->create( $submission, 0, 1, 22, 'Great!' ); - $grade_repository->create( $submission, 0, 2, 33, 'Awesome!' ); + $grade_repository->create( $submission, $answer, 1, 22, 'Great!' ); + $grade_repository->create( $submission, $answer, 2, 33, 'Awesome!' ); /* Assert. */ $this->assertSame( @@ -108,10 +112,11 @@ public function testGetAll_WhenHasGrades_ReturnsAllGrades(): void { $lesson_id = $this->factory->lesson->create(); $user_id = $this->factory->user->create(); $submission = $this->create_submission( $lesson_id, $user_id ); + $answer = $this->createMock( Answer_Interface::class ); $grade_repository = new Comments_Based_Grade_Repository(); - $grade_1 = $grade_repository->create( $submission, 0, 1, 22, 'Great!' ); - $grade_2 = $grade_repository->create( $submission, 0, 2, 33, 'Awesome!' ); + $grade_1 = $grade_repository->create( $submission, $answer, 1, 22, 'Great!' ); + $grade_2 = $grade_repository->create( $submission, $answer, 2, 33, 'Awesome!' ); /* Act. */ $grades = $grade_repository->get_all( $submission->get_id() ); @@ -134,10 +139,11 @@ public function testSaveMany_WhenCalled_SavesAllGrades(): void { $lesson_id = $this->factory->lesson->create(); $user_id = $this->factory->user->create(); $submission = $this->create_submission( $lesson_id, $user_id ); + $answer = $this->createMock( Answer_Interface::class ); $grade_repository = new Comments_Based_Grade_Repository(); - $grade_1 = $grade_repository->create( $submission, 0, 1, 22, 'Great!' ); - $grade_2 = $grade_repository->create( $submission, 0, 2, 33, 'Awesome!' ); + $grade_1 = $grade_repository->create( $submission, $answer, 1, 22, 'Great!' ); + $grade_2 = $grade_repository->create( $submission, $answer, 2, 33, 'Awesome!' ); $grade_1->set_feedback( 'Amazing!' ); $grade_2->set_feedback( 'Wow!' ); @@ -164,8 +170,9 @@ public function testDeleteAll_WhenCalled_DeletesAllGrades(): void { $user_id = $this->factory->user->create(); $grade_repository = new Comments_Based_Grade_Repository(); $submission = $this->create_submission( $lesson_id, $user_id ); + $answer = $this->createMock( Answer_Interface::class ); - $grade_repository->create( $submission, 0, 1, 22, 'Great!' ); + $grade_repository->create( $submission, $answer, 1, 22, 'Great!' ); /* Act. */ $grade_repository->delete_all( $submission ); diff --git a/tests/unit-tests/internal/quiz-submission/grade/repositories/test-class-tables-based-grade-repository.php b/tests/unit-tests/internal/quiz-submission/grade/repositories/test-class-tables-based-grade-repository.php index 07eaee9057..3030006445 100644 --- a/tests/unit-tests/internal/quiz-submission/grade/repositories/test-class-tables-based-grade-repository.php +++ b/tests/unit-tests/internal/quiz-submission/grade/repositories/test-class-tables-based-grade-repository.php @@ -2,6 +2,7 @@ namespace SenseiTest\Internal\Quiz_Submission\Grade\Repositories; +use Sensei\Internal\Quiz_Submission\Answer\Models\Tables_Based_Answer; use Sensei\Internal\Quiz_Submission\Grade\Repositories\Tables_Based_Grade_Repository; use Sensei\Internal\Quiz_Submission\Grade\Models\Grade; use Sensei\Internal\Quiz_Submission\Submission\Models\Tables_Based_Submission; @@ -28,11 +29,12 @@ public function tearDown(): void { public function testCreate_ParamsGiven_ReturnsGrade(): void { /* Arrange */ $submission = $this->createMock( Tables_Based_Submission::class ); + $answer = new Tables_Based_Answer( 2, 3, 4, 'value', new \DateTimeImmutable(), new \DateTimeImmutable() ); $wpdb = $this->createMock( \wpdb::class ); $repository = new Tables_Based_Grade_Repository( $wpdb ); /* Act */ - $grade = $repository->create( $submission, 2, 3, 4, 'feedback' ); + $grade = $repository->create( $submission, $answer, 3, 4, 'feedback' ); /* Assert */ $expected = [ @@ -45,9 +47,10 @@ public function testCreate_ParamsGiven_ReturnsGrade(): void { } - public function testCreate_ParamsGiven_InserstsData(): void { + public function testCreate_ParamsGiven_InsertsData(): void { /* Arrange */ $submission = $this->createMock( Tables_Based_Submission::class ); + $answer = new Tables_Based_Answer( 2, 3, 4, 'value', new \DateTimeImmutable(), new \DateTimeImmutable() ); $wpdb = $this->createMock( \wpdb::class ); $repository = new Tables_Based_Grade_Repository( $wpdb ); @@ -68,7 +71,7 @@ function ( $data ) { } ) ); - $repository->create( $submission, 2, 3, 4, 'feedback' ); + $repository->create( $submission, $answer, 3, 4, 'feedback' ); } public function testIntegrationCreate_ParamsGiven_ReturnsGrade(): void { @@ -76,9 +79,10 @@ public function testIntegrationCreate_ParamsGiven_ReturnsGrade(): void { global $wpdb; $repository = new Tables_Based_Grade_Repository( $wpdb ); $submission = $this->createMock( Tables_Based_Submission::class ); + $answer = new Tables_Based_Answer( 2, 3, 4, 'value', new \DateTimeImmutable(), new \DateTimeImmutable() ); /* Act */ - $grade = $repository->create( $submission, 2, 3, 4, 'feedback' ); + $grade = $repository->create( $submission, $answer, 3, 4, 'feedback' ); /* Assert */ $expected = [ @@ -131,7 +135,7 @@ function ( $where ) { $repository->save_many( $submission, $grades ); } - public function testIntegrationSaveMany_GradesGiven_UptadesData(): void { + public function testIntegrationSaveMany_GradesGiven_UpdatesData(): void { /* Arrange */ global $wpdb;