Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

accessing entity manager inside phpunittest

I have the following unit test code in symfony:

<?php

// src/Acme/DemoBundle/Tests/Utility/CalculatorTest.php
namespace Shopious\MainBundle\Tests;

class ShippingCostTest extends \PHPUnit_Framework_TestCase
{
    public function testShippingCost()
    {
        $em = $this->kernel->getContainer()->get('doctrine.orm.entity_manager');
        $query = $em->createQueryBuilder();
        $query->select('c')
              ->from("ShopiousUserBundle:City", 'c');
        $result =  $query->getQuery()->getResult();
        var_dump($result);
    }
}

and I am trying to access the entity manager here, howver it always gives me this error:

Undefined property: Acme\MainBundle\Tests\ShippingCostTest::$kernel
like image 647
aherlambang Avatar asked Feb 17 '23 10:02

aherlambang


1 Answers

To achieve this you need to create a base test class (let's call it KernelAwareTest) with following contents:

<?php

namespace Shopious\MainBundle\Tests;

require_once dirname(__DIR__).'/../../../app/AppKernel.php';

/**
 * Test case class helpful with Entity tests requiring the database interaction.
 * For regular entity tests it's better to extend standard \PHPUnit_Framework_TestCase instead.
 */
abstract class KernelAwareTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @var \Symfony\Component\HttpKernel\Kernel
     */
    protected $kernel;

    /**
     * @var \Doctrine\ORM\EntityManager
     */
    protected $entityManager;

    /**
     * @var \Symfony\Component\DependencyInjection\Container
     */
    protected $container;

    /**
     * @return null
     */
    public function setUp()
    {
        $this->kernel = new \AppKernel('test', true);
        $this->kernel->boot();

        $this->container = $this->kernel->getContainer();
        $this->entityManager = $this->container->get('doctrine')->getManager();

        $this->generateSchema();

        parent::setUp();
    }

    /**
     * @return null
     */
    public function tearDown()
    {
        $this->kernel->shutdown();

        parent::tearDown();
    }

    /**
     * @return null
     */
    protected function generateSchema()
    {
        $metadatas = $this->getMetadatas();

        if (!empty($metadatas)) {
            $tool = new \Doctrine\ORM\Tools\SchemaTool($this->entityManager);
            $tool->dropSchema($metadatas);
            $tool->createSchema($metadatas);
        }
    }

    /**
     * @return array
     */
    protected function getMetadatas()
    {
        return $this->entityManager->getMetadataFactory()->getAllMetadata();
    }
}

Then your own test class will be extended from this one:

<?php

namespace Shopious\MainBundle\Tests;
use Shopious\MainBundle\Tests\KernelAwareTest;

class ShippingCostTest extends KernelAwareTest
{ 
    public function setUp()
    {
        parent::setUp();

        // Your own setUp() goes here
    }

    // Tests themselves
}

And then use parent's class methods. In your case, to access entity manager, do:

$entityManager = $this->entityManager;
like image 152
Sobit Akhmedov Avatar answered Feb 23 '23 04:02

Sobit Akhmedov