开发者

How to test Symfony2 models using PHPUnit

开发者 https://www.devze.com 2023-04-03 08:38 出处:网络
开发者_运维百科I\'ve been trying to test a model in a Symfony2 project, but I don\'t know how to get the entity manager to save and retrive records.
开发者_运维百科

I've been trying to test a model in a Symfony2 project, but I don't know how to get the entity manager to save and retrive records.

Can anyone point me to the right docs for this?


In order to test your models, you can use setUp() method. link to docs

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class MyModelTest extends WebTestCase
{
    /**
     * @var EntityManager
     */
    private $_em;

    protected function setUp()
    {
        $kernel = static::createKernel();
        $kernel->boot();
        $this->_em = $kernel->getContainer()->get('doctrine.orm.entity_manager');
        $this->_em->beginTransaction();
    }

    /**
     * Rollback changes.
     */
    public function tearDown()
    {
        $this->_em->rollback();
    }

    public function testSomething()
    {
        $user = $this->_em->getRepository('MyAppMyBundle:MyModel')->find(1);
    }

Hope this helps you


Symfony2 models are expected to be domain objects that represent domain models in the code.

domain objects should be defined purely to implement the business behavior of the corresponding domain concept, rather than be defined by the requirements of a more specific technology framework. -- Domain-driven design - Wikipedia, the free encyclopedia

Domain objects (and its tests) should not depend on Symfony2 APIs and Doctrine APIs except if you really want to test themselves.

Writing Symfony2 unit tests is no different than writing standard PHPUnit unit tests. -- Symfony - Testing

You can test business logic (processes, rules, behaviors, etc.) represented in domain objects with PHPUnit (or Behat) and usually test doubles.


namespace Ibw\JobeetBundle\Tests\Repository;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Input\ArrayInput;
use Doctrine\Bundle\DoctrineBundle\Command\DropDatabaseDoctrineCommand;
use Doctrine\Bundle\DoctrineBundle\Command\CreateDatabaseDoctrineCommand;
use Doctrine\Bundle\DoctrineBundle\Command\Proxy\CreateSchemaDoctrineCommand;

class CategoryRepositoryTest extends WebTestCase
{
    private $em;
    private $application;

    public function setUp()
    {
        static::$kernel = static::createKernel();
        static::$kernel->boot();

        $this->application = new Application(static::$kernel);

        // drop the database
        $command = new DropDatabaseDoctrineCommand();
        $this->application->add($command);
        $input = new ArrayInput(array(
            'command' => 'doctrine:database:drop',
            '--force' => true
        ));
        $command->run($input, new NullOutput());

        // we have to close the connection after dropping the database so we don't get "No database selected" error
        $connection = $this->application->getKernel()->getContainer()->get('doctrine')->getConnection();
        if ($connection->isConnected()) {
            $connection->close();
        }

        // create the database
        $command = new CreateDatabaseDoctrineCommand();
        $this->application->add($command);
        $input = new ArrayInput(array(
            'command' => 'doctrine:database:create',
        ));
        $command->run($input, new NullOutput());

        // create schema
        $command = new CreateSchemaDoctrineCommand();
        $this->application->add($command);
        $input = new ArrayInput(array(
            'command' => 'doctrine:schema:create',
        ));
        $command->run($input, new NullOutput());

        // get the Entity Manager
        $this->em = static::$kernel->getContainer()
            ->get('doctrine')
            ->getManager();

        // load fixtures
        $client = static::createClient();
        $loader = new \Symfony\Bridge\Doctrine\DataFixtures\ContainerAwareLoader($client->getContainer());
        $loader->loadFromDirectory(static::$kernel->locateResource('@IbwJobeetBundle/DataFixtures/ORM'));
        $purger = new \Doctrine\Common\DataFixtures\Purger\ORMPurger($this->em);
        $executor = new \Doctrine\Common\DataFixtures\Executor\ORMExecutor($this->em, $purger);
        $executor->execute($loader->getFixtures());
    }

    public function testFunction()
    {
          // here you test save any object or test insert any object 
    }

    protected function tearDown()
    {
        parent::tearDown();
        $this->em->close();
    }
}

like in this Link : Jobeet Unit Test Tutorial explain how to Test Entity and Entity Repository

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号