PersistentObjectTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. namespace Doctrine\Tests\Common\Persistence;
  3. use Doctrine\Common\Persistence\PersistentObject;
  4. use Doctrine\Common\Persistence\Mapping\ClassMetadata;
  5. use Doctrine\Common\Persistence\Mapping\ReflectionService;
  6. /**
  7. * @group DDC-1448
  8. */
  9. class PersistentObjectTest extends \Doctrine\Tests\DoctrineTestCase
  10. {
  11. private $cm;
  12. private $om;
  13. private $object;
  14. public function setUp()
  15. {
  16. $this->cm = new TestObjectMetadata;
  17. $this->om = $this->getMock('Doctrine\Common\Persistence\ObjectManager');
  18. $this->om->expects($this->any())->method('getClassMetadata')
  19. ->will($this->returnValue($this->cm));
  20. $this->object = new TestObject;
  21. PersistentObject::setObjectManager($this->om);
  22. $this->object->injectObjectManager($this->om, $this->cm);
  23. }
  24. public function testGetObjectManager()
  25. {
  26. $this->assertSame($this->om, PersistentObject::getObjectManager());
  27. }
  28. public function testNonMatchingObjectManager()
  29. {
  30. $this->setExpectedException('RuntimeException');
  31. $om = $this->getMock('Doctrine\Common\Persistence\ObjectManager');
  32. $this->object->injectObjectManager($om, $this->cm);
  33. }
  34. public function testGetField()
  35. {
  36. $this->assertEquals('beberlei', $this->object->getName());
  37. }
  38. public function testSetField()
  39. {
  40. $this->object->setName("test");
  41. $this->assertEquals("test", $this->object->getName());
  42. }
  43. public function testGetIdentifier()
  44. {
  45. $this->assertEquals(1, $this->object->getId());
  46. }
  47. public function testSetIdentifier()
  48. {
  49. $this->setExpectedException('BadMethodCallException');
  50. $this->object->setId(2);
  51. }
  52. public function testSetUnknownField()
  53. {
  54. $this->setExpectedException('BadMethodCallException');
  55. $this->object->setUnknown("test");
  56. }
  57. public function testGetUnknownField()
  58. {
  59. $this->setExpectedException('BadMethodCallException');
  60. $this->object->getUnknown();
  61. }
  62. public function testGetToOneAssociation()
  63. {
  64. $this->assertNull($this->object->getParent());
  65. }
  66. public function testSetToOneAssociation()
  67. {
  68. $parent = new TestObject();
  69. $this->object->setParent($parent);
  70. $this->assertSame($parent, $this->object->getParent($parent));
  71. }
  72. public function testSetInvalidToOneAssociation()
  73. {
  74. $parent = new \stdClass();
  75. $this->setExpectedException('InvalidArgumentException');
  76. $this->object->setParent($parent);
  77. }
  78. public function testSetToOneAssociationNull()
  79. {
  80. $parent = new TestObject();
  81. $this->object->setParent($parent);
  82. $this->object->setParent(null);
  83. $this->assertNull($this->object->getParent());
  84. }
  85. public function testAddToManyAssociation()
  86. {
  87. $child = new TestObject();
  88. $this->object->addChildren($child);
  89. $this->assertSame($this->object, $child->getParent());
  90. $this->assertEquals(1, count($this->object->getChildren()));
  91. $child = new TestObject();
  92. $this->object->addChildren($child);
  93. $this->assertEquals(2, count($this->object->getChildren()));
  94. }
  95. public function testAddInvalidToManyAssociation()
  96. {
  97. $this->setExpectedException('InvalidArgumentException');
  98. $this->object->addChildren(new \stdClass());
  99. }
  100. public function testNoObjectManagerSet()
  101. {
  102. PersistentObject::setObjectManager(null);
  103. $child = new TestObject();
  104. $this->setExpectedException('RuntimeException');
  105. $child->setName("test");
  106. }
  107. public function testInvalidMethod()
  108. {
  109. $this->setExpectedException('BadMethodCallException');
  110. $this->object->asdf();
  111. }
  112. public function testAddInvalidCollection()
  113. {
  114. $this->setExpectedException('BadMethodCallException');
  115. $this->object->addAsdf(new \stdClass());
  116. }
  117. }
  118. class TestObject extends PersistentObject
  119. {
  120. protected $id = 1;
  121. protected $name = 'beberlei';
  122. protected $parent;
  123. protected $children;
  124. }
  125. class TestObjectMetadata implements ClassMetadata
  126. {
  127. public function getAssociationMappedByTargetField($assocName)
  128. {
  129. $assoc = array('children' => 'parent');
  130. return $assoc[$assocName];
  131. }
  132. public function getAssociationNames()
  133. {
  134. return array('parent', 'children');
  135. }
  136. public function getAssociationTargetClass($assocName)
  137. {
  138. return __NAMESPACE__ . '\TestObject';
  139. }
  140. public function getFieldNames()
  141. {
  142. return array('id', 'name');
  143. }
  144. public function getIdentifier()
  145. {
  146. return array('id');
  147. }
  148. public function getName()
  149. {
  150. return __NAMESPACE__ . '\TestObject';
  151. }
  152. public function getReflectionClass()
  153. {
  154. return new \ReflectionClass($this->getName());
  155. }
  156. public function getTypeOfField($fieldName)
  157. {
  158. $types = array('id' => 'integer', 'name' => 'string');
  159. return $types[$fieldName];
  160. }
  161. public function hasAssociation($fieldName)
  162. {
  163. return in_array($fieldName, array('parent', 'children'));
  164. }
  165. public function hasField($fieldName)
  166. {
  167. return in_array($fieldName, array('id', 'name'));
  168. }
  169. public function isAssociationInverseSide($assocName)
  170. {
  171. return ($assocName === 'children');
  172. }
  173. public function isCollectionValuedAssociation($fieldName)
  174. {
  175. return ($fieldName === 'children');
  176. }
  177. public function isIdentifier($fieldName)
  178. {
  179. return $fieldName === 'id';
  180. }
  181. public function isSingleValuedAssociation($fieldName)
  182. {
  183. return $fieldName === 'parent';
  184. }
  185. public function getIdentifierValues($entity)
  186. {
  187. }
  188. public function getIdentifierFieldNames()
  189. {
  190. }
  191. public function initializeReflection(ReflectionService $reflService)
  192. {
  193. }
  194. public function wakeupReflection(ReflectionService $reflService)
  195. {
  196. }
  197. }