unionofrad/li3_fixtures

li3 PHP 框架的 fixtures 库

安装次数: 54,266

依赖项: 2

建议者: 0

安全性: 0

星标: 6

关注者: 8

分支: 6

开放性问题: 0

类型:lithium-library

v1.1.1 2017-04-08 07:41 UTC

This package is auto-updated.

Last update: 2024-08-25 20:15:36 UTC


README

此插件提供 fixtures 管理。应与任何类型的 Source 适配器兼容。fixture 类支持以下数据源的提示

  • 如果 Source::enabled('schema') 返回 true,则 Fixture 通过 Source::createSchema()Source::dropSchema() 管理模式(即创建/删除)。

  • 如果 Source::enabled('sources') 返回 true,则 Fixture 允许软删除(即安全选项)。

安装

首选的安装方法是通过 composer。您可以通过以下方式将库添加为依赖项:

composer require unionofrad/li3_fixtures

li₃ 插件必须在应用程序引导阶段注册,因为它们使用不同的(更快)自动加载器。

Libraries::add('li3_fixtures')

官方手册有更多关于如何在应用程序中注册插件或使用替代安装方法(例如通过 Git)的信息。

API

Fixture 类

方法

  • Fixture::create($safe); // 仅创建源
  • Fixture::save($safe); // 创建源 + 将 fixture 的记录保存
  • Fixture::drop($safe); // 删除源。
  • Fixture::populate($records); // 向数据库中插入记录
  • Fixture::alter($mode, $fieldname, $value); // 在 ::create()/::save() 之前修改模式。

单元测试的简单示例

<?php
//app/tests/cases/models/SampleTest.php
namespace app\tests\cases\models;

use li3_fixtures\test\Fixture;

class SampleTest extends \lithium\test\Unit {

	public function testFixture() {
		$fixture = new Fixture([
			'connection' => 'lithium_mysql_test',
			'source' => 'contacts',
			'fields' => array(
				'id' => array('type' => 'id'),
				'name' => array('type' => 'string')
			),
			'records' => array(
				array('id' => 1, 'name' => 'Nate'),
				array('id' => 2, 'name' => 'Gwoo')
			)
		]);

		$fixture->save();

		$fixture->populate(['id' => 3, 'name' => 'Mehlah']);

		$fixture->drop();
	}
}
?>

Fixtures 类

Fixture 是一种包含记录和源名称或模型引用的 Schema。因此,让我们将上述 fixture 保存到类中。

<?php
//app/tests/fixture/ContactsFixture.php
namespace app\tests\fixture;

class ContactsFixture extends \li3_fixtures\test\Fixture {

	protected $_model = 'app\models\Contacts';

	protected $_fields = array(
		'id' => ['type' => 'id'],
		'name' => ['type' => 'string']
	);

	protected $_records = [
		array['id' => 1, 'name' => 'Nate'],
		array['id' => 2, 'name' => 'Gwoo']
	];
}
?>
<?php
//app/models/Contact.php
namespace app\models;

class Contacts extends \lithium\data\Model {}
?>

如果您有多个 fixtures,则使用 Fixtures 类会很有趣。

用例示例

<?php
//app/tests/integration/Sample2Test.php
namespace app\tests\integration;

use li3_fixtures\test\Fixtures;
use app\models\Contacts;
use app\models\Images;
// and so on...

class Sample2Test extends \lithium\test\Unit {

	public function setUp() {
		Fixtures::config([
			'db' => [
				'adapter' => 'Connection',
				'connection' => 'lithium_mysql_test',
				'fixtures' => array(
					'contacts' => 'app\tests\fixture\ContactsFixture',
					'images' => 'app\tests\fixture\ImagesFixture'
					// and so on...
				)
			]
		]);
		Fixtures::save('db');
	}

	public function tearDown() {
		Fixtures::clear('db');
	}

	public function testFixture() {
		var_export(Contacts::find('all')->data());
		var_export(Images::find('all')->data());
	}
}
?>

那么为什么要在 Fixture::_model 而不是 Fixture::_source 中设置呢?长话短说,模型有自己的元 'connection' 值。如果一个 fixture 与模型“链接”,则它将自动配置其元 'connection' 为 fixture 的连接,当创建或保存时。

示例

<?php
Fixtures::save('db', array('contacts'));
//The line bellow is not needed since Contacts have been configured by ContactsFixture.
Contacts::config(['meta' => ['connection' => 'lithium_mysql_test']]);
var_export(Contacts::find('all')->data());
?>

高级用例

为了互操作性,有时根据数据源调整 fixtures 是有用的。

您可以在创建之前像以下用例一样修改 Fixture 的实例

<?php
$fixture->alter('add', [
	'name' => 'enabled',
	'type' => 'boolean'
]); //Add a field

$fixture->alter('change', [
	'name' => 'published',
	'value' => function ($val) {
		return new MongoDate(strtotime($val));
	}
]); //Simple cast for fixture's values according the closure

$fixture->alter('change', [
	'name' => 'id',
	'to' => '_id',
	'value' => function ($val) {
		return new MongoId('4c3628558ead0e594' . (string) ($val + 1000000));
	}
]); //Renaming the field 'id' to '_id' + cast fixture's values according the closure

$fixture->alter('change', [
	'name' => 'bigintger',
	'type' => 'integer',
	'use' => 'bigint' //use db specific type
]); //Modifing a field type

$fixture->alter('drop', 'bigintger'); //Simply dropping a field
?>

注意

您可以使用以下方式从 Fixtures 中恢复特定的 fixture 实例:

<?php
$fixture = Fixtures::get('db', 'contacts');
?>