Skip to content

Instantly share code, notes, and snippets.

@memememomo
Created June 3, 2014 07:28
Show Gist options
  • Save memememomo/a66289677843ec3d39e4 to your computer and use it in GitHub Desktop.
Save memememomo/a66289677843ec3d39e4 to your computer and use it in GitHub Desktop.
ComposerでPHPUnitをインストールし、FuelPHPでユニットテストを行う ref: http://qiita.com/uchiko/items/b62eafc18519a5970046
...
"require": {
...
"phpunit/phpunit": "4.1.*",
"piece/stagehand-testrunner": ">=3.5.0"
},
...
$ php composer.phar update
$ cp fuel/packages/oil/config/oil.php fuel/app/config/
$ php oil test --group=Core
$ php oil test --group=App
Tests Running...This may take a few moments.
PHPUnit 4.1.1-2-ge93a391 by Sebastian Bergmann.
Configuration read from /home/uchiko/project/fuelphp-1.7/fuel/core/phpunit.xml
...............
Time: 1.32 seconds, Memory: 16.25Mb
OK (15 tests, 18 assertions)
<?php
class MyInputFilters
{
// 文字エンコーディングの検証フィルタ
public static function check_encoding($value)
{
// 配列の場合は再帰的に処理
if (is_array($value))
{
array_map(['MyInputFilters', 'check_encoding'], $value);
return $value;
}
// 文字エンコーディングを検証
if (mb_check_encoding($value, Fuel::$encoding))
{
return $value;
}
else
{
// エラーの場合はログに記録
static::log_error('Invalid character encoding', $value);
// エラーを表示してシュル用
throw new HttpInvalidInputException('Invalid input data');
}
}
// エラーをログに記録
public static function log_error($msg, $value)
{
Log::error(
$msg . ': ' . Input::uri() . ' ' . urlencode($value) . ' ' .
Input::ip() . ' "' . Input::user_agent() . '"'
);
}
}
<?php
/**
* MyInputFilters class Tests
*
* @group App
*/
class Test_MyInputFilters extends TestCase
{
public function test_check_encoding_invalid_sjis()
{
$this->setExpectedException(
'HttpInvalidInputException', 'Invalid input data'
);
$input = mb_convert_encoding('SJISの文字列です。', 'SJIS');
$test = MyInputFilters::check_encoding($input);
}
public function test_check_encoding_valid()
{
$input = '正常なUTF-8の文字列です。';
$test = MyInputFilters::check_encoding($input);
$expected = $input;
$this->assertEquals($expected, $test);
}
}
'autoload_path' => '' ,
'binary_path' => DOCROOT.'fuel/vendor/bin/phpunit' ,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment