/var/www/(Del)gepgroup.hk/php-activerecord/test/ModelCallbackTest.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
<?php
include 'helpers/config.php';

class 
ModelCallbackTest extends DatabaseTest
{
    public function 
set_up($connection_name=null)
    {
        
parent::set_up($connection_name);

        
$this->venue = new Venue();
        
$this->callback Venue::table()->callback;
    }

    public function 
register_and_invoke_callbacks($callbacks$return$closure)
    {
        if (!
is_array($callbacks))
            
$callbacks = array($callbacks);

        
$fired = array();

        foreach (
$callbacks as $name)
            
$this->callback->register($name,function($model) use (&$fired$name$return) { $fired[] = $name; return $return; });

        
$closure($this->venue);
        return 
array_intersect($callbacks,$fired);
    }

    public function 
assert_fires($callbacks$closure)
    {
        
$executed $this->register_and_invoke_callbacks($callbacks,true,$closure);
        
$this->assert_equals(count($callbacks),count($executed));
    }

    public function 
assert_does_not_fire($callbacks$closure)
    {
        
$executed $this->register_and_invoke_callbacks($callbacks,true,$closure);
        
$this->assert_equals(0,count($executed));
    }

    public function 
assert_fires_returns_false($callbacks$only_fire$closure)
    {
        if (!
is_array($only_fire))
            
$only_fire = array($only_fire);

        
$executed $this->register_and_invoke_callbacks($callbacks,false,$closure);
        
sort($only_fire);
        
$intersect array_intersect($only_fire,$executed);
        
sort($intersect);
        
$this->assert_equals($only_fire,$intersect);
    }

    public function 
test_after_construct_fires_by_default()
    {
        
$this->assert_fires('after_construct',function($model) { new Venue(); });
    }

    public function 
test_fire_validation_callbacks_on_insert()
    {
        
$this->assert_fires(array('before_validation','after_validation','before_validation_on_create','after_validation_on_create'),
            function(
$model) { $model = new Venue(); $model->save(); });
    }

    public function 
test_fire_validation_callbacks_on_update()
    {
        
$this->assert_fires(array('before_validation','after_validation','before_validation_on_update','after_validation_on_update'),
            function(
$model) { $model Venue::first(); $model->save(); });
    }

    public function 
test_validation_call_backs_not_fired_due_to_bypassing_validations()
    {
        
$this->assert_does_not_fire('before_validation',function($model) { $model->save(false); });
    }

    public function 
test_before_validation_returning_false_cancels_callbacks()
    {
        
$this->assert_fires_returns_false(array('before_validation','after_validation'),'before_validation',
            function(
$model) { $model->save(); });
    }

    public function 
test_fires_before_save_and_before_update_when_updating()
    {
        
$this->assert_fires(array('before_save','before_update'),
            function(
$model) { $model Venue::first(); $model->name "something new"$model->save(); });
    }

    public function 
test_before_save_returning_false_cancels_callbacks()
    {
        
$this->assert_fires_returns_false(array('before_save','before_create'),'before_save',
            function(
$model) { $model = new Venue(); $model->save(); });
    }

    public function 
test_destroy()
    {
        
$this->assert_fires(array('before_destroy','after_destroy'),
            function(
$model) { $model->delete(); });
    }
}
?>