定制 metamodels/phpunit-contao-database 二次开发

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

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

metamodels/phpunit-contao-database

最新稳定版本:1.0.1

Composer 安装命令:

composer require metamodels/phpunit-contao-database

包简介

MetaModels phpunit contao database classes

README 文档

README

What you can expect here.

This repository holds a small subset of classes that can be used to fake a Contao database connection.

To do so, you define the queries and their result sets.

Installation

Add "metamodels/phpunit-contao-database": "~1.0" to your composer.json in the require-dev section and you are set.

How to use it.

In your unit test you simply replace the original Contao database classes by calling the following code (before the original Contao database classes get loaded via autoloading!). A good place might be the setUp()-method of your test case.

class MyTest extends \PHPUnit_Framework_TestCase
{
    /**
     * Register the database.
     *
     * @return void
     */
    public function setUp()
    {
        \MetaModels\Test\Contao\Database::register();
    }
}

Using it in unit tests

You should use dependency injection for proper unit testing, then you can use something like this:

$objectToTest = new ClassToTest();
$database     = \MetaModels\Test\Contao\Database::getNewTestInstance();

// Inject the database into the instance.
$objectToTest->setDatabase($database);

$database
    ->getQueryCollection()
    ->theQuery('SELECT * FROM test WHERE id=?')
    ->with(1)
    ->result()
        ->addRow(
            array(
              'id'     => 1,
              'tstamp' => 343094400,
              'title'  => 'test'
            ),
        );

// This method will call the above query internally and will receive the given result.
$result = $objectToTest->testMethod();

$this->assertEquals(1, $result->getId());
$this->assertEquals(343094400, $result->getModificationTime());
$this->assertEquals('test', $result->getTitle());

If you should be in the unfortunate position that you can not use dependency injection, as the underlying code is using Database::getInstance(), you are not out of luck. You can use the method $database = \MetaModels\Test\Contao\Database::getNewTestInstance(); which will return the "default" database instance then.

However using this approach is not suggested as the instance will be shared over all unit tests.

统计信息

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

GitHub 信息

  • Stars: 2
  • Watchers: 5
  • Forks: 0
  • 开发语言: PHP

其他信息

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

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固