| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365 |
- <?php
- namespace App\Test\Service\OnChange\Organization;
- use App\Entity\Access\Access;
- use App\Entity\Booking\Course;
- use App\Entity\Education\EducationCurriculum;
- use App\Entity\Education\EducationNotationConfig;
- use App\Entity\Organization\Organization;
- use App\Entity\Organization\Parameters;
- use App\Enum\Education\AdvancedEducationNotationTypeEnum;
- use App\Message\Command\Parameters\AverageChange;
- use App\Message\Command\Typo3\Typo3DeleteCommand;
- use App\Message\Command\Typo3\Typo3UndeleteCommand;
- use App\Message\Command\Typo3\Typo3UpdateCommand;
- use App\Repository\Booking\CourseRepository;
- use App\Service\OnChange\OnChangeContext;
- use App\Service\OnChange\Organization\OnParametersChange;
- use AssertionError;
- use PHPUnit\Framework\TestCase;
- use Symfony\Component\Messenger\Envelope;
- use Symfony\Component\Messenger\MessageBusInterface;
- class OnParametersChangeTest extends TestCase
- {
- private Parameters $parameters;
- private OnParametersChange $onParametersChange;
- private CourseRepository $courseRepositoryMock;
- private \App\Service\Network\Utils $networkUtils;
- private MessageBusInterface $messageBus;
- private \App\Service\Organization\Utils $organizationUtils;
- public function setUp(): void
- {
- $this->courseRepositoryMock = $this->getMockBuilder(CourseRepository::class)->disableOriginalConstructor()->getMock();
- $this->networkUtils = $this->getMockBuilder(\App\Service\Network\Utils::class)->disableOriginalConstructor()->getMock();
- $this->organizationUtils = $this->getMockBuilder(\App\Service\Organization\Utils::class)->disableOriginalConstructor()->getMock();
- $this->messageBus = $this->getMockBuilder(MessageBusInterface::class)->disableOriginalConstructor()->getMock();
- $this->parameters = new Parameters();
- $this->onParametersChange = new OnParametersChange(
- $this->courseRepositoryMock,
- $this->networkUtils,
- $this->organizationUtils,
- $this->messageBus
- );
- }
- public function testValidate(): void
- {
- $context = $this->getMockBuilder(OnChangeContext::class)->disableOriginalConstructor()->getMock();
- // 1. Is CMF and site web enabled ; 2. Is not CMF and site web disabled ; 3. Is not CMF and site web enabled
- foreach ([[false, true], [true, false], [false, false]] as $params) {
- $parameters = $this->getMockBuilder(Parameters::class)->disableOriginalConstructor()->getMock();
- $parameters->expects(self::once())->method('getDesactivateOpentalentSiteWeb')->willReturn($params[0]);
- $this->networkUtils = $this->getMockBuilder(\App\Service\Network\Utils::class)->disableOriginalConstructor()->getMock();
- $this->networkUtils->method('isCMFAndActiveNow')->willReturn($params[1]);
- $this->onParametersChange->validate($parameters, $context);
- }
- }
- public function testValidateInvalid(): void
- {
- $parameters = $this->getMockBuilder(Parameters::class)->disableOriginalConstructor()->getMock();
- $context = $this->getMockBuilder(OnChangeContext::class)->disableOriginalConstructor()->getMock();
- // Is CMF and site web disabled
- $parameters->expects(self::once())->method('getDesactivateOpentalentSiteWeb')->willReturn(true);
- $this->networkUtils->expects(self::once())->method('isCMFAndActiveNow')->willReturn(true);
- try {
- $this->onParametersChange->validate($parameters, $context);
- throw new AssertionError('OnParametersChange::validate should have thrown an error');
- } catch (\RuntimeException) {}
- }
- public function testBeforeChange(): void
- {
- $onParametersChange = $this
- ->getMockBuilder(OnParametersChange::class)
- ->onlyMethods(['onAdvancedEducationNotationTypeChange', 'onMusicalDateChange'])
- ->disableOriginalConstructor()
- ->getMock();
- $onParametersChange
- ->expects(self::once())
- ->method('onAdvancedEducationNotationTypeChange')
- ->willReturnSelf();
- $onParametersChange
- ->expects(self::once())
- ->method('onMusicalDateChange')
- ->willReturnSelf();
- $previousParameters = $this->getMockBuilder(Parameters::class)->disableOriginalConstructor()->getMock();
- $previousParameters->method('getAdvancedEducationNotationType')->willReturn('BY_EDUCATION');
- $musicalDate = new \DateTime('2022-01-01');
- $previousParameters->method('getMusicalDate')->willReturn($musicalDate);
- $context = $this->getMockBuilder(OnChangeContext::class)->disableOriginalConstructor()->getMock();
- $context->method('previousData')->willReturn($previousParameters);
- $parameters = $this->getMockBuilder(Parameters::class)->disableOriginalConstructor()->getMock();
- $parameters->method('getAdvancedEducationNotationType')->willReturn('SOMETHING_ELSE');
- $parameters->method('getMusicalDate')->willReturn(new \DateTime('2023-01-01'));
- // Both mocked methods should be called once here
- $onParametersChange->beforeChange($parameters, $context);
- $parameters = $this->getMockBuilder(Parameters::class)->disableOriginalConstructor()->getMock();
- $parameters->method('getId')->willReturn(1);
- $parameters->method('getAdvancedEducationNotationType')->willReturn('BY_EDUCATION');
- $parameters->method('getMusicalDate')->willReturn($musicalDate);
- // None of the mocked methods should be called again here
- $onParametersChange->beforeChange($parameters, $context);
- }
- public function testOnChangeNoChange(): void
- {
- $this->messageBus->expects($this->never())->method('dispatch');
- $previousParameters = $this->getMockBuilder(Parameters::class)->disableOriginalConstructor()->getMock();
- $previousParameters->method('getId')->willReturn(1);
- $previousParameters->expects(self::once())->method('getAverage')->willReturn(20);
- $previousParameters->expects(self::once())->method('getDesactivateOpentalentSiteWeb')->willReturn(false);
- $previousParameters->expects(self::once())->method('getCustomDomain')->willReturn(null);
- $context = $this->getMockBuilder(OnChangeContext::class)->disableOriginalConstructor()->getMock();
- $context->method('previousData')->willReturn($previousParameters);
- $parameters = $this->getMockBuilder(Parameters::class)->disableOriginalConstructor()->getMock();
- $parameters->method('getId')->willReturn(1);
- $parameters->method('getAverage')->willReturn(20);
- $parameters->method('getDesactivateOpentalentSiteWeb')->willReturn(false);
- $parameters->method('getCustomDomain')->willReturn(null);
- $this->onParametersChange->onChange($parameters, $context);
- }
- public function testOnChangeAverageChanged(): void
- {
- $this->messageBus
- ->expects(self::once())
- ->method('dispatch')
- ->with(self::isInstanceOf(AverageChange::class))
- ->willReturn(new Envelope(new AverageChange(1)));
- $previousParameters = $this->getMockBuilder(Parameters::class)->disableOriginalConstructor()->getMock();
- $previousParameters->method('getId')->willReturn(1);
- $previousParameters->expects(self::once())->method('getAverage')->willReturn(20);
- $previousParameters->method('getDesactivateOpentalentSiteWeb')->willReturn(false);
- $previousParameters->method('getCustomDomain')->willReturn(null);
- $context = $this->getMockBuilder(OnChangeContext::class)->disableOriginalConstructor()->getMock();
- $context->method('previousData')->willReturn($previousParameters);
- $parameters = $this->getMockBuilder(Parameters::class)->disableOriginalConstructor()->getMock();
- $parameters->method('getId')->willReturn(1);
- $parameters->method('getDesactivateOpentalentSiteWeb')->willReturn(false);
- $parameters->method('getCustomDomain')->willReturn(null);
- $parameters->expects(self::once())->method('getAverage')->willReturn(30);
- $this->onParametersChange->onChange($parameters, $context);
- }
- public function testOnChangeCustomDomainChanged(): void
- {
- $this->messageBus
- ->expects(self::once())
- ->method('dispatch')
- ->with(self::isInstanceOf(Typo3UpdateCommand::class))
- ->willReturn(new Envelope(new Typo3UpdateCommand(1)));
- $previousParameters = $this->getMockBuilder(Parameters::class)->disableOriginalConstructor()->getMock();
- $previousParameters->method('getId')->willReturn(1);
- $previousParameters->method('getAverage')->willReturn(20);
- $previousParameters->method('getDesactivateOpentalentSiteWeb')->willReturn(false);
- $previousParameters->expects(self::once())->method('getCustomDomain')->willReturn(null);
- $context = $this->getMockBuilder(OnChangeContext::class)->disableOriginalConstructor()->getMock();
- $context->method('previousData')->willReturn($previousParameters);
- $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock();
- $organization->method('getId')->willReturn(1);
- $parameters = $this->getMockBuilder(Parameters::class)->disableOriginalConstructor()->getMock();
- $parameters->method('getId')->willReturn(1);
- $parameters->method('getOrganization')->willReturn($organization);
- $parameters->method('getDesactivateOpentalentSiteWeb')->willReturn(false);
- $parameters->expects(self::once())->method('getCustomDomain')->willReturn('custom');
- $parameters->method('getAverage')->willReturn(20);
- $this->onParametersChange->onChange($parameters, $context);
- }
- public function testOnChangeWebsiteDisabled(): void
- {
- $this->messageBus
- ->expects(self::once())
- ->method('dispatch')
- ->with(self::isInstanceOf(Typo3DeleteCommand::class))
- ->willReturn(new Envelope(new Typo3DeleteCommand(1)));
- $previousParameters = $this->getMockBuilder(Parameters::class)->disableOriginalConstructor()->getMock();
- $previousParameters->method('getId')->willReturn(1);
- $previousParameters->method('getAverage')->willReturn(20);
- $previousParameters->expects(self::once())->method('getDesactivateOpentalentSiteWeb')->willReturn(false);
- $previousParameters->method('getCustomDomain')->willReturn(null);
- $context = $this->getMockBuilder(OnChangeContext::class)->disableOriginalConstructor()->getMock();
- $context->method('previousData')->willReturn($previousParameters);
- $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock();
- $organization->method('getId')->willReturn(1);
- $parameters = $this->getMockBuilder(Parameters::class)->disableOriginalConstructor()->getMock();
- $parameters->method('getId')->willReturn(1);
- $parameters->method('getOrganization')->willReturn($organization);
- $parameters->method('getDesactivateOpentalentSiteWeb')->willReturn(true);
- $parameters->method('getCustomDomain')->willReturn(null);
- $parameters->method('getAverage')->willReturn(20);
- $this->onParametersChange->onChange($parameters, $context);
- }
- public function testOnChangeWebsiteEnabled(): void
- {
- $this->messageBus
- ->expects(self::exactly(2))
- ->method('dispatch')
- ->willReturnCallback(function ($message, $stamps = []) {
- if ($message instanceof Typo3UndeleteCommand) {
- return new Envelope(new Typo3UndeleteCommand(1));
- }
- if($message instanceof Typo3UpdateCommand) {
- return new Envelope(new Typo3UpdateCommand(1));
- }
- throw new AssertionError('unexpected message : ' . $message::class);
- });
- $previousParameters = $this->getMockBuilder(Parameters::class)->disableOriginalConstructor()->getMock();
- $previousParameters->method('getId')->willReturn(1);
- $previousParameters->method('getAverage')->willReturn(20);
- $previousParameters->expects(self::once())->method('getDesactivateOpentalentSiteWeb')->willReturn(true);
- $previousParameters->method('getCustomDomain')->willReturn(null);
- $context = $this->getMockBuilder(OnChangeContext::class)->disableOriginalConstructor()->getMock();
- $context->method('previousData')->willReturn($previousParameters);
- $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock();
- $organization->method('getId')->willReturn(1);
- $parameters = $this->getMockBuilder(Parameters::class)->disableOriginalConstructor()->getMock();
- $parameters->method('getId')->willReturn(1);
- $parameters->method('getOrganization')->willReturn($organization);
- $parameters->method('getDesactivateOpentalentSiteWeb')->willReturn(false);
- $parameters->method('getCustomDomain')->willReturn(null);
- $parameters->method('getAverage')->willReturn(20);
- $this->onParametersChange->onChange($parameters, $context);
- }
- /**
- * @see OnParametersChange::onAdvancedEducationNotationTypeChange()
- */
- public function testOnAdvancedEducationNotationTypeByTeachersChange(): void
- {
- $educationNotationConfig = new EducationNotationConfig();
- $educationCurriculum = new EducationCurriculum();
- $educationNotationConfig->addEducationCurriculum($educationCurriculum);
- $organization = new Organization();
- $organization->addEducationNotationConfig($educationNotationConfig);
- $this->parameters->setAdvancedEducationNotationType(AdvancedEducationNotationTypeEnum::BY_TEACHER()->getValue());
- $this->parameters->setOrganization($organization);
- $this->assertCount(1, $educationNotationConfig->getEducationCurriculums());
- $this->onParametersChange->onAdvancedEducationNotationTypeChange($this->parameters);
- $this->assertNull($educationNotationConfig->getEducationCurriculums()->first()->getEducationNotationConfig());
- }
- /**
- * @see OnParametersChange::onAdvancedEducationNotationTypeChange()
- */
- public function testOnAdvancedEducationNotationTypeByEducationChange(): void
- {
- $educationNotationConfig = new EducationNotationConfig();
- $teacher = new Access();
- $educationNotationConfig->addTeacher($teacher);
- $organization = new Organization();
- $organization->addEducationNotationConfig($educationNotationConfig);
- $this->parameters->setAdvancedEducationNotationType(AdvancedEducationNotationTypeEnum::BY_EDUCATION()->getValue());
- $this->parameters->setOrganization($organization);
- $this->assertCount(1, $educationNotationConfig->getTeachers());
- $this->onParametersChange->onAdvancedEducationNotationTypeChange($this->parameters);
- $this->assertNull($educationNotationConfig->getTeachers()->first()->getEducationNotationConfig());
- }
- /**
- * 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
- * @throws \Exception
- * @see OnParametersChange::onMusicalDateChange()
- */
- public function testOnMusicalDateChangeToPast(): void
- {
- $this->parameters->setMusicalDate(new \DateTime('2022-09-01'));
- $organization = new Organization();
- $this->parameters->setOrganization($organization);
- $organization->setParameters($this->parameters);
- $this->organizationUtils->expects(self::once())->method('getActivityYearSwitchDate')->willReturn(2022);
- $course = new Course();
- $course->setStartYear(2021);
- $course->setEndYear(2022);
- $course->setDatetimeStart(new \DateTime('2022-09-02'));
- $this->courseRepositoryMock
- ->method('getCoursesToFrom')
- ->willReturn([$course])
- ;
- $this->onParametersChange->onMusicalDateChange($this->parameters, new \DateTime('2022-09-05'));
- $this->assertEquals(2022, $course->getStartYear());
- $this->assertEquals(2023, $course->getEndYear());
- }
- /**
- * 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
- *
- * @throws \Exception
- * @see OnParametersChange::onMusicalDateChange()
- */
- public function testOnMusicalDateChangeToFuture(): void
- {
- $this->parameters->setMusicalDate(new \DateTime('2022-09-05'));
- $organization = new Organization();
- $this->parameters->setOrganization($organization);
- $organization->setParameters($this->parameters);
- $this->organizationUtils->expects(self::once())->method('getActivityYearSwitchDate')->willReturn(2021);
- $course = new Course();
- $course->setStartYear(2022);
- $course->setEndYear(2023);
- $course->setDatetimeStart(new \DateTime('2022-09-02'));
- $this->courseRepositoryMock
- ->method('getCoursesToFrom')
- ->willReturn([$course])
- ;
- $this->onParametersChange->onMusicalDateChange($this->parameters, new \DateTime('2022-09-01'));
- $this->assertEquals(2021, $course->getStartYear());
- $this->assertEquals(2022, $course->getEndYear());
- }
- }
|