Completed
Pull Request — master (#27)
by Will
09:02
created

AbstractTest::testStoreLargeData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 11

Duplication

Lines 18
Ratio 100 %

Importance

Changes 0
Metric Value
dl 18
loc 18
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 0
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;
0 ignored issues
show
Documentation Bug introduced by
The property $override_headers_sent was declared of type string, but false is of type false. 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...
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;
0 ignored issues
show
Bug introduced by
The property skipTest does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
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();
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
42
43
    /**
44
     * Test how this store handles large volumes of data (>1000 characters)
45
     */
46 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...
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