|
11 | 11 | use Symfony\Component\Console\Event\ConsoleExceptionEvent;
|
12 | 12 | use Symfony\Component\DependencyInjection\ContainerBuilder;
|
13 | 13 | use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
| 14 | +use Symfony\Component\HttpFoundation\Request; |
| 15 | +use Symfony\Component\HttpFoundation\RequestStack; |
14 | 16 | use Symfony\Component\HttpKernel\Event\GetResponseEvent;
|
15 | 17 | use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
|
16 | 18 | use Symfony\Component\HttpKernel\Exception\HttpException;
|
@@ -38,17 +40,21 @@ class ExceptionListenerTest extends TestCase
|
38 | 40 | /** @var EventDispatcherInterface|\PHPUnit_Framework_MockObject_MockObject */
|
39 | 41 | private $mockEventDispatcher;
|
40 | 42 |
|
| 43 | + private $mockRequestStack; |
| 44 | + |
41 | 45 | public function setUp()
|
42 | 46 | {
|
43 | 47 | $this->mockTokenStorage = $this->createMock(TokenStorageInterface::class);
|
44 | 48 | $this->mockAuthorizationChecker = $this->createMock(AuthorizationCheckerInterface::class);
|
45 | 49 | $this->mockSentryClient = $this->createMock(\Raven_Client::class);
|
46 | 50 | $this->mockEventDispatcher = $this->createMock(EventDispatcherInterface::class);
|
| 51 | + $this->mockRequestStack = $this->createMock(RequestStack::class); |
47 | 52 |
|
48 | 53 | $containerBuilder = new ContainerBuilder();
|
49 | 54 | $containerBuilder->setParameter('kernel.root_dir', 'kernel/root');
|
50 | 55 | $containerBuilder->setParameter('kernel.environment', 'test');
|
51 | 56 |
|
| 57 | + $containerBuilder->set('request_stack', $this->mockRequestStack); |
52 | 58 | $containerBuilder->set('security.token_storage', $this->mockTokenStorage);
|
53 | 59 | $containerBuilder->set('security.authorization_checker', $this->mockAuthorizationChecker);
|
54 | 60 | $containerBuilder->set('sentry.client', $this->mockSentryClient);
|
@@ -424,6 +430,54 @@ public function test_that_username_is_set_from_user_interface_if_token_present_a
|
424 | 430 | $listener->onKernelRequest($mockEvent);
|
425 | 431 | }
|
426 | 432 |
|
| 433 | + public function test_that_ip_is_set_from_request_stack() |
| 434 | + { |
| 435 | + $mockToken = $this->createMock(TokenInterface::class); |
| 436 | + |
| 437 | + $mockToken |
| 438 | + ->method('getUser') |
| 439 | + ->willReturn('some_user'); |
| 440 | + |
| 441 | + $mockToken |
| 442 | + ->method('isAuthenticated') |
| 443 | + ->willReturn(true); |
| 444 | + |
| 445 | + $mockEvent = $this->createMock(GetResponseEvent::class); |
| 446 | + |
| 447 | + $mockRequest = $this->createMock(Request::class); |
| 448 | + |
| 449 | + $mockRequest |
| 450 | + ->method('getClientIp') |
| 451 | + ->willReturn('1.2.3.4'); |
| 452 | + |
| 453 | + $this->mockRequestStack |
| 454 | + ->method('getCurrentRequest') |
| 455 | + ->willReturn($mockRequest); |
| 456 | + |
| 457 | + $mockEvent |
| 458 | + ->expects($this->once()) |
| 459 | + ->method('getRequestType') |
| 460 | + ->willReturn(HttpKernelInterface::MASTER_REQUEST); |
| 461 | + |
| 462 | + $this->mockAuthorizationChecker |
| 463 | + ->method('isGranted') |
| 464 | + ->with($this->identicalTo(AuthenticatedVoter::IS_AUTHENTICATED_REMEMBERED)) |
| 465 | + ->willReturn(true); |
| 466 | + |
| 467 | + $this->mockTokenStorage |
| 468 | + ->method('getToken') |
| 469 | + ->willReturn($mockToken); |
| 470 | + |
| 471 | + $this->mockSentryClient |
| 472 | + ->expects($this->once()) |
| 473 | + ->method('set_user_data') |
| 474 | + ->with($this->identicalTo('some_user'), null, ['ip_address' => '1.2.3.4']); |
| 475 | + |
| 476 | + $this->containerBuilder->compile(); |
| 477 | + $listener = $this->containerBuilder->get('sentry.exception_listener'); |
| 478 | + $listener->onKernelRequest($mockEvent); |
| 479 | + } |
| 480 | + |
427 | 481 | public function test_regression_with_unauthenticated_user_token_PR_78()
|
428 | 482 | {
|
429 | 483 | $mockToken = $this->createMock(TokenInterface::class);
|
|
0 commit comments