Explorar o código

add unit tests for OnAccessChange

Olivier Massot hai 6 meses
pai
achega
41795b63eb
Modificáronse 1 ficheiros con 126 adicións e 0 borrados
  1. 126 0
      tests/Unit/Service/OnChange/Access/OnAccessChangeTest.php

+ 126 - 0
tests/Unit/Service/OnChange/Access/OnAccessChangeTest.php

@@ -0,0 +1,126 @@
+<?php
+
+/** @noinspection PhpUnhandledExceptionInspection */
+
+namespace App\Tests\Unit\Service\OnChange\Access;
+
+use App\ApiResources\Profile\AccessProfile;
+use App\Entity\Access\Access;
+use App\Service\Access\AccessProfileCreator;
+use App\Service\MercureHub;
+use App\Service\OnChange\OnChangeContext;
+use App\Service\OnChange\Access\OnAccessChange;
+use PHPUnit\Framework\MockObject\MockObject;
+use PHPUnit\Framework\TestCase;
+use Symfony\Bundle\SecurityBundle\Security;
+use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken;
+use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
+
+class OnAccessChangeTest extends TestCase
+{
+    private Security | MockObject $security;
+    private AccessProfileCreator | MockObject $accessProfileCreator;
+    private MercureHub | MockObject $mercureHub;
+
+    public function setUp(): void
+    {
+        $this->security = $this->getMockBuilder(Security::class)->disableOriginalConstructor()->getMock();
+        $this->accessProfileCreator = $this->getMockBuilder(AccessProfileCreator::class)->disableOriginalConstructor()->getMock();
+        $this->mercureHub = $this->getMockBuilder(MercureHub::class)->disableOriginalConstructor()->getMock();
+    }
+
+    /**
+     * @see OnAccessChange::onChange()
+     */
+    public function testOnChange(): void
+    {
+        $onAccessChange = $this->getMockBuilder(OnAccessChange::class)
+            ->setConstructorArgs([$this->security, $this->accessProfileCreator, $this->mercureHub])
+            ->setMethodsExcept(['onChange'])
+            ->getMock();
+
+        $access = $this->getMockBuilder(Access::class)->getMock();
+        $context = $this->getMockBuilder(OnChangeContext::class)->disableOriginalConstructor()->getMock();
+
+        // The onChange method should call publishNewProfile
+        $onAccessChange->expects($this->once())
+            ->method('publishNewProfile')
+            ->with($access);
+
+        $onAccessChange->onChange($access, $context);
+    }
+
+    /**
+     * @see OnAccessChange::publishNewProfile()
+     */
+    public function testPublishNewProfileWithRegularToken(): void
+    {
+        $onAccessChange = $this->getMockBuilder(OnAccessChange::class)
+            ->setConstructorArgs([$this->security, $this->accessProfileCreator, $this->mercureHub])
+            ->setMethodsExcept(['publishNewProfile'])
+            ->getMock();
+
+        $access = $this->getMockBuilder(Access::class)->getMock();
+        $access->method('getId')->willReturn(123);
+
+        $token = $this->getMockBuilder(TokenInterface::class)->getMock();
+
+        $this->security->expects($this->once())
+            ->method('getToken')
+            ->willReturn($token);
+
+        $accessProfile = $this->getMockBuilder(AccessProfile::class)->disableOriginalConstructor()->getMock();
+
+        $this->accessProfileCreator->expects($this->once())
+            ->method('getAccessProfile')
+            ->with($access, null)
+            ->willReturn($accessProfile);
+
+        $this->mercureHub->expects($this->once())
+            ->method('publishUpdate')
+            ->with(123, $accessProfile);
+
+        $onAccessChange->publishNewProfile($access);
+    }
+
+    /**
+     * @see OnAccessChange::publishNewProfile()
+     */
+    public function testPublishNewProfileWithSwitchUserToken(): void
+    {
+        $onAccessChange = $this->getMockBuilder(OnAccessChange::class)
+            ->setConstructorArgs([$this->security, $this->accessProfileCreator, $this->mercureHub])
+            ->setMethodsExcept(['publishNewProfile'])
+            ->getMock();
+
+        $access = $this->getMockBuilder(Access::class)->getMock();
+        $access->method('getId')->willReturn(123);
+
+        $originalAccess = $this->getMockBuilder(Access::class)->getMock();
+
+        $originalToken = $this->getMockBuilder(TokenInterface::class)->getMock();
+        $originalToken->method('getUser')->willReturn($originalAccess);
+
+        $switchUserToken = $this->getMockBuilder(SwitchUserToken::class)
+            ->disableOriginalConstructor()
+            ->getMock();
+        $switchUserToken->method('getOriginalToken')->willReturn($originalToken);
+
+        $this->security->expects($this->once())
+            ->method('getToken')
+            ->willReturn($switchUserToken);
+
+        $accessProfile = $this->getMockBuilder(AccessProfile::class)->disableOriginalConstructor()->getMock();
+
+        $this->accessProfileCreator->expects($this->once())
+            ->method('getAccessProfile')
+            ->with($access, $originalAccess)
+            ->willReturn($accessProfile);
+
+        $this->mercureHub->expects($this->once())
+            ->method('publishUpdate')
+            ->with(123, $accessProfile);
+
+        $onAccessChange->publishNewProfile($access);
+    }
+}