OnAccessChangeTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /** @noinspection PhpUnhandledExceptionInspection */
  3. namespace App\Tests\Unit\Service\OnChange\Access;
  4. use App\ApiResources\Profile\AccessProfile;
  5. use App\Entity\Access\Access;
  6. use App\Service\Access\AccessProfileCreator;
  7. use App\Service\MercureHub;
  8. use App\Service\OnChange\Access\OnAccessChange;
  9. use App\Service\OnChange\OnChangeContext;
  10. use PHPUnit\Framework\MockObject\MockObject;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Bundle\SecurityBundle\Security;
  13. use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken;
  14. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  15. class OnAccessChangeTest extends TestCase
  16. {
  17. private Security|MockObject $security;
  18. private AccessProfileCreator|MockObject $accessProfileCreator;
  19. private MercureHub|MockObject $mercureHub;
  20. public function setUp(): void
  21. {
  22. $this->security = $this->getMockBuilder(Security::class)->disableOriginalConstructor()->getMock();
  23. $this->accessProfileCreator = $this->getMockBuilder(AccessProfileCreator::class)->disableOriginalConstructor()->getMock();
  24. $this->mercureHub = $this->getMockBuilder(MercureHub::class)->disableOriginalConstructor()->getMock();
  25. }
  26. /**
  27. * @see OnAccessChange::onChange()
  28. */
  29. public function testOnChange(): void
  30. {
  31. $onAccessChange = $this->getMockBuilder(OnAccessChange::class)
  32. ->setConstructorArgs([$this->security, $this->accessProfileCreator, $this->mercureHub])
  33. ->setMethodsExcept(['onChange'])
  34. ->getMock();
  35. $access = $this->getMockBuilder(Access::class)->getMock();
  36. $context = $this->getMockBuilder(OnChangeContext::class)->disableOriginalConstructor()->getMock();
  37. // The onChange method should call publishNewProfile
  38. $onAccessChange->expects($this->once())
  39. ->method('publishNewProfile')
  40. ->with($access);
  41. $onAccessChange->onChange($access, $context);
  42. }
  43. /**
  44. * @see OnAccessChange::publishNewProfile()
  45. */
  46. public function testPublishNewProfileWithRegularToken(): void
  47. {
  48. $onAccessChange = $this->getMockBuilder(OnAccessChange::class)
  49. ->setConstructorArgs([$this->security, $this->accessProfileCreator, $this->mercureHub])
  50. ->setMethodsExcept(['publishNewProfile'])
  51. ->getMock();
  52. $access = $this->getMockBuilder(Access::class)->getMock();
  53. $access->method('getId')->willReturn(123);
  54. $token = $this->getMockBuilder(TokenInterface::class)->getMock();
  55. $this->security->expects($this->once())
  56. ->method('getToken')
  57. ->willReturn($token);
  58. $accessProfile = $this->getMockBuilder(AccessProfile::class)->disableOriginalConstructor()->getMock();
  59. $this->accessProfileCreator->expects($this->once())
  60. ->method('getAccessProfile')
  61. ->with($access, null)
  62. ->willReturn($accessProfile);
  63. $this->mercureHub->expects($this->once())
  64. ->method('publishUpdate')
  65. ->with(123, $accessProfile);
  66. $onAccessChange->publishNewProfile($access);
  67. }
  68. /**
  69. * @see OnAccessChange::publishNewProfile()
  70. */
  71. public function testPublishNewProfileWithSwitchUserToken(): void
  72. {
  73. $onAccessChange = $this->getMockBuilder(OnAccessChange::class)
  74. ->setConstructorArgs([$this->security, $this->accessProfileCreator, $this->mercureHub])
  75. ->setMethodsExcept(['publishNewProfile'])
  76. ->getMock();
  77. $access = $this->getMockBuilder(Access::class)->getMock();
  78. $access->method('getId')->willReturn(123);
  79. $originalAccess = $this->getMockBuilder(Access::class)->getMock();
  80. $originalToken = $this->getMockBuilder(TokenInterface::class)->getMock();
  81. $originalToken->method('getUser')->willReturn($originalAccess);
  82. $switchUserToken = $this->getMockBuilder(SwitchUserToken::class)
  83. ->disableOriginalConstructor()
  84. ->getMock();
  85. $switchUserToken->method('getOriginalToken')->willReturn($originalToken);
  86. $this->security->expects($this->once())
  87. ->method('getToken')
  88. ->willReturn($switchUserToken);
  89. $accessProfile = $this->getMockBuilder(AccessProfile::class)->disableOriginalConstructor()->getMock();
  90. $this->accessProfileCreator->expects($this->once())
  91. ->method('getAccessProfile')
  92. ->with($access, $originalAccess)
  93. ->willReturn($accessProfile);
  94. $this->mercureHub->expects($this->once())
  95. ->method('publishUpdate')
  96. ->with(123, $accessProfile);
  97. $onAccessChange->publishNewProfile($access);
  98. }
  99. }