ValidatorTestCase.php 12.4 KB
Newer Older
1
<?php
zYne's avatar
zYne committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 *  $Id$
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information, see
 * <http://www.phpdoctrine.com>.
 */

22
/**
zYne's avatar
zYne committed
23
 * Doctrine_Validator_TestCase
24 25 26
 * TestCase for Doctrine's validation component.
 * 
 * @todo More tests to cover the full interface of Doctrine_Validator_ErrorStack.
zYne's avatar
zYne committed
27 28 29 30 31 32 33 34
 *
 * @package     Doctrine
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @category    Object Relational Mapping
 * @link        www.phpdoctrine.com
 * @since       1.0
 * @version     $Revision$
35
 */
zYne's avatar
zYne committed
36
class Doctrine_Validator_TestCase extends Doctrine_UnitTestCase {
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 113 114 115 116 117 118 119 120 121
    public function prepareTables() {
        $this->tables[] = "ValidatorTest";
        parent::prepareTables();
    }

    /**
     * Tests correct type detection.
     */
    public function testIsValidType() {
        $var = "123";
        $this->assertTrue(Doctrine_Validator::isValidType($var,"string"));
        $this->assertTrue(Doctrine_Validator::isValidType($var,"integer"));
        $this->assertTrue(Doctrine_Validator::isValidType($var,"float"));
        $this->assertFalse(Doctrine_Validator::isValidType($var,"array"));
        $this->assertFalse(Doctrine_Validator::isValidType($var,"object"));

        $var = 123;
        $this->assertTrue(Doctrine_Validator::isValidType($var,"string"));
        $this->assertTrue(Doctrine_Validator::isValidType($var,"integer"));
        $this->assertTrue(Doctrine_Validator::isValidType($var,"float"));
        $this->assertFalse(Doctrine_Validator::isValidType($var,"array"));
        $this->assertFalse(Doctrine_Validator::isValidType($var,"object"));

        $var = 123.12;
        $this->assertTrue(Doctrine_Validator::isValidType($var,"string"));
        $this->assertFalse(Doctrine_Validator::isValidType($var,"integer"));
        $this->assertTrue(Doctrine_Validator::isValidType($var,"float"));
        $this->assertFalse(Doctrine_Validator::isValidType($var,"array"));
        $this->assertFalse(Doctrine_Validator::isValidType($var,"object"));

        $var = '123.12';
        $this->assertTrue(Doctrine_Validator::isValidType($var,"string"));
        $this->assertFalse(Doctrine_Validator::isValidType($var,"integer"));
        $this->assertTrue(Doctrine_Validator::isValidType($var,"float"));
        $this->assertFalse(Doctrine_Validator::isValidType($var,"array"));
        $this->assertFalse(Doctrine_Validator::isValidType($var,"object"));

        $var = '';
        $this->assertTrue(Doctrine_Validator::isValidType($var,"string"));
        $this->assertFalse(Doctrine_Validator::isValidType($var,"integer"));
        $this->assertFalse(Doctrine_Validator::isValidType($var,"float"));
        $this->assertFalse(Doctrine_Validator::isValidType($var,"array"));
        $this->assertFalse(Doctrine_Validator::isValidType($var,"object"));

        $var = null;
        $this->assertTrue(Doctrine_Validator::isValidType($var,"string"));
        $this->assertTrue(Doctrine_Validator::isValidType($var,"integer"));
        $this->assertTrue(Doctrine_Validator::isValidType($var,"float"));
        $this->assertTrue(Doctrine_Validator::isValidType($var,"array"));
        $this->assertTrue(Doctrine_Validator::isValidType($var,"object"));

        $var = 'str';
        $this->assertTrue(Doctrine_Validator::isValidType($var,"string"));
        $this->assertFalse(Doctrine_Validator::isValidType($var,"integer"));
        $this->assertFalse(Doctrine_Validator::isValidType($var,"float"));
        $this->assertFalse(Doctrine_Validator::isValidType($var,"array"));
        $this->assertFalse(Doctrine_Validator::isValidType($var,"object"));

        $var = array();
        $this->assertFalse(Doctrine_Validator::isValidType($var,"string"));
        $this->assertFalse(Doctrine_Validator::isValidType($var,"integer"));
        $this->assertFalse(Doctrine_Validator::isValidType($var,"float"));
        $this->assertTrue(Doctrine_Validator::isValidType($var,"array"));
        $this->assertFalse(Doctrine_Validator::isValidType($var,"object"));
        
        $var = new Exception();
        $this->assertFalse(Doctrine_Validator::isValidType($var,"string"));
        $this->assertFalse(Doctrine_Validator::isValidType($var,"integer"));
        $this->assertFalse(Doctrine_Validator::isValidType($var,"float"));
        $this->assertFalse(Doctrine_Validator::isValidType($var,"array"));
        $this->assertTrue(Doctrine_Validator::isValidType($var,"object"));
    }

    /**
     * Tests Doctrine_Validator::validateRecord()
     */
    public function testValidate2() {
        $test = new ValidatorTest();
        $test->mymixed = "message";
        $test->myrange = 1;
        $test->myregexp = '123a';
        
        $validator = new Doctrine_Validator();
        $validator->validateRecord($test);

zYne's avatar
zYne committed
122
        $stack = $test->errorStack();
123 124 125

        $this->assertTrue($stack instanceof Doctrine_Validator_ErrorStack);

126 127 128 129
        $this->assertTrue(in_array('notnull', $stack['mystring']));
        $this->assertTrue(in_array('notblank', $stack['myemail2']));
        $this->assertTrue(in_array('range', $stack['myrange']));
        $this->assertTrue(in_array('regexp', $stack['myregexp']));
130 131 132 133 134 135 136 137 138 139
        $test->mystring = 'str';


        $test->save();
    }

    /**
     * Tests Doctrine_Validator::validateRecord()
     */
    public function testValidate() {
zYne's avatar
zYne committed
140
        $user = $this->connection->getTable('User')->find(4);
141

zYne's avatar
zYne committed
142 143 144 145
        $set = array('password' => 'this is an example of too long password',
                     'loginname' => 'this is an example of too long loginname',
                     'name' => 'valid name',
                     'created' => 'invalid');
146 147
        $user->setArray($set);
        $email = $user->Email;
zYne's avatar
zYne committed
148
        $email->address = 'zYne@invalid';
149 150 151 152 153 154 155

        $this->assertTrue($user->getModified() == $set);

        $validator = new Doctrine_Validator();
        $validator->validateRecord($user);


zYne's avatar
zYne committed
156
        $stack = $user->errorStack();
157 158

        $this->assertTrue($stack instanceof Doctrine_Validator_ErrorStack);
159 160 161
        $this->assertTrue(in_array('length', $stack['loginname']));
        $this->assertTrue(in_array('length', $stack['password']));
        $this->assertTrue(in_array('type', $stack['created']));
162 163

        $validator->validateRecord($email);
zYne's avatar
zYne committed
164
        $stack = $email->errorStack();
165
        $this->assertTrue(in_array('email', $stack['address']));
zYne's avatar
zYne committed
166
        $email->address = 'arnold@example.com';
167 168

        $validator->validateRecord($email);
zYne's avatar
zYne committed
169
        $stack = $email->errorStack();
170

171
        $this->assertTrue(in_array('unique', $stack['address']));
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
    }

    /**
     * Tests the Email validator. (Doctrine_Validator_Email)
     */
    public function testIsValidEmail() {

        $validator = new Doctrine_Validator_Email();

        $email = $this->connection->create("Email");
        $this->assertFalse($validator->validate($email,"address","example@example",null));
        $this->assertFalse($validator->validate($email,"address","example@@example",null));
        $this->assertFalse($validator->validate($email,"address","example@example.",null));
        $this->assertFalse($validator->validate($email,"address","example@e..",null));

        $this->assertFalse($validator->validate($email,"address","example@e..",null));

        $this->assertTrue($validator->validate($email,"address","null@pookey.co.uk",null));
        $this->assertTrue($validator->validate($email,"address","null@pookey.com",null));
        $this->assertTrue($validator->validate($email,"address","null@users.doctrine.pengus.net",null));

    }

    /**
     * Tests saving records with invalid attributes.
     */
    public function testSave() {
        $this->manager->setAttribute(Doctrine::ATTR_VLD, true);
zYne's avatar
zYne committed
200
        $this->manager->setAttribute(Doctrine::ATTR_AUTO_LENGTH_VLD, true);
201 202 203 204 205 206 207 208
        $user = $this->connection->getTable("User")->find(4);
        try {
            $user->name = "this is an example of too long name not very good example but an example nevertheless";
            $user->save();
        } catch(Doctrine_Validator_Exception $e) {
            $this->assertEqual($e->count(), 1);
            $invalidRecords = $e->getInvalidRecords();
            $this->assertEqual(count($invalidRecords), 1);
zYne's avatar
zYne committed
209
            $stack = $invalidRecords[0]->errorStack();
210
            $this->assertTrue(in_array('length', $stack['name']));
211 212 213 214 215
        }

        try {
            $user = $this->connection->create("User");
            $user->Email->address = "jackdaniels@drinkmore.info...";
zYne's avatar
zYne committed
216
            $user->name = "this is an example of too long user name not very good example but an example nevertheless";
217 218 219 220 221 222 223 224 225
            $user->save();
            $this->fail();
        } catch(Doctrine_Validator_Exception $e) {
            $this->pass();
            $a = $e->getInvalidRecords();
        }

        $this->assertTrue(is_array($a));
        
zYne's avatar
zYne committed
226 227
        $emailStack = $a[array_search($user->Email, $a)]->errorStack();
        $userStack  = $a[array_search($user, $a)]->errorStack();
228

229 230
        $this->assertTrue(in_array('email', $emailStack['address']));
        $this->assertTrue(in_array('length', $userStack['name']));
231
        $this->manager->setAttribute(Doctrine::ATTR_VLD, false);
zYne's avatar
zYne committed
232
        $this->manager->setAttribute(Doctrine::ATTR_AUTO_LENGTH_VLD, false);
233 234 235
    }

    /**
236
     * Tests whether the validate() callback works correctly
237 238
     * in descendants of Doctrine_Record.
     */
239
    public function testValidationHooks() {
240
        $this->manager->setAttribute(Doctrine::ATTR_VLD, true);
241 242 243
        
        // Tests validate() and validateOnInsert()
        $user = new User();
244 245
         try {
            $user->name = "I'm not The Saint";
246
            $user->password = "1234";
247 248 249 250 251
            $user->save();
        } catch(Doctrine_Validator_Exception $e) {
            $this->assertEqual($e->count(), 1);
            $invalidRecords = $e->getInvalidRecords();
            $this->assertEqual(count($invalidRecords), 1);
zYne's avatar
zYne committed
252 253

            $stack = $invalidRecords[0]->errorStack();
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271

            $this->assertEqual($stack->count(), 2);
            $this->assertTrue(in_array('notTheSaint', $stack['name']));  // validate() hook constraint
            $this->assertTrue(in_array('pwNotTopSecret', $stack['password'])); // validateOnInsert() hook constraint
        }
        
        // Tests validateOnUpdate()
        $user = $this->connection->getTable("User")->find(4);
        try {
            $user->name = "The Saint";  // Set correct name
            $user->password = "Top Secret"; // Set correct password
            $user->loginname = "Somebody"; // Wrong login name!
            $user->save();
            $this->fail();
        } catch(Doctrine_Validator_Exception $e) {
            $invalidRecords = $e->getInvalidRecords();
            $this->assertEqual(count($invalidRecords), 1);
            
zYne's avatar
zYne committed
272
            $stack = $invalidRecords[0]->errorStack();
273 274
            
            $this->assertEqual($stack->count(), 1);
275
            $this->assertTrue(in_array('notNobody', $stack['loginname']));  // validateOnUpdate() hook constraint
276
        }
277
        
278 279 280 281 282 283 284 285 286 287 288 289 290
        $this->manager->setAttribute(Doctrine::ATTR_VLD, false);
    }
    
    /**
     * Tests whether the validateOnInsert() callback works correctly
     * in descendants of Doctrine_Record.
     */
    public function testHookValidateOnInsert() {
        $this->manager->setAttribute(Doctrine::ATTR_VLD, true);
        
        $user = new User();
        $user->password = "1234";
        
291 292 293
        try {
            $user->save();
            $this->fail();
294
        } catch (Doctrine_Validator_Exception $ex) {
zYne's avatar
zYne committed
295
            $errors = $user->errorStack();
296
            $this->assertTrue(in_array('pwNotTopSecret', $errors['password']));
297 298
        }
        
299 300 301 302
        $this->manager->setAttribute(Doctrine::ATTR_VLD, false);
    }
}
?>