|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Bugsnag\BugsnagBundle\Tests\Callbacks; |
| 4 | + |
| 5 | +use Bugsnag\BugsnagBundle\Callbacks\UserSettingCallback; |
| 6 | +use Bugsnag\Report; |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | +use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
| 9 | +use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
| 10 | +use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; |
| 11 | +use Symfony\Component\Security\Core\User\User; |
| 12 | + |
| 13 | +class UserSettingCallbackTest extends TestCase |
| 14 | +{ |
| 15 | + public function testUserNotSetWhenDisabled() |
| 16 | + { |
| 17 | + $tokenStorageMock = $this->getMockBuilder(TokenStorageInterface::class) |
| 18 | + ->getMock(); |
| 19 | + |
| 20 | + $authorizationChecker = $this->getMockBuilder(AuthorizationCheckerInterface::class) |
| 21 | + ->getMock(); |
| 22 | + |
| 23 | + $reportMock = $this->getBugsnagReportMock(); |
| 24 | + $reportMock |
| 25 | + ->expects($this->never()) |
| 26 | + ->method('setUser'); |
| 27 | + |
| 28 | + $callback = new UserSettingCallback($tokenStorageMock, $authorizationChecker, false); |
| 29 | + $callback->registerCallback($reportMock); |
| 30 | + } |
| 31 | + |
| 32 | + public function testUserNotSetWhenServicesNotPassed() |
| 33 | + { |
| 34 | + $reportMock = $this->getBugsnagReportMock(); |
| 35 | + $reportMock |
| 36 | + ->expects($this->never()) |
| 37 | + ->method('setUser'); |
| 38 | + |
| 39 | + $callback = new UserSettingCallback(null, null, true); |
| 40 | + $callback->registerCallback($reportMock); |
| 41 | + } |
| 42 | + |
| 43 | + public function testUserIsSetWhenLoggedIn() |
| 44 | + { |
| 45 | + $user = new User('example', 'example'); |
| 46 | + |
| 47 | + $tokenMock = $this->getMockBuilder(TokenInterface::class) |
| 48 | + ->getMock(); |
| 49 | + |
| 50 | + $tokenMock |
| 51 | + ->expects($this->once()) |
| 52 | + ->method('getUser') |
| 53 | + ->willReturn($user); |
| 54 | + |
| 55 | + $tokenStorageMock = $this->getMockBuilder(TokenStorageInterface::class) |
| 56 | + ->getMock(); |
| 57 | + |
| 58 | + $tokenStorageMock |
| 59 | + ->expects($this->once()) |
| 60 | + ->method('getToken') |
| 61 | + ->willReturn($tokenMock); |
| 62 | + |
| 63 | + $authorizationChecker = $this->getMockBuilder(AuthorizationCheckerInterface::class) |
| 64 | + ->getMock(); |
| 65 | + |
| 66 | + $authorizationChecker |
| 67 | + ->expects($this->once()) |
| 68 | + ->method('isGranted') |
| 69 | + ->with('IS_AUTHENTICATED_REMEMBERED') |
| 70 | + ->willReturn(true); |
| 71 | + |
| 72 | + $reportMock = $this->getBugsnagReportMock(); |
| 73 | + $reportMock |
| 74 | + ->expects($this->once()) |
| 75 | + ->method('setUser') |
| 76 | + ->with([ |
| 77 | + 'id' => $user->getUsername() |
| 78 | + ]); |
| 79 | + |
| 80 | + $callback = new UserSettingCallback($tokenStorageMock, $authorizationChecker, true); |
| 81 | + $callback->registerCallback($reportMock); |
| 82 | + } |
| 83 | + |
| 84 | + private function getBugsnagReportMock() |
| 85 | + { |
| 86 | + return $this->getMockBuilder(Report::class) |
| 87 | + ->disableOriginalConstructor() |
| 88 | + ->getMock(); |
| 89 | + } |
| 90 | +} |
0 commit comments