😀

FuelPHP【テスト】-- oil testの拡張 --

2022/11/28に公開

FuelPHPのoil testが重い。。。。
カレントディレクトリからの実行だからテストが増えれば増えるはど重くなる。。。。
なんで単体でテスト書いてる時はファイル指定で実行できるタスクを作成してみました。

環境

  • centos6.3
  • fuelPHP1.5.3

事前準備に必要なもの

参考にしたサイト

使い方

下記コマンドでご使用ください。

php oil r test --file=fuel/core/tests/arr.php 
Tests Running...This may take a few moments.
PHPUnit 3.7.19 by Sebastian Bergmann.

Configuration read from /home/web/lo.arena.branch.org/arena/fuel/app/phpunit.xml

....................................

Time: 0 seconds, Memory: 5.25Mb

OK (36 tests, 43 assertions)

oil testとの比較

標準のoil test

php oil t --group=Arr 

Tests Running...This may take a few moments.
Notice - Undefined variable: obj in PKGPATH/rap/tests/phpcs/IncrementDecrementSpacing/object_nospacing_pass.php on line 2
Warning - Creating default object from empty value in PKGPATH/rap/tests/phpcs/IncrementDecrementSpacing/object_nospacing_pass.php on line 2
Notice - Undefined variable: obj in PKGPATH/rap/tests/phpcs/IncrementDecrementSpacing/object_spacing.php on line 2
Warning - Creating default object from empty value in PKGPATH/rap/tests/phpcs/IncrementDecrementSpacing/object_spacing.php on line 2
PHPUnit 3.7.19 by Sebastian Bergmann.

Configuration read from /home/web/lo.arena.branch.org/arena/fuel/app/phpunit.xml

....................................

Time: 1 second, Memory: 13.00Mb

OK (36 tests, 43 assertions)

拡張タスク

php oil r test --group=Arr --file=fuel/core/tests/arr.php

Tests Running...This may take a few moments.
PHPUnit 3.7.19 by Sebastian Bergmann.

Configuration read from /home/web/lo.arena.branch.org/arena/fuel/app/phpunit.xml

....................................

Time: 0 seconds, Memory: 5.25Mb

OK (36 tests, 43 assertions)

メモリの少量が約3/1に減りました。

taskの拡張本体

fuel/app/tasks/test.php
<?php
namespace Fuel\Tasks;
use \Config as Config;
use \Arr;

/**
 * oil test拡張クラス
 *
 */
class Test
{

	public function __construct()
	{
	}

	public function run()
	{
		$this->_invoke();
	}

	/**
	 * ヘルプの表示を行う
	 */
	public function help()
	{
		echo <<<HELP
使用法:
    php oil refine test --file=実行したいPHPunitのtestファイル
    php oil r test --file=実行したいPHPunitのtestファイル

コマンド一覧:
    help     ヘルプの表示
    test    phpunitの実行

説明:
    phpunitでファイル指定で実行できるようにします。(高速)

例:
    php oil r test --file=実行したいPHPunitのtestファイル

HELP;

	}

	private function _invoke()
	{
		// Suppressing this because if the file does not exist... well thats a bad thing and we can't really check
		// I know that supressing errors is bad, but if you're going to complain: shut up. - Phil
		$phpunit_autoload_path = \Config::get('oil.phpunit.autoload_path', 'PHPUnit/Autoload.php' );
		@include_once($phpunit_autoload_path);

		// Attempt to load PHUnit.  If it fails, we are done.
		if ( ! class_exists('PHPUnit_Framework_TestCase'))
		{
			throw new Exception('PHPUnit does not appear to be installed.'.PHP_EOL.PHP_EOL."\tPlease visit http://phpunit.de and install.");
		}

		// Check for a custom phpunit config, but default to the one from core
		if (file_exists(APPPATH.'phpunit.xml'))
		{
			$phpunit_config = APPPATH.'phpunit.xml';
		}
		else
		{
			$phpunit_config = COREPATH.'phpunit.xml';
		}

		// CD to the root of Fuel and call up phpunit with the path to our config
		$phpunit_command = \Config::get('oil.phpunit.binary_path', 'phpunit');
		$command = 'cd '.DOCROOT.'; '.$phpunit_command.' -c "'.$phpunit_config.'"';

		// Respect the group options
		\Cli::option('group') and $command .= ' --group '.\Cli::option('group');
		\Cli::option('exclude-group') and $command .= ' --exclude-group '.\Cli::option('exclude-group');

		// Respect the coverage-html option
		\Cli::option('coverage-html') and $command .= ' --coverage-html '.\Cli::option('coverage-html');
		\Cli::option('coverage-clover') and $command .= ' --coverage-clover '.\Cli::option('coverage-clover');
		\Cli::option('coverage-text') and $command .= ' --coverage-text='.\Cli::option('coverage-text');
		\Cli::option('coverage-php') and $command .= ' --coverage-php '.\Cli::option('coverage-php');
		\Cli::option('file') and $command .= ' '.\Cli::option('file');

		\Cli::write('Tests Running...This may take a few moments.', 'green');

		$return_code = 0;
		// var_dump($command); exit;
		foreach(explode(';', $command) as $c)
		{
			passthru($c, $return_code_task);
			// Return failure if any subtask fails
			$return_code |= $return_code_task;
		}
		exit($return_code);
	}

}

もしかしたらfuel/core/phpunit.xmlをfuel/app/phpunit.xmlにコピーしないと動かないかもしれません。

Discussion