PHP phalconユニット試験phpunit

4123 ワード

1 PHPUnit等の取り付け
composer
{
    "require": {
        "phalcon/incubator": "dev-master",
        "phpunit/phpunit": "4.8.*"
    }
}

2.PHPUnitをPhalconに統合
app、publicの兄弟ディレクトリの下にフォルダtestsを作成する
app/
public/
tests/
3.testsディレクトリの下にファイルを作成する
TestHelper.php--テストアシストファイル
PHPunit.xml--プロファイル
UnitTestCase.php--テストベースクラス--PhalconTestCaseからPHPUnit_から継承Framework_TestCase
UnitTest.php -- demo
ソースコード
TestHelper.php
<?php

use Phalcon\DI;
use Phalcon\DI\FactoryDefault;

ini_set('display_errors',1);
error_reporting(E_ALL);

define('ROOT_PATH', __DIR__);
define('PATH_LIBRARY', __DIR__ . '/../app/library/');
define('PATH_SERVICES', __DIR__ . '/../app/services/');
define('PATH_RESOURCES', __DIR__ . '/../app/resources/');

set_include_path(
    ROOT_PATH . PATH_SEPARATOR . get_include_path()
);

// Required for phalcon/incubator
include __DIR__ . "/../../vendor/autoload.php";

// Use the application autoloader to autoload the classes
// Autoload the dependencies found in composer
$loader = new \Phalcon\Loader();

$loader->registerDirs(
    array(
        ROOT_PATH
    )
);

$loader->register();

$di = new FactoryDefault();
DI::reset();

// Add any needed services to the DI here

DI::setDefault($di);

PHPunit.xml
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="./TestHelper.php"
         backupGlobals="false"
         backupStaticAttributes="false"
         verbose="true"
         colors="false"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false"
         syntaxCheck="true">
    <testsuite name="Phalcon - Testsuite">
        <directory>./</directory>
    </testsuite>
</phpunit>

UnitTestCase.php
<?php

use Phalcon\DI;
use Phalcon\Test\UnitTestCase as PhalconTestCase;

abstract class UnitTestCase extends PhalconTestCase
{
    /**
     * @var \Voice\Cache
     */
    protected $_cache;

    /**
     * @var \Phalcon\Config
     */
    protected $_config;

    /**
     * @var bool
     */
    private $_loaded = false;

    public function setUp(Phalcon\DiInterface $di = NULL, Phalcon\Config $config = NULL)
    {
        // Load any additional services that might be required during testing
        $di = DI::getDefault();

        // Get any DI components here. If you have a config, be sure to pass it to the parent

        parent::setUp($di);

        $this->_loaded = true;
    }

    /**
     * Check if the test case is setup properly
     *
     * @throws \PHPUnit_Framework_IncompleteTestError;
     */
    public function __destruct()
    {
        if (!$this->_loaded) {
            throw new \PHPUnit_Framework_IncompleteTestError('Please run parent::setUp().');
        }
    }
}

UnitTest.php
<?php

namespace Test;

/**
 * Class UnitTest
 */
class UnitTest extends \UnitTestCase
{
    public function testTestCase()
    {
        $this->assertEquals('works',
            'works',
            'This is OK'
        );

        $this->assertEquals('works',
            'works1',
            'This will fail'
        );
    }
}

4.PHPUnitの実行
コマンドラインをプロジェクトの下のtestsディレクトリに切り替えます.
PHPUnit 3.7.23 by Sebastian Bergmann. Configuration read from /private/var/www/tests/phpunit.xml Time: 3 ms, Memory: 3.25Mb There was 1 failure: 1) Test\UnitTest::testTestCase This will fail Failed asserting that two strings are equal. --- Expected +++ Actual @@ @@ -'works' +'works1' /private/var/www/tests/Test/UnitTest.php:25 FAILURES! Tests: 1, Assertions: 2, Failures: 1.