使用Symfony测试数据库插入 [英] Testing database insert using Symfony
问题描述
每个人的好日子
我过去几天一直在考虑测试驱动开发,并决定我也需要学习它。虽然我无法弄清楚如何做到这一点。
I have been looking into Test Driven Development a lot in the past few days and decided that I need to learn it as well. Although I can not figure out how to do it precisely.
我的项目依赖于Symfony2.1.6框架和Doctrine,所以我有一些需要填充的数据库表。 / p>
My project depends on the Symfony2.1.6 framework and Doctrine so I have some Database-tables that needs filling.
书(1,n) - (0,n)流派
Book (1,n) - (0,n) Genre
现在,如果我想插入一个类型录音,我首先需要写一个测试,以确保一切正在被插入(或我错了?)
Now if I want to insert a Genre-record I first need to write a test to ensure everything is being inserted as it should (or am I wrong?)
现在的问题是,我不知道如何访问我的数据库,因为它是由框架管理的。
The problem now is that I dont know how to access my Database as it is managed by the framework.
我唯一可以找到的是与LiipFunctionalTestBundle
https://github.com/liip/LiipFunctionalTestBundle ,它创建和恢复临时数据库每次我进行测试。我已经按照说明设置了一切。
The only thing I could find was with the LiipFunctionalTestBundle https://github.com/liip/LiipFunctionalTestBundle which creates and restores a temporary database everytime I run a test. I have setup everything according to the Instructions.
编辑: 我的app / config /现在:
My app/config/config_test.yml looks like this now:
imports:
- { resource: config_dev.yml }
framework:
test: ~
session:
storage_id: session.storage.filesystem
liip_functional_test: ~
web_profiler:
toolbar: false
intercept_redirects: false
swiftmailer:
disable_delivery: true
liip_functional_test:
cache_sqlite_db: true
doctrine:
dbal:
default_connection: default
connections:
default:
driver: pdo_sqlite
path: %kernel.cache_dir%/test.db
所以现在我有一个GenreTest类:
Liip不有文档,所以我只是尝试了一个这样的方法。
So now I have a GenreTest class:
Liip doesn't have documentation so I just tried an approach like this.
use Liip\FunctionalTestBundle\Test\WebTestCase;
class GenreTest extends WebTestCase {
public function testInsert() {
$container = $this->getContainer();
$registry = $container->get('doctrine');
$em = $registry->getEntityManager(null);
$genre = new Genre();
$genre->setName("testGenre");
$em->persist($genre);
$em->flush();
$result = $em->createQuery("SELECT g FROM QkprodMangressBundle:Genre g ".
"WHERE g.name = :name")
->setParameter("name", $genre->getName())
->getResult();
$this->assertEqual($result[0]->getName(), $genre->getName());
}
}
phpunit -c web /
phpunit -c web/
PDOException:找不到驱动
/.../Mangress/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php :36
/.../Mangress/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOSqlite/Driver.php
:60
/.../Mangress/vendor /doctrine/dbal/lib/Doctrine/DBAL/Connection.php:350
/.../Mangress/vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php:949
/。 ../Mangress/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:306
/.../Mangress/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:355
/.../Mangress/app/cache/test/jms_diextra/doctrine/EntityManager_510128d0a5878.ph
p:362
/.../Mangress/src/Qkprod/MangressBundle/Tests/Entity /GenreTest.php:27
失败!测试:3,断言:1,错误:1。
PDOException: could not find driver /.../Mangress/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:36 /.../Mangress/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOSqlite/Driver.php :60 /.../Mangress/vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php:350 /.../Mangress/vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php:949 /.../Mangress/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:306 /.../Mangress/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:355 /.../Mangress/app/cache/test/jms_diextra/doctrine/EntityManager_510128d0a5878.ph p:362 /.../Mangress/src/Qkprod/MangressBundle/Tests/Entity/GenreTest.php:27 FAILURES! Tests: 3, Assertions: 1, Errors: 1.
我遇到的大问题是..我如何测试类似那?
还是我甚至测试数据库通信?伪造数据库
通过自定义实现通信似乎并不像一个
的好主意,因为它将在生产
环境中使用ORM和Doctrine。
The big question I am having is.. how do I test something like that? Or do I even test database communication? Faking the database communication through a custom implementation doesn't seem like a good idea to me because it will use ORM and Doctrine in the production environment as well.
对不起...原来是一个小小的小说。
Sorry.. turned out to be a little novel here.
推荐答案
完全解决了我的问题:
-
安装db-communication所需的驱动程序;)
PDO_SQLITE驱动程序不存在..该怎么办?
sudo apt-get install php5-sqlite
在每个测试运行中重新创建架构_
https://stackoverflow.com/a/10463614/1177024
这是我针对数据库的Class测试
namespace Qkprod\MangressBundle\Entity;
use Qkprod\MangressBundle\Entity\Genre;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
private $em;
/**
* Sets up environment for testing
* Regenerates Database schema before every test-run
*/
public function setUp() {
static::$kernel = static::createKernel();
static::$kernel -> boot();
$this -> em = static::$kernel->getContainer()
->get('doctrine')
->getEntityManager();
$this->regenerateSchema();
}
protected function tearDown() {
parent::tearDown();
$this -> em -> close();
}
/**
* Drops current schema and creates a brand new one
*/
protected function regenerateSchema() {
$metadatas = $this->em->getMetadataFactory()->getAllMetadata();
if (!empty($metadatas)) {
$tool = new \Doctrine\ORM\Tools\SchemaTool($this->em);
$tool -> dropSchema($metadatas);
$tool -> createSchema($metadatas);
}
}
public function testInsert() {
$genre = new Genre();
$genre -> setName("testGenre");
$this -> em -> persist($genre);
$this -> em -> flush();
$result = $this -> em
-> createQuery("SELECT g "
. " FROM QkprodMangressBundle:Genre g "
. " WHERE g.name = :name")
-> setParameter("name", $genre -> getName())
-> getResult();
$this -> assertEquals($result[0] -> getName(), $genre -> getName());
}
}
无法获取LiipBundle工作,但您可以通过设置symfony来提高速度,以将sqlite数据库保存在内存中而不是文件系统。
https://stackoverflow.com/a/10460139/1177024
# app/config/config_test
doctrine:
dbal:
driver: pdo_sqlite
path: :memory:
memory: true
或者也许只创建一次模式而不是重新创建它,只需用新的备份覆盖数据库。
Or maybe only create the schema once and instead of recreating it, just override the database with a fresh backup.
我希望这缩短搜索有些人有同样的问题!
感谢大家帮助我一路:)你很棒!
I hope this shortens the search for some people having the same problem! Thanks to everyone helping me along the way :) You are great!
这篇关于使用Symfony测试数据库插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!