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
|
|
|
protected $usesDatabase = true; |
14
|
|
|
|
15
|
|
|
protected function setUp() |
16
|
|
|
{ |
17
|
|
|
parent::setUp(); |
18
|
|
|
|
19
|
|
|
TestCookieStore::$override_headers_sent = false; |
20
|
|
|
|
21
|
|
|
Injector::inst()->registerService( |
22
|
|
|
new TestCookieStore(), |
23
|
|
|
CookieStore::class |
24
|
|
|
); |
25
|
|
|
|
26
|
|
|
DBDatetime::set_mock_now('2010-03-15 12:00:00'); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
protected function tearDown() |
30
|
|
|
{ |
31
|
|
|
DBDatetime::clear_mock_now(); |
32
|
|
|
|
33
|
|
|
parent::tearDown(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
abstract protected function getStore(); |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Test how this store handles large volumes of data (>1000 characters) |
40
|
|
|
*/ |
41
|
|
View Code Duplication |
public function testStoreLargeData() |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
$session = uniqid(); |
44
|
|
|
$store = $this->getStore(); |
45
|
|
|
|
46
|
|
|
// Test new session is blank |
47
|
|
|
$result = $store->read($session); |
48
|
|
|
$this->assertEmpty($result); |
49
|
|
|
|
50
|
|
|
// Save data against session |
51
|
|
|
$data1 = array( |
52
|
|
|
'Large' => str_repeat('A', 600), |
53
|
|
|
'Content' => str_repeat('B', 600) |
54
|
|
|
); |
55
|
|
|
$store->write($session, serialize($data1)); |
56
|
|
|
$result = $store->read($session); |
57
|
|
|
$this->assertEquals($data1, unserialize($result)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Test storage of data |
62
|
|
|
*/ |
63
|
|
|
public function testStoreData() |
64
|
|
|
{ |
65
|
|
|
$session = uniqid(); |
66
|
|
|
$store = $this->getStore(); |
67
|
|
|
|
68
|
|
|
// Test new session is blank |
69
|
|
|
$result = $store->read($session); |
70
|
|
|
$this->assertEmpty($result); |
71
|
|
|
|
72
|
|
|
// Save data against session |
73
|
|
|
$data1 = array( |
74
|
|
|
'Color' => 'red', |
75
|
|
|
'Animal' => 'elephant' |
76
|
|
|
); |
77
|
|
|
$store->write($session, serialize($data1)); |
78
|
|
|
$result = $store->read($session); |
79
|
|
|
$this->assertEquals($data1, unserialize($result)); |
80
|
|
|
|
81
|
|
|
// Save larger data |
82
|
|
|
$data2 = array( |
83
|
|
|
'Color' => 'blue', |
84
|
|
|
'Animal' => str_repeat('bat', 100) |
85
|
|
|
); |
86
|
|
|
$store->write($session, serialize($data2)); |
87
|
|
|
$result = $store->read($session); |
88
|
|
|
$this->assertEquals($data2, unserialize($result)); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Test expiry of data |
93
|
|
|
*/ |
94
|
|
|
public function testExpiry() |
95
|
|
|
{ |
96
|
|
|
$session1 = uniqid(); |
97
|
|
|
$store = $this->getStore(); |
98
|
|
|
|
99
|
|
|
// Store data now |
100
|
|
|
$data1 = array( |
101
|
|
|
'Food' => 'Pizza' |
102
|
|
|
); |
103
|
|
|
$store->write($session1, serialize($data1)); |
104
|
|
|
$result1 = $store->read($session1); |
105
|
|
|
$this->assertEquals($data1, unserialize($result1)); |
106
|
|
|
|
107
|
|
|
// Go to the future and test that the expiry is accurate |
108
|
|
|
DBDatetime::set_mock_now('2040-03-16 12:00:00'); |
109
|
|
|
$result2 = $store->read($session1); |
110
|
|
|
|
111
|
|
|
$this->assertEmpty($result2); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.