-
Notifications
You must be signed in to change notification settings - Fork 238
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
90 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | ||
} | ||
} | ||
|
||
?> |