Commit b040bbe3 authored by meus's avatar meus

Added tests for Doctrine_Access. It uses annotations for exceptions and to...

Added tests for Doctrine_Access. It uses annotations for exceptions and to mark functions as tests. Also changed some logic in access and implementing classes to make the abstraction a little bit more solid
parent 9929d557
...@@ -135,10 +135,9 @@ abstract class Doctrine_Access extends Doctrine_Locator_Injectable implements Ar ...@@ -135,10 +135,9 @@ abstract class Doctrine_Access extends Doctrine_Locator_Injectable implements Ar
public function offsetSet($offset, $value) public function offsetSet($offset, $value)
{ {
if ( ! isset($offset)) { if ( ! isset($offset)) {
$this->add($value); return $this->add($value);
} else {
$this->set($offset, $value);
} }
return $this->set($offset, $value);
} }
/** /**
...@@ -150,4 +149,62 @@ abstract class Doctrine_Access extends Doctrine_Locator_Injectable implements Ar ...@@ -150,4 +149,62 @@ abstract class Doctrine_Access extends Doctrine_Locator_Injectable implements Ar
{ {
return $this->remove($offset); return $this->remove($offset);
} }
/**
* Remove the element with the specified offset
*
* @param mixed $offset The offset to remove
* @return boolean True if removed otherwise false
*/
public function remove($offset)
{
throw new Doctrine_Exception('Remove is not supported for ' . get_class($this));
}
/**
* Return the element with the specified offset
*
* @param mixed $offset The offset to return
* @return mixed The value of the return object
*/
public function get($offset)
{
throw new Doctrine_Exception('Get is not supported for ' . get_class($this));
}
/**
* Set the offset to the value
*
* @param mixed $offset The offset to set
* @param mixed $value The value to set the offset to
*
*/
public function set($offset, $value)
{
throw new Doctrine_Exception('Set is not supported for ' . get_class($this));
}
/**
* Check if the specified offset exists
*
* @param mixed $offset The offset to check
* @return boolean True if exists otherwise false
*/
public function contains($offset)
{
throw new Doctrine_Exception('Contains is not supported for ' . get_class($this));
}
/**
* Add the value
*
* @param mixed $value The value to add
* @return void
*/
public function add($value)
{
throw new Doctrine_Exception('Add is not supported for ' . get_class($this));
}
} }
...@@ -463,8 +463,12 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator ...@@ -463,8 +463,12 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator
* @param Doctrine_Record $record * @param Doctrine_Record $record
* @return void * @return void
*/ */
public function set($key, Doctrine_Record $record) public function set($key, $record)
{ {
if( ! $record instanceOf Doctrine_Record) {
throw new Doctrine_Record_Exception('Value variable in set is not an instance of Doctrine_Record');
}
if (isset($this->referenceField)) { if (isset($this->referenceField)) {
$record->set($this->referenceField, $this->reference, false); $record->set($this->referenceField, $this->reference, false);
} }
...@@ -477,8 +481,12 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator ...@@ -477,8 +481,12 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator
* @param string $key optional key for the record * @param string $key optional key for the record
* @return boolean * @return boolean
*/ */
public function add(Doctrine_Record $record, $key = null) public function add($record, $key = null)
{ {
if( ! $record instanceOf Doctrine_Record) {
throw new Doctrine_Record_Exception('Value variable in set is not an instance of Doctrine_Record');
}
if (isset($this->referenceField)) { if (isset($this->referenceField)) {
$value = $this->reference->get($this->relation->getLocalFieldName()); $value = $this->reference->get($this->relation->getLocalFieldName());
......
...@@ -84,8 +84,13 @@ class Doctrine_EventListener_Chain extends Doctrine_Access implements Doctrine_E ...@@ -84,8 +84,13 @@ class Doctrine_EventListener_Chain extends Doctrine_Access implements Doctrine_E
* @param Doctrine_EventListener $listener * @param Doctrine_EventListener $listener
* @return void * @return void
*/ */
public function set($key, Doctrine_EventListener $listener) public function set($key, $listener)
{ {
if( ! $listener instanceOf Doctrine_EventListener) {
throw new Doctrine_Exception('Value variable in set is not an instance of Doctrine_EventListener');
}
$this->_listeners[$key] = $listener; $this->_listeners[$key] = $listener;
} }
......
...@@ -1043,7 +1043,7 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count ...@@ -1043,7 +1043,7 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count
* @param string $name * @param string $name
* @return void * @return void
*/ */
public function __unset($fieldName) public function remove($fieldName)
{ {
if (isset($this->_data[$fieldName])) { if (isset($this->_data[$fieldName])) {
$this->_data[$fieldName] = array(); $this->_data[$fieldName] = array();
......
...@@ -151,4 +151,5 @@ abstract class Doctrine_Record_Abstract extends Doctrine_Access ...@@ -151,4 +151,5 @@ abstract class Doctrine_Record_Abstract extends Doctrine_Access
$this->_table->setAttribute($attr, $value); $this->_table->setAttribute($attr, $value);
} }
} }
} }
...@@ -84,8 +84,14 @@ class Doctrine_Record_Listener_Chain extends Doctrine_Access implements Doctrine ...@@ -84,8 +84,14 @@ class Doctrine_Record_Listener_Chain extends Doctrine_Access implements Doctrine
* @param Doctrine_Record_Listener $listener listener to be added * @param Doctrine_Record_Listener $listener listener to be added
* @return Doctrine_Record_Listener_Chain this object * @return Doctrine_Record_Listener_Chain this object
*/ */
public function set($key, Doctrine_EventListener $listener) public function set($key, $listener)
{ {
if ( ! ($listener instanceof Doctrine_Record_Listener_Interface) &&
! ($listener instanceof Doctrine_Overloadable)) {
throw new Doctrine_EventListener_Exception("Couldn't add eventlistener. Record listeners should implement either Doctrine_EventListener_Interface or Doctrine_Overloadable");
}
$this->_listeners[$key] = $listener; $this->_listeners[$key] = $listener;
} }
......
...@@ -111,28 +111,6 @@ class Doctrine_Template extends Doctrine_Record_Abstract ...@@ -111,28 +111,6 @@ class Doctrine_Template extends Doctrine_Record_Abstract
return $this->_plugin; return $this->_plugin;
} }
/**
* get
*
* @param mixed $name
* @return void
*/
public function get($name)
{
throw new Doctrine_Exception("Templates doesn't support accessors.");
}
/**
* set
*
* @param mixed $name
* @param mixed $value
* @return void
*/
public function set($name, $value)
{
throw new Doctrine_Exception("Templates doesn't support accessors.");
}
/** /**
* setUp * setUp
* *
......
<?php
require_once 'lib/DoctrineTestInit.php';
class Orm_Component_AccessTest extends Doctrine_OrmTestCase
{
private $user;
public function setUp()
{
parent::setUp();
$this->user = new ForumUser();
}
/**
* @test
*/
public function shouldMarkExistingFieldAsSetOnNewRecord()
{
$this->assertTrue(isset($this->user->username));
$this->assertTrue(isset($this->user['username']));
}
/**
* @test
*/
public function shouldMarkNonExistantFieldAsNotSetOnNewRecord()
{
$this->assertFalse(isset($this->user->rat));
$this->assertFalse(isset($this->user['rat']));
}
/**
* @test
*/
public function shouldSetSingleValueInRecord()
{
$this->user->username ='meus';
$this->assertEquals('meus', $this->user->username);
$this->assertEquals('meus', $this->user['username']);
}
/**
* @test
*/
public function shouldSetSingleValueInRecordWithOffset()
{
$this->user['username'] ='meus';
$this->assertEquals('meus', $this->user->username);
$this->assertEquals('meus', $this->user['username']);
}
/**
* @test
*/
public function shouldSetArrayOfValusInRecord()
{
$this->user->setArray(array(
'username' => 'meus',
'id' => 22));
$this->assertEquals('meus', $this->user->username);
$this->assertEquals('meus', $this->user['username']);
$this->assertEquals(22, $this->user->id);
$this->assertEquals(22, $this->user['id']);
}
/**
* @test
* @expectedException Doctrine_Record_Exception
*/
public function shouldNotBeAbleToSetNonExistantField()
{
$this->user->rat ='meus';
}
/**
* @test
* @expectedException Doctrine_Record_Exception
*/
public function shouldNotBeAbleToSetNonExistantFieldWithOffset()
{
$this->user['rat'] ='meus';
}
/**
* @test
* @expectedException Doctrine_Record_Exception
*/
public function shouldNotBeAbleToSetNonExistantFieldAsPartInSetArray()
{
$this->user->setArray(array(
'rat' => 'meus',
'id' => 22));
}
/**
* @test
*/
public function newCollectionShouldBeEmpty()
{
$col = new Doctrine_Collection('ForumUser');
$this->assertEquals(0, count($col));
$this->assertFalse(isset($coll[0]));
$this->assertFalse(isset($coll[0]));
}
/**
* @test
*/
public function shouldBeAbleToUnsetWithOffsetFromCollection()
{
$col = new Doctrine_Collection('ForumUser');
$col[0] = new ForumUser();
$this->assertTrue(isset($col[0]));
unset($col[0]);
$this->assertFalse(isset($col[0]));
}
/**
* @test
*/
public function shouldBeAbleToUnsetFromCollection()
{
$col = new Doctrine_Collection('ForumUser');
$col->test = new ForumUser();
$this->assertTrue(isset($col->test));
unset($col->test);
$this->assertFalse(isset($col->test));
}
/**
*
* @test
* @expectedException Doctrine_Exception
*/
public function shouldNotBeAbleToSetNullFieldInRecord()
{
$this->user->offsetSet(null, 'test');
}
}
...@@ -7,6 +7,7 @@ require_once 'lib/DoctrineTestInit.php'; ...@@ -7,6 +7,7 @@ require_once 'lib/DoctrineTestInit.php';
// Tests // Tests
require_once 'Orm/Component/TestTest.php'; require_once 'Orm/Component/TestTest.php';
require_once 'Orm/Component/AccessTest.php';
class Orm_Component_AllTests class Orm_Component_AllTests
{ {
...@@ -19,7 +20,8 @@ class Orm_Component_AllTests ...@@ -19,7 +20,8 @@ class Orm_Component_AllTests
{ {
$suite = new Doctrine_TestSuite('Doctrine Orm Component'); $suite = new Doctrine_TestSuite('Doctrine Orm Component');
$suite->addTestSuite('Orm_Component_TestTest'); // $suite->addTestSuite('Orm_Component_TestTest');
$suite->addTestSuite('Orm_Component_AccessTest');
return $suite; return $suite;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment