1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\HybridSessions\Tests; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Dev\SapphireTest; |
6
|
|
|
use SilverStripe\Core\Injector\Injector; |
7
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime; |
8
|
|
|
use SilverStripe\HybridSessions\Store\CookieStore; |
9
|
|
|
use SilverStripe\HybridSessions\Tests\Store\TestCookieStore; |
10
|
|
|
|
11
|
|
|
abstract class AbstractTest extends SapphireTest |
12
|
|
|
{ |
13
|
|
|
public function setUp() |
14
|
|
|
{ |
15
|
|
|
parent::setUp(); |
16
|
|
|
|
17
|
|
|
TestCookieStore::$override_headers_sent = false; |
|
|
|
|
18
|
|
|
|
19
|
|
|
Injector::nest(); |
20
|
|
|
Injector::inst()->registerService( |
21
|
|
|
new TestCookieStore(), |
22
|
|
|
CookieStore::class |
23
|
|
|
); |
24
|
|
|
|
25
|
|
|
DBDatetime::set_mock_now('2010-03-15 12:00:00'); |
26
|
|
|
|
27
|
|
|
if (get_class() === get_class($this)) { |
28
|
|
|
$this->markTestSkipped("Skipping abstract test"); |
29
|
|
|
$this->skipTest = true; |
|
|
|
|
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function tearDown() |
34
|
|
|
{ |
35
|
|
|
Injector::unnest(); |
36
|
|
|
DBDatetime::clear_mock_now(); |
37
|
|
|
|
38
|
|
|
parent::tearDown(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
abstract protected function getStore(); |
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Test how this store handles large volumes of data (>1000 characters) |
45
|
|
|
*/ |
46
|
|
View Code Duplication |
public function testStoreLargeData() |
|
|
|
|
47
|
|
|
{ |
48
|
|
|
$session = uniqid(); |
49
|
|
|
$store = $this->getStore(); |
50
|
|
|
|
51
|
|
|
// Test new session is blank |
52
|
|
|
$result = $store->read($session); |
53
|
|
|
$this->assertEmpty($result); |
54
|
|
|
|
55
|
|
|
// Save data against session |
56
|
|
|
$data1 = array( |
57
|
|
|
'Large' => str_repeat('A', 600), |
58
|
|
|
'Content' => str_repeat('B', 600) |
59
|
|
|
); |
60
|
|
|
$store->write($session, serialize($data1)); |
61
|
|
|
$result = $store->read($session); |
62
|
|
|
$this->assertEquals($data1, unserialize($result)); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Test storage of data |
67
|
|
|
*/ |
68
|
|
|
public function testStoreData() |
69
|
|
|
{ |
70
|
|
|
$session = uniqid(); |
71
|
|
|
$store = $this->getStore(); |
72
|
|
|
|
73
|
|
|
// Test new session is blank |
74
|
|
|
$result = $store->read($session); |
75
|
|
|
$this->assertEmpty($result); |
76
|
|
|
|
77
|
|
|
// Save data against session |
78
|
|
|
$data1 = array( |
79
|
|
|
'Color' => 'red', |
80
|
|
|
'Animal' => 'elephant' |
81
|
|
|
); |
82
|
|
|
$store->write($session, serialize($data1)); |
83
|
|
|
$result = $store->read($session); |
84
|
|
|
$this->assertEquals($data1, unserialize($result)); |
85
|
|
|
|
86
|
|
|
// Save larger data |
87
|
|
|
$data2 = array( |
88
|
|
|
'Color' => 'blue', |
89
|
|
|
'Animal' => str_repeat('bat', 100) |
90
|
|
|
); |
91
|
|
|
$store->write($session, serialize($data2)); |
92
|
|
|
$result = $store->read($session); |
93
|
|
|
$this->assertEquals($data2, unserialize($result)); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Test expiry of data |
98
|
|
|
*/ |
99
|
|
|
public function testExpiry() |
100
|
|
|
{ |
101
|
|
|
$session1 = uniqid(); |
102
|
|
|
$store = $this->getStore(); |
103
|
|
|
|
104
|
|
|
// Store data now |
105
|
|
|
$data1 = array( |
106
|
|
|
'Food' => 'Pizza' |
107
|
|
|
); |
108
|
|
|
$store->write($session1, serialize($data1)); |
109
|
|
|
$result1 = $store->read($session1); |
110
|
|
|
$this->assertEquals($data1, unserialize($result1)); |
111
|
|
|
|
112
|
|
|
// Go to the future and test that the expiry is accurate |
113
|
|
|
DBDatetime::set_mock_now('2040-03-16 12:00:00'); |
114
|
|
|
$result2 = $store->read($session1); |
115
|
|
|
|
116
|
|
|
$this->assertEmpty($result2); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.