OnParametersChangeTest.php 22 KB

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