From 84c47fb9312d23ff50d39b740d9b0491229561e6 Mon Sep 17 00:00:00 2001 From: Timotheus Pokorra Date: Sat, 4 Jan 2025 08:58:29 +0100 Subject: [PATCH] improve unit tests testGetFormAllowEditWithoutAnswers Signed-off-by: Timotheus Pokorra --- tests/Unit/Service/FormsServiceTest.php | 193 +++++------------------- 1 file changed, 40 insertions(+), 153 deletions(-) diff --git a/tests/Unit/Service/FormsServiceTest.php b/tests/Unit/Service/FormsServiceTest.php index ad425d472..51bbc360f 100644 --- a/tests/Unit/Service/FormsServiceTest.php +++ b/tests/Unit/Service/FormsServiceTest.php @@ -256,12 +256,7 @@ public function dataGetForm() { ]; } - /** - * @dataProvider dataGetForm - * - * @param array $expected - */ - public function testGetForm(array $expected) { + private function prepareFormTest(bool $withUserMock) { // The form $form = new Form(); $form->setId(42); @@ -283,13 +278,15 @@ public function testGetForm(array $expected) { // User & Group Formatting $user = $this->createMock(IUser::class); - $user->expects($this->once()) - ->method('getDisplayName') - ->willReturn('Some User'); - $this->userManager->expects($this->once()) - ->method('get') - ->with('someUser') - ->willReturn($user); + if ($withUserMock) { + $user->expects($this->once()) + ->method('getDisplayName') + ->willReturn('Some User'); + $this->userManager->expects($this->once()) + ->method('get') + ->with('someUser') + ->willReturn($user); + } // Questions $question1 = new Question(); @@ -339,6 +336,30 @@ public function testGetForm(array $expected) { $share->setShareWith('someUser'); $share->setPermissions([Constants::PERMISSION_SUBMIT]); + $answerEntity = new Answer(); + $answerEntity->setId(1); + $answerEntity->setSubmissionId(12); + $answerEntity->setFileId(112); + $answerEntity->setQuestionId(1); + $answerEntity->setText('Option 1'); + $answer2Entity = new Answer(); + $answer2Entity->setId(2); + $answer2Entity->setSubmissionId(12); + $answer2Entity->setQuestionId(2); + $answer2Entity->setText('London'); + $answers = [ $answerEntity, $answer2Entity ]; + + return [$form, $user, $share, $answers]; + } + + /** + * @dataProvider dataGetForm + * + * @param array $expected + */ + public function testGetForm(array $expected) { + [$form, $user, $share, $answers] = $this->prepareFormTest(true); + $this->shareMapper->expects($this->any()) ->method('findByForm') ->with(42) @@ -439,83 +460,15 @@ public function dataGetFormWithAnswers() { * @param array $expected */ public function testGetFormAllowEditWithAnswers(array $expected) { + [$form, $user, $share, $answers] = $this->prepareFormTest(false); + // The form, with AllowEdit - $form = new Form(); - $form->setId(42); - $form->setState(0); // default => 0 means active - $form->setHash('abcdefg'); - $form->setTitle('Form 1'); - $form->setDescription('Description Text'); - $form->setOwnerId('currentUser'); - $form->setCreated(123456789); - $form->setAccess([ - 'permitAllUsers' => false, - 'showToAllUsers' => false, - ]); - $form->setExpires(0); - $form->setIsAnonymous(false); $form->setSubmitMultiple(false); $form->setAllowEdit(true); - $form->setShowExpiration(false); - $form->setLastUpdated(123456789); $submission = new Submission(); $submission->setId(12); - // Questions - $question1 = new Question(); - $question1->setId(1); - $question1->setFormId(42); - $question1->setOrder(1); - $question1->setType('dropdown'); - $question1->setIsRequired(false); - $question1->setExtraSettings([ - 'shuffleOptions' => true - ]); - $question1->setText('Question 1'); - $question1->setDescription('This is our first question.'); - $question2 = new Question(); - $question2->setId(2); - $question2->setFormId(42); - $question2->setOrder(2); - $question2->setType('short'); - $question2->setIsRequired(true); - $question2->setText('Question 2'); - $question2->setDescription(''); - $question2->setName('city'); - $question2->setExtraSettings([]); - $this->questionMapper->expects($this->once()) - ->method('findByForm') - ->with(42) - ->willReturn([$question1, $question2]); - - // Options - $option1 = new Option(); - $option1->setId(1); - $option1->setQuestionId(1); - $option1->setText('Option 1'); - $option2 = new Option(); - $option2->setId(2); - $option2->setQuestionId(1); - $option2->setText('Option 2'); - $this->optionMapper->expects($this->any()) - ->method('findByQuestion') - ->with(1) - ->willReturn([$option1, $option2]); - - $answerEntity = new Answer(); - $answerEntity->setId(1); - $answerEntity->setSubmissionId(12); - $answerEntity->setFileId(112); - $answerEntity->setQuestionId(1); - $answerEntity->setText('Option 1'); - $answer2Entity = new Answer(); - $answer2Entity->setId(2); - $answer2Entity->setSubmissionId(12); - $answer2Entity->setQuestionId(2); - $answer2Entity->setText('London'); - $answers = [ $answerEntity, $answer2Entity ]; - $this->submissionMapper->expects($this->once()) ->method('findByFormAndUser') ->with(42, 'currentUser') @@ -540,87 +493,21 @@ public function testGetFormAllowEditWithAnswers(array $expected) { * * @param array $expected */ - public function testGetFormAllowEditWithoutAnswers(array $expected) { - // drop the answers for this test + public function testGetFormAllowEditWithoutSubmission(array $expected) { + // drop the submissions and answers for this test unset($expected['answers']); unset($expected['newSubmission']); unset($expected['submissionId']); + [$form, $user, $share, $answers] = $this->prepareFormTest(false); // The form, with AllowEdit - $form = new Form(); - $form->setId(42); - $form->setState(0); // default => 0 means active - $form->setHash('abcdefg'); - $form->setTitle('Form 1'); - $form->setDescription('Description Text'); - $form->setOwnerId('currentUser'); - $form->setCreated(123456789); - $form->setAccess([ - 'permitAllUsers' => false, - 'showToAllUsers' => false, - ]); - $form->setExpires(0); - $form->setIsAnonymous(false); $form->setSubmitMultiple(false); $form->setAllowEdit(true); - $form->setShowExpiration(false); - $form->setLastUpdated(123456789); - - $submission = new Submission(); - $submission->setId(12); - - // Questions - $question1 = new Question(); - $question1->setId(1); - $question1->setFormId(42); - $question1->setOrder(1); - $question1->setType('dropdown'); - $question1->setIsRequired(false); - $question1->setExtraSettings([ - 'shuffleOptions' => true - ]); - $question1->setText('Question 1'); - $question1->setDescription('This is our first question.'); - $question2 = new Question(); - $question2->setId(2); - $question2->setFormId(42); - $question2->setOrder(2); - $question2->setType('short'); - $question2->setIsRequired(true); - $question2->setText('Question 2'); - $question2->setDescription(''); - $question2->setName('city'); - $question2->setExtraSettings([]); - $this->questionMapper->expects($this->once()) - ->method('findByForm') - ->with(42) - ->willReturn([$question1, $question2]); - - // Options - $option1 = new Option(); - $option1->setId(1); - $option1->setQuestionId(1); - $option1->setText('Option 1'); - $option2 = new Option(); - $option2->setId(2); - $option2->setQuestionId(1); - $option2->setText('Option 2'); - $this->optionMapper->expects($this->any()) - ->method('findByQuestion') - ->with(1) - ->willReturn([$option1, $option2]); - - $answers = [ ]; $this->submissionMapper->expects($this->once()) ->method('findByFormAndUser') ->with(42, 'currentUser') - ->willReturn($submission); - - $this->answerMapper->expects($this->once()) - ->method('findBySubmission') - ->with(12) - ->willReturn($answers); + ->willThrowException(new DoesNotExistException('Test exception')); $this->submissionMapper->expects($this->once()) ->method('countSubmissions')