ReadOnlyTest.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace Doctrine\Tests\ORM\Functional;
  3. require_once __DIR__ . '/../../TestInit.php';
  4. /**
  5. * Functional Query tests.
  6. *
  7. * @group DDC-692
  8. */
  9. class ReadOnlyTest extends \Doctrine\Tests\OrmFunctionalTestCase
  10. {
  11. protected function setUp()
  12. {
  13. parent::setUp();
  14. try {
  15. $this->_schemaTool->createSchema(array(
  16. $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\ReadOnlyEntity'),
  17. ));
  18. } catch(\Exception $e) {
  19. }
  20. }
  21. public function testReadOnlyEntityNeverChangeTracked()
  22. {
  23. $readOnly = new ReadOnlyEntity("Test1", 1234);
  24. $this->_em->persist($readOnly);
  25. $this->_em->flush();
  26. $readOnly->name = "Test2";
  27. $readOnly->numericValue = 4321;
  28. $this->_em->flush();
  29. $this->_em->clear();
  30. $dbReadOnly = $this->_em->find('Doctrine\Tests\ORM\Functional\ReadOnlyEntity', $readOnly->id);
  31. $this->assertEquals("Test1", $dbReadOnly->name);
  32. $this->assertEquals(1234, $dbReadOnly->numericValue);
  33. }
  34. /**
  35. * @group DDC-1659
  36. */
  37. public function testClearReadOnly()
  38. {
  39. $readOnly = new ReadOnlyEntity("Test1", 1234);
  40. $this->_em->persist($readOnly);
  41. $this->_em->flush();
  42. $this->_em->getUnitOfWork()->markReadOnly($readOnly);
  43. $this->_em->clear();
  44. $this->assertFalse($this->_em->getUnitOfWork()->isReadOnly($readOnly));
  45. }
  46. /**
  47. * @group DDC-1659
  48. */
  49. public function testClearEntitiesReadOnly()
  50. {
  51. $readOnly = new ReadOnlyEntity("Test1", 1234);
  52. $this->_em->persist($readOnly);
  53. $this->_em->flush();
  54. $this->_em->getUnitOfWork()->markReadOnly($readOnly);
  55. $this->_em->clear(get_class($readOnly));
  56. $this->assertFalse($this->_em->getUnitOfWork()->isReadOnly($readOnly));
  57. }
  58. }
  59. /**
  60. * @Entity(readOnly=true)
  61. */
  62. class ReadOnlyEntity
  63. {
  64. /**
  65. * @Id @GeneratedValue @Column(type="integer")
  66. * @var int
  67. */
  68. public $id;
  69. /** @column(type="string") */
  70. public $name;
  71. /** @Column(type="integer") */
  72. public $numericValue;
  73. public function __construct($name, $number)
  74. {
  75. $this->name = $name;
  76. $this->numericValue = $number;
  77. }
  78. }