EntityWrapperTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace Wrapper;
  3. use Tool\BaseTestCaseORM;
  4. use Doctrine\Common\EventManager;
  5. use Wrapper\Fixture\Entity\Article;
  6. use Gedmo\Tool\Wrapper\EntityWrapper;
  7. /**
  8. * Entity wrapper tests
  9. *
  10. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  11. * @link http://www.gediminasm.org
  12. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  13. */
  14. class EntityWrapperTest extends BaseTestCaseORM
  15. {
  16. const ARTICLE = "Wrapper\\Fixture\\Entity\\Article";
  17. protected function setUp()
  18. {
  19. parent::setUp();
  20. $this->getMockSqliteEntityManager(new EventManager);
  21. $this->populate();
  22. }
  23. public function testManaged()
  24. {
  25. $test = $this->em->find(self::ARTICLE, array('id' => 1));
  26. $this->assertInstanceOf(self::ARTICLE, $test);
  27. $wrapped = new EntityWrapper($test, $this->em);
  28. $this->assertEquals(1, $wrapped->getIdentifier());
  29. $this->assertEquals('test', $wrapped->getPropertyValue('title'));
  30. $wrapped->setPropertyValue('title', 'changed');
  31. $this->assertEquals('changed', $wrapped->getPropertyValue('title'));
  32. $this->assertTrue($wrapped->hasValidIdentifier());
  33. }
  34. public function testProxy()
  35. {
  36. $this->em->clear();
  37. $test = $this->em->getReference(self::ARTICLE, array('id' => 1));
  38. $this->assertInstanceOf('Doctrine\\ORM\\Proxy\\Proxy', $test);
  39. $wrapped = new EntityWrapper($test, $this->em);
  40. $id = $wrapped->getIdentifier(false);
  41. $this->assertTrue(is_array($id));
  42. $this->assertCount(1, $id);
  43. $this->assertArrayHasKey('id', $id);
  44. $this->assertEquals(1, $id['id']);
  45. $this->assertEquals('test', $wrapped->getPropertyValue('title'));
  46. }
  47. public function testDetachedEntity()
  48. {
  49. $test = $this->em->find(self::ARTICLE, array('id' => 1));
  50. $this->em->clear();
  51. $wrapped = new EntityWrapper($test, $this->em);
  52. $this->assertEquals(1, $wrapped->getIdentifier());
  53. $this->assertEquals('test', $wrapped->getPropertyValue('title'));
  54. }
  55. public function testDetachedProxy()
  56. {
  57. $test = $this->em->getReference(self::ARTICLE, array('id' => 1));
  58. $this->em->clear();
  59. $wrapped = new EntityWrapper($test, $this->em);
  60. $this->assertEquals(1, $wrapped->getIdentifier());
  61. $this->assertEquals('test', $wrapped->getPropertyValue('title'));
  62. }
  63. public function testSomeFunctions()
  64. {
  65. $test = new Article;
  66. $wrapped = new EntityWrapper($test, $this->em);
  67. $wrapped->populate(array('title' => 'test'));
  68. $this->assertEquals('test', $wrapped->getPropertyValue('title'));
  69. $this->assertFalse($wrapped->hasValidIdentifier());
  70. }
  71. protected function getUsedEntityFixtures()
  72. {
  73. return array(
  74. self::ARTICLE
  75. );
  76. }
  77. private function populate()
  78. {
  79. $test = new Article;
  80. $test->setTitle("test");
  81. $this->em->persist($test);
  82. $this->em->flush();
  83. }
  84. }