hi-media/pdo-tools
最新稳定版本:v1.1.1
Composer 安装命令:
composer require hi-media/pdo-tools
包简介
Simplifies the build of a database for testing purposes and allows you to easily verify the contents of a database after operations.
README 文档
README
pdo-tools simplifies the build of a database for testing purposes and allows you to easily verify the contents of a database after operations.
Specifically pdo-tools is a small PHP class extending PHPUnit_Framework_TestCase (see PHPUnit)
allowing to dynamically build entirely new test databases, with:
- no database, schema or privileges prerequisites,
- keep the last
Nbuilt databases (customizable, for monitoring and debugging), - easy fixtures,
- additional specific assertions,
- powerful combination of
PDO/PDOStatementmock objects and callbacks, e.g. to mock a third-party database, - ready-to-use with Jenkins and local PHPUnit.
Table of Contents
- Installation
- Usage
- Documentation
- Change log
- Contributions
- Versioning & Git branching model
- Copyrights & licensing
Installation
-
Class autoloading and dependencies are managed by Composer so install it following the instructions on Composer: Installation - *nix or just run the following command:
$ curl -sS https://getcomposer.org/installer | php -
Add dependency to
Himedia\PDOToolsinto require section of yourcomposer.json:{ "require": { "hi-media/pdo-tools": "1.*" } }and run
php composer.phar installfrom the terminal into the root folder of your project. -
Include Composer's autoloader:
<?php require_once 'vendor/autoload.php'; …
Usage
Instantiation
Example:
class MyTestCase extends DbTestCase { public function __construct($sName = null, array $aData = array(), $sDataName = '') { // BUILD_NUMBER environment variable is handled by Jenkins: $iSuffix = isset($_SERVER['BUILD_NUMBER']) ? $_SERVER['BUILD_NUMBER'] : floor(microtime(true)); $sTestDbName = "tests_$iSuffix"; $aDbBuilderDsn = array( 'driver' => 'pgsql', 'hostname' => 'localhost', 'port' => 5432, 'dbname' => $sTestDbName, 'username' => 'user', 'password' => '' ); $sDbBuildFile = '/path/to/buildfile.php'; parent::__construct($aDbBuilderDsn, array(), $sDbBuildFile); } }
where /path/to/buildfile.php (example here)
is a build file describing how to create a fresh database, roles/users and schemas,
with listing of fixtures to load
Simple test
Example including:
- fixture loading,
- comparison between result of executed SQL query, converted into CSV, and a CSV file,
- clean up
public function testSimple () { // Load SQL dump file, possibly gzipped (.gz): $this->loadSqlDumpFile('/path/to/dump.sql'); // calls to tested program… // Asserts that SQL query result is equal to CSV file content: $this->assertQueryResultEqualsCsv('SELECT … FROM A', '/path/to/expected.csv'); // Asserts that SQL query doesn't return any rows: $this->assertQueryReturnsNoRows('SELECT * FROM B'); // Optional clean up: $this->loadSqlDumpFile('/path/to/clean_up.sql'); }
where /path/to/fixture.sql is a typical SQL dump file, gzipped or not.
Note that in CSV files, following field's values are converted:
'∅'⇒null't'⇒true'f'⇒false
Mocking a third-party database
In this example, successives calls to PDOStatement::fetch() method on the third-party database…
- will return content of
/path/to/fixture.csvfile like calls toPDOStatement::fetch(PDO::FETCH_ASSOC)if initial statement ofPDO::query()was'SELECT … FROM A', andfalseif no more lines in specified CSV, - will return results of successives calls to the user callback
if initial statement of
PDO::query()was'SELECT … FROM B'
All queries are internally normalized to simplify matching.
public function testWithMockDb () { /* @var $oMockPDO \Himedia\DW\Tests\Mocks\PDO|\PHPUnit_Framework_MockObject_MockObject */ $oMockPDO = $this->getMock('Himedia\PDOTools\Mocks\PDO', array('query')); $that = $this; $oMockPDO->expects($this->any())->method('query')->will( $this->returnCallback( function ($sQuery) use ($that, $sResourcePath) { return $that->getPdoStmtMock( $sQuery, array( 'SELECT … FROM A' => '/path/to/fixture.csv', 'SELECT … FROM B' => function () { static $i = 0; return ++$i > 10 ? false : array('id' => $i, 'name' => md5(rand())); } ) ); } ) ); // injection of $oMockPDO, to mock third-party database… // calls to tested program… // assertions… }
Documentation
API documentation
is generated by ApiGen in the doc/api folder.
$ vendor/bin/apigen -c apigen.neon
Change log
See CHANGELOG file for details.
Contributions
All suggestions for improvement or direct contributions are very welcome. Please report bugs or ask questions using the Issue Tracker.
Versioning & Git branching model
For transparency into our release cycle and in striving to maintain backward compatibility, Padocc's engine is maintained under the Semantic Versioning guidelines. We'll adhere to these rules whenever possible.
The git branching model used for development is the one described and assisted by twgit tool: https://github.com/Twenga/twgit.
Copyrights & licensing
Licensed under the GNU Lesser General Public License v3 (LGPL version 3). See LICENSE file for details.
hi-media/pdo-tools 适用场景与选型建议
hi-media/pdo-tools 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.64k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2014 年 12 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「phpunit」 「Fixture」 「mock」 「pdo」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 hi-media/pdo-tools 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 hi-media/pdo-tools 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 hi-media/pdo-tools 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Behat redis context for testing
Dibi is Database Abstraction Library for PHP
HiDev plugin for PHPUnit
Testing Suite For Lumen like Laravel does.
Fixtures replacement with a straightforward definition syntax
A cli tool which generates unit tests.
统计信息
- 总下载量: 1.64k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 31
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: LGPL-3.0
- 更新时间: 2014-12-02