2

Based on my reading of the PHPUnit documentation, I expect the following code to create a mock of the AuthorizationChecker class that will return false when the isGranted() method is called. However, the original method is called instead. What am I doing wrong?

    $auth = $this->getMockBuilder('Symfony\Component\Security\Core\Authorization\AuthorizationChecker')
        ->disableOriginalConstructor()
        ->setMethods(['isGranted'])
        ->getMock();
    $auth->expects($this->any())
        ->method('isGranted')
        ->with($this->anything())
        ->will($this->returnValue(false));

    $this->assertFalse($auth->isGranted('TEST'));

Replacing the argument to setMethods() with null or an empty array, or removing the call entirely, has no effect. Nor does removing the expects() and with() calls.

Joe Walker
  • 23
  • 4
  • Hi, You can try mocking the AuthorizationCheckerInterface interface that is not finally: depends what you need to do, usually class define as usage the interface of the class. – Matteo Mar 08 '16 at 06:43
  • 1
    Hi Matteo, thanks for the answer and suggestion. I did end up just mocking the interface, which works well. – Joe Walker Mar 09 '16 at 01:25

2 Answers2

1

The method you are looking to mock is declared as final, so you can't test doubled as described in the doc:

Limitation: final, private, and static methods Please note that final, private and static methods cannot be stubbed or mocked. They are ignored by PHPUnit's test double functionality and retain their original behavior.

You can take a look at this for a partial solution.

Hope this help

Community
  • 1
  • 1
Matteo
  • 37,680
  • 11
  • 100
  • 115
1

I was able to achieve this by mocking out all of the internals used by the AuthorizationChecker class and then injecting my mock into the container.

    $tokenMock = $this->getMockBuilder( UsernamePasswordToken::class )
                      ->disableOriginalConstructor()
                      ->setMethods( array( 'isAuthenticated' ) )
                      ->getMock();
    $tokenMock->method( 'isAuthenticated' )
              ->willReturn( true );
    $tokenStorageMock = $this->getMockBuilder( TokenStorage::class )
                             ->disableOriginalConstructor()
                             ->setMethods( array( 'getToken' ) )
                             ->getMock();
    $tokenStorageMock->method( 'getToken' )
                     ->willReturn( $tokenMock );
    $authenticationManagerMock = $this->getMockBuilder( AuthenticationManagerInterface::class )
                                      ->disableOriginalConstructor()
                                      ->getMock();
    $accessDecisionManagerMock = $this->getMockBuilder( AccessDecisionManager::class )
                                      ->disableOriginalConstructor()
                                      ->setMethods( array( 'decide' ) )
                                      ->getMock();
    $accessDecisionManagerMock->method( 'decide' )
                              ->willReturn( 'true' );
    $authorizationCheckerMock = $this->getMockBuilder( AuthorizationChecker::class )
                                     ->setConstructorArgs(
                                         array(
                                             $tokenStorageMock,
                                             $authenticationManagerMock,
                                             $accessDecisionManagerMock,
                                             false
                                         )
                                     )
                                     ->setMethods( array( 'isGranted' ) )
                                     ->getMock();
    $authorizationCheckerMock->method( 'isGranted' )
                             ->will( $this->returnValue( true ) );
    $this->getContainer()
         ->set( 'security.authorization_checker', $authorizationCheckerMock );
David Baucum
  • 2,162
  • 24
  • 25