| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- <?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\Access\OnAccessChange;
- use App\Service\OnChange\OnChangeContext;
- 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);
- }
- }
|