9
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Posted at

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にコピーしないと動かないかもしれません。

9
9
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?