eloquent.test.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <?php
  2. class EloquentTest extends PHPUnit_Framework_TestCase {
  3. /**
  4. * Test the Model constructor.
  5. *
  6. * @group laravel
  7. */
  8. public function testAttributesAreSetByConstructor()
  9. {
  10. $array = array('name' => 'Taylor', 'age' => 25, 'setter' => 'foo');
  11. $model = new Model($array);
  12. $this->assertEquals('Taylor', $model->name);
  13. $this->assertEquals(25, $model->age);
  14. $this->assertEquals('setter: foo', $model->setter);
  15. }
  16. /**
  17. * Test the Model::fill method.
  18. *
  19. * @group laravel
  20. */
  21. public function testAttributesAreSetByFillMethod()
  22. {
  23. $array = array('name' => 'Taylor', 'age' => 25, 'setter' => 'foo');
  24. $model = new Model();
  25. $model->fill($array);
  26. $this->assertEquals('Taylor', $model->name);
  27. $this->assertEquals(25, $model->age);
  28. $this->assertEquals('setter: foo', $model->setter);
  29. }
  30. /**
  31. * Test the Model::fill_raw method.
  32. *
  33. * @group laravel
  34. */
  35. public function testAttributesAreSetByFillRawMethod()
  36. {
  37. $array = array('name' => 'Taylor', 'age' => 25, 'setter' => 'foo');
  38. $model = new Model();
  39. $model->fill_raw($array);
  40. $this->assertEquals($array, $model->attributes);
  41. }
  42. /**
  43. * Test the Model::fill method with accessible.
  44. *
  45. * @group laravel
  46. */
  47. public function testAttributesAreSetByFillMethodWithAccessible()
  48. {
  49. Model::$accessible = array('name', 'age');
  50. $array = array('name' => 'Taylor', 'age' => 25, 'foo' => 'bar');
  51. $model = new Model();
  52. $model->fill($array);
  53. $this->assertEquals('Taylor', $model->name);
  54. $this->assertEquals(25, $model->age);
  55. $this->assertNull($model->foo);
  56. Model::$accessible = null;
  57. }
  58. /**
  59. * Test the Model::fill method with empty accessible array.
  60. *
  61. * @group laravel
  62. */
  63. public function testAttributesAreSetByFillMethodWithEmptyAccessible()
  64. {
  65. Model::$accessible = array();
  66. $array = array('name' => 'Taylor', 'age' => 25, 'foo' => 'bar');
  67. $model = new Model();
  68. $model->fill($array);
  69. $this->assertEquals(array(), $model->attributes);
  70. $this->assertNull($model->name);
  71. $this->assertNull($model->age);
  72. $this->assertNull($model->foo);
  73. Model::$accessible = null;
  74. }
  75. /**
  76. * Test the Model::fill_raw method with accessible.
  77. *
  78. * @group laravel
  79. */
  80. public function testAttributesAreSetByFillRawMethodWithAccessible()
  81. {
  82. Model::$accessible = array('name', 'age');
  83. $array = array('name' => 'taylor', 'age' => 25, 'setter' => 'foo');
  84. $model = new Model();
  85. $model->fill_raw($array);
  86. $this->assertEquals($array, $model->attributes);
  87. Model::$accessible = null;
  88. }
  89. /**
  90. * Test the Model::__set method.
  91. *
  92. * @group laravel
  93. */
  94. public function testAttributeMagicSetterMethodChangesAttribute()
  95. {
  96. Model::$accessible = array('setter');
  97. $array = array('setter' => 'foo', 'getter' => 'bar');
  98. $model = new Model($array);
  99. $model->setter = 'bar';
  100. $model->getter = 'foo';
  101. $this->assertEquals('setter: bar', $model->get_attribute('setter'));
  102. $this->assertEquals('foo', $model->get_attribute('getter'));
  103. Model::$accessible = null;
  104. }
  105. /**
  106. * Test the Model::__get method.
  107. *
  108. * @group laravel
  109. */
  110. public function testAttributeMagicGetterMethodReturnsAttribute()
  111. {
  112. $array = array('setter' => 'foo', 'getter' => 'bar');
  113. $model = new Model($array);
  114. $this->assertEquals('setter: foo', $model->setter);
  115. $this->assertEquals('getter: bar', $model->getter);
  116. }
  117. /**
  118. * Test the Model::set_* method.
  119. *
  120. * @group laravel
  121. */
  122. public function testAttributeSetterMethodChangesAttribute()
  123. {
  124. Model::$accessible = array('setter');
  125. $array = array('setter' => 'foo', 'getter' => 'bar');
  126. $model = new Model($array);
  127. $model->set_setter('bar');
  128. $model->set_getter('foo');
  129. $this->assertEquals('setter: bar', $model->get_attribute('setter'));
  130. $this->assertEquals('foo', $model->get_attribute('getter'));
  131. Model::$accessible = null;
  132. }
  133. /**
  134. * Test the Model::get_* method.
  135. *
  136. * @group laravel
  137. */
  138. public function testAttributeGetterMethodReturnsAttribute()
  139. {
  140. $array = array('setter' => 'foo', 'getter' => 'bar');
  141. $model = new Model($array);
  142. $this->assertEquals('setter: foo', $model->get_setter());
  143. $this->assertEquals('getter: bar', $model->get_getter());
  144. }
  145. /**
  146. * Test determination of dirty/changed attributes.
  147. *
  148. * @group laravel
  149. */
  150. public function testDeterminationOfChangedAttributes()
  151. {
  152. $array = array('name' => 'Taylor', 'age' => 25, 'foo' => null);
  153. $model = new Model($array, true);
  154. $model->name = 'Otwell';
  155. $model->new = null;
  156. $this->assertTrue($model->changed('name'));
  157. $this->assertFalse($model->changed('age'));
  158. $this->assertFalse($model->changed('foo'));
  159. $this->assertFalse($model->changed('new'));
  160. $this->assertTrue($model->dirty());
  161. $this->assertEquals(array('name' => 'Otwell', 'new' => null), $model->get_dirty());
  162. $model->sync();
  163. $this->assertFalse($model->changed('name'));
  164. $this->assertFalse($model->changed('age'));
  165. $this->assertFalse($model->changed('foo'));
  166. $this->assertFalse($model->changed('new'));
  167. $this->assertFalse($model->dirty());
  168. $this->assertEquals(array(), $model->get_dirty());
  169. }
  170. /**
  171. * Test the Model::purge method.
  172. *
  173. * @group laravel
  174. */
  175. public function testAttributePurge()
  176. {
  177. $array = array('name' => 'Taylor', 'age' => 25);
  178. $model = new Model($array);
  179. $model->name = 'Otwell';
  180. $model->age = 26;
  181. $model->purge('name');
  182. $this->assertFalse($model->changed('name'));
  183. $this->assertNull($model->name);
  184. $this->assertTrue($model->changed('age'));
  185. $this->assertEquals(26, $model->age);
  186. $this->assertEquals(array('age' => 26), $model->get_dirty());
  187. }
  188. /**
  189. * Test the Model::table method.
  190. *
  191. * @group laravel
  192. */
  193. public function testTableMethodReturnsCorrectName()
  194. {
  195. $model = new Model();
  196. $this->assertEquals('models', $model->table());
  197. Model::$table = 'table';
  198. $this->assertEquals('table', $model->table());
  199. Model::$table = null;
  200. $this->assertEquals('models', $model->table());
  201. }
  202. /**
  203. * Test the Model::to_array method.
  204. *
  205. * @group laravel
  206. */
  207. public function testConvertingToArray()
  208. {
  209. Model::$hidden = array('password', 'hidden');
  210. $array = array('name' => 'Taylor', 'age' => 25, 'password' => 'laravel', 'null' => null);
  211. $model = new Model($array);
  212. $first = new Model(array('first' => 'foo', 'password' => 'hidden'));
  213. $second = new Model(array('second' => 'bar', 'password' => 'hidden'));
  214. $third = new Model(array('third' => 'baz', 'password' => 'hidden'));
  215. $model->relationships['one'] = new Model(array('foo' => 'bar', 'password' => 'hidden'));
  216. $model->relationships['many'] = array($first, $second, $third);
  217. $model->relationships['hidden'] = new Model(array('should' => 'not_visible'));
  218. $model->relationships['null'] = null;
  219. $this->assertEquals(array(
  220. 'name' => 'Taylor', 'age' => 25, 'null' => null,
  221. 'one' => array('foo' => 'bar'),
  222. 'many' => array(
  223. array('first' => 'foo'),
  224. array('second' => 'bar'),
  225. array('third' => 'baz'),
  226. ),
  227. 'null' => null,
  228. ), $model->to_array());
  229. }
  230. }