EducationNotationUtilsTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. namespace App\Tests\Unit\Service\Education;
  3. use App\Entity\Access\Access;
  4. use App\Entity\Education\CriteriaNotation;
  5. use App\Entity\Education\EducationNotation;
  6. use App\Entity\Education\EducationStudent;
  7. use App\Entity\Organization\Organization;
  8. use App\Entity\Organization\Parameters;
  9. use App\Enum\Education\TypeCriteriaEnum;
  10. use App\Service\Education\EducationNotationUtils;
  11. use PHPUnit\Framework\TestCase;
  12. class TestableEducationNotationUtils extends EducationNotationUtils {
  13. public function calculNotationByAMaxNote(EducationNotation $educationNotation, float $noteMax): ?float {
  14. return parent::calculNotationByAMaxNote($educationNotation, $noteMax);
  15. }
  16. }
  17. class EducationNotationUtilsTest extends TestCase
  18. {
  19. /**
  20. * @see EducationNotationUtils::getNotationTransformed()
  21. */
  22. public function testGetNotationTransformed(): void
  23. {
  24. $educationNotationUtils = $this->getMockBuilder(TestableEducationNotationUtils::class)
  25. ->setMethodsExcept(['getNotationTransformed'])
  26. ->getMock();
  27. $noteMax = 80;
  28. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  29. $parameters->method('getAverage')->willReturn($noteMax);
  30. $organization = $this->getMockBuilder(Organization::class)->getMock();
  31. $organization->method('getParameters')->willReturn($parameters);
  32. $access = $this->getMockBuilder(Access::class)->getMock();
  33. $access->method('getOrganization')->willReturn($organization);
  34. $educationStudent = $this->getMockBuilder(EducationStudent::class)->getMock();
  35. $educationStudent->method('getAccess')->willReturn($access);
  36. $educationNotation = $this->getMockBuilder(EducationNotation::class)->getMock();
  37. $educationNotation->method('getEducationStudent')->willReturn($educationStudent);
  38. $educationNotationUtils->expects(self::once())->method('calculNotationByAMaxNote')->with($educationNotation, $noteMax);
  39. $educationNotationUtils->getNotationTransformed($educationNotation);
  40. }
  41. /**
  42. * @see EducationNotationUtils::getNotationOriginal()
  43. */
  44. public function testGetNotationOriginal(): void
  45. {
  46. $educationNotationUtils = $this->getMockBuilder(TestableEducationNotationUtils::class)
  47. ->setMethodsExcept(['getNotationOriginal'])
  48. ->getMock();
  49. $noteMax = 40;
  50. $criteriaNotation = $this->getMockBuilder(CriteriaNotation::class)->getMock();
  51. $criteriaNotation->method('getNoteMax')->willReturn($noteMax);
  52. $educationNotation = $this->getMockBuilder(EducationNotation::class)->getMock();
  53. $educationNotation->method('getCriteriaNotation')->willReturn($criteriaNotation);
  54. $educationNotationUtils->expects(self::once())->method('calculNotationByAMaxNote')->with($educationNotation, $noteMax);
  55. $educationNotationUtils->getNotationOriginal($educationNotation);
  56. }
  57. /**
  58. * @see EducationNotationUtils::getNotationOriginal()
  59. */
  60. public function testGetNotationOriginalWithoutCriteria(): void
  61. {
  62. $educationNotationUtils = $this->getMockBuilder(TestableEducationNotationUtils::class)
  63. ->setMethodsExcept(['getNotationOriginal'])
  64. ->getMock();
  65. $educationNotation = $this->getMockBuilder(EducationNotation::class)->getMock();
  66. $educationNotation->method('getCriteriaNotation')->willReturn(null);
  67. $educationNotationUtils->expects(self::never())->method('calculNotationByAMaxNote');
  68. $this->assertNull($educationNotationUtils->getNotationOriginal($educationNotation));
  69. }
  70. /**
  71. * @see EducationNotationUtils::calculNotationByAMaxNote()
  72. */
  73. public function testCalculNotationByAMaxNote(): void
  74. {
  75. $educationNotationUtils = $this->getMockBuilder(TestableEducationNotationUtils::class)
  76. ->setMethodsExcept(['calculNotationByAMaxNote'])
  77. ->getMock();
  78. $note1 = 60.0;
  79. $note2 = 0.0;
  80. $note3 = 100.0;
  81. $noteMax = 20;
  82. $criteriaNotation = $this->getMockBuilder(CriteriaNotation::class)->getMock();
  83. $criteriaNotation->method('getType')->willReturn(TypeCriteriaEnum::WITH_NOTATION()->getValue());
  84. $educationNotation1 = $this->getMockBuilder(EducationNotation::class)->getMock();
  85. $educationNotation1->method('getCriteriaNotation')->willReturn($criteriaNotation);
  86. $educationNotation1->method('getNote')->willReturn($note1);
  87. $educationNotation2 = $this->getMockBuilder(EducationNotation::class)->getMock();
  88. $educationNotation2->method('getCriteriaNotation')->willReturn($criteriaNotation);
  89. $educationNotation2->method('getNote')->willReturn($note2);
  90. $educationNotation3 = $this->getMockBuilder(EducationNotation::class)->getMock();
  91. $educationNotation3->method('getCriteriaNotation')->willReturn($criteriaNotation);
  92. $educationNotation3->method('getNote')->willReturn($note3);
  93. $this->assertEquals(
  94. 12,
  95. $educationNotationUtils->calculNotationByAMaxNote($educationNotation1, $noteMax)
  96. );
  97. $this->assertEquals(
  98. 0,
  99. $educationNotationUtils->calculNotationByAMaxNote($educationNotation2, $noteMax)
  100. );
  101. $this->assertEquals(
  102. 20,
  103. $educationNotationUtils->calculNotationByAMaxNote($educationNotation3, $noteMax)
  104. );
  105. }
  106. /**
  107. * If $educationNotation has no note, it should return null
  108. * @see EducationNotationUtils::calculNotationByAMaxNote()
  109. */
  110. public function testCalculNotationByAMaxNoteWithoutNote(): void
  111. {
  112. $educationNotationUtils = $this->getMockBuilder(TestableEducationNotationUtils::class)
  113. ->setMethodsExcept(['calculNotationByAMaxNote'])
  114. ->getMock();
  115. $note = null;
  116. $noteMax = 20;
  117. $educationNotation = $this->getMockBuilder(EducationNotation::class)->getMock();
  118. $educationNotation->expects(self::atLeastOnce())->method('getNote')->willReturn($note);
  119. $this->assertEquals(
  120. null,
  121. $educationNotationUtils->calculNotationByAMaxNote($educationNotation, $noteMax)
  122. );
  123. }
  124. /**
  125. * If $educationNotation has no criteriaNotation, it should return null
  126. * @see EducationNotationUtils::calculNotationByAMaxNote()
  127. */
  128. public function testCalculNotationByAMaxNoteWithoutCriteria(): void
  129. {
  130. $educationNotationUtils = $this->getMockBuilder(TestableEducationNotationUtils::class)
  131. ->setMethodsExcept(['calculNotationByAMaxNote'])
  132. ->getMock();
  133. $note = 50.0;
  134. $noteMax = 20;
  135. $educationNotation = $this->getMockBuilder(EducationNotation::class)->getMock();
  136. $educationNotation->expects(self::atLeastOnce())->method('getNote')->willReturn($note);
  137. $educationNotation->expects(self::atLeastOnce())->method('getCriteriaNotation')->willReturn(null);
  138. $this->assertEquals(
  139. null,
  140. $educationNotationUtils->calculNotationByAMaxNote($educationNotation, $noteMax)
  141. );
  142. }
  143. /**
  144. * If $criteriaNotation has WITHOUT_NOTATION type, this should return null
  145. * @see EducationNotationUtils::calculNotationByAMaxNote()
  146. */
  147. public function testCalculNotationByAMaxNoteWithoutNotation(): void
  148. {
  149. $educationNotationUtils = $this->getMockBuilder(TestableEducationNotationUtils::class)
  150. ->setMethodsExcept(['calculNotationByAMaxNote'])
  151. ->getMock();
  152. $note = 60.0;
  153. $noteMax = 20;
  154. $criteriaNotation = $this->getMockBuilder(CriteriaNotation::class)->getMock();
  155. $criteriaNotation->expects(self::once())->method('getType')->willReturn(TypeCriteriaEnum::WITHOUT_NOTATION()->getValue());
  156. $educationNotation = $this->getMockBuilder(EducationNotation::class)->getMock();
  157. $educationNotation->expects(self::atLeastOnce())->method('getNote')->willReturn($note);
  158. $educationNotation->expects(self::atLeastOnce())->method('getCriteriaNotation')->willReturn($criteriaNotation);
  159. $this->assertEquals(
  160. null,
  161. $educationNotationUtils->calculNotationByAMaxNote($educationNotation, $noteMax)
  162. );
  163. }
  164. }