EducationNotationUtilsTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. namespace App\Test\Service\Access;
  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. public function testCalculNotationByAMaxNote(): void
  71. {
  72. $educationNotationUtils = $this->getMockBuilder(TestableEducationNotationUtils::class)
  73. ->setMethodsExcept(['calculNotationByAMaxNote'])
  74. ->getMock();
  75. $note1 = 60.0;
  76. $note2 = 0.0;
  77. $note3 = 100.0;
  78. $noteMax = 20;
  79. $criteriaNotation = $this->getMockBuilder(CriteriaNotation::class)->getMock();
  80. $criteriaNotation->method('getType')->willReturn(TypeCriteriaEnum::WITH_NOTATION()->getValue());
  81. $educationNotation1 = $this->getMockBuilder(EducationNotation::class)->getMock();
  82. $educationNotation1->method('getCriteriaNotation')->willReturn($criteriaNotation);
  83. $educationNotation1->method('getNote')->willReturn($note1);
  84. $educationNotation2 = $this->getMockBuilder(EducationNotation::class)->getMock();
  85. $educationNotation2->method('getCriteriaNotation')->willReturn($criteriaNotation);
  86. $educationNotation2->method('getNote')->willReturn($note2);
  87. $educationNotation3 = $this->getMockBuilder(EducationNotation::class)->getMock();
  88. $educationNotation3->method('getCriteriaNotation')->willReturn($criteriaNotation);
  89. $educationNotation3->method('getNote')->willReturn($note3);
  90. $this->assertEquals(
  91. 12,
  92. $educationNotationUtils->calculNotationByAMaxNote($educationNotation1, $noteMax)
  93. );
  94. $this->assertEquals(
  95. 0,
  96. $educationNotationUtils->calculNotationByAMaxNote($educationNotation2, $noteMax)
  97. );
  98. $this->assertEquals(
  99. 20,
  100. $educationNotationUtils->calculNotationByAMaxNote($educationNotation3, $noteMax)
  101. );
  102. }
  103. /**
  104. * If $educationNotation has no note, it should return null
  105. */
  106. public function testCalculNotationByAMaxNoteWithoutNote(): void
  107. {
  108. $educationNotationUtils = $this->getMockBuilder(TestableEducationNotationUtils::class)
  109. ->setMethodsExcept(['calculNotationByAMaxNote'])
  110. ->getMock();
  111. $note = null;
  112. $noteMax = 20;
  113. $educationNotation = $this->getMockBuilder(EducationNotation::class)->getMock();
  114. $educationNotation->expects(self::atLeastOnce())->method('getNote')->willReturn($note);
  115. $this->assertEquals(
  116. null,
  117. $educationNotationUtils->calculNotationByAMaxNote($educationNotation, $noteMax)
  118. );
  119. }
  120. /**
  121. * If $educationNotation has no criteriaNotation, it should return null
  122. */
  123. public function testCalculNotationByAMaxNoteWithoutCriteria(): void
  124. {
  125. $educationNotationUtils = $this->getMockBuilder(TestableEducationNotationUtils::class)
  126. ->setMethodsExcept(['calculNotationByAMaxNote'])
  127. ->getMock();
  128. $note = 50.0;
  129. $noteMax = 20;
  130. $educationNotation = $this->getMockBuilder(EducationNotation::class)->getMock();
  131. $educationNotation->expects(self::atLeastOnce())->method('getNote')->willReturn($note);
  132. $educationNotation->expects(self::atLeastOnce())->method('getCriteriaNotation')->willReturn(null);
  133. $this->assertEquals(
  134. null,
  135. $educationNotationUtils->calculNotationByAMaxNote($educationNotation, $noteMax)
  136. );
  137. }
  138. /**
  139. * If $criteriaNotation has WITHOUT_NOTATION type, this should return null
  140. */
  141. public function testCalculNotationByAMaxNoteWithoutNotation(): void
  142. {
  143. $educationNotationUtils = $this->getMockBuilder(TestableEducationNotationUtils::class)
  144. ->setMethodsExcept(['calculNotationByAMaxNote'])
  145. ->getMock();
  146. $note = 60.0;
  147. $noteMax = 20;
  148. $criteriaNotation = $this->getMockBuilder(CriteriaNotation::class)->getMock();
  149. $criteriaNotation->expects(self::once())->method('getType')->willReturn(TypeCriteriaEnum::WITHOUT_NOTATION()->getValue());
  150. $educationNotation = $this->getMockBuilder(EducationNotation::class)->getMock();
  151. $educationNotation->expects(self::atLeastOnce())->method('getNote')->willReturn($note);
  152. $educationNotation->expects(self::atLeastOnce())->method('getCriteriaNotation')->willReturn($criteriaNotation);
  153. $this->assertEquals(
  154. null,
  155. $educationNotationUtils->calculNotationByAMaxNote($educationNotation, $noteMax)
  156. );
  157. }
  158. }