Skip to content

Instantly share code, notes, and snippets.

@araiguma
Last active November 6, 2017 07:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save araiguma/3b2dccc924c99e055227b2c5d05a2398 to your computer and use it in GitHub Desktop.
Save araiguma/3b2dccc924c99e055227b2c5d05a2398 to your computer and use it in GitHub Desktop.
unit test of Araiguma\Common\Validator\Custom.php
<?php
namespace Test;
use \Phalcon\Validation;
use \Araiguma\Common\Validator\Custom;
use \Phalcon\Validation\Message;
class CustomTest extends \UnitTestCase
{
/**
* @var \Validation
*/
private $validation;
/**
* 初期設定
*/
public function setUp()
{
parent::setUp();
$this->validation = new Validation();
}
/**
* デストラクタ
*/
public function __destruct()
{
}
public function provider_for_paramsがnullの場合の確認テスト()
{
return array(
array(array('val'=>1), null, 0, 'custom関数がtrueを返すのでOK'),
array(array('val'=>2), null, 1, 'custom関数がfalseを返すのでNG'),
);
}
/**
* @dataProvider provider_for_paramsがnullの場合の確認テスト
* @test
*/
public function paramsがnullの場合の確認テスト($arr, $params, $errCount, $message)
{
$func = function($validation, $attribute, $value) use ($params)
{
if ($value != 1)
{
$validation->appendMessage(new Message("test"));
return false;
}
return true;
};
$this->validation->add(
'val',
new Custom($func)
);
$messages = $this->validation->validate($arr);
$this->assertSame(count($messages),$errCount, $message);
}
public function provider_for_paramsに値を入れた場合のテスト()
{
return array(
array(array('val'=>1), 1, 0, 'custom関数がtrueを返すのでOK'),
array(array('val'=>2), 2, 0, 'custom関数がfalseを返すのでNG'),
array(array('val'=>2), 3, 1, 'custom関数がfalseを返すのでNG'),
);
}
/**
* @dataProvider provider_for_paramsに値を入れた場合のテスト
* @test
*/
public function paramsに値を入れた場合のテスト($arr, $param, $errCount, $message)
{
$func = function($validation, $attribute, $value) use ($param){
if ($value != $param)
{
$validation->appendMessage(new Message("test"));
return false;
}
return true;
};
$this->validation->add(
'val',
new Custom($func)
);
$messages = $this->validation->validate($arr);
$this->assertSame(count($messages),$errCount, $message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment