MongoDocumentWrapperTest.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace Wrapper;
  3. use Tool\BaseTestCaseMongoODM;
  4. use Doctrine\Common\EventManager;
  5. use Wrapper\Fixture\Document\Article;
  6. use Gedmo\Tool\Wrapper\MongoDocumentWrapper;
  7. /**
  8. * Mongo Document 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 MongoDocumentWrapperTest extends BaseTestCaseMongoODM
  15. {
  16. const ARTICLE = "Wrapper\\Fixture\\Document\\Article";
  17. private $articleId;
  18. protected function setUp()
  19. {
  20. parent::setUp();
  21. $this->getMockDocumentManager(new EventManager);
  22. $this->populate();
  23. }
  24. public function testManaged()
  25. {
  26. $test = $this->dm->find(self::ARTICLE, $this->articleId);
  27. $this->assertInstanceOf(self::ARTICLE, $test);
  28. $wrapped = new MongoDocumentWrapper($test, $this->dm);
  29. $this->assertEquals($this->articleId, $wrapped->getIdentifier());
  30. $this->assertEquals('test', $wrapped->getPropertyValue('title'));
  31. $wrapped->setPropertyValue('title', 'changed');
  32. $this->assertEquals('changed', $wrapped->getPropertyValue('title'));
  33. $this->assertTrue($wrapped->hasValidIdentifier());
  34. }
  35. public function testProxy()
  36. {
  37. $this->dm->clear();
  38. $test = $this->dm->getReference(self::ARTICLE, $this->articleId);
  39. $this->assertInstanceOf('Doctrine\\ODM\\MongoDB\\Proxy\\Proxy', $test);
  40. $wrapped = new MongoDocumentWrapper($test, $this->dm);
  41. $id = $wrapped->getIdentifier(false);
  42. $this->assertEquals($this->articleId, $id);
  43. $this->assertEquals('test', $wrapped->getPropertyValue('title'));
  44. }
  45. public function testDetachedEntity()
  46. {
  47. $test = $this->dm->find(self::ARTICLE, $this->articleId);
  48. $this->dm->clear();
  49. $wrapped = new MongoDocumentWrapper($test, $this->dm);
  50. $this->assertEquals($this->articleId, $wrapped->getIdentifier());
  51. $this->assertEquals('test', $wrapped->getPropertyValue('title'));
  52. }
  53. public function testDetachedProxy()
  54. {
  55. $test = $this->dm->getReference(self::ARTICLE, $this->articleId);
  56. $this->dm->clear();
  57. $wrapped = new MongoDocumentWrapper($test, $this->dm);
  58. $this->assertEquals($this->articleId, $wrapped->getIdentifier());
  59. $this->assertEquals('test', $wrapped->getPropertyValue('title'));
  60. }
  61. public function testSomeFunctions()
  62. {
  63. $test = new Article;
  64. $wrapped = new MongoDocumentWrapper($test, $this->dm);
  65. $wrapped->populate(array('title' => 'test'));
  66. $this->assertEquals('test', $wrapped->getPropertyValue('title'));
  67. $this->assertFalse($wrapped->hasValidIdentifier());
  68. }
  69. private function populate()
  70. {
  71. $test = new Article;
  72. $test->setTitle("test");
  73. $this->dm->persist($test);
  74. $this->dm->flush();
  75. $this->articleId = $test->getId();
  76. }
  77. }