ValidatorTestCase.php 14.8 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
    public function prepareTables() {
romanb's avatar
romanb committed
38 39 40
        $this->tables[] = 'ValidatorTest';
        $this->tables[] = 'ValidatorTest_Person';
        $this->tables[] = 'ValidatorTest_FootballPlayer';
41 42
        parent::prepareTables();
    }
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 122 123
    /**
     * 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
124
        $stack = $test->errorStack();
125 126 127

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

128 129 130 131
        $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']));
132 133 134 135 136 137 138 139 140 141
        $test->mystring = 'str';


        $test->save();
    }

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

zYne's avatar
zYne committed
144 145 146 147
        $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');
148 149
        $user->setArray($set);
        $email = $user->Email;
zYne's avatar
zYne committed
150
        $email->address = 'zYne@invalid';
151 152 153 154 155 156 157

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

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


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

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

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

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

173
        $this->assertTrue(in_array('unique', $stack['address']));
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 200 201
    }

    /**
     * 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
202
        $this->manager->setAttribute(Doctrine::ATTR_AUTO_LENGTH_VLD, true);
203 204 205 206 207 208 209 210
        $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
211
            $stack = $invalidRecords[0]->errorStack();
212
            $this->assertTrue(in_array('length', $stack['name']));
213 214 215 216 217
        }

        try {
            $user = $this->connection->create("User");
            $user->Email->address = "jackdaniels@drinkmore.info...";
zYne's avatar
zYne committed
218
            $user->name = "this is an example of too long user name not very good example but an example nevertheless";
219 220 221 222 223
            $user->save();
            $this->fail();
        } catch(Doctrine_Validator_Exception $e) {
            $this->pass();
            $a = $e->getInvalidRecords();
romanb's avatar
romanb committed
224
            //var_dump($a[1]->getErrorStack());
225 226
        }
        
romanb's avatar
romanb committed
227 228 229 230 231
        $this->assertTrue(is_array($a));
        //var_dump(array_search($user, $a));
        $emailStack = $user->Email->errorStack();
        $userStack  = $user->errorStack();
        //var_dump($userStack);
232

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

    /**
240
     * Tests whether the validate() callback works correctly
241 242
     * in descendants of Doctrine_Record.
     */
243
    public function testValidationHooks() {
244
        $this->manager->setAttribute(Doctrine::ATTR_VLD, true);
245 246 247
        
        // Tests validate() and validateOnInsert()
        $user = new User();
248 249
         try {
            $user->name = "I'm not The Saint";
250
            $user->password = "1234";
251 252 253 254 255
            $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
256 257

            $stack = $invalidRecords[0]->errorStack();
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275

            $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
276
            $stack = $invalidRecords[0]->errorStack();
277 278
            
            $this->assertEqual($stack->count(), 1);
279
            $this->assertTrue(in_array('notNobody', $stack['loginname']));  // validateOnUpdate() hook constraint
280
        }
281
        
282 283 284 285 286 287 288 289 290 291 292 293 294
        $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";
        
295 296 297
        try {
            $user->save();
            $this->fail();
298
        } catch (Doctrine_Validator_Exception $ex) {
zYne's avatar
zYne committed
299
            $errors = $user->errorStack();
300
            $this->assertTrue(in_array('pwNotTopSecret', $errors['password']));
301 302
        }
        
303 304
        $this->manager->setAttribute(Doctrine::ATTR_VLD, false);
    }
romanb's avatar
romanb committed
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
    
    /*
    public function testIssue()
    {
        $this->manager->setAttribute(Doctrine::ATTR_VLD, true);
        
        try {
            $person = new ValidatorTest_Person();
            $person->name = '';  // will raise a validation exception since name must be 'notblank'
            $person->is_football_player = true;
        
            $person->ValidatorTest_FootballPlayer->team_name = 'liverpool';
            $person->ValidatorTest_FootballPlayer->goals_count = 2;
        
            $person->save();
        }
        catch(Doctrine_Validator_Exception $e) {
            $this->fail("test");
            //var_dump($person->getErrorStack());
            //var_dump($person->ValidatorTest_FootballPlayer->getErrorStack());
        }
        
        $this->manager->setAttribute(Doctrine::ATTR_VLD, false);
    }
    */
    
    /**
     * Enter description here...
     *
     * @todo move to a separate test file (tests/Validator/UniqueTestCase) .
     */
336
    public function testSetSameUniqueValueOnSameRecordThrowsNoException()
romanb's avatar
romanb committed
337 338 339 340
    {
        $this->manager->setAttribute(Doctrine::ATTR_VLD, true);
        
        $r = new ValidatorTest_Person();
341
        $r->identifier = '1234';
romanb's avatar
romanb committed
342 343 344
        $r->save();
        
        $r = $this->connection->getTable('ValidatorTest_Person')->findAll()->getFirst();
345
        $r->identifier = 1234;
romanb's avatar
romanb committed
346 347 348
        try {
           $r->save();
        }
349
        catch (Doctrine_Validator_Exception $e) {
romanb's avatar
romanb committed
350 351 352
           $this->fail("Validator exception raised without reason!");
        }
        
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
        $r->delete(); // clean up
        
        $this->manager->setAttribute(Doctrine::ATTR_VLD, false);
    }
    
    public function testSetSameUniqueValueOnDifferentRecordThrowsException()
    {
        $this->manager->setAttribute(Doctrine::ATTR_VLD, true);
        
        $r = new ValidatorTest_Person();
        $r->identifier = '1234';
        $r->save();
        
        $r = new ValidatorTest_Person();
        $r->identifier = 1234;
        try {
            $r->save();
            $this->fail("No validator exception thrown on unique validation.");
        } catch (Doctrine_Validator_Exception $e) {
            $this->pass();
        }
        $r->delete(); // clean up
        
romanb's avatar
romanb committed
376 377
        $this->manager->setAttribute(Doctrine::ATTR_VLD, false);
    }
378 379
}
?>