/var/www/(Del)gepgroup.hk/php-activerecord/test/ValidatesFormatOfTest.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?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'));
    }
};
?>