Skip to content

Instantly share code, notes, and snippets.

@araiguma araiguma/CustomTest.php

Last active Nov 6, 2017
Embed
What would you like to do?
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_paramsnullの場合の確認テスト()
{
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 paramsnullの場合の確認テスト($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
You can’t perform that action at this time.