OnParametersChangeTest.php 21 KB

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