KNP Menu Bundle won't access entities when being run in controller being functionally tested

500 views Asked by At

This one really has me stumped.

I'm trying to run functional tests on my application, but am running into problems with the KNP Menu Bundle, and I'm not entirely sure where I'm going wrong.

I have some entities being loaded by Doctrine Fixtures, and I know they are there - no other part of the testing suite has an issue with finding them and asserting they are what I want them to be.

In my setUp() method of the test (extending WebTestCase), I create a client and inject the entity manager into it, like so:

/**
 * Create Client and inject services
 */
$this->client = static::createClient();
$this->client->getContainer()->set( 'doctrine.orm.entity_manager', $this->em );

None of the other tests have an issue with using that entity manager and finding the entities.

I'm using the menu as a service, and the definition is as follows:

the_site.menu_builder:
    class: TheSite\SiteBundle\Menu\MenuBuilder
    arguments: [ "@knp_menu.factory", "@doctrine.orm.entity_manager" ]

the_site.menu.breadcrumb:
    class: Knp\Menu\MenuItem
    factory_service: the_site.menu_builder
    factory_method: breadcrumbMenu
    arguments: [ "@request" ]
    scope: request
    tags:
        - { name: knp_menu.menu, alias: breadcrumb }

The code that it seems to choke on is this:

public function breadcrumbMenu( Request $request )
{
     // ......

      /**
       * @var $category NewsCategory
       */
      $category = $this->em->getRepository( 'TheSiteSiteBundle:Category' )->findOneByCanonical( $request->get( 'category' ) );
      $categories = [];

      do
      {
        $categories[] = $category;

        $category     = $category->getParent();
      } while( $category !== null );

      // .....
}

I did a little investigation (including some very complex var_dump'ing ;) ) and it seems that it just can't find any of the categories set by the fixtures. If it helps at all, the entities are being set inside of a doctrine transaction, but still - the other tests proved that the client managed to find those entities just fine before!

$request->get( 'category' ) produces exactly what it should do, so as far as I can see it's an issue with the entity manager.

If more code is needed I'd be more than happy to share.

Edit: I might take what I said back - now it doesn't seem to be loading entities anywhere. Maybe there where still some in the database or something!

0

There are 0 answers