ClassMetadataTest.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. <?php
  2. namespace Doctrine\Tests\ORM\Mapping;
  3. use Doctrine\ORM\Mapping\ClassMetadata;
  4. use Doctrine\ORM\Events;
  5. require_once __DIR__ . '/../../TestInit.php';
  6. require_once __DIR__ . '/../../Models/Global/GlobalNamespaceModel.php';
  7. class ClassMetadataTest extends \Doctrine\Tests\OrmTestCase
  8. {
  9. public function testClassMetadataInstanceSerialization()
  10. {
  11. $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
  12. $cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
  13. // Test initial state
  14. $this->assertTrue(count($cm->getReflectionProperties()) == 0);
  15. $this->assertInstanceOf('ReflectionClass', $cm->reflClass);
  16. $this->assertEquals('Doctrine\Tests\Models\CMS\CmsUser', $cm->name);
  17. $this->assertEquals('Doctrine\Tests\Models\CMS\CmsUser', $cm->rootEntityName);
  18. $this->assertEquals(array(), $cm->subClasses);
  19. $this->assertEquals(array(), $cm->parentClasses);
  20. $this->assertEquals(ClassMetadata::INHERITANCE_TYPE_NONE, $cm->inheritanceType);
  21. // Customize state
  22. $cm->setInheritanceType(ClassMetadata::INHERITANCE_TYPE_SINGLE_TABLE);
  23. $cm->setSubclasses(array("One", "Two", "Three"));
  24. $cm->setParentClasses(array("UserParent"));
  25. $cm->setCustomRepositoryClass("UserRepository");
  26. $cm->setDiscriminatorColumn(array('name' => 'disc', 'type' => 'integer'));
  27. $cm->mapOneToOne(array('fieldName' => 'phonenumbers', 'targetEntity' => 'CmsAddress', 'mappedBy' => 'foo'));
  28. $cm->markReadOnly();
  29. $cm->addNamedQuery(array('name' => 'dql', 'query' => 'foo'));
  30. $this->assertEquals(1, count($cm->associationMappings));
  31. $serialized = serialize($cm);
  32. $cm = unserialize($serialized);
  33. $cm->wakeupReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
  34. // Check state
  35. $this->assertTrue(count($cm->getReflectionProperties()) > 0);
  36. $this->assertEquals('Doctrine\Tests\Models\CMS', $cm->namespace);
  37. $this->assertInstanceOf('ReflectionClass', $cm->reflClass);
  38. $this->assertEquals('Doctrine\Tests\Models\CMS\CmsUser', $cm->name);
  39. $this->assertEquals('UserParent', $cm->rootEntityName);
  40. $this->assertEquals(array('Doctrine\Tests\Models\CMS\One', 'Doctrine\Tests\Models\CMS\Two', 'Doctrine\Tests\Models\CMS\Three'), $cm->subClasses);
  41. $this->assertEquals(array('UserParent'), $cm->parentClasses);
  42. $this->assertEquals('Doctrine\Tests\Models\CMS\UserRepository', $cm->customRepositoryClassName);
  43. $this->assertEquals(array('name' => 'disc', 'type' => 'integer', 'fieldName' => 'disc'), $cm->discriminatorColumn);
  44. $this->assertTrue($cm->associationMappings['phonenumbers']['type'] == ClassMetadata::ONE_TO_ONE);
  45. $this->assertEquals(1, count($cm->associationMappings));
  46. $oneOneMapping = $cm->getAssociationMapping('phonenumbers');
  47. $this->assertTrue($oneOneMapping['fetch'] == ClassMetadata::FETCH_LAZY);
  48. $this->assertEquals('phonenumbers', $oneOneMapping['fieldName']);
  49. $this->assertEquals('Doctrine\Tests\Models\CMS\CmsAddress', $oneOneMapping['targetEntity']);
  50. $this->assertTrue($cm->isReadOnly);
  51. $this->assertEquals(array('dql' => array('name'=>'dql','query'=>'foo','dql'=>'foo')), $cm->namedQueries);
  52. }
  53. public function testFieldIsNullable()
  54. {
  55. $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
  56. $cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
  57. // Explicit Nullable
  58. $cm->mapField(array('fieldName' => 'status', 'nullable' => true, 'type' => 'string', 'length' => 50));
  59. $this->assertTrue($cm->isNullable('status'));
  60. // Explicit Not Nullable
  61. $cm->mapField(array('fieldName' => 'username', 'nullable' => false, 'type' => 'string', 'length' => 50));
  62. $this->assertFalse($cm->isNullable('username'));
  63. // Implicit Not Nullable
  64. $cm->mapField(array('fieldName' => 'name', 'type' => 'string', 'length' => 50));
  65. $this->assertFalse($cm->isNullable('name'), "By default a field should not be nullable.");
  66. }
  67. /**
  68. * @group DDC-115
  69. */
  70. public function testMapAssocationInGlobalNamespace()
  71. {
  72. require_once __DIR__."/../../Models/Global/GlobalNamespaceModel.php";
  73. $cm = new ClassMetadata('DoctrineGlobal_Article');
  74. $cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
  75. $cm->mapManyToMany(array(
  76. 'fieldName' => 'author',
  77. 'targetEntity' => 'DoctrineGlobal_User',
  78. 'joinTable' => array(
  79. 'name' => 'bar',
  80. 'joinColumns' => array(array('name' => 'bar_id', 'referencedColumnName' => 'id')),
  81. 'inverseJoinColumns' => array(array('name' => 'baz_id', 'referencedColumnName' => 'id')),
  82. ),
  83. ));
  84. $this->assertEquals("DoctrineGlobal_User", $cm->associationMappings['author']['targetEntity']);
  85. }
  86. public function testMapManyToManyJoinTableDefaults()
  87. {
  88. $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
  89. $cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
  90. $cm->mapManyToMany(
  91. array(
  92. 'fieldName' => 'groups',
  93. 'targetEntity' => 'CmsGroup'
  94. ));
  95. $assoc = $cm->associationMappings['groups'];
  96. //$this->assertInstanceOf('Doctrine\ORM\Mapping\ManyToManyMapping', $assoc);
  97. $this->assertEquals(array(
  98. 'name' => 'cmsuser_cmsgroup',
  99. 'joinColumns' => array(array('name' => 'cmsuser_id', 'referencedColumnName' => 'id', 'onDelete' => 'CASCADE')),
  100. 'inverseJoinColumns' => array(array('name' => 'cmsgroup_id', 'referencedColumnName' => 'id', 'onDelete' => 'CASCADE'))
  101. ), $assoc['joinTable']);
  102. $this->assertTrue($assoc['isOnDeleteCascade']);
  103. }
  104. public function testSerializeManyToManyJoinTableCascade()
  105. {
  106. $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
  107. $cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
  108. $cm->mapManyToMany(
  109. array(
  110. 'fieldName' => 'groups',
  111. 'targetEntity' => 'CmsGroup'
  112. ));
  113. /* @var $assoc \Doctrine\ORM\Mapping\ManyToManyMapping */
  114. $assoc = $cm->associationMappings['groups'];
  115. $assoc = unserialize(serialize($assoc));
  116. $this->assertTrue($assoc['isOnDeleteCascade']);
  117. }
  118. /**
  119. * @group DDC-115
  120. */
  121. public function testSetDiscriminatorMapInGlobalNamespace()
  122. {
  123. require_once __DIR__."/../../Models/Global/GlobalNamespaceModel.php";
  124. $cm = new ClassMetadata('DoctrineGlobal_User');
  125. $cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
  126. $cm->setDiscriminatorMap(array('descr' => 'DoctrineGlobal_Article', 'foo' => 'DoctrineGlobal_User'));
  127. $this->assertEquals("DoctrineGlobal_Article", $cm->discriminatorMap['descr']);
  128. $this->assertEquals("DoctrineGlobal_User", $cm->discriminatorMap['foo']);
  129. }
  130. /**
  131. * @group DDC-115
  132. */
  133. public function testSetSubClassesInGlobalNamespace()
  134. {
  135. require_once __DIR__."/../../Models/Global/GlobalNamespaceModel.php";
  136. $cm = new ClassMetadata('DoctrineGlobal_User');
  137. $cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
  138. $cm->setSubclasses(array('DoctrineGlobal_Article'));
  139. $this->assertEquals("DoctrineGlobal_Article", $cm->subClasses[0]);
  140. }
  141. /**
  142. * @group DDC-268
  143. */
  144. public function testSetInvalidVersionMapping_ThrowsException()
  145. {
  146. $field = array();
  147. $field['fieldName'] = 'foo';
  148. $field['type'] = 'string';
  149. $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
  150. $cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
  151. $this->setExpectedException('Doctrine\ORM\Mapping\MappingException');
  152. $cm->setVersionMapping($field);
  153. }
  154. public function testGetSingleIdentifierFieldName_MultipleIdentifierEntity_ThrowsException()
  155. {
  156. $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
  157. $cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
  158. $cm->isIdentifierComposite = true;
  159. $this->setExpectedException('Doctrine\ORM\Mapping\MappingException');
  160. $cm->getSingleIdentifierFieldName();
  161. }
  162. public function testDuplicateAssociationMappingException()
  163. {
  164. $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
  165. $cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
  166. $a1 = array('fieldName' => 'foo', 'sourceEntity' => 'stdClass', 'targetEntity' => 'stdClass', 'mappedBy' => 'foo');
  167. $a2 = array('fieldName' => 'foo', 'sourceEntity' => 'stdClass', 'targetEntity' => 'stdClass', 'mappedBy' => 'foo');
  168. $cm->addInheritedAssociationMapping($a1);
  169. $this->setExpectedException('Doctrine\ORM\Mapping\MappingException');
  170. $cm->addInheritedAssociationMapping($a2);
  171. }
  172. public function testDuplicateColumnName_ThrowsMappingException()
  173. {
  174. $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
  175. $cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
  176. $cm->mapField(array('fieldName' => 'name', 'columnName' => 'name'));
  177. $this->setExpectedException('Doctrine\ORM\Mapping\MappingException');
  178. $cm->mapField(array('fieldName' => 'username', 'columnName' => 'name'));
  179. }
  180. public function testDuplicateColumnName_DiscriminatorColumn_ThrowsMappingException()
  181. {
  182. $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
  183. $cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
  184. $cm->mapField(array('fieldName' => 'name', 'columnName' => 'name'));
  185. $this->setExpectedException('Doctrine\ORM\Mapping\MappingException');
  186. $cm->setDiscriminatorColumn(array('name' => 'name'));
  187. }
  188. public function testDuplicateColumnName_DiscriminatorColumn2_ThrowsMappingException()
  189. {
  190. $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
  191. $cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
  192. $cm->setDiscriminatorColumn(array('name' => 'name'));
  193. $this->setExpectedException('Doctrine\ORM\Mapping\MappingException');
  194. $cm->mapField(array('fieldName' => 'name', 'columnName' => 'name'));
  195. }
  196. public function testDuplicateFieldAndAssocationMapping1_ThrowsException()
  197. {
  198. $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
  199. $cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
  200. $cm->mapField(array('fieldName' => 'name', 'columnName' => 'name'));
  201. $this->setExpectedException('Doctrine\ORM\Mapping\MappingException');
  202. $cm->mapOneToOne(array('fieldName' => 'name', 'targetEntity' => 'CmsUser'));
  203. }
  204. public function testDuplicateFieldAndAssocationMapping2_ThrowsException()
  205. {
  206. $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
  207. $cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
  208. $cm->mapOneToOne(array('fieldName' => 'name', 'targetEntity' => 'CmsUser'));
  209. $this->setExpectedException('Doctrine\ORM\Mapping\MappingException');
  210. $cm->mapField(array('fieldName' => 'name', 'columnName' => 'name'));
  211. }
  212. /**
  213. * @group DDC-1224
  214. */
  215. public function testGetTemporaryTableNameSchema()
  216. {
  217. $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
  218. $cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
  219. $cm->setTableName('foo.bar');
  220. $this->assertEquals('foo_bar_id_tmp', $cm->getTemporaryIdTableName());
  221. }
  222. public function testDefaultTableName()
  223. {
  224. $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
  225. $cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
  226. // When table's name is not given
  227. $primaryTable = array();
  228. $cm->setPrimaryTable($primaryTable);
  229. $this->assertEquals('CmsUser', $cm->getTableName());
  230. $this->assertEquals('CmsUser', $cm->table['name']);
  231. $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsAddress');
  232. $cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
  233. // When joinTable's name is not given
  234. $cm->mapManyToMany(array(
  235. 'fieldName' => 'user',
  236. 'targetEntity' => 'CmsUser',
  237. 'inversedBy' => 'users',
  238. 'joinTable' => array('joinColumns' => array(array('referencedColumnName' => 'id')),
  239. 'inverseJoinColumns' => array(array('referencedColumnName' => 'id')))));
  240. $this->assertEquals('cmsaddress_cmsuser', $cm->associationMappings['user']['joinTable']['name']);
  241. }
  242. public function testDefaultJoinColumnName()
  243. {
  244. $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsAddress');
  245. $cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
  246. // this is really dirty, but it's the simpliest way to test whether
  247. // joinColumn's name will be automatically set to user_id
  248. $cm->mapOneToOne(array(
  249. 'fieldName' => 'user',
  250. 'targetEntity' => 'CmsUser',
  251. 'joinColumns' => array(array('referencedColumnName' => 'id'))));
  252. $this->assertEquals('user_id', $cm->associationMappings['user']['joinColumns'][0]['name']);
  253. $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsAddress');
  254. $cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
  255. $cm->mapManyToMany(array(
  256. 'fieldName' => 'user',
  257. 'targetEntity' => 'CmsUser',
  258. 'inversedBy' => 'users',
  259. 'joinTable' => array('name' => 'user_CmsUser',
  260. 'joinColumns' => array(array('referencedColumnName' => 'id')),
  261. 'inverseJoinColumns' => array(array('referencedColumnName' => 'id')))));
  262. $this->assertEquals('cmsaddress_id', $cm->associationMappings['user']['joinTable']['joinColumns'][0]['name']);
  263. $this->assertEquals('cmsuser_id', $cm->associationMappings['user']['joinTable']['inverseJoinColumns'][0]['name']);
  264. }
  265. /**
  266. * @group DDC-886
  267. */
  268. public function testSetMultipleIdentifierSetsComposite()
  269. {
  270. $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
  271. $cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
  272. $cm->mapField(array('fieldName' => 'name'));
  273. $cm->mapField(array('fieldName' => 'username'));
  274. $cm->setIdentifier(array('name', 'username'));
  275. $this->assertTrue($cm->isIdentifierComposite);
  276. }
  277. /**
  278. * @group DDC-944
  279. */
  280. public function testMappingNotFound()
  281. {
  282. $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
  283. $cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
  284. $this->setExpectedException('Doctrine\ORM\Mapping\MappingException', "No mapping found for field 'foo' on class 'Doctrine\Tests\Models\CMS\CmsUser'.");
  285. $cm->getFieldMapping('foo');
  286. }
  287. /**
  288. * @group DDC-961
  289. */
  290. public function testJoinTableMappingDefaults()
  291. {
  292. $cm = new ClassMetadata('DoctrineGlobal_Article');
  293. $cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
  294. $cm->mapManyToMany(array('fieldName' => 'author', 'targetEntity' => 'Doctrine\Tests\Models\CMS\CmsUser'));
  295. $this->assertEquals('doctrineglobal_article_cmsuser', $cm->associationMappings['author']['joinTable']['name']);
  296. }
  297. /**
  298. * @group DDC-117
  299. */
  300. public function testMapIdentifierAssociation()
  301. {
  302. $cm = new ClassMetadata('Doctrine\Tests\Models\DDC117\DDC117ArticleDetails');
  303. $cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
  304. $cm->mapOneToOne(array(
  305. 'fieldName' => 'article',
  306. 'id' => true,
  307. 'targetEntity' => 'Doctrine\Tests\Models\DDC117\DDC117Article',
  308. 'joinColumns' => array(),
  309. ));
  310. $this->assertTrue($cm->containsForeignIdentifier, "Identifier Association should set 'containsForeignIdentifier' boolean flag.");
  311. $this->assertEquals(array("article"), $cm->identifier);
  312. }
  313. /**
  314. * @group DDC-117
  315. */
  316. public function testOrphanRemovalIdentifierAssociation()
  317. {
  318. $cm = new ClassMetadata('Doctrine\Tests\Models\DDC117\DDC117ArticleDetails');
  319. $cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
  320. $this->setExpectedException('Doctrine\ORM\Mapping\MappingException', 'The orphan removal option is not allowed on an association that');
  321. $cm->mapOneToOne(array(
  322. 'fieldName' => 'article',
  323. 'id' => true,
  324. 'targetEntity' => 'Doctrine\Tests\Models\DDC117\DDC117Article',
  325. 'orphanRemoval' => true,
  326. 'joinColumns' => array(),
  327. ));
  328. }
  329. /**
  330. * @group DDC-117
  331. */
  332. public function testInverseIdentifierAssocation()
  333. {
  334. $cm = new ClassMetadata('Doctrine\Tests\Models\DDC117\DDC117ArticleDetails');
  335. $cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
  336. $this->setExpectedException('Doctrine\ORM\Mapping\MappingException', 'An inverse association is not allowed to be identifier in');
  337. $cm->mapOneToOne(array(
  338. 'fieldName' => 'article',
  339. 'id' => true,
  340. 'mappedBy' => 'details', // INVERSE!
  341. 'targetEntity' => 'Doctrine\Tests\Models\DDC117\DDC117Article',
  342. 'joinColumns' => array(),
  343. ));
  344. }
  345. /**
  346. * @group DDC-117
  347. */
  348. public function testIdentifierAssocationManyToMany()
  349. {
  350. $cm = new ClassMetadata('Doctrine\Tests\Models\DDC117\DDC117ArticleDetails');
  351. $cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
  352. $this->setExpectedException('Doctrine\ORM\Mapping\MappingException', 'Many-to-many or one-to-many associations are not allowed to be identifier in');
  353. $cm->mapManyToMany(array(
  354. 'fieldName' => 'article',
  355. 'id' => true,
  356. 'targetEntity' => 'Doctrine\Tests\Models\DDC117\DDC117Article',
  357. 'joinColumns' => array(),
  358. ));
  359. }
  360. /**
  361. * @group DDC-996
  362. */
  363. public function testEmptyFieldNameThrowsException()
  364. {
  365. $this->setExpectedException('Doctrine\ORM\Mapping\MappingException',
  366. "The field or association mapping misses the 'fieldName' attribute in entity 'Doctrine\Tests\Models\CMS\CmsUser'.");
  367. $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
  368. $cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
  369. $cm->mapField(array('fieldName' => ''));
  370. }
  371. public function testRetrievalOfNamedQueries()
  372. {
  373. $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
  374. $cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
  375. $this->assertEquals(0, count($cm->getNamedQueries()));
  376. $cm->addNamedQuery(array(
  377. 'name' => 'userById',
  378. 'query' => 'SELECT u FROM __CLASS__ u WHERE u.id = ?1'
  379. ));
  380. $this->assertEquals(1, count($cm->getNamedQueries()));
  381. }
  382. public function testExistanceOfNamedQuery()
  383. {
  384. $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
  385. $cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
  386. $cm->addNamedQuery(array(
  387. 'name' => 'all',
  388. 'query' => 'SELECT u FROM __CLASS__ u'
  389. ));
  390. $this->assertTrue($cm->hasNamedQuery('all'));
  391. $this->assertFalse($cm->hasNamedQuery('userById'));
  392. }
  393. public function testRetrieveOfNamedQuery()
  394. {
  395. $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
  396. $cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
  397. $cm->addNamedQuery(array(
  398. 'name' => 'userById',
  399. 'query' => 'SELECT u FROM __CLASS__ u WHERE u.id = ?1'
  400. ));
  401. $this->assertEquals('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = ?1', $cm->getNamedQuery('userById'));
  402. }
  403. public function testNamingCollisionNamedQueryShouldThrowException()
  404. {
  405. $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
  406. $cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
  407. $this->setExpectedException('Doctrine\ORM\Mapping\MappingException');
  408. $cm->addNamedQuery(array(
  409. 'name' => 'userById',
  410. 'query' => 'SELECT u FROM __CLASS__ u WHERE u.id = ?1'
  411. ));
  412. $cm->addNamedQuery(array(
  413. 'name' => 'userById',
  414. 'query' => 'SELECT u FROM __CLASS__ u WHERE u.id = ?1'
  415. ));
  416. }
  417. /**
  418. * @group DDC-1068
  419. */
  420. public function testClassCaseSensitivity()
  421. {
  422. $user = new \Doctrine\Tests\Models\CMS\CmsUser();
  423. $cm = new ClassMetadata('DOCTRINE\TESTS\MODELS\CMS\CMSUSER');
  424. $cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
  425. $this->assertEquals('Doctrine\Tests\Models\CMS\CmsUser', $cm->name);
  426. }
  427. /**
  428. * @group DDC-659
  429. */
  430. public function testLifecycleCallbackNotFound()
  431. {
  432. $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
  433. $cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
  434. $cm->addLifecycleCallback('notfound', 'postLoad');
  435. $this->setExpectedException("Doctrine\ORM\Mapping\MappingException", "Entity 'Doctrine\Tests\Models\CMS\CmsUser' has no method 'notfound' to be registered as lifecycle callback.");
  436. $cm->validateLifecycleCallbacks(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
  437. }
  438. /**
  439. * @group ImproveErrorMessages
  440. */
  441. public function testTargetEntityNotFound()
  442. {
  443. $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
  444. $cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
  445. $cm->mapManyToOne(array('fieldName' => 'address', 'targetEntity' => 'UnknownClass'));
  446. $this->setExpectedException("Doctrine\ORM\Mapping\MappingException", "The target-entity Doctrine\Tests\Models\CMS\UnknownClass cannot be found in 'Doctrine\Tests\Models\CMS\CmsUser#address'.");
  447. $cm->validateAssocations();
  448. }
  449. }