|
| 1 | +<?php |
| 2 | + |
| 3 | +use PHPUnit\Framework\TestCase; |
| 4 | + |
| 5 | +/** |
| 6 | + * Test for the SQLPermanentStorage class. |
| 7 | + */ |
| 8 | +class Test_Core_Storage_SQLPermanentStorage extends TestCase |
| 9 | +{ |
| 10 | + private static $sql; |
| 11 | + |
| 12 | + public static function setUpBeforeClass() |
| 13 | + { |
| 14 | + // Create instance |
| 15 | + $config = \SimpleSAML_Configuration::loadFromArray([ |
| 16 | + 'datadir' => sys_get_temp_dir(), |
| 17 | + ]); |
| 18 | + self::$sql = new sspmod_core_Storage_SQLPermanentStorage('test', $config); |
| 19 | + } |
| 20 | + |
| 21 | + public static function tearDownAfterClass() |
| 22 | + { |
| 23 | + self::$sql = null; |
| 24 | + unlink(sys_get_temp_dir().'/sqllite/test.sqlite'); |
| 25 | + } |
| 26 | + |
| 27 | + public function testSet() |
| 28 | + { |
| 29 | + // Set a new value |
| 30 | + self::$sql->set('testtype', 'testkey1', 'testkey2', 'testvalue', 0); |
| 31 | + |
| 32 | + // Test getCondition |
| 33 | + $result = self::$sql->get(); |
| 34 | + $this->assertEquals('testvalue', $result['value']); |
| 35 | + } |
| 36 | + |
| 37 | + public function testSetOverwrite() |
| 38 | + { |
| 39 | + // Overwrite existing value |
| 40 | + self::$sql->set('testtype', 'testkey1', 'testkey2', 'testvaluemodified', 0); |
| 41 | + |
| 42 | + // Test that the value was actually overwriten |
| 43 | + $result = self::$sql->getValue('testtype', 'testkey1', 'testkey2'); |
| 44 | + $this->assertEquals('testvaluemodified', $result); |
| 45 | + |
| 46 | + $result = self::$sql->getList('testtype', 'testkey1', 'testkey2'); |
| 47 | + $this->assertEquals('testvaluemodified', $result[0]['value']); |
| 48 | + } |
| 49 | + |
| 50 | + public function testNonexistentKey() |
| 51 | + { |
| 52 | + // Test that getting some non-existing key will return null |
| 53 | + $result = self::$sql->getValue('testtype_nonexistent', 'testkey1_nonexistent', 'testkey2_nonexistent'); |
| 54 | + $this->assertNull($result); |
| 55 | + $result = self::$sql->getList('testtype_nonexistent', 'testkey1_nonexistent', 'testkey2_nonexistent'); |
| 56 | + $this->assertNull($result); |
| 57 | + $result = self::$sql->get('testtype_nonexistent', 'testkey1_nonexistent', 'testkey2_nonexistent'); |
| 58 | + $this->assertNull($result); |
| 59 | + } |
| 60 | + |
| 61 | + public function testExpiration() |
| 62 | + { |
| 63 | + // Make sure the earlier created entry has expired now |
| 64 | + sleep(1); |
| 65 | + |
| 66 | + // Now add a second entry that never expires |
| 67 | + self::$sql->set('testtype', 'testkey1_nonexpiring', 'testkey2_nonexpiring', 'testvalue_nonexpiring', null); |
| 68 | + |
| 69 | + // Expire entries and verify that only the second one is left |
| 70 | + self::$sql->removeExpired(); |
| 71 | + $result = self::$sql->getValue('testtype', 'testkey1', 'testkey2'); |
| 72 | + $this->assertNull($result); |
| 73 | + $result = self::$sql->getValue('testtype', 'testkey1_nonexpiring', 'testkey2_nonexpiring'); |
| 74 | + $this->assertEquals('testvalue_nonexpiring', $result); |
| 75 | + } |
| 76 | + |
| 77 | + public function testRemove() |
| 78 | + { |
| 79 | + // Now remove the nonexpiring entry and make sure it's gone |
| 80 | + self::$sql->remove('testtype', 'testkey1_nonexpiring', 'testkey2_nonexpiring'); |
| 81 | + $result = self::$sql->getValue('testtype', 'testkey1_nonexpiring', 'testkey2_nonexpiring'); |
| 82 | + $this->assertNull($result); |
| 83 | + } |
| 84 | +} |
0 commit comments