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
|
<?php
declare(strict_types=1);
namespace SimpleSAML\Test\Module\saml\IdP;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use SimpleSAML\Configuration;
use SimpleSAML\Error;
use SimpleSAML\Module\saml\IdP\SQLNameID;
use SimpleSAML\Store;
/**
* Test for the SQLNameID helper class.
*
* @author Pavel Brousek <brousek@ics.muni.cz>
* @package SimpleSAMLphp
*/
class SQLNameIDTest extends TestCase
{
/**
* @param array $config
* @return void
*/
private function addGetDelete(array $config = []): void
{
SQLNameID::add('idp', 'sp', 'user', 'value', $config);
$this->assertEquals('value', SQLNameID::get('idp', 'sp', 'user', $config));
SQLNameID::delete('idp', 'sp', 'user', $config);
$this->assertNull(SQLNameID::get('idp', 'sp', 'user', $config));
}
/**
* Test Store.
* @test
* @return void
*/
public function testSQLStore(): void
{
Configuration::loadFromArray([
'store.type' => 'sql',
'store.sql.dsn' => 'sqlite::memory:',
'store.sql.prefix' => 'phpunit_',
], '[ARRAY]', 'simplesaml');
$this->addGetDelete();
$config = Configuration::getInstance();
/** @var \SimpleSAML\Store $store */
$store = Store::getInstance();
$this->clearInstance($config, Configuration::class);
$this->clearInstance($store, Store::class);
}
/**
* Test incompatible Store.
* @test
* @return void
*/
public function testIncompatibleStore(): void
{
Configuration::loadFromArray([
'store.type' => 'memcache',
], '[ARRAY]', 'simplesaml');
$store = Store::getInstance();
$this->assertInstanceOf(Store\Memcache::class, $store);
$this->expectException(Error\Exception::class);
$this->addGetDelete();
$config = Configuration::getInstance();
/** @var \SimpleSAML\Store $store */
$store = Store::getInstance();
$this->clearInstance($config, Configuration::class);
$this->clearInstance($store, Store::class);
}
/**
* Test Database.
* @test
* @return void
*/
public function testDatabase(): void
{
$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,
],
],
];
$this->addGetDelete($config);
}
/**
* @param \SimpleSAML\Configuration|\SimpleSAML\Store $service
* @param class-string $className
* @return void
*/
protected function clearInstance($service, string $className): void
{
$reflectedClass = new ReflectionClass($className);
$reflectedInstance = $reflectedClass->getProperty('instance');
$reflectedInstance->setAccessible(true);
$reflectedInstance->setValue($service, null);
$reflectedInstance->setAccessible(false);
}
}
|