EducationNotationUtilsTest.php 8.1 KB

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