lorenzo / cakephp-fixturize
提供自定义Fixture类,以帮助在纯SQL中管理测试Fixture
Requires
This package is auto-updated.
Last update: 2024-08-25 21:15:38 UTC
README
管理Fixture可以说是 CakePHP 中单元测试中最困难且无聊的过程之一。当您开始一个全新的项目时,让您的 fixture 随着代码一起进步是很简单的,因为更改量通常很小。但是一旦您的应用程序达到某个阶段,实现新功能实际上是非常困难的,因为更改功能会消耗您大量的时间。
另一个处理 fixture 是一项艰巨任务的情况是,当试图测试一个没有先前测试的现有项目时。生成初始的 fixture 集合是很困难的,因为选择与您要测试的功能相关的数据。
此插件允许您导入用纯 SQL 表达的查询,无论是作为文件还是直接从种子数据库导入。这可以帮助您使用您想要的工具来运行模式迁移,或使用 SQL 操作您的数据,以便可以再次导入。
此外,它提供了一个控制台外壳,可以加载您现有的 fixture 文件到目标数据库连接中,这样您可以将现有的任何代码迁移到 SQL 管理的 fixture。
要求
- CakePHP 2.x
- MySQL
安装
安装此插件有几种选择
[Composer]
将以下内容添加到您的 composer.json 中相应的配置键
{
"extra": {
"installer-paths": {
"Plugin/Fixturize": ["lorenzo/cakephp-fixturize"]
}
},
"require" : {
"lorenzo/cakephp-fixturize": "master"
}
}
[手动]
- 下载此文件:https://github.com/lorenzo/cakephp-fixturize/zipball/master
- 解压下载内容。
- 将生成的文件夹复制到
app/Plugin
- 将您刚复制的文件夹重命名为
Fixturize
[GIT Submodule]
在您的应用程序目录中键入
git submodule add git@github.com:lorenzo/cakephp-fixturize.git app/Plugin/Fixturize
git submodule init
git submodule update
[GIT Clone]
在您的插件目录中键入
git clone git://github.com/lorenzo/cakephp-fixturize.git app/Plugin/Fixturize
启用插件
在您的 app/Config/bootstrap.php
文件中启用插件
CakePlugin::load('Fixturize');
用法
您可以使用此插件以多种方式,但通常,您首先想要将现有的一组 fixture 导入到测试数据库中。
加载现有 fixture 到目标连接
如果您需要将现有的基于 PHP 的 fixture 加载到数据库中(无论是将其迁移到基于 SQL 的版本还是快速可视化),请在控制台中执行此命令
./Console/cake Fixturize.fixture_loader app.event,app.tag,app.category --datasource test
它将加载逗号分隔的 fixture 模式和数据列表到数据源 'test'。
从 app 或插件加载所有 fixture
您也可以不指定所有需要的 fixture,然后外壳将自动从 app 加载所有 fixture
./Console/cake Fixturize.fixture_loader --datasource test
要加载插件中的所有 fixture,您必须指定插件
./Console/cake Fixturize.fixture_loader --plugin SomePluginName --datasource test
从 SQL 文件加载您的 fixture
当数据量可管理时,直接从可以迁移、再次转储并使用像 GIT 这样的版本控制系统管理的 SQL 文件加载它是不错的选择。
Fixture SQL 文件可以包含表创建语句、任何 alter tables(例如外键)和数据插入。但您也可以通过 fixture 中的 $fields
和 $records
属性来管理模式或记录,就像您在 fixture 类中定义它们一样。
如果选择在 SQL 文件中有模式创建语句,请确保 CREATE 语句包含 IF NOT EXISTS
。
文件应存储在 app/Test/Fixture/SQL/
或 app/YourPlugin/Test/Fixture/SQL
并具有 .sql 扩展名。
示例
<?php App::uses('SQLTestFixture', 'Fixturize.TestSuite/Fixture'); /** * CategoryFixture * */ class CategoryFixture extends SQLTestFixture { public $plugin = 'MyPlugin'; // Can be ommited if the sql file is locate in app public $file = 'overriding_file_name.sql'; // By default it would use categories.sql }
这就是你需要的全部!
直接从数据库加载固定数据
当数据量增加时,你可能需要考虑将所有固定数据直接存储在数据库中,这样在运行每个测试之前,你可以直接从其中复制架构和数据。这比从SQL文件加载固定数据要快得多。
这需要在 app/Config/database.php
中创建一个新的数据库配置,以便连接到种子数据库(包含测试数据的数据库)
public $test_seed = array( 'datasource' => 'Database/Mysql', 'persistent' => false, 'host' => 'localhost', 'login' => 'root', 'password' => 'root', 'database' => 'test_seed', 'prefix' => '', 'encoding' => 'utf8', );
警告
请确保你还有一个指向单独数据库的 $test
数据库配置。
$test_seed
包含种子数据,而 $test
是一个空数据库,CakePHP 测试逻辑将在其中运行查询。
这是使用导入数据固定类的一个示例
<?php App::uses('TableCopyTestFixture', 'Fixturize.TestSuite/Fixture'); /** * CategoryFixture * */ class CategoryFixture extends TableCopyTestFixture { }
是的,就这么简单!