1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
|
<?php
declare(strict_types=1);
namespace SimpleSAML\Test;
use Exception;
use PDO;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use SimpleSAML\Configuration;
use SimpleSAML\Database;
/**
* This test ensures that the \SimpleSAML\Database class can properly
* query a database.
*
* It currently uses sqlite to test, but an alternate config.php file
* should be created for test cases to ensure that it will work
* in an environment.
*
* @author Tyler Antonio, University of Alberta. <tantonio@ualberta.ca>
* @package SimpleSAMLphp
*/
class DatabaseTest extends TestCase
{
/**
* @var \SimpleSAML\Configuration
*/
protected $config;
/**
* @var \SimpleSAML\Database
*/
protected $db;
/**
* Make protected functions available for testing
*
* @param string $getMethod The method to get.
* @return mixed The method itself.
*/
protected static function getMethod($getMethod)
{
$class = new ReflectionClass(Database::class);
$method = $class->getMethod($getMethod);
$method->setAccessible(true);
return $method;
}
/**
* @covers SimpleSAML\Database::getInstance
* @covers SimpleSAML\Database::generateInstanceId
* @covers SimpleSAML\Database::__construct
* @covers SimpleSAML\Database::connect
* @return void
*/
public function setUp()
{
$config = [
'database.dsn' => 'sqlite::memory:',
'database.username' => null,
'database.password' => null,
'database.prefix' => 'phpunit_',
'database.persistent' => true,
'database.slaves' => [],
];
$this->config = new Configuration($config, "test/SimpleSAML/DatabaseTest.php");
// Ensure that we have a functional configuration class
$this->assertInstanceOf(Configuration::class, $this->config);
$this->assertEquals($config['database.dsn'], $this->config->getString('database.dsn'));
$this->db = Database::getInstance($this->config);
// Ensure that we have a functional database class.
$this->assertInstanceOf(Database::class, $this->db);
}
/**
* @covers SimpleSAML\Database::getInstance
* @covers SimpleSAML\Database::generateInstanceId
* @covers SimpleSAML\Database::__construct
* @covers SimpleSAML\Database::connect
* @test
* @return void
*/
public function connectionFailure(): void
{
$this->expectException(Exception::class);
$config = [
'database.dsn' => 'mysql:host=localhost;dbname=saml',
'database.username' => 'notauser',
'database.password' => 'notausersinvalidpassword',
'database.prefix' => 'phpunit_',
'database.persistent' => true,
'database.slaves' => [],
];
$this->config = new Configuration($config, "test/SimpleSAML/DatabaseTest.php");
Database::getInstance($this->config);
}
/**
* @covers SimpleSAML\Database::getInstance
* @covers SimpleSAML\Database::generateInstanceId
* @covers SimpleSAML\Database::__construct
* @covers SimpleSAML\Database::connect
* @test
* @return void
*/
public function instances(): void
{
$config = [
'database.dsn' => 'sqlite::memory:',
'database.username' => null,
'database.password' => null,
'database.prefix' => 'phpunit_',
'database.persistent' => true,
'database.slaves' => [],
];
$config2 = [
'database.dsn' => 'sqlite::memory:',
'database.username' => null,
'database.password' => null,
'database.prefix' => 'phpunit2_',
'database.persistent' => true,
'database.slaves' => [],
];
$config1 = new Configuration($config, "test/SimpleSAML/DatabaseTest.php");
$config2 = new Configuration($config2, "test/SimpleSAML/DatabaseTest.php");
$config3 = new Configuration($config, "test/SimpleSAML/DatabaseTest.php");
$db1 = Database::getInstance($config1);
$db2 = Database::getInstance($config2);
$db3 = Database::getInstance($config3);
$generateInstanceId = self::getMethod('generateInstanceId');
$instance1 = $generateInstanceId->invokeArgs($db1, [$config1]);
$instance2 = $generateInstanceId->invokeArgs($db2, [$config2]);
$instance3 = $generateInstanceId->invokeArgs($db3, [$config3]);
// Assert that $instance1 and $instance2 have different instance ids
$this->assertNotEquals(
$instance1,
$instance2,
"Database instances should be different, but returned the same id"
);
// Assert that $instance1 and $instance3 have identical instance ids
$this->assertEquals(
$instance1,
$instance3,
"Database instances should have the same id, but returned different id"
);
// Assert that $db1 and $db2 are different instances
$this->assertNotEquals(
spl_object_hash($db1),
spl_object_hash($db2),
"Database instances should be different, but returned the same spl_object_hash"
);
// Assert that $db1 and $db3 are identical instances
$this->assertEquals(
spl_object_hash($db1),
spl_object_hash($db3),
"Database instances should be the same, but returned different spl_object_hash"
);
}
/**
* @covers SimpleSAML\Database::getInstance
* @covers SimpleSAML\Database::generateInstanceId
* @covers SimpleSAML\Database::__construct
* @covers SimpleSAML\Database::connect
* @covers SimpleSAML\Database::getSlave
* @test
* @return void
*/
public function slaves(): void
{
$getSlave = self::getMethod('getSlave');
$master = spl_object_hash(\PHPUnit\Framework\Assert::readAttribute($this->db, 'dbMaster'));
$slave = spl_object_hash($getSlave->invokeArgs($this->db, []));
$this->assertTrue(($master == $slave), "getSlave should have returned the master database object");
$config = [
'database.dsn' => 'sqlite::memory:',
'database.username' => null,
'database.password' => null,
'database.prefix' => 'phpunit_',
'database.persistent' => true,
'database.slaves' => [
[
'dsn' => 'sqlite::memory:',
'username' => null,
'password' => null,
],
],
];
$sspConfiguration = new Configuration($config, "test/SimpleSAML/DatabaseTest.php");
$msdb = Database::getInstance($sspConfiguration);
$slaves = \PHPUnit\Framework\Assert::readAttribute($msdb, 'dbSlaves');
$gotSlave = spl_object_hash($getSlave->invokeArgs($msdb, []));
$this->assertEquals(
spl_object_hash($slaves[0]),
$gotSlave,
"getSlave should have returned a slave database object"
);
}
/**
* @covers SimpleSAML\Database::applyPrefix
* @test
* @return void
*/
public function prefix(): void
{
$prefix = $this->config->getString('database.prefix');
$table = "saml20_idp_hosted";
$pftable = $this->db->applyPrefix($table);
$this->assertEquals($prefix . $table, $pftable, "Did not properly apply the table prefix");
}
/**
* @test
*/
public function testGetDriver(): void
{
$this->assertEquals('sqlite', $this->db->getDriver());
}
/**
* @covers SimpleSAML\Database::write
* @covers SimpleSAML\Database::read
* @covers SimpleSAML\Database::exec
* @covers SimpleSAML\Database::query
* @test
* @return void
*/
public function querying(): void
{
$table = $this->db->applyPrefix("sspdbt");
$this->assertEquals($this->config->getString('database.prefix') . "sspdbt", $table);
$this->db->write(
"CREATE TABLE IF NOT EXISTS $table (ssp_key INT(16) NOT NULL, ssp_value TEXT NOT NULL)"
);
/** @var \PDOStatement $query1 */
$query1 = $this->db->read("SELECT * FROM $table");
$this->assertEquals(0, $query1->fetch(), "Table $table is not empty when it should be.");
$ssp_key = time();
$ssp_value = md5(strval(rand(0, 10000)));
$stmt = $this->db->write(
"INSERT INTO $table (ssp_key, ssp_value) VALUES (:ssp_key, :ssp_value)",
['ssp_key' => [$ssp_key, PDO::PARAM_INT], 'ssp_value' => $ssp_value]
);
$this->assertEquals(1, $stmt, "Could not insert data into $table.");
/** @var \PDOStatement $query2 */
$query2 = $this->db->read("SELECT * FROM $table WHERE ssp_key = :ssp_key", ['ssp_key' => $ssp_key]);
$data = $query2->fetch();
$this->assertEquals($data['ssp_value'], $ssp_value, "Inserted data doesn't match what is in the database");
}
/**
* @covers SimpleSAML\Database::read
* @covers SimpleSAML\Database::query
* @test
* @return void
*/
public function readFailure(): void
{
$this->expectException(Exception::class);
$table = $this->db->applyPrefix("sspdbt");
$this->assertEquals($this->config->getString('database.prefix') . "sspdbt", $table);
$this->db->read("SELECT * FROM $table");
}
/**
* @covers SimpleSAML\Database::write
* @covers SimpleSAML\Database::exec
* @test
* @return void
*/
public function noSuchTable(): void
{
$this->expectException(Exception::class);
$this->db->write("DROP TABLE phpunit_nonexistent");
}
/**
* @return void
*/
public function tearDown()
{
$table = $this->db->applyPrefix("sspdbt");
$this->db->write("DROP TABLE IF EXISTS $table");
unset($this->config);
unset($this->db);
}
}
|