定制 lorenzo/cakephp-fixturize 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

lorenzo/cakephp-fixturize

Composer 安装命令:

composer require lorenzo/cakephp-fixturize

包简介

Provides custom Fixture classes to help managing test fixtures in pure SQL

README 文档

README

Managing fixtures is arguably the most difficult and boring process of unit testing in CakePHP. When you start a fresh project it's quite easy to make your fixtures progress along with your code, as the amount of changes are usually small. But once your applications reaches a certain point, it's actually quite hard to implement new features, as changing features would consume a great deal of your time.

Another case where handling fixtures is a daunting task is when trying to test an existing project with no previous tests. Generating the initial set of fixtures is hard as selecting the data that is relevant to the features you want to test.

This plugin allows you to import queries expressed in pure SQL, either as files or by importing directly from a seed database. This helps you use the tools you want for running migrations of your schema, or manipulate your data using SQL so it can be imported again.

Additionally, it provides a console shell to load your existing fixture files in a target database connection, so you can migrate any existing code you have to SQL managed fixtures.

Requirements

  • CakePHP 2.x
  • MySQL

Installation

There are a few ways to choose from for installing this plugin:

[Composer]

Add the following to your composer.json in the corresponding configuration keys:

{
	"extra": {
		"installer-paths": {
			"Plugin/Fixturize": ["lorenzo/cakephp-fixturize"]
	}
},
	"require" : {
		"lorenzo/cakephp-fixturize": "master"
	}
}

[Manual]

[GIT Submodule]

In your app directory type:

git submodule add git@github.com:lorenzo/cakephp-fixturize.git app/Plugin/Fixturize
git submodule init
git submodule update

[GIT Clone]

In your plugin directory type:

git clone git://github.com/lorenzo/cakephp-fixturize.git app/Plugin/Fixturize

Enable plugin

Enable the plugin your app/Config/bootstrap.php file:

    CakePlugin::load('Fixturize');

Usage

You can use this plugin in multiple ways, but typically, you'll want to start by importing an existing set of fixtures into a test database.

Load existing fixtures into a target connection

If you need to load your existing PHP based fixtures into a database (either for migrating them to a SQL based version or for quick visualization) then execute this command in the console:

./Console/cake Fixturize.fixture_loader app.event,app.tag,app.category --datasource test

It will load the comma separated list of fixtures schema and data into the datasource 'test'.

Load all fixtures from app or plugin

Instead of typing all the fixtures you need you can also simply not specify and, the shell will load all fixtures from the app then

./Console/cake Fixturize.fixture_loader --datasource test

To load all fixtures from a plugin you'll have to specify the plugin as well

./Console/cake Fixturize.fixture_loader --plugin SomePluginName --datasource test

Loading your fixtures from SQL files

When your amount of data is manageable, it's a good option to load it directly from SQL files that can be migrated, dumped again and managed with a versioning system like GIT.

Fixture SQL files can contain the table creation statement, any alter tables (for example foreign keys) and data inserts. But you can also manage the schema or the records via the $fields and $records property in your fixture as you would normally do if you define them in the fixture class.

If you choose to have the schema creation statements in the SQL file, make sure the CREATE statement contains IF NOT EXISTS.

Files should be stored in app/Test/Fixture/SQL/ or app/YourPlugin/Test/Fixture/SQL and have the .sql extension.

Example:

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

And that's all you need!

Loading your fixtures directly from a database

When the amount of data increases, you might consider having all your fixture data directly in a database, so you can copy schema and data directly from it before running each test. This is also considerably faster than loading fixture from a SQL file.

This requires creating a new database config in app/Config/database.php to connect to the seed database (the one containing the test data):

	public $test_seed = array(
		'datasource' => 'Database/Mysql',
		'persistent' => false,
		'host' => 'localhost',
		'login' => 'root',
		'password' => 'root',
		'database' => 'test_seed',
		'prefix' => '',
		'encoding' => 'utf8',
	);

Warning

Please ensure that you also have a $test database config that is pointing to a separate database.

$test_seed contains the seed data whereas $test is an empty database where the CakePHP Test logic will use to run queries on.

This is an example of using the import data fixture class:

	<?php

	App::uses('TableCopyTestFixture', 'Fixturize.TestSuite/Fixture');

	/**
	 * CategoryFixture
	 *
	 */
	class CategoryFixture extends TableCopyTestFixture {

	}

Yes, as easy as that!

lorenzo/cakephp-fixturize 适用场景与选型建议

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

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

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

围绕 lorenzo/cakephp-fixturize 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 13.41k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 29
  • 点击次数: 25
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 28
  • Watchers: 4
  • Forks: 7
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2013-06-05