EntityTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\tests\cases\data;
  9. use lithium\data\Entity;
  10. use lithium\data\Schema;
  11. class EntityTest extends \lithium\test\Unit {
  12. protected $_model = 'lithium\tests\mocks\data\MockPost';
  13. public function testSchemaAccess() {
  14. $fields = array('foo' => array('type' => 'string'));
  15. $schema = new Schema(compact('fields'));
  16. $entity = new Entity(compact('schema'));
  17. $this->assertEqual($schema, $entity->schema());
  18. }
  19. public function testPropertyAccess() {
  20. $entity = new Entity(array('model' => 'Foo', 'exists' => false));
  21. $this->assertEqual('Foo', $entity->model());
  22. $this->assertFalse($entity->exists());
  23. $entity = new Entity(array('exists' => true));
  24. $this->assertTrue($entity->exists());
  25. $expected = array(
  26. 'exists' => true, 'data' => array(), 'update' => array(), 'increment' => array()
  27. );
  28. $this->assertEqual($expected, $entity->export());
  29. }
  30. public function testPropertyIssetEmpty() {
  31. $entity = new Entity(array(
  32. 'model' => 'Foo',
  33. 'exists' => true,
  34. 'data' => array('test_field' => 'foo'),
  35. 'relationships' => array('test_relationship' => array('test_me' => 'bar'))
  36. ));
  37. $this->assertEqual('foo', $entity->test_field);
  38. $this->assertEqual(array('test_me' => 'bar'), $entity->test_relationship);
  39. $this->assertTrue(isset($entity->test_field));
  40. $this->assertTrue(isset($entity->test_relationship));
  41. $this->assertFalse(empty($entity->test_field));
  42. $this->assertFalse(empty($entity->test_relationship));
  43. $this->assertTrue(empty($entity->test_invisible_field));
  44. $this->assertTrue(empty($entity->test_invisible_relationship));
  45. }
  46. public function testIncrement() {
  47. $entity = new Entity(array('data' => array('counter' => 0)));
  48. $this->assertEqual(0, $entity->counter);
  49. $entity->increment('counter');
  50. $this->assertEqual(1, $entity->counter);
  51. $entity->decrement('counter', 5);
  52. $this->assertEqual(-4, $entity->counter);
  53. $this->assertNull($entity->increment);
  54. $entity->increment('foo');
  55. $this->assertEqual(1, $entity->foo);
  56. $this->assertFalse(isset($entity->bar));
  57. $entity->bar = 'blah';
  58. $entity->sync();
  59. $this->expectException("/^Field 'bar' cannot be incremented.$/");
  60. $entity->increment('bar');
  61. }
  62. public function testMethodDispatch() {
  63. $model = $this->_model;
  64. $data = array('foo' => true);
  65. $entity = new Entity(compact('model', 'data'));
  66. $this->assertTrue($entity->validates());
  67. $model::instanceMethods(array('testInstanceMethod' => function($entity) {
  68. return 'testInstanceMethod';
  69. }));
  70. $this->assertEqual('testInstanceMethod', $entity->testInstanceMethod($entity));
  71. $this->expectException("/^Unhandled method call `foo`.$/");
  72. $entity->foo();
  73. }
  74. public function testMethodDispatchWithNoModel() {
  75. $data = array('foo' => true);
  76. $entity = new Entity(compact('data'));
  77. $this->expectException("/^No model bound to call `foo`.$/");
  78. $entity->foo();
  79. }
  80. public function testMethodDispatchWithEntityAsModel() {
  81. $data = array('foo' => true);
  82. $model = 'lithium\data\Entity';
  83. $entity = new Entity(compact('model', 'data'));
  84. $this->expectException("/^No model bound to call `foo`.$/");
  85. $entity->foo();
  86. }
  87. public function testErrors() {
  88. $entity = new Entity();
  89. $errors = array('foo' => 'Something bad happened.');
  90. $this->assertEqual(array(), $entity->errors());
  91. $entity->errors($errors);
  92. $this->assertEqual($errors, $entity->errors());
  93. $this->assertEqual('Something bad happened.', $entity->errors('foo'));
  94. }
  95. public function testConversion() {
  96. $data = array('foo' => '!!', 'bar' => '??', 'baz' => '--');
  97. $entity = new Entity(compact('data'));
  98. $this->assertEqual($data, $entity->to('array'));
  99. $this->assertEqual($data, $entity->data());
  100. $this->assertEqual($entity, $entity->to('foo'));
  101. }
  102. public function testModified() {
  103. $entity = new Entity();
  104. $this->assertEqual(array(), $entity->modified());
  105. $data = array('foo' => 'bar', 'baz' => 'dib');
  106. $entity->set($data);
  107. $this->assertEqual(array('foo' => true, 'baz' => true), $entity->modified());
  108. $this->assertTrue($entity->modified('foo'));
  109. $this->assertTrue($entity->modified('baz'));
  110. /**
  111. * and last, checking a non-existing field
  112. */
  113. $this->assertNull($entity->modified('ole'));
  114. $subentity = new Entity();
  115. $subentity->set($data);
  116. $entity->set(array('ble' => $subentity));
  117. $this->assertEqual(array('foo' => true, 'baz' => true, 'ble' => true), $entity->modified());
  118. $this->assertTrue($entity->ble->modified('foo'));
  119. $this->assertFalse($entity->ble->modified('iak'));
  120. $this->assertEqual($entity->ble->modified(), array('foo' => true, 'baz' => true));
  121. $data = array('foo' => 'bar', 'baz' => 'dib'); //it's the default data array in the test
  122. $entity = new Entity();
  123. $entity->set($data);
  124. $entity->sync();
  125. /**
  126. * Checking empty values
  127. */
  128. $entity->foo = '';
  129. $this->assertTrue($entity->modified('foo'));
  130. $this->assertEqual(array('foo' => true, 'baz' => false), $entity->modified());
  131. /**
  132. * and checking null values
  133. */
  134. $entity->sync();
  135. $entity->foo = null;
  136. $this->assertTrue($entity->modified('foo'));
  137. }
  138. /**
  139. * Tests that an entity can be cast to a string based on its bound model's meta data.
  140. */
  141. public function testStringCasting() {
  142. $model = $this->_model;
  143. $old = $model::meta('title') ?: 'title';
  144. $model::meta('title', 'firstName');
  145. $object = new Entity(compact('model'));
  146. $object->firstName = 'Bob';
  147. $this->assertEqual('Bob', (string) $object);
  148. $object->firstName = 'Rob';
  149. $this->assertEqual('Rob', (string) $object);
  150. $model::meta('title', $old);
  151. }
  152. public function testRespondsTo() {
  153. $model = $this->_model;
  154. $data = array('foo' => true);
  155. $entity = new Entity(compact('model', 'data'));
  156. $this->assertTrue($entity->respondsTo('foobar'));
  157. $this->assertTrue($entity->respondsTo('findByFoo'));
  158. $this->assertFalse($entity->respondsTo('barbaz'));
  159. $this->assertTrue($entity->respondsTo('model'));
  160. $this->assertTrue($entity->respondsTo('instances'));
  161. }
  162. public function testRespondsToParentCall() {
  163. $model = $this->_model;
  164. $data = array('foo' => true);
  165. $entity = new Entity(compact('model', 'data'));
  166. $this->assertTrue($entity->respondsTo('applyFilter'));
  167. $this->assertFalse($entity->respondsTo('fooBarBaz'));
  168. }
  169. }
  170. ?>