Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Jan 8, 2020. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion library/Zend/Db/Adapter/Driver/Sqlsrv/Sqlsrv.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
9 changes: 7 additions & 2 deletions library/Zend/Db/Adapter/Driver/Sqlsrv/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace ZendTest\Db\Adapter\Driver\Sqlsrv;

abstract class AbstractIntegrationTest extends \PHPUnit_Framework_TestCase
{
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.');
}
}
}
26 changes: 1 addition & 25 deletions tests/ZendTest/Db/Adapter/Driver/Sqlsrv/ConnectionIntegrationTest.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
36 changes: 23 additions & 13 deletions tests/ZendTest/Db/Adapter/Driver/Sqlsrv/SqlSrvIntegrationTest.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}

}