| Server IP : 210.245.233.93 / Your IP : 216.73.216.226 Web Server : Apache/2.2.15 (CentOS) System : Linux webserver2.onesolution.com.hk 2.6.32-754.35.1.el6.x86_64 #1 SMP Sat Nov 7 12:42:14 UTC 2020 x86_64 User : apache ( 48) PHP Version : 5.3.3 Disable Function : exec, shell_exec, system, passthru, popen, proc_open, pcntl_exec MySQL : ON | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /var/www/(Del)gepgroup.hk/php-activerecord/test/ |
Upload File : |
<?php
include 'helpers/config.php';
class BookFormat extends ActiveRecord\Model
{
static $table = 'books';
static $validates_format_of = array(
array('name')
);
};
class ValidatesFormatOfTest extends DatabaseTest
{
public function set_up($connection_name=null)
{
parent::set_up($connection_name);
BookFormat::$validates_format_of[0] = array('name');
}
public function test_format()
{
BookFormat::$validates_format_of[0]['with'] = '/^[a-z\W]*$/';
$book = new BookFormat(array('author_id' => 1, 'name' => 'testing reg'));
$book->save();
$this->assert_false($book->errors->is_invalid('name'));
BookFormat::$validates_format_of[0]['with'] = '/[0-9]/';
$book = new BookFormat(array('author_id' => 1, 'name' => 12));
$book->save();
$this->assert_false($book->errors->is_invalid('name'));
}
public function test_invalid_null()
{
BookFormat::$validates_format_of[0]['with'] = '/[^0-9]/';
$book = new BookFormat;
$book->name = null;
$book->save();
$this->assert_true($book->errors->is_invalid('name'));
}
public function test_invalid_blank()
{
BookFormat::$validates_format_of[0]['with'] = '/[^0-9]/';
$book = new BookFormat;
$book->name = '';
$book->save();
$this->assert_true($book->errors->is_invalid('name'));
}
public function test_valid_blank_andallow_blank()
{
BookFormat::$validates_format_of[0]['allow_blank'] = true;
BookFormat::$validates_format_of[0]['with'] = '/[^0-9]/';
$book = new BookFormat(array('author_id' => 1, 'name' => ''));
$book->save();
$this->assert_false($book->errors->is_invalid('name'));
}
public function test_valid_null_and_allow_null()
{
BookFormat::$validates_format_of[0]['allow_null'] = true;
BookFormat::$validates_format_of[0]['with'] = '/[^0-9]/';
$book = new BookFormat();
$book->author_id = 1;
$book->name = null;
$book->save();
$this->assert_false($book->errors->is_invalid('name'));
}
/**
* @expectedException ActiveRecord\ValidationsArgumentError
*/
public function test_invalid_lack_of_with_key()
{
$book = new BookFormat;
$book->name = null;
$book->save();
}
/**
* @expectedException ActiveRecord\ValidationsArgumentError
*/
public function test_invalid_with_expression_as_non_string()
{
BookFormat::$validates_format_of[0]['with'] = array('test');
$book = new BookFormat;
$book->name = null;
$book->save();
}
public function test_invalid_with_expression_as_non_regexp()
{
BookFormat::$validates_format_of[0]['with'] = 'blah';
$book = new BookFormat;
$book->name = 'blah';
$book->save();
$this->assert_true($book->errors->is_invalid('name'));
}
public function test_custom_message()
{
BookFormat::$validates_format_of[0]['message'] = 'is using a custom message.';
BookFormat::$validates_format_of[0]['with'] = '/[^0-9]/';
$book = new BookFormat;
$book->name = null;
$book->save();
$this->assert_equals('is using a custom message.', $book->errors->on('name'));
}
};
?>