OnParametersChangeTest.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. <?php
  2. /** @noinspection PhpUnhandledExceptionInspection */
  3. namespace App\Tests\Unit\Service\OnChange\Organization;
  4. use App\Entity\Access\Access;
  5. use App\Entity\Booking\Course;
  6. use App\Entity\Education\EducationCurriculum;
  7. use App\Entity\Education\EducationNotationConfig;
  8. use App\Entity\Organization\Organization;
  9. use App\Entity\Organization\Parameters;
  10. use App\Enum\Education\AdvancedEducationNotationTypeEnum;
  11. use App\Message\Command\Parameters\AverageChange;
  12. use App\Message\Command\Typo3\Typo3DeleteCommand;
  13. use App\Message\Command\Typo3\Typo3UndeleteCommand;
  14. use App\Message\Command\Typo3\Typo3UpdateCommand;
  15. use App\Repository\Booking\CourseRepository;
  16. use App\Service\Network\Utils as NetworkUtils;
  17. use App\Service\OnChange\OnChangeContext;
  18. use App\Service\OnChange\Organization\OnParametersChange;
  19. use App\Service\Organization\Utils as OrganizationUtils;
  20. use AssertionError;
  21. use Doctrine\Common\Collections\ArrayCollection;
  22. use PHPUnit\Framework\TestCase;
  23. use Symfony\Component\Messenger\Envelope;
  24. use Symfony\Component\Messenger\MessageBusInterface;
  25. class OnParametersChangeTest extends TestCase
  26. {
  27. private CourseRepository $courseRepository;
  28. private NetworkUtils $networkUtils;
  29. private MessageBusInterface $messageBus;
  30. private OrganizationUtils $organizationUtils;
  31. public function setUp(): void
  32. {
  33. $this->courseRepository = $this->getMockBuilder(CourseRepository::class)->disableOriginalConstructor()->getMock();
  34. $this->networkUtils = $this->getMockBuilder(NetworkUtils::class)->disableOriginalConstructor()->getMock();
  35. $this->organizationUtils = $this->getMockBuilder(OrganizationUtils::class)->disableOriginalConstructor()->getMock();
  36. $this->messageBus = $this->getMockBuilder(MessageBusInterface::class)->disableOriginalConstructor()->getMock();
  37. }
  38. /**
  39. * @see OnParametersChange::validate()
  40. */
  41. public function testValidateValid(): void
  42. {
  43. $onParametersChange = $this->getMockBuilder(OnParametersChange::class)
  44. ->setConstructorArgs([$this->courseRepository, $this->networkUtils, $this->organizationUtils, $this->messageBus])
  45. ->setMethodsExcept(['validate'])
  46. ->getMock();
  47. $context = $this->getMockBuilder(OnChangeContext::class)->disableOriginalConstructor()->getMock();
  48. $consecutiveParams = [
  49. [false, true], // Is CMF and site web enabled
  50. [true, false], // Is not CMF and site web disabled
  51. [false, false] // Is not CMF and site web enabled
  52. ];
  53. foreach ($consecutiveParams as $params) {
  54. $organization = $this->getMockBuilder(Organization::class)->getMock();
  55. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  56. $parameters->method('getOrganization')->willReturn($organization);
  57. $parameters->expects(self::once())->method('getDesactivateOpentalentSiteWeb')->willReturn($params[0]);
  58. $this->networkUtils = $this->getMockBuilder(NetworkUtils::class)->getMock();
  59. $this->networkUtils->method('isCMFAndActiveNow')->with($organization)->willReturn($params[1]);
  60. $onParametersChange->validate($parameters, $context);
  61. }
  62. }
  63. /**
  64. * @see OnParametersChange::validate()
  65. */
  66. public function testValidateInvalid(): void
  67. {
  68. $onParametersChange = $this->getMockBuilder(OnParametersChange::class)
  69. ->setConstructorArgs([$this->courseRepository, $this->networkUtils, $this->organizationUtils, $this->messageBus])
  70. ->setMethodsExcept(['validate'])
  71. ->getMock();
  72. $organization = $this->getMockBuilder(Organization::class)->getMock();
  73. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  74. $parameters->method('getOrganization')->willReturn($organization);
  75. $context = $this->getMockBuilder(OnChangeContext::class)->disableOriginalConstructor()->getMock();
  76. // Is CMF and site web disabled
  77. $parameters->expects(self::once())->method('getDesactivateOpentalentSiteWeb')->willReturn(true);
  78. $this->networkUtils->expects(self::once())->method('isCMFAndActiveNow')->with($organization)->willReturn(true);
  79. $this->expectException(\RuntimeException::class);
  80. $onParametersChange->validate($parameters, $context);
  81. }
  82. /**
  83. * @see OnParametersChange::beforeChange()
  84. */
  85. public function testBeforeChange(): void
  86. {
  87. $onParametersChange = $this
  88. ->getMockBuilder(OnParametersChange::class)
  89. ->setConstructorArgs([$this->courseRepository, $this->networkUtils, $this->organizationUtils, $this->messageBus])
  90. ->setMethodsExcept(['beforeChange'])
  91. ->getMock();
  92. $onParametersChange->expects(self::once())->method('onAdvancedEducationNotationTypeChange');
  93. $onParametersChange->expects(self::once())->method('onMusicalDateChange');
  94. $musicalDate = new \DateTime('2022-01-01');
  95. $previousParameters = $this->getMockBuilder(Parameters::class)->getMock();
  96. $previousParameters->method('getAdvancedEducationNotationType')->willReturn('BY_EDUCATION');
  97. $previousParameters->method('getMusicalDate')->willReturn($musicalDate);
  98. $context = $this->getMockBuilder(OnChangeContext::class)->disableOriginalConstructor()->getMock();
  99. $context->method('previousData')->willReturn($previousParameters);
  100. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  101. $parameters->method('getAdvancedEducationNotationType')->willReturn('SOMETHING_ELSE');
  102. $parameters->method('getMusicalDate')->willReturn(new \DateTime('2023-01-01'));
  103. // Both mocked methods should be called once here
  104. $onParametersChange->beforeChange($parameters, $context);
  105. }
  106. /**
  107. * @see OnParametersChange::beforeChange()
  108. */
  109. public function testBeforeChangeNoChange(): void
  110. {
  111. $onParametersChange = $this
  112. ->getMockBuilder(OnParametersChange::class)
  113. ->setConstructorArgs([$this->courseRepository, $this->networkUtils, $this->organizationUtils, $this->messageBus])
  114. ->setMethodsExcept(['beforeChange'])
  115. ->getMock();
  116. $onParametersChange->expects(self::never())->method('onAdvancedEducationNotationTypeChange');
  117. $onParametersChange->expects(self::never())->method('onMusicalDateChange');
  118. $musicalDate = new \DateTime('2022-01-01');
  119. $previousParameters = $this->getMockBuilder(Parameters::class)->getMock();
  120. $previousParameters->method('getAdvancedEducationNotationType')->willReturn('BY_EDUCATION');
  121. $previousParameters->method('getMusicalDate')->willReturn($musicalDate);
  122. $context = $this->getMockBuilder(OnChangeContext::class)->disableOriginalConstructor()->getMock();
  123. $context->method('previousData')->willReturn($previousParameters);
  124. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  125. $parameters->method('getId')->willReturn(1);
  126. $parameters->method('getAdvancedEducationNotationType')->willReturn('BY_EDUCATION');
  127. $parameters->method('getMusicalDate')->willReturn($musicalDate);
  128. // None of the mocked methods should be called again here
  129. $onParametersChange->beforeChange($parameters, $context);
  130. }
  131. /**
  132. * @see OnParametersChange::onChange()
  133. */
  134. public function testOnChangeNoChange(): void
  135. {
  136. $onParametersChange = $this
  137. ->getMockBuilder(OnParametersChange::class)
  138. ->setConstructorArgs([$this->courseRepository, $this->networkUtils, $this->organizationUtils, $this->messageBus])
  139. ->setMethodsExcept(['onChange'])
  140. ->getMock();
  141. $this->messageBus->expects($this->never())->method('dispatch');
  142. $previousParameters = $this->getMockBuilder(Parameters::class)->getMock();
  143. $previousParameters->method('getId')->willReturn(1);
  144. $previousParameters->method('getAverage')->willReturn(20);
  145. $previousParameters->method('getDesactivateOpentalentSiteWeb')->willReturn(false);
  146. $previousParameters->method('getCustomDomain')->willReturn(null);
  147. $context = $this->getMockBuilder(OnChangeContext::class)->disableOriginalConstructor()->getMock();
  148. $context->method('previousData')->willReturn($previousParameters);
  149. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  150. $parameters->method('getId')->willReturn(1);
  151. $parameters->method('getAverage')->willReturn(20);
  152. $parameters->method('getDesactivateOpentalentSiteWeb')->willReturn(false);
  153. $parameters->method('getCustomDomain')->willReturn(null);
  154. $onParametersChange->onChange($parameters, $context);
  155. }
  156. /**
  157. * @see OnParametersChange::onChange()
  158. */
  159. public function testOnChangeCustomDomainChanged(): void
  160. {
  161. $onParametersChange = $this
  162. ->getMockBuilder(OnParametersChange::class)
  163. ->setConstructorArgs([$this->courseRepository, $this->networkUtils, $this->organizationUtils, $this->messageBus])
  164. ->setMethodsExcept(['onChange'])
  165. ->getMock();
  166. $this->messageBus
  167. ->expects(self::once())
  168. ->method('dispatch')
  169. ->with(self::isInstanceOf(Typo3UpdateCommand::class))
  170. ->willReturn(new Envelope(new Typo3UpdateCommand(1)));
  171. $previousParameters = $this->getMockBuilder(Parameters::class)->getMock();
  172. $previousParameters->method('getId')->willReturn(1);
  173. $previousParameters->method('getAverage')->willReturn(20);
  174. $previousParameters->method('getDesactivateOpentalentSiteWeb')->willReturn(false);
  175. $previousParameters->expects(self::once())->method('getCustomDomain')->willReturn(null);
  176. $context = $this->getMockBuilder(OnChangeContext::class)->disableOriginalConstructor()->getMock();
  177. $context->method('previousData')->willReturn($previousParameters);
  178. $organization = $this->getMockBuilder(Organization::class)->getMock();
  179. $organization->method('getId')->willReturn(1);
  180. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  181. $parameters->method('getId')->willReturn(1);
  182. $parameters->method('getOrganization')->willReturn($organization);
  183. $parameters->method('getDesactivateOpentalentSiteWeb')->willReturn(false);
  184. $parameters->expects(self::once())->method('getCustomDomain')->willReturn('custom');
  185. $parameters->method('getAverage')->willReturn(20);
  186. $onParametersChange->onChange($parameters, $context);
  187. }
  188. /**
  189. * @see OnParametersChange::onChange()
  190. */
  191. public function testOnChangeWebsiteDisabled(): void
  192. {
  193. $onParametersChange = $this
  194. ->getMockBuilder(OnParametersChange::class)
  195. ->setConstructorArgs([$this->courseRepository, $this->networkUtils, $this->organizationUtils, $this->messageBus])
  196. ->setMethodsExcept(['onChange'])
  197. ->getMock();
  198. $this->messageBus
  199. ->expects(self::once())
  200. ->method('dispatch')
  201. ->with(self::isInstanceOf(Typo3DeleteCommand::class))
  202. ->willReturn(new Envelope(new Typo3DeleteCommand(1)));
  203. $previousParameters = $this->getMockBuilder(Parameters::class)->getMock();
  204. $previousParameters->method('getId')->willReturn(1);
  205. $previousParameters->method('getAverage')->willReturn(20);
  206. $previousParameters->expects(self::once())->method('getDesactivateOpentalentSiteWeb')->willReturn(false);
  207. $previousParameters->method('getCustomDomain')->willReturn(null);
  208. $context = $this->getMockBuilder(OnChangeContext::class)->disableOriginalConstructor()->getMock();
  209. $context->method('previousData')->willReturn($previousParameters);
  210. $organization = $this->getMockBuilder(Organization::class)->getMock();
  211. $organization->method('getId')->willReturn(1);
  212. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  213. $parameters->method('getId')->willReturn(1);
  214. $parameters->method('getOrganization')->willReturn($organization);
  215. $parameters->method('getDesactivateOpentalentSiteWeb')->willReturn(true);
  216. $parameters->method('getCustomDomain')->willReturn(null);
  217. $parameters->method('getAverage')->willReturn(20);
  218. $onParametersChange->onChange($parameters, $context);
  219. }
  220. /**
  221. * @see OnParametersChange::onChange()
  222. */
  223. public function testOnChangeWebsiteEnabled(): void
  224. {
  225. $onParametersChange = $this
  226. ->getMockBuilder(OnParametersChange::class)
  227. ->setConstructorArgs([$this->courseRepository, $this->networkUtils, $this->organizationUtils, $this->messageBus])
  228. ->setMethodsExcept(['onChange'])
  229. ->getMock();
  230. $this->messageBus
  231. ->expects(self::exactly(2))
  232. ->method('dispatch')
  233. ->willReturnCallback(function ($message) {
  234. if ($message instanceof Typo3UndeleteCommand) {
  235. return new Envelope(new Typo3UndeleteCommand(1));
  236. }
  237. if($message instanceof Typo3UpdateCommand) {
  238. return new Envelope(new Typo3UpdateCommand(1));
  239. }
  240. throw new AssertionError('unexpected message : ' . $message::class);
  241. });
  242. $previousParameters = $this->getMockBuilder(Parameters::class)->getMock();
  243. $previousParameters->method('getId')->willReturn(1);
  244. $previousParameters->method('getAverage')->willReturn(20);
  245. $previousParameters->expects(self::once())->method('getDesactivateOpentalentSiteWeb')->willReturn(true);
  246. $previousParameters->method('getCustomDomain')->willReturn(null);
  247. $context = $this->getMockBuilder(OnChangeContext::class)->disableOriginalConstructor()->getMock();
  248. $context->method('previousData')->willReturn($previousParameters);
  249. $organization = $this->getMockBuilder(Organization::class)->getMock();
  250. $organization->method('getId')->willReturn(1);
  251. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  252. $parameters->method('getId')->willReturn(1);
  253. $parameters->method('getOrganization')->willReturn($organization);
  254. $parameters->method('getDesactivateOpentalentSiteWeb')->willReturn(false);
  255. $parameters->method('getCustomDomain')->willReturn(null);
  256. $parameters->method('getAverage')->willReturn(20);
  257. $onParametersChange->onChange($parameters, $context);
  258. }
  259. /**
  260. * @see OnParametersChange::onAdvancedEducationNotationTypeChange()
  261. */
  262. public function testOnAdvancedEducationNotationTypeByTeachersChange(): void
  263. {
  264. $onParametersChange = $this
  265. ->getMockBuilder(OnParametersChange::class)
  266. ->setConstructorArgs([$this->courseRepository, $this->networkUtils, $this->organizationUtils, $this->messageBus])
  267. ->setMethodsExcept(['onAdvancedEducationNotationTypeChange'])
  268. ->getMock();
  269. $educationCurriculum = $this->getMockBuilder(EducationCurriculum::class)->getMock();
  270. $educationNotationConfig = $this->getMockBuilder(EducationNotationConfig::class)->getMock();
  271. $educationNotationConfig->method('getEducationCurriculums')->willReturn(new ArrayCollection([$educationCurriculum]));
  272. $organization = $this->getMockBuilder(Organization::class)->getMock();
  273. $organization->method('getEducationNotationConfigs')->willReturn(new ArrayCollection([$educationNotationConfig]));
  274. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  275. $parameters->method('getOrganization')->willReturn($organization);
  276. $parameters->method('getAdvancedEducationNotationType')->willReturn(AdvancedEducationNotationTypeEnum::BY_TEACHER()->getValue());
  277. $educationCurriculum->expects(self::once())->method('setEducationNotationConfig')->with(null);
  278. $onParametersChange->onAdvancedEducationNotationTypeChange($parameters);
  279. }
  280. /**
  281. * @see OnParametersChange::onAdvancedEducationNotationTypeChange()
  282. */
  283. public function testOnAdvancedEducationNotationTypeByEducationChange(): void
  284. {
  285. $onParametersChange = $this
  286. ->getMockBuilder(OnParametersChange::class)
  287. ->setConstructorArgs([$this->courseRepository, $this->networkUtils, $this->organizationUtils, $this->messageBus])
  288. ->setMethodsExcept(['onAdvancedEducationNotationTypeChange'])
  289. ->getMock();
  290. $teacher = $this->getMockBuilder(Access::class)->getMock();
  291. $educationNotationConfig = $this->getMockBuilder(EducationNotationConfig::class)->getMock();
  292. $educationNotationConfig->method('getTeachers')->willReturn(new ArrayCollection([$teacher]));
  293. $organization = $this->getMockBuilder(Organization::class)->getMock();
  294. $organization->method('getEducationNotationConfigs')->willReturn(new ArrayCollection([$educationNotationConfig]));
  295. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  296. $parameters->method('getOrganization')->willReturn($organization);
  297. $parameters->method('getAdvancedEducationNotationType')->willReturn(AdvancedEducationNotationTypeEnum::BY_EDUCATION()->getValue());
  298. $teacher->expects(self::once())->method('setEducationNotationConfig')->with(null);
  299. $onParametersChange->onAdvancedEducationNotationTypeChange($parameters);
  300. }
  301. /**
  302. * Un cours qui débute le 02/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
  303. * @throws \Exception
  304. * @see OnParametersChange::onMusicalDateChange()
  305. */
  306. public function testOnMusicalDateChangeToPast(): void
  307. {
  308. $onParametersChange = $this
  309. ->getMockBuilder(OnParametersChange::class)
  310. ->setConstructorArgs([$this->courseRepository, $this->networkUtils, $this->organizationUtils, $this->messageBus])
  311. ->setMethodsExcept(['onMusicalDateChange'])
  312. ->getMock();
  313. $organization = $this->getMockBuilder(Organization::class)->getMock();
  314. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  315. $organization->method('getParameters')->willReturn($parameters);
  316. $parameters->method('getOrganization')->willReturn($organization);
  317. $parameters->method('getMusicalDate')->willReturn(new \DateTime('2022-09-01'));
  318. $startDate = new \DateTime('2022-09-02');
  319. $this->organizationUtils
  320. ->expects(self::once())
  321. ->method('getActivityYearSwitchDate')
  322. ->with($organization, $startDate)
  323. ->willReturn(2022);
  324. $course = $this->getMockBuilder(Course::class)->getMock();
  325. $course->method('getStartYear')->willReturn(2021);
  326. $course->method('getEndYear')->willReturn(2022);
  327. $course->method('getDatetimeStart')->willReturn($startDate);
  328. $this->courseRepository->method('getCoursesToFrom')->willReturn([$course]);
  329. $course->expects(self::once())->method('setStartYear')->with(2022);
  330. $course->expects(self::once())->method('setEndYear')->with(2023);
  331. $onParametersChange->onMusicalDateChange($parameters, new \DateTime('2022-09-05'));
  332. }
  333. /**
  334. * Un cours qui débute le 02/09/2022, si l'année musical passe du 01/09 au 05/09 alors le cours passe de l'année 2022/2023 à 2021/2022
  335. *
  336. * @throws \Exception
  337. * @see OnParametersChange::onMusicalDateChange()
  338. */
  339. public function testOnMusicalDateChangeToFuture(): void
  340. {
  341. $onParametersChange = $this
  342. ->getMockBuilder(OnParametersChange::class)
  343. ->setConstructorArgs([$this->courseRepository, $this->networkUtils, $this->organizationUtils, $this->messageBus])
  344. ->setMethodsExcept(['onMusicalDateChange'])
  345. ->getMock();
  346. $organization = $this->getMockBuilder(Organization::class)->getMock();
  347. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  348. $organization->method('getParameters')->willReturn($parameters);
  349. $parameters->method('getOrganization')->willReturn($organization);
  350. $parameters->method('getMusicalDate')->willReturn(new \DateTime('2022-09-05'));
  351. $startDate = new \DateTime('2022-09-02');
  352. $this->organizationUtils
  353. ->expects(self::once())
  354. ->method('getActivityYearSwitchDate')
  355. ->with($organization, $startDate)
  356. ->willReturn(2021);
  357. $course = $this->getMockBuilder(Course::class)->getMock();
  358. $course->method('getStartYear')->willReturn(2022);
  359. $course->method('getEndYear')->willReturn(2023);
  360. $course->method('getDatetimeStart')->willReturn($startDate);
  361. $this->courseRepository
  362. ->method('getCoursesToFrom')
  363. ->willReturn([$course])
  364. ;
  365. $course->expects(self::once())->method('setStartYear')->with(2021);
  366. $course->expects(self::once())->method('setEndYear')->with(2022);
  367. $onParametersChange->onMusicalDateChange($parameters, new \DateTime('2022-09-01'));
  368. }
  369. }