1<?php 2/* 3 * $Id$ 4 * 5 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 6 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 7 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 8 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 9 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 10 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 11 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 12 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 13 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 14 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 15 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 16 * 17 * This software consists of voluntary contributions made by many individuals 18 * and is licensed under the LGPL. For more information, see 19 * <http://www.doctrine-project.org>. 20 */ 21 22/** 23 * Doctrine_Ticket_1652_TestCase 24 * 25 * @package Doctrine 26 * @author floriank 27 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL 28 * @category Object Relational Mapping 29 * @link www.doctrine-project.org 30 * @since 1.1 31 * @version $Revision$ 32 */ 33class Doctrine_Ticket_1652_TestCase extends Doctrine_UnitTestCase 34{ 35 public function prepareTables() 36 { 37 $this->tables = array(); 38 $this->tables[] = 'Ticket_1652_User'; 39 parent::prepareTables(); 40 } 41 42 public function prepareData() 43 { 44 $user = new Ticket_1652_User(); 45 $user->id = 1; 46 $user->name = "floriank"; 47 $user->save(); 48 } 49 50 public function testValidate() { 51 $doctrine = new ReflectionClass('Doctrine_Core'); 52 if ($doctrine->hasConstant('VALIDATE_USER')) { 53 Doctrine_Manager::getInstance()->setAttribute(Doctrine_Core::ATTR_VALIDATE, Doctrine_Core::VALIDATE_USER); 54 } else { 55 //I only want my overridden Record->validate()-methods for validation 56 Doctrine_Manager::getInstance()->setAttribute(Doctrine_Core::ATTR_VALIDATE, 57 Doctrine_Core::VALIDATE_ALL & 58 ~Doctrine_Core::VALIDATE_LENGTHS & 59 ~Doctrine_Core::VALIDATE_CONSTRAINTS & 60 ~Doctrine_Core::VALIDATE_TYPES); 61 } 62 63 $user = Doctrine_Core::getTable('Ticket_1652_User')->findOneById(1); 64 $user->name = "test"; 65 if ($user->isValid()) { 66 try { 67 $user->save(); 68 } catch (Doctrine_Validator_Exception $dve) { 69 // ignore 70 } 71 } 72 73 $user = Doctrine_Core::getTable('Ticket_1652_User')->findOneById(1); 74 75 $this->assertNotEqual($user->name, 'test'); 76 //reset validation to default for further testcases 77 Doctrine_Manager::getInstance()->setAttribute(Doctrine_Core::ATTR_VALIDATE, Doctrine_Core::VALIDATE_NONE); 78 } 79} 80 81class Ticket_1652_User extends Doctrine_Record 82{ 83 public function setTableDefinition() 84 { 85 $this->hasColumn('id', 'integer', null, array('primary' => true, 'autoincrement' => true)); 86 $this->hasColumn('name', 'string', 30); 87 } 88 89 protected function validate() { 90 if ($this->name == "test") { 91 $this->getErrorStack()->add("badName", "No testnames allowed!"); 92 return false; 93 } 94 } 95} 96