-
-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #182 from slaff/feature/sqlsrv-integration-tests
Added MS SQL server that can be used to better test the integration.
- Loading branch information
Showing
14 changed files
with
268 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#!/usr/bin/env bash | ||
set -x | ||
|
||
# Install MSSQL client development libraries | ||
curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add - | ||
sudo curl https://packages.microsoft.com/config/ubuntu/18.04/prod.list -o /etc/apt/sources.list.d/mssql-release.list | ||
sudo apt-get update | ||
sudo ACCEPT_EULA=Y apt-get install -y msodbcsql17 mssql-tools unixodbc-dev | ||
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile | ||
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc | ||
|
||
export PATH="$PATH:/opt/mssql-tools/bin" | ||
|
||
# Install SqlServer PHP extensions | ||
pecl install sqlsrv | ||
pecl install pdo_sqlsrv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
"pdo-sqlite", | ||
"mysqli", | ||
"pgsql", | ||
"sqlite3" | ||
"sqlite3", | ||
"sqlsrv" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
<?php | ||
|
||
/** | ||
* @see https://github.com/laminas/laminas-db for the canonical source repository | ||
* @copyright https://github.com/laminas/laminas-db/blob/master/COPYRIGHT.md | ||
* @license https://github.com/laminas/laminas-db/blob/master/LICENSE.md New BSD License | ||
*/ | ||
|
||
namespace LaminasIntegrationTest\Db\Platform; | ||
|
||
use function sqlsrv_connect; | ||
use function sqlsrv_errors; | ||
use function sqlsrv_query; | ||
|
||
class SqlServerFixtureLoader implements FixtureLoader | ||
{ | ||
|
||
private $fixtureFilePrefix = __DIR__ . '/../TestFixtures/sqlsrv'; | ||
/** | ||
* @var resource | ||
*/ | ||
private $connection; | ||
|
||
public function createDatabase() | ||
{ | ||
$this->connect(); | ||
|
||
if (false === sqlsrv_query($this->connection, sprintf( | ||
<<<'SQL' | ||
IF NOT EXISTS(SELECT * FROM sys.databases WHERE name = '%s') | ||
BEGIN | ||
CREATE DATABASE [%s] | ||
END | ||
SQL, | ||
getenv('TESTS_LAMINAS_DB_ADAPTER_DRIVER_SQLSRV_DATABASE'), | ||
getenv('TESTS_LAMINAS_DB_ADAPTER_DRIVER_SQLSRV_DATABASE') | ||
))) { | ||
throw new \Exception(sprintf( | ||
"I cannot create the MSSQL %s database: %s", | ||
getenv('TESTS_LAMINAS_DB_ADAPTER_DRIVER_SQLSRV_DATABASE'), | ||
print_r(sqlsrv_errors(), true) | ||
)); | ||
} | ||
|
||
sqlsrv_query($this->connection, 'USE ' . getenv('TESTS_LAMINAS_DB_ADAPTER_DRIVER_SQLSRV_DATABASE')); | ||
|
||
$fixtures = [ | ||
'tables' => $this->fixtureFilePrefix.'.sql', | ||
'views' => $this->fixtureFilePrefix.'-views.sql', | ||
'triggers' => $this->fixtureFilePrefix.'-triggers.sql', | ||
]; | ||
|
||
foreach ($fixtures as $name => $fixtureFile) { | ||
if (false === sqlsrv_query($this->connection, file_get_contents($fixtureFile))) { | ||
throw new \Exception(sprintf( | ||
"I cannot create the %s for %s database. Check the %s file. %s ", | ||
$name, | ||
getenv('TESTS_LAMINAS_DB_ADAPTER_DRIVER_SQLSRV_DATABASE'), | ||
$fixtureFile, | ||
print_r(sqlsrv_errors(), true) | ||
)); | ||
} | ||
} | ||
|
||
$this->disconnect(); | ||
} | ||
|
||
public function dropDatabase() | ||
{ | ||
$this->connect(); | ||
|
||
sqlsrv_query($this->connection, "USE master"); | ||
sqlsrv_query($this->connection, sprintf( | ||
"ALTER DATABASE %s SET SINGLE_USER WITH ROLLBACK IMMEDIATE", | ||
getenv('TESTS_LAMINAS_DB_ADAPTER_DRIVER_SQLSRV_DATABASE') | ||
)); | ||
|
||
if (false == sqlsrv_query($this->connection, sprintf( | ||
"DROP DATABASE %s", | ||
getenv('TESTS_LAMINAS_DB_ADAPTER_DRIVER_SQLSRV_DATABASE') | ||
))) { | ||
throw new \Exception(sprintf( | ||
"Unable to drop database %s. %s", | ||
getenv('TESTS_LAMINAS_DB_ADAPTER_DRIVER_SQLSRV_DATABASE'), | ||
print_r(sqlsrv_errors(), true) | ||
)); | ||
} | ||
|
||
$this->disconnect(); | ||
} | ||
|
||
protected function connect() | ||
{ | ||
$this->connection = sqlsrv_connect( | ||
getenv('TESTS_LAMINAS_DB_ADAPTER_DRIVER_SQLSRV_HOSTNAME'), | ||
[ | ||
'UID' => getenv('TESTS_LAMINAS_DB_ADAPTER_DRIVER_SQLSRV_USERNAME'), | ||
'PWD' => getenv('TESTS_LAMINAS_DB_ADAPTER_DRIVER_SQLSRV_PASSWORD'), | ||
] | ||
); | ||
|
||
if (false === $this->connection) { | ||
throw new \Exception(sprintf( | ||
"Unable to connect %s. %s", | ||
getenv('TESTS_LAMINAS_DB_ADAPTER_DRIVER_SQLSRV_DATABASE'), | ||
print_r(sqlsrv_errors(), true) | ||
)); | ||
} | ||
} | ||
|
||
protected function disconnect() | ||
{ | ||
$this->connection = null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
CREATE TRIGGER after_test_update ON test | ||
AFTER UPDATE | ||
AS | ||
BEGIN | ||
INSERT INTO test_audit_trail(test_id, test_value_old, test_value_new, changed) | ||
SELECT | ||
id, | ||
value, | ||
inserted.value, | ||
GETDATE() | ||
FROM inserted | ||
END; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
CREATE VIEW test_view | ||
AS ( | ||
SELECT | ||
name AS v_name, | ||
value AS v_value | ||
FROM | ||
test | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
CREATE TABLE test ( | ||
id INT NOT NULL IDENTITY, | ||
name VARCHAR(255) NOT NULL, | ||
value VARCHAR(255) NOT NULL, | ||
PRIMARY KEY (id) | ||
); | ||
|
||
INSERT INTO test (name, value) VALUES | ||
('foo', 'bar'), | ||
('bar', 'baz'), | ||
('123a', 'bar'), | ||
('123', 'bar'); | ||
|
||
CREATE TABLE test_charset ( | ||
id INT NOT NULL IDENTITY, | ||
field$ VARCHAR(255) NOT NULL, | ||
field_ VARCHAR(255) NOT NULL, | ||
PRIMARY KEY (id) | ||
); | ||
|
||
INSERT INTO test_charset (field$, field_) VALUES | ||
('foo', 'bar'), | ||
('bar', 'baz'); | ||
|
||
CREATE TABLE test_audit_trail ( | ||
id INT NOT NULL IDENTITY, | ||
test_id INT NOT NULL, | ||
test_value_old VARCHAR(255) NOT NULL, | ||
test_value_new VARCHAR(255) NOT NULL, | ||
changed DATETIME2(0), | ||
PRIMARY KEY (id) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters