File: SQLPermanentStorageTest.php

package info (click to toggle)
simplesamlphp 1.19.7-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 42,920 kB
  • sloc: php: 202,044; javascript: 14,867; xml: 2,700; sh: 225; perl: 82; makefile: 70; python: 5
file content (123 lines) | stat: -rw-r--r-- 3,606 bytes parent folder | download | duplicates (3)
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
<?php

declare(strict_types=1);

namespace SimpleSAML\Test\Module\core\Storage;

use PHPUnit\Framework\TestCase;
use SimpleSAML\Configuration;
use SimpleSAML\Module\core\Storage\SQLPermanentStorage;

/**
 * Test for the SQLPermanentStorage class.
 */
class SQLPermanentStorageTest extends TestCase
{
    /** @var \SimpleSAML\Module\core\Storage\SQLPermanentStorage */
    private static $sql;


    /**
     * @return void
     */
    public static function setUpBeforeClass()
    {
        // Create instance
        $config = Configuration::loadFromArray([
            'datadir' => sys_get_temp_dir(),
        ]);
        self::$sql = new SQLPermanentStorage('test', $config);
    }


    /**
     * @return void
     */
    public static function tearDownAfterClass()
    {
        self::$sql = null;
        unlink(sys_get_temp_dir() . '/sqllite/test.sqlite');
    }


    /**
     * @return void
     */
    public function testSet(): void
    {
        // Set a new value
        self::$sql->set('testtype', 'testkey1', 'testkey2', 'testvalue', 2);

        // Test getCondition
        /** @var array $result */
        $result = self::$sql->get();
        $this->assertEquals('testvalue', $result['value']);
    }


    /**
     * @return void
     */
    public function testSetOverwrite(): void
    {
        // Overwrite existing value
        self::$sql->set('testtype', 'testkey1', 'testkey2', 'testvaluemodified', 2);

        // Test that the value was actually overwriten
        $result = self::$sql->getValue('testtype', 'testkey1', 'testkey2');
        $this->assertEquals('testvaluemodified', $result);

        /** @var array $result */
        $result = self::$sql->getList('testtype', 'testkey1', 'testkey2');
        $this->assertEquals('testvaluemodified', $result[0]['value']);
    }


    /**
     * @return void
     */
    public function testNonexistentKey(): void
    {
        // Test that getting some non-existing key will return null / empty array
        $result = self::$sql->getValue('testtype_nonexistent', 'testkey1_nonexistent', 'testkey2_nonexistent');
        $this->assertNull($result);
        $result = self::$sql->getList('testtype_nonexistent', 'testkey1_nonexistent', 'testkey2_nonexistent');
        $this->assertEmpty($result);
        $result = self::$sql->get('testtype_nonexistent', 'testkey1_nonexistent', 'testkey2_nonexistent');
        $this->assertNull($result);
    }


    /**
     * @return void
     */
    public function testExpiration(): void
    {
        // Make sure the earlier created entry has expired now
        sleep(3);

        // Make sure we can't get the expired entry anymore
        $result = self::$sql->getValue('testtype', 'testkey1', 'testkey2');
        $this->assertNull($result);

        // Now add a second entry that never expires
        self::$sql->set('testtype', 'testkey1_nonexpiring', 'testkey2_nonexpiring', 'testvalue_nonexpiring', null);

        // Expire entries and verify that only the second one is still there
        self::$sql->removeExpired();
        $result = self::$sql->getValue('testtype', 'testkey1_nonexpiring', 'testkey2_nonexpiring');
        $this->assertEquals('testvalue_nonexpiring', $result);
    }


    /**
     * @return void
     */
    public function testRemove(): void
    {
        // Now remove the nonexpiring entry and make sure it's gone
        self::$sql->remove('testtype', 'testkey1_nonexpiring', 'testkey2_nonexpiring');
        $result = self::$sql->getValue('testtype', 'testkey1_nonexpiring', 'testkey2_nonexpiring');
        $this->assertNull($result);
    }
}