CookieStoreTest::testStoreLargeData()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20

Duplication

Lines 20
Ratio 100 %

Importance

Changes 0
Metric Value
dl 20
loc 20
rs 9.6
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace SilverStripe\HybridSessions\Tests;
4
5
use SilverStripe\Core\Injector\Injector;
6
use SilverStripe\Core\TempFolder;
7
use SilverStripe\HybridSessions\Store\CookieStore;
8
use SilverStripe\HybridSessions\Tests\Store\TestCookieStore;
9
10
class CookieStoreTest extends AbstractTest
11
{
12
    protected function getStore()
13
    {
14
        $store = Injector::inst()->get(CookieStore::class);
15
        $store->setKey(uniqid());
16
        $store->open(TempFolder::getTempFolder(BASE_PATH) . '/' . __CLASS__, 'SESSIONCOOKIE');
17
18
        return $store;
19
    }
20
21 View Code Duplication
    public function testStoreLargeData()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
22
    {
23
        $session = uniqid();
24
        $store = $this->getStore();
25
26
        // Test new session is blank
27
        $result = $store->read($session);
28
        $this->assertEmpty($result);
29
30
        // Save data against session
31
        $data1 = array(
32
            'Large' => str_repeat('A', 600),
33
            'Content' => str_repeat('B', 600)
34
        );
35
        $store->write($session, serialize($data1));
36
        $result = $store->read($session);
37
38
        // Cookies should not try to store data that large
39
        $this->assertEmpty($result);
40
    }
41
42
    /**
43
     * Ensure that subsequent reads without the necessary write do not report data
44
     */
45
    public function testReadInvalidatesData()
46
    {
47
        $session = uniqid();
48
        $store = $this->getStore();
49
50
        // Test new session is blank
51
        $result = $store->read($session);
52
        $this->assertEmpty($result);
53
54
        // Save data against session
55
        $data1 = array(
56
            'Color' => 'red',
57
            'Animal' => 'elephant'
58
        );
59
        $store->write($session, serialize($data1));
60
        $result = $store->read($session);
61
        $this->assertEquals($data1, unserialize($result));
62
63
        // Since we have read the data into the result, the application could modify this content
64
        // and be unable to write it back due to headers being sent. We should thus assume
65
        // that subsequent reads without a successful write do not purport to have valid data
66
        $data1['Color'] = 'blue';
67
        $result = $store->read($session);
68
        $this->assertEmpty($result);
69
70
        // Check that writing to cookie fails after headers are sent and these results remain
71
        // invalidated
72
        TestCookieStore::$override_headers_sent = true;
0 ignored issues
show
Documentation Bug introduced by
The property $override_headers_sent was declared of type string, but true is of type boolean. Maybe add a type cast?

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.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
73
        $store->write($session, serialize($data1));
74
        $result = $store->read($session);
75
        $this->assertEmpty($result);
76
    }
77
}
78