PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/active_record/tests/cases/lmbARAttributesLazyLoadingTest.class.php

http://github.com/limb-php-framework/limb
PHP | 271 lines | 197 code | 55 blank | 19 comment | 0 complexity | 4e6d5b645e82eb0c493d6ece4587e358 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0, MPL-2.0-no-copyleft-exception, GPL-2.0
  1. <?php
  2. /*
  3. * Limb PHP Framework
  4. *
  5. * @link http://limb-project.com
  6. * @copyright Copyright &copy; 2004-2009 BIT(http://bit-creative.com)
  7. * @license LGPL http://www.gnu.org/copyleft/lesser.html
  8. */
  9. class lmbARAttributesLazyLoadingTest extends lmbARBaseTestCase
  10. {
  11. protected $tables_to_cleanup = array('test_one_table_object');
  12. function testLazyFind()
  13. {
  14. $object = $this->_createActiveRecord($annotation = 'Some annotation', $content = 'Some content');
  15. $object2 = lmbActiveRecord :: findById('LazyTestOneTableObject', $object->getId());
  16. $this->_checkLazyness($object2, $annotation, $content);
  17. }
  18. function testLazyLoadById()
  19. {
  20. $object = $this->_createActiveRecord($annotation = 'Some annotation', $content = 'Some content');
  21. $object2 = new LazyTestOneTableObject();
  22. $object2->loadById($object->getId());
  23. $this->_checkLazyness($object2, $annotation, $content);
  24. }
  25. function testForceToLoadAllLazyAttributes()
  26. {
  27. $object1 = $this->_createActiveRecord('Some annotation', 'Some content');
  28. $object2 = $this->_createActiveRecord('Some other annotation', 'Some other content');
  29. $query = lmbARQuery :: create('LazyTestOneTableObject', $params = array('with_lazy_attributes' => ''));
  30. $arr = $query->fetch()->getArray();
  31. $this->assertTrue(array_key_exists('annotation', $arr[0]->exportRaw()));
  32. $this->assertTrue(array_key_exists('content', $arr[0]->exportRaw()));
  33. $this->assertTrue(array_key_exists('annotation', $arr[1]->exportRaw()));
  34. $this->assertTrue(array_key_exists('content', $arr[1]->exportRaw()));
  35. }
  36. function testForceToLoadSomeLazyAttributes()
  37. {
  38. $object1 = $this->_createActiveRecord('Some annotation', 'Some content');
  39. $object2 = $this->_createActiveRecord('Some other annotation', 'Some other content');
  40. $query = lmbARQuery :: create('LazyTestOneTableObject', $params = array('with_lazy_attributes' => array('annotation')));
  41. $arr = $query->fetch()->getArray();
  42. $this->assertTrue(array_key_exists('annotation', $arr[0]->exportRaw()));
  43. $this->assertFalse(array_key_exists('content', $arr[0]->exportRaw()));
  44. $this->assertTrue(array_key_exists('annotation', $arr[1]->exportRaw()));
  45. $this->assertFalse(array_key_exists('content', $arr[1]->exportRaw()));
  46. }
  47. function testLazyWorksOkForEagerJoin_OneToOneRelations()
  48. {
  49. $person = new PersonForLazyAttributesTest();
  50. $person->setName('Some name');
  51. $lazy_object = $this->_createActiveRecord($annotation = 'Some annotation', $content = 'Some content');
  52. $person->set('lazy_object', $lazy_object);
  53. $person->save();
  54. $person_loaded = lmbActiveRecord :: findOne('PersonForLazyAttributesTest',
  55. array('criteria' => 'person_for_test.id = ' . $person->getId(),
  56. 'join' => 'lazy_object'));
  57. $lazy_object2 = $person_loaded->getLazyObject();
  58. $this->_checkLazyness($lazy_object2, $annotation, $content);
  59. }
  60. function testForceToLoadAllLazyAttributes_ForEagerJoin_OneToOneRelations()
  61. {
  62. $person = new PersonForLazyAttributesTest();
  63. $person->setName('Some name');
  64. $lazy_object = $this->_createActiveRecord($annotation = 'Some annotation', $content = 'Some content');
  65. $person->set('lazy_object', $lazy_object);
  66. $person->save();
  67. $person_loaded = lmbActiveRecord :: findOne('PersonForLazyAttributesTest',
  68. array('criteria' => 'person_for_test.id = ' . $person->getId(),
  69. 'join' => array('lazy_object' => array('with_lazy_attributes' => ''))));
  70. $lazy_object2 = $person_loaded->getLazyObject();
  71. $this->assertTrue(array_key_exists('annotation', $lazy_object2->exportRaw()));
  72. $this->assertTrue(array_key_exists('content', $lazy_object2->exportRaw()));
  73. }
  74. function testLazyWorksOkForEagerJoin_ForParentObject_OneToOneRelations()
  75. {
  76. $person = new PersonForLazyAttributesTest();
  77. $person->setName($name = 'Some name');
  78. $lazy_object = $this->_createActiveRecord($annotation = 'Some annotation', $content = 'Some content');
  79. $person->set('lazy_object', $lazy_object);
  80. $person->save();
  81. $person_loaded = lmbActiveRecord :: findOne('PersonForLazyAttributesTest',
  82. array('criteria' => 'person_for_test.id = ' . $person->getId(),
  83. 'join' => 'lazy_object'));
  84. $this->assertFalse(array_key_exists('name', $person_loaded->exportRaw()));
  85. }
  86. function testLazyWorksOkForEagerAttach_OneToOneRelations()
  87. {
  88. $person = new PersonForLazyAttributesTest();
  89. $person->setName('Some name');
  90. $lazy_object = $this->_createActiveRecord($annotation = 'Some annotation', $content = 'Some content');
  91. $person->set('lazy_object', $lazy_object);
  92. $person->save();
  93. $person_loaded = lmbActiveRecord :: findOne('PersonForLazyAttributesTest',
  94. array('criteria' => 'person_for_test.id = ' . $person->getId(),
  95. 'attach' => 'lazy_object'));
  96. $lazy_object2 = $person_loaded->getLazyObject();
  97. $this->_checkLazyness($lazy_object2, $annotation, $content);
  98. }
  99. function testExportIsNotLazy()
  100. {
  101. $object = $this->_createActiveRecord($annotation = 'Some annotation', $content = 'Some content');
  102. $object2 = lmbActiveRecord :: findById('LazyTestOneTableObject', $object->getId());
  103. $exported = $object2->export();
  104. $this->assertEqual($exported['annotation'], $annotation);
  105. $this->assertEqual($exported['content'], $content);
  106. }
  107. function testCustomLazyFieldsInFindById()
  108. {
  109. $object = new TestOneTableObject();
  110. $object->setAnnotation($annotation = "Annotation");
  111. $object->setContent($content = "Content");
  112. $object->save();
  113. $object2 = lmbActiveRecord :: findById('TestOneTableObject', array('id' => $object->getId(), 'fields' => array('annotation')));
  114. $fields = $object2->exportRaw();
  115. //checking which props were actually loaded
  116. $this->assertEqual($fields, array('id' => $object->getId(), 'annotation' => $annotation));
  117. //lazy loading in action
  118. $this->assertEqual($object2->getAnnotation(), $annotation);
  119. $this->assertEqual($object2->getContent(), $content);
  120. }
  121. function testCustomLazyFieldsInFind()
  122. {
  123. $object = new TestOneTableObject();
  124. $object->setAnnotation($annotation = "Annotation");
  125. $object->setContent($content = "Content");
  126. $object->save();
  127. $rs = lmbActiveRecord :: find('TestOneTableObject', array('fields' => array('annotation')));
  128. $object2 = $rs->at(0);
  129. $fields = $object2->exportRaw();
  130. //checking which props were actually loaded
  131. $this->assertEqual($fields, array('id' => $object->getId(), 'annotation' => $annotation));
  132. //lazy loading in action
  133. $this->assertEqual($object2->getAnnotation(), $annotation);
  134. $this->assertEqual($object2->getContent(), $content);
  135. }
  136. function testCustomLazyFieldsInFindFirst()
  137. {
  138. $object = new TestOneTableObject();
  139. $object->setAnnotation($annotation = "Annotation");
  140. $object->setContent($content = "Content");
  141. $object->save();
  142. $object2 = lmbActiveRecord :: findFirst('TestOneTableObject', array('fields' => array('annotation')));
  143. $fields = $object2->exportRaw();
  144. //checking which props were actually loaded
  145. $this->assertEqual($fields, array('id' => $object->getId(), 'annotation' => $annotation));
  146. //lazy loading in action
  147. $this->assertEqual($object2->getAnnotation(), $annotation);
  148. $this->assertEqual($object2->getContent(), $content);
  149. }
  150. function testLazyFieldsInOneToManyRelations()
  151. {
  152. $course = $this->creator->createCourse();
  153. $l1 = $this->creator->createLecture($course);
  154. $l1->setTitle('Lecture1');
  155. $l1->save();
  156. $l2 = $this->creator->createLecture($course);
  157. $l2->setTitle('Lecture2');
  158. $l2->save();
  159. //all fields are lazy
  160. $lectures = $course->getLectures()->find(array('fields' => array()));
  161. $this->assertEqual(sizeof($lectures), 2);
  162. $fields1 = $lectures[0]->exportRaw();
  163. $this->assertFalse(isset($fields1['title']));
  164. //lazy loading kicks in
  165. $this->assertEqual($lectures[0]->getTitle(), 'Lecture1');
  166. $fields2 = $lectures[1]->exportRaw();
  167. $this->assertFalse(isset($fields2['title']));
  168. //lazy loading kicks in
  169. $this->assertEqual($lectures[1]->getTitle(), 'Lecture2');
  170. }
  171. function testLazyFieldsInManyToManyRelations()
  172. {
  173. $group = $this->creator->createGroup();
  174. $u1 = $this->creator->createUser();
  175. $u1->setFirstName("bob1");
  176. $u1->save();
  177. $u2 = $this->creator->createUser();
  178. $u2->setFirstName("bob2");
  179. $u2->save();
  180. $group->getUsers()->add($u1);
  181. $group->getUsers()->add($u2);
  182. //all fields are lazy
  183. $users = $group->getUsers()->find(array('fields' => array()));
  184. $this->assertEqual(sizeof($users), 2);
  185. $fields1 = $users[0]->exportRaw();
  186. $this->assertFalse(isset($fields1['title']));
  187. //lazy loading kicks in
  188. $this->assertEqual($users[0]->getFirstName(), 'bob1');
  189. $fields2 = $users[1]->exportRaw();
  190. $this->assertFalse(isset($fields2['title']));
  191. //lazy loading kicks in
  192. $this->assertEqual($users[1]->getFirstName(), 'bob2');
  193. }
  194. protected function _checkLazyness($object, $annotation, $content)
  195. {
  196. $this->assertTrue($object->has('news_date'));
  197. $this->assertFalse(array_key_exists('annotation', $object->exportRaw()));
  198. $this->assertTrue($object->has('annotation'));
  199. $this->assertEqual($object->getAnnotation(), $annotation);
  200. $this->assertTrue($object->has('annotation'));
  201. $this->assertTrue(array_key_exists('annotation', $object->exportRaw()));
  202. $this->assertFalse(array_key_exists('content', $object->exportRaw()));
  203. $this->assertTrue($object->has('content'));
  204. $this->assertEqual($object->getContent(), $content);
  205. $this->assertTrue($object->has('content'));
  206. $this->assertTrue(array_key_exists('content', $object->exportRaw()));
  207. }
  208. protected function _createActiveRecord($annotation, $content)
  209. {
  210. $object = new LazyTestOneTableObject();
  211. $object->setAnnotation($annotation);
  212. $object->setContent($content);
  213. $object->save();
  214. return $object;
  215. }
  216. }