Skip to content

Commit 84c47fb

Browse files
committed
improve unit tests testGetFormAllowEditWithoutAnswers
Signed-off-by: Timotheus Pokorra <[email protected]>
1 parent d5e4b22 commit 84c47fb

File tree

1 file changed

+40
-153
lines changed

1 file changed

+40
-153
lines changed

tests/Unit/Service/FormsServiceTest.php

Lines changed: 40 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -256,12 +256,7 @@ public function dataGetForm() {
256256
];
257257
}
258258

259-
/**
260-
* @dataProvider dataGetForm
261-
*
262-
* @param array $expected
263-
*/
264-
public function testGetForm(array $expected) {
259+
private function prepareFormTest(bool $withUserMock) {
265260
// The form
266261
$form = new Form();
267262
$form->setId(42);
@@ -283,13 +278,15 @@ public function testGetForm(array $expected) {
283278

284279
// User & Group Formatting
285280
$user = $this->createMock(IUser::class);
286-
$user->expects($this->once())
287-
->method('getDisplayName')
288-
->willReturn('Some User');
289-
$this->userManager->expects($this->once())
290-
->method('get')
291-
->with('someUser')
292-
->willReturn($user);
281+
if ($withUserMock) {
282+
$user->expects($this->once())
283+
->method('getDisplayName')
284+
->willReturn('Some User');
285+
$this->userManager->expects($this->once())
286+
->method('get')
287+
->with('someUser')
288+
->willReturn($user);
289+
}
293290

294291
// Questions
295292
$question1 = new Question();
@@ -339,6 +336,30 @@ public function testGetForm(array $expected) {
339336
$share->setShareWith('someUser');
340337
$share->setPermissions([Constants::PERMISSION_SUBMIT]);
341338

339+
$answerEntity = new Answer();
340+
$answerEntity->setId(1);
341+
$answerEntity->setSubmissionId(12);
342+
$answerEntity->setFileId(112);
343+
$answerEntity->setQuestionId(1);
344+
$answerEntity->setText('Option 1');
345+
$answer2Entity = new Answer();
346+
$answer2Entity->setId(2);
347+
$answer2Entity->setSubmissionId(12);
348+
$answer2Entity->setQuestionId(2);
349+
$answer2Entity->setText('London');
350+
$answers = [ $answerEntity, $answer2Entity ];
351+
352+
return [$form, $user, $share, $answers];
353+
}
354+
355+
/**
356+
* @dataProvider dataGetForm
357+
*
358+
* @param array $expected
359+
*/
360+
public function testGetForm(array $expected) {
361+
[$form, $user, $share, $answers] = $this->prepareFormTest(true);
362+
342363
$this->shareMapper->expects($this->any())
343364
->method('findByForm')
344365
->with(42)
@@ -439,83 +460,15 @@ public function dataGetFormWithAnswers() {
439460
* @param array $expected
440461
*/
441462
public function testGetFormAllowEditWithAnswers(array $expected) {
463+
[$form, $user, $share, $answers] = $this->prepareFormTest(false);
464+
442465
// The form, with AllowEdit
443-
$form = new Form();
444-
$form->setId(42);
445-
$form->setState(0); // default => 0 means active
446-
$form->setHash('abcdefg');
447-
$form->setTitle('Form 1');
448-
$form->setDescription('Description Text');
449-
$form->setOwnerId('currentUser');
450-
$form->setCreated(123456789);
451-
$form->setAccess([
452-
'permitAllUsers' => false,
453-
'showToAllUsers' => false,
454-
]);
455-
$form->setExpires(0);
456-
$form->setIsAnonymous(false);
457466
$form->setSubmitMultiple(false);
458467
$form->setAllowEdit(true);
459-
$form->setShowExpiration(false);
460-
$form->setLastUpdated(123456789);
461468

462469
$submission = new Submission();
463470
$submission->setId(12);
464471

465-
// Questions
466-
$question1 = new Question();
467-
$question1->setId(1);
468-
$question1->setFormId(42);
469-
$question1->setOrder(1);
470-
$question1->setType('dropdown');
471-
$question1->setIsRequired(false);
472-
$question1->setExtraSettings([
473-
'shuffleOptions' => true
474-
]);
475-
$question1->setText('Question 1');
476-
$question1->setDescription('This is our first question.');
477-
$question2 = new Question();
478-
$question2->setId(2);
479-
$question2->setFormId(42);
480-
$question2->setOrder(2);
481-
$question2->setType('short');
482-
$question2->setIsRequired(true);
483-
$question2->setText('Question 2');
484-
$question2->setDescription('');
485-
$question2->setName('city');
486-
$question2->setExtraSettings([]);
487-
$this->questionMapper->expects($this->once())
488-
->method('findByForm')
489-
->with(42)
490-
->willReturn([$question1, $question2]);
491-
492-
// Options
493-
$option1 = new Option();
494-
$option1->setId(1);
495-
$option1->setQuestionId(1);
496-
$option1->setText('Option 1');
497-
$option2 = new Option();
498-
$option2->setId(2);
499-
$option2->setQuestionId(1);
500-
$option2->setText('Option 2');
501-
$this->optionMapper->expects($this->any())
502-
->method('findByQuestion')
503-
->with(1)
504-
->willReturn([$option1, $option2]);
505-
506-
$answerEntity = new Answer();
507-
$answerEntity->setId(1);
508-
$answerEntity->setSubmissionId(12);
509-
$answerEntity->setFileId(112);
510-
$answerEntity->setQuestionId(1);
511-
$answerEntity->setText('Option 1');
512-
$answer2Entity = new Answer();
513-
$answer2Entity->setId(2);
514-
$answer2Entity->setSubmissionId(12);
515-
$answer2Entity->setQuestionId(2);
516-
$answer2Entity->setText('London');
517-
$answers = [ $answerEntity, $answer2Entity ];
518-
519472
$this->submissionMapper->expects($this->once())
520473
->method('findByFormAndUser')
521474
->with(42, 'currentUser')
@@ -540,87 +493,21 @@ public function testGetFormAllowEditWithAnswers(array $expected) {
540493
*
541494
* @param array $expected
542495
*/
543-
public function testGetFormAllowEditWithoutAnswers(array $expected) {
544-
// drop the answers for this test
496+
public function testGetFormAllowEditWithoutSubmission(array $expected) {
497+
// drop the submissions and answers for this test
545498
unset($expected['answers']);
546499
unset($expected['newSubmission']);
547500
unset($expected['submissionId']);
501+
[$form, $user, $share, $answers] = $this->prepareFormTest(false);
548502

549503
// The form, with AllowEdit
550-
$form = new Form();
551-
$form->setId(42);
552-
$form->setState(0); // default => 0 means active
553-
$form->setHash('abcdefg');
554-
$form->setTitle('Form 1');
555-
$form->setDescription('Description Text');
556-
$form->setOwnerId('currentUser');
557-
$form->setCreated(123456789);
558-
$form->setAccess([
559-
'permitAllUsers' => false,
560-
'showToAllUsers' => false,
561-
]);
562-
$form->setExpires(0);
563-
$form->setIsAnonymous(false);
564504
$form->setSubmitMultiple(false);
565505
$form->setAllowEdit(true);
566-
$form->setShowExpiration(false);
567-
$form->setLastUpdated(123456789);
568-
569-
$submission = new Submission();
570-
$submission->setId(12);
571-
572-
// Questions
573-
$question1 = new Question();
574-
$question1->setId(1);
575-
$question1->setFormId(42);
576-
$question1->setOrder(1);
577-
$question1->setType('dropdown');
578-
$question1->setIsRequired(false);
579-
$question1->setExtraSettings([
580-
'shuffleOptions' => true
581-
]);
582-
$question1->setText('Question 1');
583-
$question1->setDescription('This is our first question.');
584-
$question2 = new Question();
585-
$question2->setId(2);
586-
$question2->setFormId(42);
587-
$question2->setOrder(2);
588-
$question2->setType('short');
589-
$question2->setIsRequired(true);
590-
$question2->setText('Question 2');
591-
$question2->setDescription('');
592-
$question2->setName('city');
593-
$question2->setExtraSettings([]);
594-
$this->questionMapper->expects($this->once())
595-
->method('findByForm')
596-
->with(42)
597-
->willReturn([$question1, $question2]);
598-
599-
// Options
600-
$option1 = new Option();
601-
$option1->setId(1);
602-
$option1->setQuestionId(1);
603-
$option1->setText('Option 1');
604-
$option2 = new Option();
605-
$option2->setId(2);
606-
$option2->setQuestionId(1);
607-
$option2->setText('Option 2');
608-
$this->optionMapper->expects($this->any())
609-
->method('findByQuestion')
610-
->with(1)
611-
->willReturn([$option1, $option2]);
612-
613-
$answers = [ ];
614506

615507
$this->submissionMapper->expects($this->once())
616508
->method('findByFormAndUser')
617509
->with(42, 'currentUser')
618-
->willReturn($submission);
619-
620-
$this->answerMapper->expects($this->once())
621-
->method('findBySubmission')
622-
->with(12)
623-
->willReturn($answers);
510+
->willThrowException(new DoesNotExistException('Test exception'));
624511

625512
$this->submissionMapper->expects($this->once())
626513
->method('countSubmissions')

0 commit comments

Comments
 (0)