77
88abstract class AbstractFunctionalTest extends TestCase
99{
10- protected abstract function createStore (array $ data = array ());
10+ protected $ defaults ;
11+
12+ protected abstract function createStore (array $ data = null );
13+
14+ protected function getStore (array $ data = null )
15+ {
16+ $ store = $ this ->createStore ($ data );
17+ if ($ this ->defaults ) {
18+ $ store ->setDefaults ($ this ->defaults );
19+ }
20+ return $ store ;
21+ }
1122
1223 public function tearDown (): void
1324 {
1425 m::close ();
26+ $ this ->defaults = [];
1527 }
1628
1729 protected function assertStoreEquals ($ store , $ expected , $ message = '' )
1830 {
1931 $ this ->assertEquals ($ expected , $ store ->all (), $ message );
2032 $ store ->save ();
21- $ store = $ this ->createStore ();
33+ $ store = $ this ->getStore ();
2234 $ this ->assertEquals ($ expected , $ store ->all (), $ message );
2335 }
2436
2537 protected function assertStoreKeyEquals ($ store , $ key , $ expected , $ message = '' )
2638 {
2739 $ this ->assertEquals ($ expected , $ store ->get ($ key ), $ message );
2840 $ store ->save ();
29- $ store = $ this ->createStore ();
41+ $ store = $ this ->getStore ();
3042 $ this ->assertEquals ($ expected , $ store ->get ($ key ), $ message );
3143 }
3244
3345 /** @test */
3446 public function store_is_initially_empty ()
3547 {
36- $ store = $ this ->createStore ();
48+ $ store = $ this ->getStore ();
3749 $ this ->assertEquals (array (), $ store ->all ());
3850 }
3951
4052 /** @test */
4153 public function written_changes_are_saved ()
4254 {
43- $ store = $ this ->createStore ();
55+ $ store = $ this ->getStore ();
4456 $ store ->set ('foo ' , 'bar ' );
4557 $ this ->assertStoreKeyEquals ($ store , 'foo ' , 'bar ' );
4658 }
4759
4860 /** @test */
4961 public function nested_keys_are_nested ()
5062 {
51- $ store = $ this ->createStore ();
63+ $ store = $ this ->getStore ();
5264 $ store ->set ('foo.bar ' , 'baz ' );
5365 $ this ->assertStoreEquals ($ store , array ('foo ' => array ('bar ' => 'baz ' )));
5466 }
5567
5668 /** @test */
5769 public function cannot_set_nested_key_on_non_array_member ()
5870 {
59- $ store = $ this ->createStore ();
71+ $ store = $ this ->getStore ();
6072 $ store ->set ('foo ' , 'bar ' );
6173 $ this ->expectException ('UnexpectedValueException ' );
6274 $ this ->expectExceptionMessage ('Non-array segment encountered ' );
@@ -66,7 +78,7 @@ public function cannot_set_nested_key_on_non_array_member()
6678 /** @test */
6779 public function can_forget_key ()
6880 {
69- $ store = $ this ->createStore ();
81+ $ store = $ this ->getStore ();
7082 $ store ->set ('foo ' , 'bar ' );
7183 $ store ->set ('bar ' , 'baz ' );
7284 $ this ->assertStoreEquals ($ store , array ('foo ' => 'bar ' , 'bar ' => 'baz ' ));
@@ -78,7 +90,7 @@ public function can_forget_key()
7890 /** @test */
7991 public function can_forget_nested_key ()
8092 {
81- $ store = $ this ->createStore ();
93+ $ store = $ this ->getStore ();
8294 $ store ->set ('foo.bar ' , 'baz ' );
8395 $ store ->set ('foo.baz ' , 'bar ' );
8496 $ store ->set ('bar.foo ' , 'baz ' );
@@ -119,9 +131,18 @@ public function can_forget_nested_key()
119131 /** @test */
120132 public function can_forget_all ()
121133 {
122- $ store = $ this ->createStore (array ('foo ' => 'bar ' ));
134+ $ store = $ this ->getStore (array ('foo ' => 'bar ' ));
123135 $ this ->assertStoreEquals ($ store , array ('foo ' => 'bar ' ));
124136 $ store ->forgetAll ();
125137 $ this ->assertStoreEquals ($ store , array ());
126138 }
139+
140+ /** @test */
141+ public function defaults_are_respected ()
142+ {
143+ $ this ->defaults = ['foo ' => 'default ' , 'bar ' => 'default ' ];
144+ $ store = $ this ->getStore (array ('foo ' => 'bar ' ));
145+ $ this ->assertStoreEquals ($ store , ['foo ' => 'bar ' ]);
146+ $ this ->assertStoreKeyEquals ($ store , ['foo ' , 'bar ' ], ['foo ' => 'bar ' , 'bar ' => 'default ' ]);
147+ }
127148}
0 commit comments