Skip to content

Commit

Permalink
Test grades created if not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
merkushin committed Aug 30, 2023
1 parent 1a04a2a commit 439446f
Showing 1 changed file with 68 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

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;
Expand Down Expand Up @@ -263,7 +265,7 @@ public function testSaveMany_UseTablesSetToFalse_DoesntUseTablesBasedRepository(
$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 );
Expand Down Expand Up @@ -319,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 );
}
}

0 comments on commit 439446f

Please sign in to comment.