Explorar el Código

add unittests for OnParametersChange (validate, onChange, beforeChange)

Olivier Massot hace 3 años
padre
commit
132163c73f

+ 2 - 2
src/Service/OnChange/Organization/OnParametersChange.php

@@ -43,7 +43,7 @@ class OnParametersChange extends OnChangeDefault
     /**
      * @param Parameters $parameters
      */
-    public function prePersist($parameters, OnChangeContext $context): void{
+    public function beforeChange($parameters, OnChangeContext $context): void{
         if(
             $context->previousData() &&
             $context->previousData()->getAdvancedEducationNotationType() !== $parameters->getAdvancedEducationNotationType()
@@ -66,7 +66,7 @@ class OnParametersChange extends OnChangeDefault
     /**
      * @param Parameters $parameters
      */
-    public function postPersist($parameters, OnChangeContext $context): void{
+    public function onChange($parameters, OnChangeContext $context): void{
         //La note maximale du suivi pédagogique change
         if(
             $context->previousData() &&

+ 217 - 0
tests/Service/OnChange/Organization/OnParametersChangeTest.php

@@ -8,9 +8,17 @@ 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\Organization\OnChangeContext;
 use App\Service\OnChange\Organization\OnParametersChange;
+use AssertionError;
 use PHPUnit\Framework\TestCase;
+use Prophecy\Argument;
+use Symfony\Component\Messenger\Envelope;
 use Symfony\Component\Messenger\MessageBusInterface;
 
 class OnParametersChangeTest extends TestCase
@@ -34,6 +42,215 @@ class OnParametersChangeTest extends TestCase
         );
     }
 
+    public function testValidate() {
+        $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() {
+        $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() {
+        $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() {
+        $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() {
+        $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() {
+        $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() {
+        $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 testOnChangeWebsiteReenabled(): 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()
      */