1+ <?php
2+ /**
3+ * Test for the store:Mongo data store.
4+ *
5+ * For the full copyright and license information, please view the LICENSE file that was distributed with this source
6+ * code.
7+ *
8+ * @author Chris Beaton <[email protected] > 9+ * @package prolificinteractive/simplesamlphp-module-mongo
10+ */
11+
12+ namespace SimpleSAML \Test \Module \mongo \Store ;
13+
14+ use PHPUnit \Framework \TestCase ;
15+ use \SimpleSAML_Configuration as Configuration ;
16+
17+ final class StoreTest extends TestCase
18+ {
19+ public function testSingleHostConnection ()
20+ {
21+ Configuration::setConfigDir (__DIR__ . '/fixture/single-host ' );
22+ new \sspmod_mongo_Store_Store ();
23+ $ this ->assertTrue (true );
24+ }
25+
26+ public function testGet ()
27+ {
28+ $ store = new \sspmod_mongo_Store_Store ();
29+ $ connection = $ store ->getConnection ();
30+ $ database = getenv ('DB_MONGODB_DATABASE ' );
31+ $ collection = $ connection ->{$ database }->session ;
32+ $ collection ->remove ();
33+ $ this ->assertEquals (0 , $ collection ->count ());
34+
35+ $ type = 'session ' ;
36+ $ key = 'SESSION_ID ' ;
37+ $ value = array ('some ' => 'thing ' );
38+ $ expire = time () + 1000000 ;
39+
40+ $ result = $ store ->get ($ type , $ key );
41+ $ this ->assertNull ($ result );
42+
43+ $ store ->set ($ type , $ key , $ value , $ expire );
44+ $ this ->assertEquals (1 , $ collection ->count ());
45+
46+ $ result = $ store ->get ($ type , $ key );
47+ $ this ->assertEquals ($ result , $ value );
48+ $ this ->assertEquals (1 , $ collection ->count ());
49+ }
50+
51+ public function testExpiredGet ()
52+ {
53+ $ store = new \sspmod_mongo_Store_Store ();
54+ $ connection = $ store ->getConnection ();
55+ $ database = getenv ('DB_MONGODB_DATABASE ' );
56+ $ collection = $ connection ->{$ database }->session ;
57+ $ collection ->remove ();
58+ $ this ->assertEquals (0 , $ collection ->count ());
59+
60+ $ type = 'session ' ;
61+ $ key = 'SESSION_ID ' ;
62+ $ value = array ('some ' => 'thing ' );
63+ $ expire = 0 ;
64+ $ store ->set ($ type , $ key , $ value , $ expire );
65+ $ this ->assertEquals (1 , $ collection ->count ());
66+
67+ $ result = $ store ->get ($ type , $ key );
68+ $ this ->assertNull ($ result );
69+ $ this ->assertEquals (0 , $ collection ->count ());
70+ }
71+
72+ public function testSet ()
73+ {
74+ $ store = new \sspmod_mongo_Store_Store ();
75+ $ connection = $ store ->getConnection ();
76+ $ database = getenv ('DB_MONGODB_DATABASE ' );
77+ $ collection = $ connection ->{$ database }->session ;
78+ $ collection ->remove ();
79+ $ this ->assertEquals (0 , $ collection ->count ());
80+
81+ $ type = 'session ' ;
82+ $ key = 'SESSION_ID ' ;
83+ $ value = array ('some ' => 'thing ' );
84+ $ expire = time () + 1000000 ;
85+
86+ $ result = $ store ->get ($ type , $ key );
87+ $ this ->assertNull ($ result );
88+
89+ $ store ->set ($ type , $ key , $ value , $ expire );
90+ $ this ->assertEquals (1 , $ collection ->count ());
91+
92+ $ result = $ store ->get ($ type , $ key );
93+ $ this ->assertEquals ($ result , $ value );
94+ $ this ->assertEquals (1 , $ collection ->count ());
95+
96+ $ value = array ('some ' => 'otherthing ' );
97+ $ result = $ store ->set ($ type , $ key , $ value , $ expire );
98+ $ this ->assertEquals (1 , $ collection ->count ());
99+ $ this ->assertEquals ($ expire , $ result );
100+ $ result = $ store ->get ($ type , $ key );
101+ $ this ->assertEquals ($ result , $ value );
102+ $ this ->assertEquals (1 , $ collection ->count ());
103+ }
104+
105+ public function testDelete ()
106+ {
107+ $ store = new \sspmod_mongo_Store_Store ();
108+ $ connection = $ store ->getConnection ();
109+ $ database = getenv ('DB_MONGODB_DATABASE ' );
110+ $ collection = $ connection ->{$ database }->session ;
111+ $ collection ->remove ();
112+ $ this ->assertEquals (0 , $ collection ->count ());
113+
114+ $ type = 'session ' ;
115+ $ key = 'SESSION_ID ' ;
116+ $ value = array ('some ' => 'thing ' );
117+ $ expire = time () + 1000000 ;
118+ $ store ->set ($ type , $ key , $ value , $ expire );
119+ $ this ->assertEquals (1 , $ collection ->count ());
120+
121+ $ store ->delete ($ type , $ key );
122+ $ this ->assertEquals (0 , $ collection ->count ());
123+ }
124+ }
0 commit comments