diff --git a/src/TableGateway/Feature/SequenceFeature.php b/src/TableGateway/Feature/SequenceFeature.php index 3c72fd3b6f..bb940bf6f8 100644 --- a/src/TableGateway/Feature/SequenceFeature.php +++ b/src/TableGateway/Feature/SequenceFeature.php @@ -21,7 +21,7 @@ class SequenceFeature extends AbstractFeature protected $primaryKeyField; /** - * @var string + * @var string|array */ protected $sequenceName; @@ -89,7 +89,7 @@ public function nextSequenceId() $sql = 'SELECT ' . $platform->quoteIdentifier($this->sequenceName) . '.NEXTVAL as "nextval" FROM dual'; break; case 'PostgreSQL': - $sql = 'SELECT NEXTVAL(\'"' . $this->sequenceName . '"\')'; + $sql = 'SELECT NEXTVAL(\'' . $platform->quoteIdentifierChain($this->sequenceName) . '\')'; break; default : return; @@ -117,7 +117,7 @@ public function lastSequenceId() $sql = 'SELECT ' . $platform->quoteIdentifier($this->sequenceName) . '.CURRVAL as "currval" FROM dual'; break; case 'PostgreSQL': - $sql = 'SELECT CURRVAL(\'' . $this->sequenceName . '\')'; + $sql = 'SELECT CURRVAL(\'' . $platform->quoteIdentifierChain($this->sequenceName) . '\')'; break; default : return; diff --git a/test/TableGateway/Feature/FeatureSetTest.php b/test/TableGateway/Feature/FeatureSetTest.php index bfc6271c17..cb19237a9d 100644 --- a/test/TableGateway/Feature/FeatureSetTest.php +++ b/test/TableGateway/Feature/FeatureSetTest.php @@ -15,6 +15,7 @@ use Zend\Db\TableGateway\Feature\SequenceFeature; use Zend\Db\TableGateway\Feature\MetadataFeature; use Zend\Db\Metadata\Object\ConstraintObject; +use ZendTest\Db\TestAsset\TrustingPostgresqlPlatform; class FeatureSetTest extends \PHPUnit_Framework_TestCase { @@ -126,11 +127,9 @@ public function testCanCallMagicCallReturnsFalseWhenNoFeaturesHaveBeenAdded() */ public function testCallMagicCallSucceedsForValidMethodOfAddedFeature() { - $sequenceName = 'table_sequence'; + $sequenceName = '"schema"."table_sequence"'; - $platformMock = $this->getMock('Zend\Db\Adapter\Platform\Postgresql'); - $platformMock->expects($this->any()) - ->method('getName')->will($this->returnValue('PostgreSQL')); + $platformStub = new TrustingPostgresqlPlatform(); $resultMock = $this->getMock('Zend\Db\Adapter\Driver\Pgsql\Result'); $resultMock->expects($this->any()) @@ -149,7 +148,7 @@ public function testCallMagicCallSucceedsForValidMethodOfAddedFeature() ->disableOriginalConstructor() ->getMock(); $adapterMock->expects($this->any()) - ->method('getPlatform')->will($this->returnValue($platformMock)); + ->method('getPlatform')->will($this->returnValue($platformStub)); $adapterMock->expects($this->any()) ->method('createStatement')->will($this->returnValue($statementMock)); @@ -162,7 +161,7 @@ public function testCallMagicCallSucceedsForValidMethodOfAddedFeature() $reflectionProperty->setAccessible(true); $reflectionProperty->setValue($tableGatewayMock, $adapterMock); - $feature = new SequenceFeature('id', 'table_sequence'); + $feature = new SequenceFeature('id', ['schema', 'table_sequence']); $feature->setTableGateway($tableGatewayMock); $featureSet = new FeatureSet; $featureSet->addFeature($feature); diff --git a/test/TableGateway/Feature/SequenceFeatureTest.php b/test/TableGateway/Feature/SequenceFeatureTest.php index b3edc59c33..80f501b279 100644 --- a/test/TableGateway/Feature/SequenceFeatureTest.php +++ b/test/TableGateway/Feature/SequenceFeatureTest.php @@ -10,39 +10,26 @@ namespace ZendTest\Db\TableGateway\Feature; use PHPUnit_Framework_TestCase; +use Zend\Db\Adapter\Platform\PlatformInterface; +use ZendTest\Db\TestAsset; use Zend\Db\TableGateway\Feature\SequenceFeature; class SequenceFeatureTest extends PHPUnit_Framework_TestCase { - /** @var SequenceFeature */ - protected $feature = null; - /** @var \Zend\Db\TableGateway\TableGateway */ protected $tableGateway = null; /** @var string primary key name */ protected $primaryKeyField = 'id'; - /** @var string sequence name */ - protected $sequenceName = 'table_sequence'; - - public function setup() - { - $this->feature = new SequenceFeature($this->primaryKeyField, $this->sequenceName); - } - /** * @dataProvider nextSequenceIdProvider */ - public function testNextSequenceId($platformName, $statementSql) + public function testNextSequenceId($platformName, $sequenceName, $statementSql) { - $platform = $this->getMockForAbstractClass('Zend\Db\Adapter\Platform\PlatformInterface', ['getName']); - $platform->expects($this->any()) - ->method('getName') - ->will($this->returnValue($platformName)); - $platform->expects($this->any()) - ->method('quoteIdentifier') - ->will($this->returnValue($this->sequenceName)); + $feature = new SequenceFeature($this->primaryKeyField, $sequenceName); + + $platform = $this->getPlatformStub($platformName); $adapter = $this->getMock('Zend\Db\Adapter\Adapter', ['getPlatform', 'createStatement'], [], '', false); $adapter->expects($this->any()) ->method('getPlatform') @@ -62,13 +49,36 @@ public function testNextSequenceId($platformName, $statementSql) ->method('createStatement') ->will($this->returnValue($statement)); $this->tableGateway = $this->getMockForAbstractClass('Zend\Db\TableGateway\TableGateway', ['table', $adapter], '', true); - $this->feature->setTableGateway($this->tableGateway); - $this->feature->nextSequenceId(); + $feature->setTableGateway($this->tableGateway); + $feature->nextSequenceId(); } public function nextSequenceIdProvider() { - return [['PostgreSQL', 'SELECT NEXTVAL(\'"' . $this->sequenceName . '"\')'], - ['Oracle', 'SELECT ' . $this->sequenceName . '.NEXTVAL as "nextval" FROM dual']]; + return [ + //@TODO MS SQL SERVER 2016 now supports sequences too + ['PostgreSQL', 'table_sequence', 'SELECT NEXTVAL(\'"table_sequence"\')'], + ['PostgreSQL', ['schema','table_sequence'], 'SELECT NEXTVAL(\'"schema"."table_sequence"\')'], + ['Oracle', 'table_sequence', 'SELECT "table_sequence".NEXTVAL as "nextval" FROM dual'] + ]; + } + + /** + * Data provider + * @TODO this method is replicated in a several tests. Seems common enough to put in common utility, trait or abstract test class + * + * @param string $platform + * + * @return PlatformInterface + */ + protected function getPlatformStub($platform) + { + switch ($platform) { + case 'Oracle' : $platform = new TestAsset\TrustingOraclePlatform(); break; + case 'PostgreSQL' : $platform = new TestAsset\TrustingPostgresqlPlatform(); break; + default : $platform = null; + } + + return $platform; } } diff --git a/test/TestAsset/TrustingPostgresqlPlatform.php b/test/TestAsset/TrustingPostgresqlPlatform.php new file mode 100644 index 0000000000..a5c2ddf396 --- /dev/null +++ b/test/TestAsset/TrustingPostgresqlPlatform.php @@ -0,0 +1,20 @@ +quoteTrustedValue($value); + } +}