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
This commit is contained in:
parent
9929d5574d
commit
b040bbe35d
@ -135,10 +135,9 @@ abstract class Doctrine_Access extends Doctrine_Locator_Injectable implements Ar
|
||||
public function offsetSet($offset, $value)
|
||||
{
|
||||
if ( ! isset($offset)) {
|
||||
$this->add($value);
|
||||
} else {
|
||||
$this->set($offset, $value);
|
||||
return $this->add($value);
|
||||
}
|
||||
return $this->set($offset, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -150,4 +149,62 @@ abstract class Doctrine_Access extends Doctrine_Locator_Injectable implements Ar
|
||||
{
|
||||
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
|
||||
* @param Doctrine_Record $record
|
||||
* @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)) {
|
||||
$record->set($this->referenceField, $this->reference, false);
|
||||
}
|
||||
@ -477,8 +481,12 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator
|
||||
* @param string $key optional key for the record
|
||||
* @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)) {
|
||||
$value = $this->reference->get($this->relation->getLocalFieldName());
|
||||
|
||||
|
@ -84,8 +84,13 @@ class Doctrine_EventListener_Chain extends Doctrine_Access implements Doctrine_E
|
||||
* @param Doctrine_EventListener $listener
|
||||
* @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;
|
||||
}
|
||||
|
||||
|
@ -1043,7 +1043,7 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count
|
||||
* @param string $name
|
||||
* @return void
|
||||
*/
|
||||
public function __unset($fieldName)
|
||||
public function remove($fieldName)
|
||||
{
|
||||
if (isset($this->_data[$fieldName])) {
|
||||
$this->_data[$fieldName] = array();
|
||||
|
@ -38,19 +38,19 @@ abstract class Doctrine_Record_Abstract extends Doctrine_Access
|
||||
* @param Doctrine_ClassMetadata
|
||||
*/
|
||||
protected $_table;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @var Doctrine_Mapper
|
||||
*/
|
||||
protected $_mapper;
|
||||
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
public function setTableDefinition()
|
||||
{}
|
||||
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
@ -76,7 +76,7 @@ abstract class Doctrine_Record_Abstract extends Doctrine_Access
|
||||
{
|
||||
return $this->_table;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the mapper of the entity.
|
||||
*
|
||||
@ -122,12 +122,12 @@ abstract class Doctrine_Record_Abstract extends Doctrine_Access
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
public function setAttribute($attr, $value)
|
||||
{
|
||||
$this->_table->setAttribute($attr, $value);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* attribute
|
||||
* sets or retrieves an option
|
||||
@ -151,4 +151,5 @@ abstract class Doctrine_Record_Abstract extends Doctrine_Access
|
||||
$this->_table->setAttribute($attr, $value);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -84,8 +84,14 @@ class Doctrine_Record_Listener_Chain extends Doctrine_Access implements Doctrine
|
||||
* @param Doctrine_Record_Listener $listener listener to be added
|
||||
* @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;
|
||||
}
|
||||
|
||||
|
@ -110,29 +110,7 @@ class Doctrine_Template extends Doctrine_Record_Abstract
|
||||
{
|
||||
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
|
||||
*
|
||||
|
150
tests/Orm/Component/AccessTest.php
Normal file
150
tests/Orm/Component/AccessTest.php
Normal file
@ -0,0 +1,150 @@
|
||||
<?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';
|
||||
|
||||
// Tests
|
||||
require_once 'Orm/Component/TestTest.php';
|
||||
require_once 'Orm/Component/AccessTest.php';
|
||||
|
||||
class Orm_Component_AllTests
|
||||
{
|
||||
@ -19,7 +20,8 @@ class Orm_Component_AllTests
|
||||
{
|
||||
$suite = new Doctrine_TestSuite('Doctrine Orm Component');
|
||||
|
||||
$suite->addTestSuite('Orm_Component_TestTest');
|
||||
// $suite->addTestSuite('Orm_Component_TestTest');
|
||||
$suite->addTestSuite('Orm_Component_AccessTest');
|
||||
|
||||
return $suite;
|
||||
}
|
||||
@ -27,4 +29,4 @@ class Orm_Component_AllTests
|
||||
|
||||
if (PHPUnit_MAIN_METHOD == 'Dbal_Component_AllTests::main') {
|
||||
Dbal_Component_AllTests::main();
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user