Skip to content

Commit

Permalink
Adding test case for \data\Entity.
Browse files Browse the repository at this point in the history
  • Loading branch information
nateabele committed Jan 29, 2011
1 parent 1c58235 commit 318a6dd
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
3 changes: 2 additions & 1 deletion libraries/lithium/data/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace lithium\data;

use BadMethodCallException;
use UnexpectedValueException;
use lithium\data\Source;
use lithium\util\Collection as Col;

Expand Down Expand Up @@ -268,7 +269,7 @@ public function schema($field = null) {
break;
}
if ($field) {
return isset($self->_schema[$field]) ? $self->_schema[$field] : null;
return isset($schema[$field]) ? $schema[$field] : null;
}
return $schema;
}
Expand Down
1 change: 1 addition & 0 deletions libraries/lithium/data/source/MongoDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,7 @@ public function cast($entity, array $data, array $options = array()) {
if (!$data) {
return $data;
}

if (is_string($entity)) {
$model = $entity;
$entity = null;
Expand Down
87 changes: 87 additions & 0 deletions libraries/lithium/tests/cases/data/EntityTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
/**
* Lithium: the most rad php framework
*
* @copyright Copyright 2010, Union of RAD (http://union-of-rad.org)
* @license http://opensource.org/licenses/bsd-license.php The BSD License
*
*/

namespace lithium\tests\cases\data;

use lithium\data\Entity;

class EntityTest extends \lithium\test\Unit {

protected $_model = 'lithium\tests\mocks\data\source\MockMongoPost';

public function testSchemaAccess() {
$schema = array('foo' => array('type' => 'string'));
$entity = new Entity(compact('schema'));
$this->assertEqual($schema, $entity->schema());
}

public function testPropertyAccess() {
$entity = new Entity(array('model' => 'Foo', 'exists' => false));
$this->assertEqual('Foo', $entity->model());
$this->assertFalse($entity->exists());

$entity = new Entity(array('exists' => true));
$this->assertTrue($entity->exists());

$expected = array(
'exists' => true, 'data' => array(), 'update' => array(), 'increment' => array()
);
$this->assertEqual($expected, $entity->export());
}

public function testIncrement() {
$entity = new Entity(array('data' => array('counter' => 0)));
$this->assertEqual(0, $entity->counter);

$entity->increment('counter');
$this->assertEqual(1, $entity->counter);

$entity->decrement('counter', 5);
$this->assertEqual(-4, $entity->counter);

$this->assertNull($entity->increment);
$entity->increment('foo');
$this->assertEqual(1, $entity->foo);

$this->assertFalse(isset($entity->bar));
$entity->bar = 'blah';
$entity->update();

$this->expectException("/^Field 'bar' cannot be incremented.$/");
$entity->increment('bar');
}

public function testMethodDispatch() {
$entity = new Entity(array('model' => $this->_model, 'data' => array('foo' => true)));
$this->assertTrue($entity->validates());
$this->expectException("/^No model bound or unhandled method call 'foo'.$/");
$entity->foo();
}

public function testErrors() {
$entity = new Entity();
$errors = array('foo' => 'Something bad happened.');
$this->assertEqual(array(), $entity->errors());

$entity->errors($errors);
$this->assertEqual($errors, $entity->errors());
$this->assertEqual('Something bad happened.', $entity->errors('foo'));
}

public function testConversion() {
$data = array('foo' => '!!', 'bar' => '??', 'baz' => '--');
$entity = new Entity(compact('data'));

$this->assertEqual($data, $entity->to('array'));
$this->assertEqual($data, $entity->data());
$this->assertEqual($entity, $entity->to('foo'));
}
}

?>

0 comments on commit 318a6dd

Please sign in to comment.