承接 unionofrad/li3_fixtures 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

unionofrad/li3_fixtures

Composer 安装命令:

composer require --dev unionofrad/li3_fixtures

包简介

The fixtures library for the li3 PHP framework

README 文档

README

This plugin provide fixtures managment. Should work with any kind of Source adapters. The fixture class support the following datasource's hints:

  • If Source::enabled('schema') returns true, the Fixture manage schema (i.e create/drop) via Source::createSchema() & Source::dropSchema().

  • If Source::enabled('sources') returns true, the Fixture allow soft drop (i.e safe options).

Installation

The preferred installation method is via composer. You can add the library as a dependency via:

composer require unionofrad/li3_fixtures

li₃ plugins must be registered within your application bootstrap phase as they use a different (faster) autoloader.

Libraries::add('li3_fixtures')

The official manual has more information on how to register the plugin with the app or use alternative installation methods (i.e. via GIT).

API

The Fixture class

Methods:

  • Fixture::create($safe); //Create the source only
  • Fixture::save($safe); //Create the source + save the fixture's records in.
  • Fixture::drop($safe); //Drop the source.
  • Fixture::populate($records); //Insert a record in the database
  • Fixture::alter($mode, $fieldname, $value); //Altering the schema before ::create()/::save().

Simple example of unit test:

<?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();
	}
}
?>

The Fixtures class

Fixture is a kind of Schema which contain records and a source name or a reference to a model. So let save the above fixture in a class.

<?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 {}
?>

If you have numbers of fixtures, it will be interesting to use the Fixtures class.

Example of use case:

<?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());
	}
}
?>

Ok so why it's better to set the Fixture::_model instead of Fixture::_source ? Long story short, models had their own meta 'connection' value. If a fixture is "linked" with a model, it will automagically configure its meta 'connection' to the fixture's connection when is created or saved.

Example:

<?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());
?>

Advanced use case

For interoperability, sometimes it's usefull to adjust fixtures according a datasources.

You can alter Fixture's instance before creating it like the following use case:

<?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
?>

Note :

You can recover a specific fixture's instance from Fixtures using:

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

unionofrad/li3_fixtures 适用场景与选型建议

unionofrad/li3_fixtures 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 83.02k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2013 年 05 月 23 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「fixtures」 「testing」 「lithium」 「li3」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 unionofrad/li3_fixtures 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 unionofrad/li3_fixtures 我们能提供哪些服务?
定制开发 / 二次开发

基于 unionofrad/li3_fixtures 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 83.02k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 22
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 6
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2013-05-23