2006-12-28 00:34:26 +03:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* $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
|
2008-02-22 21:11:35 +03:00
|
|
|
* <http://www.phpdoctrine.org>.
|
2006-12-28 00:34:26 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Doctrine_Record_State_TestCase
|
|
|
|
*
|
|
|
|
* @package Doctrine
|
|
|
|
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
|
* @category Object Relational Mapping
|
2008-02-22 21:11:35 +03:00
|
|
|
* @link www.phpdoctrine.org
|
2006-12-28 00:34:26 +03:00
|
|
|
* @since 1.0
|
|
|
|
* @version $Revision$
|
|
|
|
*/
|
2007-07-26 00:47:25 +04:00
|
|
|
class Doctrine_Record_State_TestCase extends Doctrine_UnitTestCase
|
|
|
|
{
|
|
|
|
public function prepareTables()
|
|
|
|
{
|
|
|
|
$this->tables = array('Entity');
|
|
|
|
|
|
|
|
parent::prepareTables();
|
|
|
|
}
|
|
|
|
|
2007-08-09 21:09:20 +04:00
|
|
|
public function prepareData()
|
|
|
|
{ }
|
2007-07-26 00:47:25 +04:00
|
|
|
|
2007-08-09 21:09:20 +04:00
|
|
|
public function testAssigningAutoincId()
|
|
|
|
{
|
|
|
|
$user = new User();
|
|
|
|
|
|
|
|
$this->assertEqual($user->id, null);
|
|
|
|
|
|
|
|
$user->name = 'zYne';
|
|
|
|
|
|
|
|
$user->save();
|
|
|
|
|
|
|
|
$this->assertEqual($user->id, 1);
|
|
|
|
|
|
|
|
$user->id = 2;
|
|
|
|
|
|
|
|
$this->assertEqual($user->id, 2);
|
|
|
|
|
|
|
|
$user->save();
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-08-09 21:09:20 +04:00
|
|
|
/**
|
|
|
|
public function testAssignFieldsToProxies()
|
2007-07-26 00:47:25 +04:00
|
|
|
{
|
|
|
|
$user = new User();
|
|
|
|
$user->name = 'someuser';
|
|
|
|
$user->password = '123';
|
|
|
|
$user->save();
|
|
|
|
|
|
|
|
$this->connection->clear();
|
|
|
|
|
|
|
|
$user = $this->connection->queryOne("SELECT u.name FROM User u WHERE u.name = 'someuser'");
|
|
|
|
$this->assertEqual($user->name, 'someuser');
|
|
|
|
|
|
|
|
$user->name = 'someother';
|
|
|
|
$this->assertEqual($user->name, 'someother');
|
|
|
|
|
|
|
|
$user->save();
|
|
|
|
$this->assertEqual($user->name, 'someother');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAssignUnknownState()
|
|
|
|
{
|
2006-12-28 00:34:26 +03:00
|
|
|
$user = new User();
|
|
|
|
try {
|
|
|
|
$user->state(123123);
|
|
|
|
$this->fail();
|
|
|
|
} catch(Doctrine_Record_State_Exception $e) {
|
|
|
|
$this->pass();
|
|
|
|
}
|
2008-05-14 01:20:34 +04:00
|
|
|
$this->assertEqual($user->state(), Doctrine_Entity::STATE_TCLEAN);
|
2006-12-28 00:34:26 +03:00
|
|
|
try {
|
|
|
|
$user->state('some unknown state');
|
|
|
|
$this->fail();
|
2008-05-14 01:20:34 +04:00
|
|
|
} catch(Doctrine_Entity_State_Exception $e) {
|
2006-12-28 00:34:26 +03:00
|
|
|
$this->pass();
|
|
|
|
}
|
2008-05-14 01:20:34 +04:00
|
|
|
$this->assertEqual($user->state(), Doctrine_Entity::STATE_TCLEAN);
|
2006-12-28 00:34:26 +03:00
|
|
|
}
|
|
|
|
|
2007-07-26 00:47:25 +04:00
|
|
|
public function testAssignDirtyState()
|
|
|
|
{
|
2006-12-28 00:34:26 +03:00
|
|
|
$user = new User();
|
|
|
|
|
2008-05-14 01:20:34 +04:00
|
|
|
$user->state(Doctrine_Entity::STATE_DIRTY);
|
2006-12-28 00:34:26 +03:00
|
|
|
|
2008-05-14 01:20:34 +04:00
|
|
|
$this->assertEqual($user->state(), Doctrine_Entity::STATE_DIRTY);
|
2006-12-28 00:34:26 +03:00
|
|
|
|
|
|
|
$user->state('dirty');
|
|
|
|
|
2008-05-14 01:20:34 +04:00
|
|
|
$this->assertEqual($user->state(), Doctrine_Entity::STATE_DIRTY);
|
2006-12-28 00:34:26 +03:00
|
|
|
}
|
2007-07-26 00:47:25 +04:00
|
|
|
|
|
|
|
public function testAssignCleanState()
|
|
|
|
{
|
2006-12-28 00:34:26 +03:00
|
|
|
$user = new User();
|
|
|
|
|
2008-05-14 01:20:34 +04:00
|
|
|
$user->state(Doctrine_Entity::STATE_CLEAN);
|
2006-12-28 00:34:26 +03:00
|
|
|
|
2008-05-14 01:20:34 +04:00
|
|
|
$this->assertEqual($user->state(), Doctrine_Entity::STATE_CLEAN);
|
2006-12-28 00:34:26 +03:00
|
|
|
|
|
|
|
$user->state('clean');
|
|
|
|
|
2008-05-14 01:20:34 +04:00
|
|
|
$this->assertEqual($user->state(), Doctrine_Entity::STATE_CLEAN);
|
2006-12-28 00:34:26 +03:00
|
|
|
}
|
2007-07-26 00:47:25 +04:00
|
|
|
|
|
|
|
public function testAssignTransientCleanState()
|
|
|
|
{
|
2006-12-28 00:34:26 +03:00
|
|
|
$user = new User();
|
|
|
|
|
2008-05-14 01:20:34 +04:00
|
|
|
$user->state(Doctrine_Entity::STATE_TCLEAN);
|
2006-12-28 00:34:26 +03:00
|
|
|
|
2008-05-14 01:20:34 +04:00
|
|
|
$this->assertEqual($user->state(), Doctrine_Entity::STATE_TCLEAN);
|
2006-12-28 00:34:26 +03:00
|
|
|
|
|
|
|
$user->state('tclean');
|
|
|
|
|
2008-05-14 01:20:34 +04:00
|
|
|
$this->assertEqual($user->state(), Doctrine_Entity::STATE_TCLEAN);
|
2006-12-28 00:34:26 +03:00
|
|
|
}
|
2007-07-26 00:47:25 +04:00
|
|
|
|
|
|
|
public function testAssignTransientDirtyState()
|
|
|
|
{
|
2006-12-28 00:34:26 +03:00
|
|
|
$user = new User();
|
|
|
|
|
2008-05-14 01:20:34 +04:00
|
|
|
$user->state(Doctrine_Entity::STATE_TDIRTY);
|
2006-12-28 00:34:26 +03:00
|
|
|
|
2008-05-14 01:20:34 +04:00
|
|
|
$this->assertEqual($user->state(), Doctrine_Entity::STATE_TDIRTY);
|
2006-12-28 00:34:26 +03:00
|
|
|
|
|
|
|
$user->state('tdirty');
|
|
|
|
|
2008-05-14 01:20:34 +04:00
|
|
|
$this->assertEqual($user->state(), Doctrine_Entity::STATE_TDIRTY);
|
2006-12-28 00:34:26 +03:00
|
|
|
}
|
2007-07-26 00:47:25 +04:00
|
|
|
|
|
|
|
public function testAssignProxyState()
|
|
|
|
{
|
2006-12-28 00:34:26 +03:00
|
|
|
$user = new User();
|
|
|
|
|
2008-05-14 01:20:34 +04:00
|
|
|
$user->state(Doctrine_Entity::STATE_PROXY);
|
2006-12-28 00:34:26 +03:00
|
|
|
|
2008-05-14 01:20:34 +04:00
|
|
|
$this->assertEqual($user->state(), Doctrine_Entity::STATE_PROXY);
|
2006-12-28 00:34:26 +03:00
|
|
|
|
|
|
|
$user->state('proxy');
|
|
|
|
|
2008-05-14 01:20:34 +04:00
|
|
|
$this->assertEqual($user->state(), Doctrine_Entity::STATE_PROXY);
|
2006-12-28 00:34:26 +03:00
|
|
|
}
|
2007-07-26 00:47:25 +04:00
|
|
|
|
2007-04-17 21:25:08 +04:00
|
|
|
public function testProxiesAreAutomaticallyUpdatedWithFetches()
|
|
|
|
{
|
|
|
|
$user = new User();
|
|
|
|
$user->name = 'someuser';
|
|
|
|
$user->password = '123';
|
|
|
|
$user->save();
|
|
|
|
|
|
|
|
$this->connection->clear();
|
|
|
|
|
|
|
|
$user = $this->connection->queryOne("SELECT u.name FROM User u WHERE u.name = 'someuser'");
|
|
|
|
|
2008-05-14 01:20:34 +04:00
|
|
|
$this->assertEqual($user->state(), Doctrine_Entity::STATE_PROXY);
|
2007-04-17 21:25:08 +04:00
|
|
|
|
|
|
|
$user2 = $this->connection->queryOne("FROM User u WHERE u.name = 'someuser'");
|
|
|
|
|
|
|
|
$this->assertEqual($user->getOID(), $user2->getOID());
|
|
|
|
|
|
|
|
$count = count($this->dbh);
|
|
|
|
|
|
|
|
$this->assertEqual($user->password, '123');
|
|
|
|
|
|
|
|
$this->assertEqual($count, count($this->dbh));
|
2007-08-09 21:09:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testProxyToDirtyToProxy() {
|
2007-09-03 18:57:18 +04:00
|
|
|
|
|
|
|
define('UNAME','someuser') ;
|
|
|
|
define('UPWD1','123') ;
|
|
|
|
define('UPWD2','456') ;
|
|
|
|
define('ULNAME','somelogin') ;
|
|
|
|
|
|
|
|
$user = new User() ;
|
|
|
|
$user->name = UNAME ;
|
|
|
|
$user->password = UPWD1 ;
|
|
|
|
$user->loginname = ULNAME ;
|
|
|
|
$user->save() ;
|
|
|
|
|
2007-08-09 21:09:20 +04:00
|
|
|
$this->assertEqual($user->name,UNAME) ;
|
|
|
|
$this->assertEqual($user->password,UPWD1) ;
|
|
|
|
$this->assertEqual($user->loginname,ULNAME) ;
|
|
|
|
|
|
|
|
// to make sure it is saved correctly
|
|
|
|
$user1 = $this->connection->queryOne("FROM User u WHERE u.name = '" . UNAME . "'");
|
2008-05-14 01:20:34 +04:00
|
|
|
$this->assertEqual($user1->state(), Doctrine_Entity::STATE_CLEAN);
|
2007-08-09 21:09:20 +04:00
|
|
|
$this->assertEqual($user1->name,UNAME) ;
|
|
|
|
$this->assertEqual($user1->password,UPWD1) ;
|
|
|
|
$this->assertEqual($user1->loginname,ULNAME) ;
|
|
|
|
|
2007-09-03 18:57:18 +04:00
|
|
|
$this->connection->clear() ;
|
|
|
|
//$this->clearCache() ;
|
|
|
|
|
|
|
|
// now lets fetch partially the object
|
|
|
|
//$users = Doctrine_Query::create($this->connection)->select('u.name')->from('User u')->where("u.name='someuser'")->execute() ;
|
2007-08-09 21:09:20 +04:00
|
|
|
//$user2 = $users[0] ;
|
2007-09-03 18:57:18 +04:00
|
|
|
$user2 = $this->connection->queryOne("SELECT u.name FROM User u WHERE u.name = '" . UNAME . "'");
|
2007-08-09 21:09:20 +04:00
|
|
|
|
|
|
|
// the object should be in state proxy with only 'name' fetched ...
|
2008-05-14 01:20:34 +04:00
|
|
|
$this->assertEqual($user2->state(), Doctrine_Entity::STATE_PROXY);
|
2007-08-09 21:09:20 +04:00
|
|
|
$this->assertEqual($user2->name,UNAME) ;
|
|
|
|
$this->assertEqual($user2->password,null) ;
|
|
|
|
$this->assertEqual($user2->loginname,null) ;
|
|
|
|
|
|
|
|
// lets edit the object
|
|
|
|
$user2->password = UPWD2 ;
|
|
|
|
|
|
|
|
// now it should be dirty (but may be PDIRTY ... ?)
|
2008-05-14 01:20:34 +04:00
|
|
|
$this->assertEqual($user2->state(),Doctrine_Entity::STATE_DIRTY) ;
|
2007-08-09 21:09:20 +04:00
|
|
|
$this->assertEqual($user2->name,UNAME) ;
|
|
|
|
$this->assertEqual($user2->password,UPWD2) ;
|
|
|
|
$this->assertEqual($user2->loginname,null) ;
|
|
|
|
|
|
|
|
// lets save
|
|
|
|
$user2->save() ;
|
|
|
|
|
|
|
|
// the logic would suggest the object to go back to PROXY mode (becausse $user2->loginname is null aka not sync with DB)
|
2008-05-14 01:20:34 +04:00
|
|
|
$boolState = ($user2->loginname == null) && ($user2->state() === Doctrine_Entity::STATE_PROXY) ;
|
2007-08-09 21:09:20 +04:00
|
|
|
// this one will currently fail
|
|
|
|
$this->assertTrue($boolState) ;
|
|
|
|
// this will also currently fail (becausse it currently goes back to STATE_CLEAN, which shouldnt be the case)
|
2008-05-14 01:20:34 +04:00
|
|
|
//$this->assertEqual($user2->state(), Doctrine_Entity::STATE_PROXY);
|
2007-08-09 21:09:20 +04:00
|
|
|
$this->assertEqual($user2->name,UNAME) ;
|
|
|
|
$this->assertEqual($user2->password,UPWD2) ;
|
|
|
|
$this->assertEqual($user2->loginname,null) ;
|
2007-08-02 22:57:34 +04:00
|
|
|
}
|
2007-08-09 21:09:20 +04:00
|
|
|
*/
|
2006-12-28 00:34:26 +03:00
|
|
|
}
|