FormAuthenticationEntryPointTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Security\Http\Tests\EntryPoint;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpKernel\HttpKernelInterface;
  14. use Symfony\Component\Security\Http\EntryPoint\FormAuthenticationEntryPoint;
  15. class FormAuthenticationEntryPointTest extends TestCase
  16. {
  17. public function testStart()
  18. {
  19. $request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')->disableOriginalConstructor()->disableOriginalClone()->getMock();
  20. $response = new Response();
  21. $httpKernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
  22. $httpUtils = $this->getMockBuilder('Symfony\Component\Security\Http\HttpUtils')->getMock();
  23. $httpUtils
  24. ->expects($this->once())
  25. ->method('createRedirectResponse')
  26. ->with($this->equalTo($request), $this->equalTo('/the/login/path'))
  27. ->will($this->returnValue($response))
  28. ;
  29. $entryPoint = new FormAuthenticationEntryPoint($httpKernel, $httpUtils, '/the/login/path', false);
  30. $this->assertEquals($response, $entryPoint->start($request));
  31. }
  32. public function testStartWithUseForward()
  33. {
  34. $request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')->disableOriginalConstructor()->disableOriginalClone()->getMock();
  35. $subRequest = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')->disableOriginalConstructor()->disableOriginalClone()->getMock();
  36. $response = new Response('', 200);
  37. $httpUtils = $this->getMockBuilder('Symfony\Component\Security\Http\HttpUtils')->getMock();
  38. $httpUtils
  39. ->expects($this->once())
  40. ->method('createRequest')
  41. ->with($this->equalTo($request), $this->equalTo('/the/login/path'))
  42. ->will($this->returnValue($subRequest))
  43. ;
  44. $httpKernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
  45. $httpKernel
  46. ->expects($this->once())
  47. ->method('handle')
  48. ->with($this->equalTo($subRequest), $this->equalTo(HttpKernelInterface::SUB_REQUEST))
  49. ->will($this->returnValue($response))
  50. ;
  51. $entryPoint = new FormAuthenticationEntryPoint($httpKernel, $httpUtils, '/the/login/path', true);
  52. $entryPointResponse = $entryPoint->start($request);
  53. $this->assertEquals($response, $entryPointResponse);
  54. $this->assertEquals(401, $entryPointResponse->headers->get('X-Status-Code'));
  55. }
  56. }