Skip to content

Instantly share code, notes, and snippets.

@adamgoucher
Last active December 12, 2015 05:38
Show Gist options
  • Save adamgoucher/4723168 to your computer and use it in GitHub Desktop.
Save adamgoucher/4723168 to your computer and use it in GitHub Desktop.
cross-platform-ability is a pain in the butt. so saunterphp provides an abstract class that individual saunter projects much subclass (its abstract, duh), which then gets used in their scripts.
class ClientSQLProvider extends SQLServerProvider {
function get_invite_token($email) {
$sql = "select * from users where email = '$email'";
$result = $this->connection->query($sql);
return $result->fetch();
}
}
class ProviderTest extends PHPUnit_Framework_TestCase {
/*
* @test
*/
public function test_creation() {
// this would normally be read from an external config
$os = strtolower(PHP_OS);
if (substr($os, 0, 3) == 'win') {
$server_or_source = 'dev.client.com';
} elseif ($os === 'darwin') {
$server_or_source = 'dev';
}
$p = new ClientSQLProvider($server_or_source, 'username', 'password');
$token = $p->get_invite_token('adam@element34.ca');
$this->assertEquals($token['CreationDate'], '2013-01-16 16:09:02.670');
}
}
abstract class SQLServerProvider {
function __construct($server_or_source, $username, $password) {
$os = strtolower(PHP_OS);
if (substr($os, 0, 3) == 'win') {
// http://msdn.microsoft.com/en-us/library/cc296182(v=sql.90).aspx
$this->connection = new PDO("sqlsrv:server=$server_or_source", $username, $password);
} elseif ($os === 'darwin') {
// http://www.php.net/manual/en/ref.pdo-odbc.php
$this->connection = new PDO("odbc:$server_or_source", $username, $password);
} else {
throw new Exception("{$PHP_OS} is not [yet] supported. patches welcome :)");
}
}
function __destruct() {
$this->connection = null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment