mbarquin/datapool 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

mbarquin/datapool

Composer 安装命令:

composer require mbarquin/datapool

包简介

PHP DataPool, data provider object for PHPUnit dataprovider tag

README 文档

README

Introduction

This library is intended to be used with PHPUnit tool. DataPool is an iterable object which can be returned to a standard @dataprovider tagged function or it can be used to get specific datasets from a big datapool.

  • It allows keeping low weight indexed array values separated from tests logic.
  • It can return specific datasets attending to its dataset index.
  • It can encapsulate dataset as an array to avoid large parameters lists.

Installation

You can install the component in the following ways:

Usage

The main use purpose is via heritage, the final class will only contains a definition index and a dataArray, we can instance it into our test case as a dataprovider or as a normal object which vill provide us with predefined test cases.

There are more use cases in the example file /example/tests/src/exampleContactsModelTest.php

Custom dataPool Example:

    /**
     * Contacts datapool file for Testing purposes
     */
    class ContactsDataPool extends \DataPool\DataPool
    {

        /**
         * @var array Datapoolm fields definition to be merged with data
         */
        protected $definition = array(
            'name',
            'surname',
            'phone',
            'result'
        );

        /**
         * @var array Datasets array to be merged with definition and returned to tests
         */
        public $dataArray = array(
            'Test1'                  => array('Jack', 'Travis', '555999666', true),
            'Test2'                  => array('Mathew', 'Jones', '555888555', true),
            'Test3.NameSurnameEmpty' => array('', '', '555666555', false),
            'TestCase1.PhoneToLong'  => array('Gregor', 'Jones', '5550005518899', false),
            'TestCase2.NoName'       => array('', 'Lock', '555000559', false)
        );

    }// End ContactsDataPool.

Protected array $definition is defined to avoid data indexes being duplicated along all defined datasets. Datasets can be returned with this $definition values as indexes, we can set up this behaviour with the function setReturnIndexes(false|true), by default setted to FALSE.

Public array $dataArray will contain an array with all possible tests datasets, it can be classified by index to allow returning smaller portions of this dataArray via getRowsByIndex($index).

###Tests file example:###

namespace Example\tests\src;

class exampleContactsModelTest extends \PHPUnit_Framework_TestCase {

    static private $dataPool = null;

    /**
     * Static instance of ContactsDataPool
     *
     * @return \Example\tests\files\ContactsDataPool
     */
    public function getDataPool() {
        // If not instanciated yet...
        if (is_object(self::$dataPool) === false) {
            self::$dataPool = new \Example\tests\files\ContactsDataPool();
        }
        // Avoid mixing behaviors during tests
        self::$dataPool->setReturnArray(false);
        self::$dataPool->setReturnIndexes(false);

        return self::$dataPool;
    }

Function getDataPool() is a dataprovider which sets and returns an iterable object. It can be used in any common case as standard @dataprovider function's return, it's configured to avoid any index usage as a usual PHPUnit dataprovider.

We have set:

setReturnArray(false) in order to get each dataset value as a different test function parameter.

setReturnIndexes(false) to avoid returned dataset indexation with $definition values (array_combine)



    public function getDataPoolAsArray() {
        $dataPool = $this->getDataPool();
        $dataPool->setReturnArray(true);
        $dataPool->setReturnIndexes(true);

        return $dataPool->getRowsByIndex('Case');
    }

Function getDataPoolAsArray() returns an iterable dataset from previously instanced dataProvider object. We want to get only test cases indexed by "Case" so we must return the result array from getRowsByIndex('Case') function.

We have changed:

setReturnArray(true) in order to get each dataset fields encapsulated in an array.

setReturnIndexes(true) to force the indexation of each returned dataset with $definition values (array_combine)



    /**
     * @dataProvider getDataPoolAsArray
     */
    public function testArrayDataProviderInsert($regData) {
        $contModel = new \Example\src\exampleContactsModel();
        $expected  = $regData['result'];
        unset($regData['result']);

        $result = $contModel->insert($regData);
        if ($result === false) {
            $this->assertEquals($expected, $result);
        } else {
            $this->assertTrue(is_integer($result));
        }
    }

As in testArrayDataProviderInsert, with data encapsulated as array we can easily perform tests on ORM objects or avoid large parameters lists in tests with many data values.



    /**
     * @dataProvider getDataPool
     */
    public function testPureDataProviderInsert($name, $surname, $phone, $expected) {
        $contModel      = new \Example\src\exampleContactsModel();

        $reg['name']    = $name;
        $reg['surname'] = $surname;
        $reg['phone']   = $phone;

        $result = $contModel->insert($reg);
        if ($result === false) {
            $this->assertEquals($expected, $result);
        } else {
            $this->assertTrue(is_integer($result));
        }
    }

}// End exampleContactsModelTest.

Function testPureDataProviderInsert makes assertions using ContactsDataPool object as a usual dataprovider array.

mbarquin/datapool 适用场景与选型建议

mbarquin/datapool 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 17 次下载、GitHub Stars 达 0, 最近一次更新时间为 2015 年 09 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2015-09-03