定制 hi-media/pdo-tools 二次开发

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

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

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 N built databases (customizable, for monitoring and debugging),
  • easy fixtures,
  • additional specific assertions,
  • powerful combination of PDO/PDOStatement mock objects and callbacks, e.g. to mock a third-party database,
  • ready-to-use with Jenkins and local PHPUnit.

Table of Contents

Installation

  1. 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
  2. Add dependency to Himedia\PDOTools into require section of your composer.json:

    {
        "require": {
            "hi-media/pdo-tools": "1.*"
        }
    }

    and run php composer.phar install from the terminal into the root folder of your project.

  3. 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.csv file like calls to PDOStatement::fetch(PDO::FETCH_ASSOC) if initial statement of PDO::query() was 'SELECT … FROM A', and false if 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 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 1.64k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 31
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 2
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: LGPL-3.0
  • 更新时间: 2014-12-02