OnParametersChangeTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php
  2. namespace App\Test\Service\OnChange\Organization;
  3. use App\Entity\Access\Access;
  4. use App\Entity\Booking\Course;
  5. use App\Entity\Education\EducationCurriculum;
  6. use App\Entity\Education\EducationNotationConfig;
  7. use App\Entity\Organization\Organization;
  8. use App\Entity\Organization\Parameters;
  9. use App\Enum\Education\AdvancedEducationNotationTypeEnum;
  10. use App\Message\Command\Parameters\AverageChange;
  11. use App\Message\Command\Typo3\Typo3DeleteCommand;
  12. use App\Message\Command\Typo3\Typo3UndeleteCommand;
  13. use App\Message\Command\Typo3\Typo3UpdateCommand;
  14. use App\Repository\Booking\CourseRepository;
  15. use App\Service\OnChange\Organization\OnChangeContext;
  16. use App\Service\OnChange\Organization\OnParametersChange;
  17. use AssertionError;
  18. use PHPUnit\Framework\TestCase;
  19. use Prophecy\Argument;
  20. use Symfony\Component\Messenger\Envelope;
  21. use Symfony\Component\Messenger\MessageBusInterface;
  22. class OnParametersChangeTest extends TestCase
  23. {
  24. private Parameters $parameters;
  25. private OnParametersChange $onParametersChange;
  26. private CourseRepository $courseRepositoryMock;
  27. private \App\Service\Network\Utils $networkUtils;
  28. private MessageBusInterface $messageBus;
  29. public function setUp():void
  30. {
  31. $this->courseRepositoryMock = $this->getMockBuilder(CourseRepository::class)->disableOriginalConstructor()->getMock();
  32. $this->networkUtils = $this->getMockBuilder(\App\Service\Network\Utils::class)->disableOriginalConstructor()->getMock();
  33. $this->messageBus = $this->getMockBuilder(MessageBusInterface::class)->disableOriginalConstructor()->getMock();
  34. $this->parameters = new Parameters();
  35. $this->onParametersChange = new OnParametersChange(
  36. $this->courseRepositoryMock,
  37. $this->networkUtils,
  38. $this->messageBus
  39. );
  40. }
  41. public function testValidate() {
  42. $context = $this->getMockBuilder(OnChangeContext::class)->disableOriginalConstructor()->getMock();
  43. // 1. Is CMF and site web enabled ; 2. Is not CMF and site web disabled ; 3. Is not CMF and site web enabled
  44. foreach ([[false, true], [true, false], [false, false]] as $params) {
  45. $parameters = $this->getMockBuilder(Parameters::class)->disableOriginalConstructor()->getMock();
  46. $parameters->expects(self::once())->method('getDesactivateOpentalentSiteWeb')->willReturn($params[0]);
  47. $this->networkUtils = $this->getMockBuilder(\App\Service\Network\Utils::class)->disableOriginalConstructor()->getMock();
  48. $this->networkUtils->method('isCMFAndActiveNow')->willReturn($params[1]);
  49. $this->onParametersChange->validate($parameters, $context);
  50. }
  51. }
  52. public function testValidateInvalid() {
  53. $parameters = $this->getMockBuilder(Parameters::class)->disableOriginalConstructor()->getMock();
  54. $context = $this->getMockBuilder(OnChangeContext::class)->disableOriginalConstructor()->getMock();
  55. // Is CMF and site web disabled
  56. $parameters->expects(self::once())->method('getDesactivateOpentalentSiteWeb')->willReturn(true);
  57. $this->networkUtils->expects(self::once())->method('isCMFAndActiveNow')->willReturn(true);
  58. try {
  59. $this->onParametersChange->validate($parameters, $context);
  60. throw new AssertionError('OnParametersChange::validate should have thrown an error');
  61. } catch (\RuntimeException) {}
  62. }
  63. public function testBeforeChange() {
  64. $onParametersChange = $this
  65. ->getMockBuilder(OnParametersChange::class)
  66. ->onlyMethods(['onAdvancedEducationNotationTypeChange', 'onMusicalDateChange'])
  67. ->disableOriginalConstructor()
  68. ->getMock();
  69. $onParametersChange
  70. ->expects(self::once())
  71. ->method('onAdvancedEducationNotationTypeChange')
  72. ->willReturnSelf();
  73. $onParametersChange
  74. ->expects(self::once())
  75. ->method('onMusicalDateChange')
  76. ->willReturnSelf();
  77. $previousParameters = $this->getMockBuilder(Parameters::class)->disableOriginalConstructor()->getMock();
  78. $previousParameters->method('getAdvancedEducationNotationType')->willReturn('BY_EDUCATION');
  79. $musicalDate = new \DateTime('2022-01-01');
  80. $previousParameters->method('getMusicalDate')->willReturn($musicalDate);
  81. $context = $this->getMockBuilder(OnChangeContext::class)->disableOriginalConstructor()->getMock();
  82. $context->method('previousData')->willReturn($previousParameters);
  83. $parameters = $this->getMockBuilder(Parameters::class)->disableOriginalConstructor()->getMock();
  84. $parameters->method('getAdvancedEducationNotationType')->willReturn('SOMETHING_ELSE');
  85. $parameters->method('getMusicalDate')->willReturn(new \DateTime('2023-01-01'));
  86. // Both mocked methods should be called once here
  87. $onParametersChange->beforeChange($parameters, $context);
  88. $parameters = $this->getMockBuilder(Parameters::class)->disableOriginalConstructor()->getMock();
  89. $parameters->method('getId')->willReturn(1);
  90. $parameters->method('getAdvancedEducationNotationType')->willReturn('BY_EDUCATION');
  91. $parameters->method('getMusicalDate')->willReturn($musicalDate);
  92. // None of the mocked methods should be called again here
  93. $onParametersChange->beforeChange($parameters, $context);
  94. }
  95. public function testOnChangeNoChange() {
  96. $this->messageBus->expects($this->never())->method('dispatch');
  97. $previousParameters = $this->getMockBuilder(Parameters::class)->disableOriginalConstructor()->getMock();
  98. $previousParameters->method('getId')->willReturn(1);
  99. $previousParameters->expects(self::once())->method('getAverage')->willReturn(20);
  100. $previousParameters->expects(self::once())->method('getDesactivateOpentalentSiteWeb')->willReturn(false);
  101. $previousParameters->expects(self::once())->method('getCustomDomain')->willReturn(null);
  102. $context = $this->getMockBuilder(OnChangeContext::class)->disableOriginalConstructor()->getMock();
  103. $context->method('previousData')->willReturn($previousParameters);
  104. $parameters = $this->getMockBuilder(Parameters::class)->disableOriginalConstructor()->getMock();
  105. $parameters->method('getId')->willReturn(1);
  106. $parameters->method('getAverage')->willReturn(20);
  107. $parameters->method('getDesactivateOpentalentSiteWeb')->willReturn(false);
  108. $parameters->method('getCustomDomain')->willReturn(null);
  109. $this->onParametersChange->onChange($parameters, $context);
  110. }
  111. public function testOnChangeAverageChanged() {
  112. $this->messageBus
  113. ->expects(self::once())
  114. ->method('dispatch')
  115. ->with(self::isInstanceOf(AverageChange::class))
  116. ->willReturn(new Envelope(new AverageChange(1)));
  117. $previousParameters = $this->getMockBuilder(Parameters::class)->disableOriginalConstructor()->getMock();
  118. $previousParameters->method('getId')->willReturn(1);
  119. $previousParameters->expects(self::once())->method('getAverage')->willReturn(20);
  120. $previousParameters->method('getDesactivateOpentalentSiteWeb')->willReturn(false);
  121. $previousParameters->method('getCustomDomain')->willReturn(null);
  122. $context = $this->getMockBuilder(OnChangeContext::class)->disableOriginalConstructor()->getMock();
  123. $context->method('previousData')->willReturn($previousParameters);
  124. $parameters = $this->getMockBuilder(Parameters::class)->disableOriginalConstructor()->getMock();
  125. $parameters->method('getId')->willReturn(1);
  126. $parameters->method('getDesactivateOpentalentSiteWeb')->willReturn(false);
  127. $parameters->method('getCustomDomain')->willReturn(null);
  128. $parameters->expects(self::once())->method('getAverage')->willReturn(30);
  129. $this->onParametersChange->onChange($parameters, $context);
  130. }
  131. public function testOnChangeCustomDomainChanged() {
  132. $this->messageBus
  133. ->expects(self::once())
  134. ->method('dispatch')
  135. ->with(self::isInstanceOf(Typo3UpdateCommand::class))
  136. ->willReturn(new Envelope(new Typo3UpdateCommand(1)));
  137. $previousParameters = $this->getMockBuilder(Parameters::class)->disableOriginalConstructor()->getMock();
  138. $previousParameters->method('getId')->willReturn(1);
  139. $previousParameters->method('getAverage')->willReturn(20);
  140. $previousParameters->method('getDesactivateOpentalentSiteWeb')->willReturn(false);
  141. $previousParameters->expects(self::once())->method('getCustomDomain')->willReturn(null);
  142. $context = $this->getMockBuilder(OnChangeContext::class)->disableOriginalConstructor()->getMock();
  143. $context->method('previousData')->willReturn($previousParameters);
  144. $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock();
  145. $organization->method('getId')->willReturn(1);
  146. $parameters = $this->getMockBuilder(Parameters::class)->disableOriginalConstructor()->getMock();
  147. $parameters->method('getId')->willReturn(1);
  148. $parameters->method('getOrganization')->willReturn($organization);
  149. $parameters->method('getDesactivateOpentalentSiteWeb')->willReturn(false);
  150. $parameters->expects(self::once())->method('getCustomDomain')->willReturn('custom');
  151. $parameters->method('getAverage')->willReturn(20);
  152. $this->onParametersChange->onChange($parameters, $context);
  153. }
  154. public function testOnChangeWebsiteDisabled() {
  155. $this->messageBus
  156. ->expects(self::once())
  157. ->method('dispatch')
  158. ->with(self::isInstanceOf(Typo3DeleteCommand::class))
  159. ->willReturn(new Envelope(new Typo3DeleteCommand(1)));
  160. $previousParameters = $this->getMockBuilder(Parameters::class)->disableOriginalConstructor()->getMock();
  161. $previousParameters->method('getId')->willReturn(1);
  162. $previousParameters->method('getAverage')->willReturn(20);
  163. $previousParameters->expects(self::once())->method('getDesactivateOpentalentSiteWeb')->willReturn(false);
  164. $previousParameters->method('getCustomDomain')->willReturn(null);
  165. $context = $this->getMockBuilder(OnChangeContext::class)->disableOriginalConstructor()->getMock();
  166. $context->method('previousData')->willReturn($previousParameters);
  167. $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock();
  168. $organization->method('getId')->willReturn(1);
  169. $parameters = $this->getMockBuilder(Parameters::class)->disableOriginalConstructor()->getMock();
  170. $parameters->method('getId')->willReturn(1);
  171. $parameters->method('getOrganization')->willReturn($organization);
  172. $parameters->method('getDesactivateOpentalentSiteWeb')->willReturn(true);
  173. $parameters->method('getCustomDomain')->willReturn(null);
  174. $parameters->method('getAverage')->willReturn(20);
  175. $this->onParametersChange->onChange($parameters, $context);
  176. }
  177. public function testOnChangeWebsiteReenabled(): void
  178. {
  179. $this->messageBus
  180. ->expects(self::exactly(2))
  181. ->method('dispatch')
  182. ->willReturnCallback(function ($message, $stamps = []) {
  183. if ($message instanceof Typo3UndeleteCommand) {
  184. return new Envelope(new Typo3UndeleteCommand(1));
  185. }
  186. if($message instanceof Typo3UpdateCommand) {
  187. return new Envelope(new Typo3UpdateCommand(1));
  188. }
  189. throw new AssertionError('unexpected message : ' . $message::class);
  190. });
  191. $previousParameters = $this->getMockBuilder(Parameters::class)->disableOriginalConstructor()->getMock();
  192. $previousParameters->method('getId')->willReturn(1);
  193. $previousParameters->method('getAverage')->willReturn(20);
  194. $previousParameters->expects(self::once())->method('getDesactivateOpentalentSiteWeb')->willReturn(true);
  195. $previousParameters->method('getCustomDomain')->willReturn(null);
  196. $context = $this->getMockBuilder(OnChangeContext::class)->disableOriginalConstructor()->getMock();
  197. $context->method('previousData')->willReturn($previousParameters);
  198. $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock();
  199. $organization->method('getId')->willReturn(1);
  200. $parameters = $this->getMockBuilder(Parameters::class)->disableOriginalConstructor()->getMock();
  201. $parameters->method('getId')->willReturn(1);
  202. $parameters->method('getOrganization')->willReturn($organization);
  203. $parameters->method('getDesactivateOpentalentSiteWeb')->willReturn(false);
  204. $parameters->method('getCustomDomain')->willReturn(null);
  205. $parameters->method('getAverage')->willReturn(20);
  206. $this->onParametersChange->onChange($parameters, $context);
  207. }
  208. /**
  209. * @see OnParametersChange::onAdvancedEducationNotationTypeChange()
  210. */
  211. public function testOnAdvancedEducationNotationTypeByTeachersChange(){
  212. $educationNotationConfig = new EducationNotationConfig();
  213. $educationCurriculum = new EducationCurriculum();
  214. $educationNotationConfig->addEducationCurriculum($educationCurriculum);
  215. $organization = new Organization();
  216. $organization->addEducationNotationConfig($educationNotationConfig);
  217. $this->parameters->setAdvancedEducationNotationType(AdvancedEducationNotationTypeEnum::BY_TEACHER()->getValue());
  218. $this->parameters->setOrganization($organization);
  219. $this->assertCount(1, $educationNotationConfig->getEducationCurriculums());
  220. $this->onParametersChange->onAdvancedEducationNotationTypeChange($this->parameters);
  221. $this->assertNull($educationNotationConfig->getEducationCurriculums()->first()->getEducationNotationConfig());
  222. }
  223. /**
  224. * @see OnParametersChange::onAdvancedEducationNotationTypeChange()
  225. */
  226. public function testOnAdvancedEducationNotationTypeByEducationChange(){
  227. $educationNotationConfig = new EducationNotationConfig();
  228. $teacher = new Access();
  229. $educationNotationConfig->addTeacher($teacher);
  230. $organization = new Organization();
  231. $organization->addEducationNotationConfig($educationNotationConfig);
  232. $this->parameters->setAdvancedEducationNotationType(AdvancedEducationNotationTypeEnum::BY_EDUCATION()->getValue());
  233. $this->parameters->setOrganization($organization);
  234. $this->assertCount(1, $educationNotationConfig->getTeachers());
  235. $this->onParametersChange->onAdvancedEducationNotationTypeChange($this->parameters);
  236. $this->assertNull($educationNotationConfig->getTeachers()->first()->getEducationNotationConfig());
  237. }
  238. /**
  239. * Un cours qui débute le 2/09/2022, si l'année musical passe du 05/09 au 01/09 alors le cours passe de l'année 2021/2022 à 2022/2023
  240. * @throws \Exception
  241. * @see OnParametersChange::onMusicalDateChange()
  242. */
  243. public function testOnMusicalDateChange(){
  244. $organization = new Organization();
  245. $this->parameters->setMusicalDate(new \DateTime('2022-09-01'));
  246. $organization->setParameters($this->parameters);
  247. $course = new Course();
  248. $course->setStartYear(2021);
  249. $course->setEndYear(2022);
  250. $course->setDatetimeStart(new \DateTime('2022-09-02'));
  251. $this->courseRepositoryMock
  252. ->method('getCoursesToFrom')
  253. ->willReturn([$course])
  254. ;
  255. $this->onParametersChange->onMusicalDateChange($organization, new \DateTime('2022-09-05'));
  256. $this->assertEquals(2022, $course->getStartYear());
  257. $this->assertEquals(2023, $course->getEndYear());
  258. }
  259. }