ReadOnlyTest.php 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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->number = 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->number);
  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. /**
  48. * @Entity(readOnly=true)
  49. */
  50. class ReadOnlyEntity
  51. {
  52. /**
  53. * @Id @GeneratedValue @Column(type="integer")
  54. * @var int
  55. */
  56. public $id;
  57. /** @column(type="string") */
  58. public $name;
  59. /** @Column(type="integer", name="number_col") */
  60. public $number;
  61. public function __construct($name, $number)
  62. {
  63. $this->name = $name;
  64. $this->number = $number;
  65. }
  66. }