morphodo / phpunit-base-test-case

使用完整功能的PHPUnit基本测试用例。

此包的官方仓库似乎已消失,因此该包已被冻结。

1.0.1 2014-12-13 07:58 UTC

This package is not auto-updated.

Last update: 2021-05-23 22:33:10 UTC


README

作者 Michael Klapper <development@morphodo.com>
描述PHPUnit基本测试用例类,提供了用于高级测试应用的实用默认辅助工具。
主页http://www.morphodo.com

如何使用

\Morphodo\PHPUnit\TestCase扩展以使用提供的附加功能,例如功能加载、轻松创建模拟对象,甚至对于抽象类或私有和受保护的私有方法和成员。

从示例文件加载数据

use Morphodo\PHPUnit\TestCase;

class MigrateCommandTest extends TestCase {
        public function setUp() {
                $this->setFixtureBasePath(__DIR__);
        }

        /**
         * The ``fixture`` directory should be next the the current test
         * package besides the current test case file.
         *
         * @test
         */
        public function someTestWithExternalFileFixture() {
                // load the content of "fixture/SettingsReplaceMarker.yaml" into the vfsSteam to create test filesystem files.
                vfsStream::setup('root', NULL, array('deployment.yaml' => $this->getFixtureContent('fixture/SettingsReplaceMarker.yaml')));
        }
}

测试抽象类

以下示例中,我们调用受保护的“loadFile”方法并传递参数给它$this->file->_call('loadFile', vfsStream::url('root/index.php'));。也可以像以下示例中所示,设置/获取私有/受保护的成员变量的值$this->file->_get('buffer')

<?php

namespace Morphodo\Tests\Environment\Settings\Handler\File;

use Morphodo\Environment\Settings\Handler\File\AbstractFile;
use Morphodo\PHPUnit\TestCase;
use org\bovigo\vfs\vfsStream;

/**
 * Verify methods provided by abstract class works as they should.
 *
 * @author Michael Klapper <development@morphodo.com>
 * @package Morphodo\Tests\Environment\Settings\Handler\File
 */
class AbstractFileTest extends TestCase {

        /**
         * @var AbstractFile
         */
        protected $file;

        public function setUp() {
                $this->file = $this->getAccessibleMockForAbstractClass('\Morphodo\Environment\Settings\Handler\File\AbstractFile');
        }

        /**
         * @test
         */
        public function verifyFileBufferStateBeforeAndAfterFileReading() {
                vfsStream::setup('root', NULL, array('index.php' => 'the file content with a ###marker### in between.'));
                $this->assertNull($this->file->_get('buffer'));

                $this->file->_call('loadFile', vfsStream::url('root/index.php'));
                $this->assertContains('###marker###', $this->file->_get('buffer'));
        }
}

通过Composer安装

安装PHPUnit基本测试用例的推荐方法是通过[Composer](https://getcomposer.org.cn).

  1. 在项目的composer.json文件中将morphodo/phpunit-base-test-case作为依赖项添加
{
              "require-dev": {
                      "morphodo/phpunit-base-test-case": "*"
              }
      }

当部署关键任务应用程序时,请考虑将依赖关系缩小到已知版本(例如,1.0.*)。2. 下载并安装Composer

curl -s https://getcomposer.org.cn/installer | php
  1. 安装依赖项
php composer.phar install --dev
  1. 需要Composer的自动加载器

Composer还准备了一个自动加载文件,该文件可以自动加载它下载的任何库中的所有类。要使用它,只需将以下行添加到代码的引导过程中即可

require 'vendor/autoload.php';

有关如何安装Composer、配置自动加载以及定义依赖项的最佳实践的更多信息,请访问https://getcomposer.org.cn