OnParametersChangeTest.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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 testOnChangeAverageChanged(): 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(AverageChange::class))
  170. ->willReturn(new Envelope(new AverageChange(1)));
  171. $previousParameters = $this->getMockBuilder(Parameters::class)->getMock();
  172. $previousParameters->method('getId')->willReturn(1);
  173. $previousParameters->expects(self::once())->method('getAverage')->willReturn(20);
  174. $previousParameters->method('getDesactivateOpentalentSiteWeb')->willReturn(false);
  175. $previousParameters->method('getCustomDomain')->willReturn(null);
  176. $context = $this->getMockBuilder(OnChangeContext::class)->disableOriginalConstructor()->getMock();
  177. $context->method('previousData')->willReturn($previousParameters);
  178. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  179. $parameters->method('getId')->willReturn(1);
  180. $parameters->method('getDesactivateOpentalentSiteWeb')->willReturn(false);
  181. $parameters->method('getCustomDomain')->willReturn(null);
  182. $parameters->expects(self::once())->method('getAverage')->willReturn(30);
  183. $onParametersChange->onChange($parameters, $context);
  184. }
  185. /**
  186. * @see OnParametersChange::onChange()
  187. */
  188. public function testOnChangeCustomDomainChanged(): void
  189. {
  190. $onParametersChange = $this
  191. ->getMockBuilder(OnParametersChange::class)
  192. ->setConstructorArgs([$this->courseRepository, $this->networkUtils, $this->organizationUtils, $this->messageBus])
  193. ->setMethodsExcept(['onChange'])
  194. ->getMock();
  195. $this->messageBus
  196. ->expects(self::once())
  197. ->method('dispatch')
  198. ->with(self::isInstanceOf(Typo3UpdateCommand::class))
  199. ->willReturn(new Envelope(new Typo3UpdateCommand(1)));
  200. $previousParameters = $this->getMockBuilder(Parameters::class)->getMock();
  201. $previousParameters->method('getId')->willReturn(1);
  202. $previousParameters->method('getAverage')->willReturn(20);
  203. $previousParameters->method('getDesactivateOpentalentSiteWeb')->willReturn(false);
  204. $previousParameters->expects(self::once())->method('getCustomDomain')->willReturn(null);
  205. $context = $this->getMockBuilder(OnChangeContext::class)->disableOriginalConstructor()->getMock();
  206. $context->method('previousData')->willReturn($previousParameters);
  207. $organization = $this->getMockBuilder(Organization::class)->getMock();
  208. $organization->method('getId')->willReturn(1);
  209. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  210. $parameters->method('getId')->willReturn(1);
  211. $parameters->method('getOrganization')->willReturn($organization);
  212. $parameters->method('getDesactivateOpentalentSiteWeb')->willReturn(false);
  213. $parameters->expects(self::once())->method('getCustomDomain')->willReturn('custom');
  214. $parameters->method('getAverage')->willReturn(20);
  215. $onParametersChange->onChange($parameters, $context);
  216. }
  217. /**
  218. * @see OnParametersChange::onChange()
  219. */
  220. public function testOnChangeWebsiteDisabled(): void
  221. {
  222. $onParametersChange = $this
  223. ->getMockBuilder(OnParametersChange::class)
  224. ->setConstructorArgs([$this->courseRepository, $this->networkUtils, $this->organizationUtils, $this->messageBus])
  225. ->setMethodsExcept(['onChange'])
  226. ->getMock();
  227. $this->messageBus
  228. ->expects(self::once())
  229. ->method('dispatch')
  230. ->with(self::isInstanceOf(Typo3DeleteCommand::class))
  231. ->willReturn(new Envelope(new Typo3DeleteCommand(1)));
  232. $previousParameters = $this->getMockBuilder(Parameters::class)->getMock();
  233. $previousParameters->method('getId')->willReturn(1);
  234. $previousParameters->method('getAverage')->willReturn(20);
  235. $previousParameters->expects(self::once())->method('getDesactivateOpentalentSiteWeb')->willReturn(false);
  236. $previousParameters->method('getCustomDomain')->willReturn(null);
  237. $context = $this->getMockBuilder(OnChangeContext::class)->disableOriginalConstructor()->getMock();
  238. $context->method('previousData')->willReturn($previousParameters);
  239. $organization = $this->getMockBuilder(Organization::class)->getMock();
  240. $organization->method('getId')->willReturn(1);
  241. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  242. $parameters->method('getId')->willReturn(1);
  243. $parameters->method('getOrganization')->willReturn($organization);
  244. $parameters->method('getDesactivateOpentalentSiteWeb')->willReturn(true);
  245. $parameters->method('getCustomDomain')->willReturn(null);
  246. $parameters->method('getAverage')->willReturn(20);
  247. $onParametersChange->onChange($parameters, $context);
  248. }
  249. /**
  250. * @see OnParametersChange::onChange()
  251. */
  252. public function testOnChangeWebsiteEnabled(): void
  253. {
  254. $onParametersChange = $this
  255. ->getMockBuilder(OnParametersChange::class)
  256. ->setConstructorArgs([$this->courseRepository, $this->networkUtils, $this->organizationUtils, $this->messageBus])
  257. ->setMethodsExcept(['onChange'])
  258. ->getMock();
  259. $this->messageBus
  260. ->expects(self::exactly(2))
  261. ->method('dispatch')
  262. ->willReturnCallback(function ($message) {
  263. if ($message instanceof Typo3UndeleteCommand) {
  264. return new Envelope(new Typo3UndeleteCommand(1));
  265. }
  266. if($message instanceof Typo3UpdateCommand) {
  267. return new Envelope(new Typo3UpdateCommand(1));
  268. }
  269. throw new AssertionError('unexpected message : ' . $message::class);
  270. });
  271. $previousParameters = $this->getMockBuilder(Parameters::class)->getMock();
  272. $previousParameters->method('getId')->willReturn(1);
  273. $previousParameters->method('getAverage')->willReturn(20);
  274. $previousParameters->expects(self::once())->method('getDesactivateOpentalentSiteWeb')->willReturn(true);
  275. $previousParameters->method('getCustomDomain')->willReturn(null);
  276. $context = $this->getMockBuilder(OnChangeContext::class)->disableOriginalConstructor()->getMock();
  277. $context->method('previousData')->willReturn($previousParameters);
  278. $organization = $this->getMockBuilder(Organization::class)->getMock();
  279. $organization->method('getId')->willReturn(1);
  280. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  281. $parameters->method('getId')->willReturn(1);
  282. $parameters->method('getOrganization')->willReturn($organization);
  283. $parameters->method('getDesactivateOpentalentSiteWeb')->willReturn(false);
  284. $parameters->method('getCustomDomain')->willReturn(null);
  285. $parameters->method('getAverage')->willReturn(20);
  286. $onParametersChange->onChange($parameters, $context);
  287. }
  288. /**
  289. * @see OnParametersChange::onAdvancedEducationNotationTypeChange()
  290. */
  291. public function testOnAdvancedEducationNotationTypeByTeachersChange(): void
  292. {
  293. $onParametersChange = $this
  294. ->getMockBuilder(OnParametersChange::class)
  295. ->setConstructorArgs([$this->courseRepository, $this->networkUtils, $this->organizationUtils, $this->messageBus])
  296. ->setMethodsExcept(['onAdvancedEducationNotationTypeChange'])
  297. ->getMock();
  298. $educationCurriculum = $this->getMockBuilder(EducationCurriculum::class)->getMock();
  299. $educationNotationConfig = $this->getMockBuilder(EducationNotationConfig::class)->getMock();
  300. $educationNotationConfig->method('getEducationCurriculums')->willReturn(new ArrayCollection([$educationCurriculum]));
  301. $organization = $this->getMockBuilder(Organization::class)->getMock();
  302. $organization->method('getEducationNotationConfigs')->willReturn(new ArrayCollection([$educationNotationConfig]));
  303. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  304. $parameters->method('getOrganization')->willReturn($organization);
  305. $parameters->method('getAdvancedEducationNotationType')->willReturn(AdvancedEducationNotationTypeEnum::BY_TEACHER()->getValue());
  306. $educationCurriculum->expects(self::once())->method('setEducationNotationConfig')->with(null);
  307. $onParametersChange->onAdvancedEducationNotationTypeChange($parameters);
  308. }
  309. /**
  310. * @see OnParametersChange::onAdvancedEducationNotationTypeChange()
  311. */
  312. public function testOnAdvancedEducationNotationTypeByEducationChange(): void
  313. {
  314. $onParametersChange = $this
  315. ->getMockBuilder(OnParametersChange::class)
  316. ->setConstructorArgs([$this->courseRepository, $this->networkUtils, $this->organizationUtils, $this->messageBus])
  317. ->setMethodsExcept(['onAdvancedEducationNotationTypeChange'])
  318. ->getMock();
  319. $teacher = $this->getMockBuilder(Access::class)->getMock();
  320. $educationNotationConfig = $this->getMockBuilder(EducationNotationConfig::class)->getMock();
  321. $educationNotationConfig->method('getTeachers')->willReturn(new ArrayCollection([$teacher]));
  322. $organization = $this->getMockBuilder(Organization::class)->getMock();
  323. $organization->method('getEducationNotationConfigs')->willReturn(new ArrayCollection([$educationNotationConfig]));
  324. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  325. $parameters->method('getOrganization')->willReturn($organization);
  326. $parameters->method('getAdvancedEducationNotationType')->willReturn(AdvancedEducationNotationTypeEnum::BY_EDUCATION()->getValue());
  327. $teacher->expects(self::once())->method('setEducationNotationConfig')->with(null);
  328. $onParametersChange->onAdvancedEducationNotationTypeChange($parameters);
  329. }
  330. /**
  331. * 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
  332. * @throws \Exception
  333. * @see OnParametersChange::onMusicalDateChange()
  334. */
  335. public function testOnMusicalDateChangeToPast(): void
  336. {
  337. $onParametersChange = $this
  338. ->getMockBuilder(OnParametersChange::class)
  339. ->setConstructorArgs([$this->courseRepository, $this->networkUtils, $this->organizationUtils, $this->messageBus])
  340. ->setMethodsExcept(['onMusicalDateChange'])
  341. ->getMock();
  342. $organization = $this->getMockBuilder(Organization::class)->getMock();
  343. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  344. $organization->method('getParameters')->willReturn($parameters);
  345. $parameters->method('getOrganization')->willReturn($organization);
  346. $parameters->method('getMusicalDate')->willReturn(new \DateTime('2022-09-01'));
  347. $startDate = new \DateTime('2022-09-02');
  348. $this->organizationUtils
  349. ->expects(self::once())
  350. ->method('getActivityYearSwitchDate')
  351. ->with($organization, $startDate)
  352. ->willReturn(2022);
  353. $course = $this->getMockBuilder(Course::class)->getMock();
  354. $course->method('getStartYear')->willReturn(2021);
  355. $course->method('getEndYear')->willReturn(2022);
  356. $course->method('getDatetimeStart')->willReturn($startDate);
  357. $this->courseRepository->method('getCoursesToFrom')->willReturn([$course]);
  358. $course->expects(self::once())->method('setStartYear')->with(2022);
  359. $course->expects(self::once())->method('setEndYear')->with(2023);
  360. $onParametersChange->onMusicalDateChange($parameters, new \DateTime('2022-09-05'));
  361. }
  362. /**
  363. * 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
  364. *
  365. * @throws \Exception
  366. * @see OnParametersChange::onMusicalDateChange()
  367. */
  368. public function testOnMusicalDateChangeToFuture(): void
  369. {
  370. $onParametersChange = $this
  371. ->getMockBuilder(OnParametersChange::class)
  372. ->setConstructorArgs([$this->courseRepository, $this->networkUtils, $this->organizationUtils, $this->messageBus])
  373. ->setMethodsExcept(['onMusicalDateChange'])
  374. ->getMock();
  375. $organization = $this->getMockBuilder(Organization::class)->getMock();
  376. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  377. $organization->method('getParameters')->willReturn($parameters);
  378. $parameters->method('getOrganization')->willReturn($organization);
  379. $parameters->method('getMusicalDate')->willReturn(new \DateTime('2022-09-05'));
  380. $startDate = new \DateTime('2022-09-02');
  381. $this->organizationUtils
  382. ->expects(self::once())
  383. ->method('getActivityYearSwitchDate')
  384. ->with($organization, $startDate)
  385. ->willReturn(2021);
  386. $course = $this->getMockBuilder(Course::class)->getMock();
  387. $course->method('getStartYear')->willReturn(2022);
  388. $course->method('getEndYear')->willReturn(2023);
  389. $course->method('getDatetimeStart')->willReturn($startDate);
  390. $this->courseRepository
  391. ->method('getCoursesToFrom')
  392. ->willReturn([$course])
  393. ;
  394. $course->expects(self::once())->method('setStartYear')->with(2021);
  395. $course->expects(self::once())->method('setEndYear')->with(2022);
  396. $onParametersChange->onMusicalDateChange($parameters, new \DateTime('2022-09-01'));
  397. }
  398. }