diff --git a/library/Zend/Db/Adapter/Driver/Sqlsrv/Sqlsrv.php b/library/Zend/Db/Adapter/Driver/Sqlsrv/Sqlsrv.php index fe6cef695bb..1b041e4cdeb 100644 --- a/library/Zend/Db/Adapter/Driver/Sqlsrv/Sqlsrv.php +++ b/library/Zend/Db/Adapter/Driver/Sqlsrv/Sqlsrv.php @@ -140,7 +140,9 @@ public function createStatement($sqlOrResource = null) } $statement->initialize($this->connection->getResource()); } elseif (is_resource($sqlOrResource)) { - $statement->setResource($sqlOrResource); + $statement->initialize($sqlOrResource); // will check the resource type + } else { + throw new Exception\InvalidArgumentException('createStatement() only accepts an SQL string or a Sqlsrv resource'); } return $statement; } diff --git a/library/Zend/Db/Adapter/Driver/Sqlsrv/Statement.php b/library/Zend/Db/Adapter/Driver/Sqlsrv/Statement.php index d8c11afb7f4..2607337f9a9 100644 --- a/library/Zend/Db/Adapter/Driver/Sqlsrv/Statement.php +++ b/library/Zend/Db/Adapter/Driver/Sqlsrv/Statement.php @@ -89,11 +89,16 @@ public function setDriver(Sqlsrv $driver) public function initialize($resource) { $resourceType = get_resource_type($resource); - if ($resourceType != 'SQL Server Connection' && $resourceType != 'SQL Server Statement') { + + if ($resourceType == 'SQL Server Connection') { + $this->sqlsrv = $resource; + } elseif ($resourceType == 'SQL Server Statement') { + $this->resource = $resource; + $this->isPrepared = true; + } else { throw new Exception\InvalidArgumentException('Invalid resource provided to ' . __CLASS__); } - $this->sqlsrv = $resource; return $this; } diff --git a/tests/ZendTest/Db/Adapter/Driver/Sqlsrv/AbstractIntegrationTest.php b/tests/ZendTest/Db/Adapter/Driver/Sqlsrv/AbstractIntegrationTest.php new file mode 100755 index 00000000000..0cd2707acb7 --- /dev/null +++ b/tests/ZendTest/Db/Adapter/Driver/Sqlsrv/AbstractIntegrationTest.php @@ -0,0 +1,30 @@ + 'ZEND_DB_ADAPTER_DRIVER_SQLSRV_HOSTNAME', + 'username' => 'ZEND_DB_ADAPTER_DRIVER_SQLSRV_USERNAME', + 'password' => 'ZEND_DB_ADAPTER_DRIVER_SQLSRV_PASSWORD', + ); + + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + */ + protected function setUp() + { + foreach ($this->variables as $name => $value) { + if (!isset($GLOBALS[$value])) { + $this->fail('Missing required variable ' . $value . ' from phpunit.xml for this integration test'); + } + $this->variables[$name] = $GLOBALS[$value]; + } + + if (!extension_loaded('sqlsrv')) { + $this->fail('The phpunit group integration-sqlsrv was enabled, but the extension is not loaded.'); + } + } +} \ No newline at end of file diff --git a/tests/ZendTest/Db/Adapter/Driver/Sqlsrv/ConnectionIntegrationTest.php b/tests/ZendTest/Db/Adapter/Driver/Sqlsrv/ConnectionIntegrationTest.php old mode 100644 new mode 100755 index 6b665396362..90e5000b58f --- a/tests/ZendTest/Db/Adapter/Driver/Sqlsrv/ConnectionIntegrationTest.php +++ b/tests/ZendTest/Db/Adapter/Driver/Sqlsrv/ConnectionIntegrationTest.php @@ -8,32 +8,8 @@ * @group integration * @group integration-sqlserver */ -class ConnectionIntegrationTest extends \PHPUnit_Framework_TestCase +class ConnectionIntegrationTest extends AbstractIntegrationTest { - protected $variables = array( - 'hostname' => 'ZEND_DB_ADAPTER_DRIVER_SQLSRV_HOSTNAME', - 'username' => 'ZEND_DB_ADAPTER_DRIVER_SQLSRV_USERNAME', - 'password' => 'ZEND_DB_ADAPTER_DRIVER_SQLSRV_PASSWORD', - ); - - /** - * Sets up the fixture, for example, opens a network connection. - * This method is called before a test is executed. - */ - protected function setUp() - { - foreach ($this->variables as $name => $value) { - if (!isset($GLOBALS[$value])) { - $this->fail('Missing required variable ' . $value . ' from phpunit.xml for this integration test'); - } - $this->variables[$name] = $GLOBALS[$value]; - } - - if (!extension_loaded('sqlsrv')) { - $this->fail('The phpunit group integration-sqlsrv was enabled, but the extension is not loaded.'); - } - } - /** * @covers Zend\Db\Adapter\Driver\Sqlsrv\Connection::getCurrentSchema */ diff --git a/tests/ZendTest/Db/Adapter/Driver/Sqlsrv/SqlSrvIntegrationTest.php b/tests/ZendTest/Db/Adapter/Driver/Sqlsrv/SqlSrvIntegrationTest.php old mode 100644 new mode 100755 index 79e80fb52cd..ce087597a6d --- a/tests/ZendTest/Db/Adapter/Driver/Sqlsrv/SqlSrvIntegrationTest.php +++ b/tests/ZendTest/Db/Adapter/Driver/Sqlsrv/SqlSrvIntegrationTest.php @@ -7,20 +7,8 @@ * @group integration * @group integration-sqlsrv */ -class SqlsrvIntegrationTest extends \PHPUnit_Framework_TestCase +class SqlsrvIntegrationTest extends AbstractIntegrationTest { - - /** - * Sets up the fixture, for example, opens a network connection. - * This method is called before a test is executed. - */ - protected function setUp() - { - if (!extension_loaded('sqlsrv')) { - $this->fail('The phpunit group integration-sqlsrv was enabled, but the extension is not loaded.'); - } - } - /** * @group integration-sqlserver * @covers Zend\Db\Adapter\Driver\Sqlsrv\Sqlsrv::checkEnvironment @@ -31,4 +19,26 @@ public function testCheckEnvironment() $this->assertNull($sqlserver->checkEnvironment()); } + public function testCreateStatement() + { + $driver = new Sqlsrv(array()); + + $resource = sqlsrv_connect( + $this->variables['hostname'], array( + 'UID' => $this->variables['username'], + 'PWD' => $this->variables['password'] + ) + ); + + $driver->getConnection()->setResource($resource); + + $stmt = $driver->createStatement('SELECT 1'); + $this->assertInstanceOf('Zend\Db\Adapter\Driver\Sqlsrv\Statement', $stmt); + $stmt = $driver->createStatement($resource); + $this->assertInstanceOf('Zend\Db\Adapter\Driver\Sqlsrv\Statement', $stmt); + + $this->setExpectedException('Zend\Db\Adapter\Exception\InvalidArgumentException', 'only accepts an SQL string or a Sqlsrv resource'); + $driver->createStatement(new \stdClass); + } + }