php - 如何使用 PHPUnit 测试 Symfony2 模型

标签 php symfony doctrine-orm phpunit

我一直在尝试在 Symfony2 项目中测试模型,但我不知道如何让实体管理器保存和检索记录。

任何人都可以为我指出正确的文档吗?

最佳答案

为了测试您的模型,您可以使用 setUp() 方法。 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);
    }

希望对你有帮助

关于php - 如何使用 PHPUnit 测试 Symfony2 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7313791/

相关文章:

php - "Using $this when not in object context"出现在 Silex/phpunit 案例中

php - 为 doctrine2 查询添加所有相关项的计数

doctrine-orm - 如何在Doctrine2中的YML中创建唯一约束?

php - 使用 php 连接到 mysql 时,仅在使用 localhost(而不是 127.0.0.1)时出现 mysql 2002 错误

php - 为什么 strtotime ('a' ) 返回时间?

php - 使用 Symfony3 组件时,Silex FormServiceProvider 无法加载类型 "form"

Symfony2 : How to filter the options of an entity-choice form field by a certain attribute?

PHP - 无法动态实例化类

php - 插入记录 PHP/PDO 抛出错误

facebook - 使用 FOSUserBundle 在 oAuth 上进行 Symfony 身份验证