Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8970109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:41:10+00:00 2026-06-15T17:41:10+00:00

I am trying to use Mockery in order to unit test my sf2 functions.

  • 0

I am trying to use Mockery in order to unit test my sf2 functions.
I am strugeling with my first attempt.

First try at test a class which uses security context:

public function setSecurityContext(SecurityContext $securityContext)
{
    $this->securityContext = $securityContext;
    try {
        $this->isLoggedIn = $securityContext->isGranted('IS_AUTHENTICATED_FULLY');
        $this->user = $securityContext->getToken()->getUser();
    } catch (\Exception $e) {
        $this->isLoggedIn = false;
        $this->user = $securityContext->getToken()->getUser();
    }
}

I create a testsetSecurityContext function like this:

public function testsetSecurityContext()
{
    /* @var $securityContext SecurityContext */
    $securityContext = m::mock('Symfony\Component\Security\Core\SecurityContext');

    $securityContext->shouldReceive('isGranted')
    ->with('IS_AUTHENTICATED_FULLY')
    ->once()
    ->andReturn(true);

    $factory = m::mock('Knp\Menu\FactoryInterface');

    $menu = new MenuBuilder($factory);

    $menu->setSecurityContext($securityContext);
}

When running unit test, I receive the error:

testsetSecurityContext

Mockery\Exception: The method isGranted is marked final and it is not possible to generate a mock object with such a method defined. You should instead pass an instance of this object to Mockery to create a partial mock.

So I change my test function accordingly:

public function testsetSecurityContext()
{
    /* @var $securityContext SecurityContext */
    $securityContext = m::mock(new \Symfony\Component\Security\Core\SecurityContext());
    /* ... skipped ... */
}

Now I receive that error:

testsetSecurityContext

ErrorException: Catchable Fatal Error: Argument 1 passed to Symfony\Component\Security\Core\SecurityContext::__construct() must implement interface Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface, none given, called in ..MenuBuilderTest.php on line 91 and defined in ..Symfony\Component\Security\Core\SecurityContext.php line 41

So I modify my code again:

public function testsetSecurityContext()
{

    $auth = m::mock('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface');

    /* @var $securityContext SecurityContext */
    $securityContext = m::mock(new \Symfony\Component\Security\Core\SecurityContext($auth));

    /* ... skipped ... */

}

And I get another error:

testsetSecurityContext

ErrorException: Catchable Fatal Error: Argument 2 passed to Symfony\Component\Security\Core\SecurityContext::__construct() must implement interface Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface, none given, called in …\MenuBuilderTest.php on line 94 and defined in …\Symfony\Component\Security\Core\SecurityContext.php line 41

I endup with:

public function testsetSecurityContext()
{

    $am = m::mock('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface');
    $adm = m::mock('Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface');

    /* @var $securityContext SecurityContext */
    $securityContext = m::mock(new \Symfony\Component\Security\Core\SecurityContext($am, $adm));

    $securityContext->shouldReceive('isGranted')
    ->with('IS_AUTHENTICATED_FULLY')
    ->once()
    ->andReturn(true);

    $factory = m::mock('Knp\Menu\FactoryInterface');

    $menu = new MenuBuilder($factory);

    $menu->setSecurityContext($securityContext);
}

And that is still not OK as I get that error:

testsetSecurityContext

ErrorException: Catchable Fatal Error: Argument 1 passed to Atos\Worldline\Fm\Integration\Ucs\EventFlowAnalyser\Menu\MenuBuilder::setSecurityContext() must be an instance of Symfony\Component\Security\Core\SecurityContext, instance of Mockery_50c5c1e0e68d2 given, called in ..\MenuBuilderTest.php on line 106 and defined in ..\MenuBuilder.php line 140

I really would appreciate some help before I end-up with 100 lines test to test a 8 lines function…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T17:41:11+00:00Added an answer on June 15, 2026 at 5:41 pm

    Instead of mocking the instance, go for the interface it implements. It almost always works better and nearly everything in Symfony2 has well defined interfaces.

    If MenuBuilder is a custom class, it should use the interface too rather than the actual implementation.

    Symfony\Component\Security\Core\SecurityContextInterface

    public function testsetSecurityContext()
    {
        /* @var $securityContext SecurityContext */
        $securityContext = m::mock('Symfony\Component\Security\Core\SecurityContextInterface');
    
        $securityContext->shouldReceive('isGranted')
        ->with('IS_AUTHENTICATED_FULLY')
        ->once()
        ->andReturn(true);
    
        $factory = m::mock('Knp\Menu\FactoryInterface');
    
        $menu = new MenuBuilder($factory);
    
        $menu->setSecurityContext($securityContext);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying use strtotime and date functions within a class to add a
I was trying use a set of filter functions to run the appropriate routine,
I'm trying use the FormWizard for to submit an order charge in wizard done
Im trying use the spinner control result in order to point it to another
I'm trying use an object which wasn't available until SDK level 5. It seems
I'm trying use jquery ui tabs together with js and mvc3. <div class=demo> <div
Im trying use a Java annotation in a Groovy class but have trouble to
I'm trying use jquery to remove a class from an element (not knowing ahead
I'm trying use a UIScrollview inclined 350 degrees, the first thing I thought was
I am trying use filehelpers class builder but I am kinda confused on what

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.