概述 快速入门 教程 手册 最佳实践 组件 参考 贡献

发布于 2015-08-27 16:48:46 | 116 次阅读 | 评论: 0 | 来源: 网络整理

Unit testing Doctrine repositories in a Symfony project is not recommended. When you’re dealing with a repository, you’re really dealing with something that’s meant to be tested against a real database connection.

Fortunately, you can easily test your queries against a real database, as described below.

Functional Testing

If you need to actually execute a query, you will need to boot the kernel to get a valid connection. In this case, you’ll extend the KernelTestCase, which makes all of this quite easy:

// src/Acme/StoreBundle/Tests/Entity/ProductRepositoryFunctionalTest.php
namespace AcmeStoreBundleTestsEntity;

use SymfonyBundleFrameworkBundleTestKernelTestCase;

class ProductRepositoryFunctionalTest extends KernelTestCase
{
    /**
     * @var DoctrineORMEntityManager
     */
    private $em;

    /**
     * {@inheritDoc}
     */
    public function setUp()
    {
        self::bootKernel();
        $this->em = static::$kernel->getContainer()
            ->get('doctrine')
            ->getManager()
        ;
    }

    public function testSearchByCategoryName()
    {
        $products = $this->em
            ->getRepository('AcmeStoreBundle:Product')
            ->searchByCategoryName('foo')
        ;

        $this->assertCount(1, $products);
    }

    /**
     * {@inheritDoc}
     */
    protected function tearDown()
    {
        parent::tearDown();
        $this->em->close();
    }
}
最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务