|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Doctrine\Tests\ORM\Functional; |
| 4 | + |
| 5 | +use Doctrine\DBAL\Schema\Sequence; |
| 6 | + |
| 7 | +require_once __DIR__ . '/../../TestInit.php'; |
| 8 | + |
| 9 | +class SequenceEmulatedIdentityStrategyTest extends \Doctrine\Tests\OrmFunctionalTestCase |
| 10 | +{ |
| 11 | + /** |
| 12 | + * {@inheritdoc} |
| 13 | + */ |
| 14 | + protected function setUp() |
| 15 | + { |
| 16 | + parent::setUp(); |
| 17 | + |
| 18 | + if ( ! $this->_em->getConnection()->getDatabasePlatform()->usesSequenceEmulatedIdentityColumns()) { |
| 19 | + $this->markTestSkipped( |
| 20 | + 'This test is special to platforms emulating IDENTITY key generation strategy through sequences.' |
| 21 | + ); |
| 22 | + } else { |
| 23 | + try { |
| 24 | + $this->_schemaTool->createSchema( |
| 25 | + array($this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\SequenceEmulatedIdentityEntity')) |
| 26 | + ); |
| 27 | + } catch (\Exception $e) { |
| 28 | + // Swallow all exceptions. We do not test the schema tool here. |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * {@inheritdoc} |
| 35 | + */ |
| 36 | + protected function tearDown() |
| 37 | + { |
| 38 | + parent::tearDown(); |
| 39 | + |
| 40 | + $connection = $this->_em->getConnection(); |
| 41 | + $platform = $connection->getDatabasePlatform(); |
| 42 | + |
| 43 | + // drop sequence manually due to dependency |
| 44 | + $connection->exec( |
| 45 | + $platform->getDropSequenceSQL( |
| 46 | + new Sequence($platform->getIdentitySequenceName('seq_identity', 'id')) |
| 47 | + ) |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + public function testPreSavePostSaveCallbacksAreInvoked() |
| 52 | + { |
| 53 | + $entity = new SequenceEmulatedIdentityEntity(); |
| 54 | + $entity->setValue('hello'); |
| 55 | + $this->_em->persist($entity); |
| 56 | + $this->_em->flush(); |
| 57 | + $this->assertTrue(is_numeric($entity->getId())); |
| 58 | + $this->assertTrue($entity->getId() > 0); |
| 59 | + $this->assertTrue($this->_em->contains($entity)); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +/** @Entity @Table(name="seq_identity") */ |
| 64 | +class SequenceEmulatedIdentityEntity |
| 65 | +{ |
| 66 | + /** @Id @Column(type="integer") @GeneratedValue(strategy="IDENTITY") */ |
| 67 | + private $id; |
| 68 | + |
| 69 | + /** @Column(type="string") */ |
| 70 | + private $value; |
| 71 | + |
| 72 | + public function getId() |
| 73 | + { |
| 74 | + return $this->id; |
| 75 | + } |
| 76 | + |
| 77 | + public function getValue() |
| 78 | + { |
| 79 | + return $this->value; |
| 80 | + } |
| 81 | + |
| 82 | + public function setValue($value) |
| 83 | + { |
| 84 | + $this->value = $value; |
| 85 | + } |
| 86 | +} |
0 commit comments