PostgreSQLIdentityStrategyTest.php 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace Doctrine\Tests\ORM\Functional;
  3. use Doctrine\ORM\Event\PreUpdateEventArgs;
  4. require_once __DIR__ . '/../../TestInit.php';
  5. class PostgreSQLIdentityStrategyTest extends \Doctrine\Tests\OrmFunctionalTestCase
  6. {
  7. protected function setUp() {
  8. parent::setUp();
  9. if ($this->_em->getConnection()->getDatabasePlatform()->getName() != 'postgresql') {
  10. $this->markTestSkipped('This test is special to the PostgreSQL IDENTITY key generation strategy.');
  11. } else {
  12. try {
  13. $this->_schemaTool->createSchema(array(
  14. $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\PostgreSQLIdentityEntity'),
  15. ));
  16. } catch (\Exception $e) {
  17. // Swallow all exceptions. We do not test the schema tool here.
  18. }
  19. }
  20. }
  21. protected function tearDown() {
  22. parent::tearDown();
  23. // drop sequence manually due to dependency
  24. $this->_em->getConnection()->exec('DROP SEQUENCE postgresqlidentityentity_id_seq CASCADE');
  25. }
  26. public function testPreSavePostSaveCallbacksAreInvoked()
  27. {
  28. $entity = new PostgreSQLIdentityEntity();
  29. $entity->setValue('hello');
  30. $this->_em->persist($entity);
  31. $this->_em->flush();
  32. $this->assertTrue(is_numeric($entity->getId()));
  33. $this->assertTrue($entity->getId() > 0);
  34. $this->assertTrue($this->_em->contains($entity));
  35. }
  36. }
  37. /** @Entity */
  38. class PostgreSQLIdentityEntity {
  39. /** @Id @Column(type="integer") @GeneratedValue(strategy="IDENTITY") */
  40. private $id;
  41. /** @Column(type="string") */
  42. private $value;
  43. public function getId() {return $this->id;}
  44. public function getValue() {return $this->value;}
  45. public function setValue($value) {$this->value = $value;}
  46. }