Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
15403 manish.sha 1
<?php
2
/**
3
 * Tests subset validations
4
 *
5
 * PHP version 5
6
 *
7
 * Copyright (c) 2012, Radig Soluções em TI (http://radig.com.br)
8
 *
9
 * Licensed under The MIT License
10
 * Redistributions of files must retain the above copyright notice.
11
 *
12
 * @filesource
13
 * @copyright     Copyright (c) 2012, Radig Soluções em Ti (http://radig.com.br)
14
 * @link          http://github.com/radig/
15
 * @package       Mongodb
16
 * @subpackage    Mongodb.Test.Case.Behavior
17
 * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
18
 */
19
App::uses('Model', 'Model');
20
App::uses('AppModel', 'Model');
21
 
22
 
23
/**
24
 * MyCompany class
25
 *
26
 * @uses          Post
27
 * @package       Mongodb
28
 * @subpackage    Mongodb.Test.Case.Behavior
29
 */
30
class MyCompany extends AppModel {
31
 
32
/**
33
 * useDbConfig property
34
 * DataSource automatically prepend 'test_' to this name
35
 *
36
 * @var string 'mongo'
37
 * @access public
38
 */
39
    public $useDbConfig = 'mongo';
40
 
41
/**
42
 * mongoSchema property
43
 * MongoDb Schema for this model
44
 *
45
 * @var array
46
 * @access public
47
 */
48
    public $mongoSchema = array(
49
        'name'  => array('type' => 'string'),
50
        'address' => array(
51
            'street'  => array('type' => 'string'),
52
            'number'  => array('type' => 'number'),
53
        ),
54
    );
55
 
56
/**
57
 * actsAs property
58
 *
59
 * @var array
60
 * @access public
61
 */
62
    public $actsAs = array(
63
        'Mongodb.SubCollectionValidator'
64
    );
65
 
66
/**
67
 * validate property
68
 *
69
 * @var array
70
 * @access public
71
 */
72
    public $validate = array(
73
        'name' => 'notempty'
74
    );
75
 
76
/**
77
 * collection validate property
78
 *
79
 * @var array
80
 * @access public
81
 */
82
    public $collectionValidate = array(
83
        'address' => array(
84
            'street' => array(
85
                'rule' => array('notempty'),
86
                'message' => 'only letters and numbers'
87
            ),
88
            'number' => array(
89
                'rule' => 'numeric'
90
            )
91
        )
92
    );
93
}
94
 
95
/**
96
 * SubCollectionValidatorBehaviorTest class
97
 *
98
 * @uses          CakeTestCase
99
 * @package       Mongodb
100
 * @subpackage    Mongodb.Test.Case.Behavior
101
 */
102
class SubCollectionValidatorBehaviorTest extends CakeTestCase {
103
 
104
/**
105
 * Sets up the environment for each test method
106
 *
107
 * @return void
108
 * @access public
109
 */
110
    public function setUp() {
111
        $this->Company = ClassRegistry::init(array('class' => 'MyCompany', 'ds' => 'test_mongo'), true);
112
    }
113
 
114
    public function startTest($method) {
115
        //clear Company attributes
116
        $this->Company->create();
117
    }
118
 
119
/**
120
 * Destroys the environment after each test method is run
121
 *
122
 * @return void
123
 * @access public
124
 */
125
    public function tearDown() {
126
        unset($this->Company);
127
    }
128
 
129
/**
130
 * testValidateFailure method
131
 *
132
 * @return void
133
 * @access public
134
 */
135
    public function testValidateFailure() {
136
        $expected = false;
137
        $result = $this->Company->save(array(
138
            'name' => 'Radig',
139
            'address' => array('street' => null, 'number' => 141)
140
        ));
141
        $this->assertEqual($expected, $result);
142
 
143
        $expected = array('street' => array('only letters and numbers'));
144
        $result = $this->Company->validationErrors;
145
        $this->assertEqual($expected, $result);
146
    }
147
 
148
    /**
149
 * testValidateSuccess method
150
 *
151
 * @return void
152
 * @access public
153
 */
154
    public function testValidateSuccess() {
155
        $data = array(
156
            'name' => 'Radig',
157
            'address' => array('street' => 'abc123', 'number' => 141)
158
        );
159
 
160
        $result = $this->Company->save($data);
161
        $this->assertNotEmpty($result);
162
 
163
        $expected = array();
164
        $result = $this->Company->validationErrors;
165
        $this->assertEqual($expected, $result);
166
    }
167
}