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

CookieStoreTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 68
Duplicated Lines 41.18 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 4
dl 28
loc 68
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getStore() 8 8 1
A testStoreLargeData() 20 20 1
B testReadInvalidatesData() 0 32 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace SilverStripe\HybridSessions\Tests;
4
5
use SilverStripe\Core\Injector\Injector;
6
use SilverStripe\Core\TempFolder;
7
use SilverStripe\HybridSessions\Tests\AbstractTest;
8
use SilverStripe\HybridSessions\Store\CookieStore;
9
use SilverStripe\HybridSessions\Tests\Store\TestCookieStore;
10
11
class CookieStoreTest extends AbstractTest
12
{
13 View Code Duplication
    protected function getStore()
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...
14
    {
15
        $store = Injector::inst()->get(CookieStore::class);
16
        $store->setKey(uniqid());
17
        $store->open(TempFolder::getTempFolder(BASE_PATH).'/'.__CLASS__, 'SESSIONCOOKIE');
18
19
        return $store;
20
    }
21
22 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...
23
    {
24
        $session = uniqid();
25
        $store = $this->getStore();
26
27
        // Test new session is blank
28
        $result = $store->read($session);
29
        $this->assertEmpty($result);
30
31
        // Save data against session
32
        $data1 = array(
33
            'Large' => str_repeat('A', 600),
34
            'Content' => str_repeat('B', 600)
35
        );
36
        $store->write($session, serialize($data1));
37
        $result = $store->read($session);
38
39
        // Cookies should not try to store data that large
40
        $this->assertEmpty($result);
41
    }
42
43
    /**
44
     * Ensure that subsequent reads without the necessary write do not report data
45
     */
46
    public function testReadInvalidatesData()
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
            'Color' => 'red',
58
            'Animal' => 'elephant'
59
        );
60
        $store->write($session, serialize($data1));
61
        $result = $store->read($session);
62
        $this->assertEquals($data1, unserialize($result));
63
64
        // Since we have read the data into the result, the application could modify this content
65
        // and be unable to write it back due to headers being sent. We should thus assume
66
        // that subsequent reads without a successful write do not purport to have valid data
67
        $data1['Color'] = 'blue';
68
        $result = $store->read($session);
69
        $this->assertEmpty($result);
70
71
        // Check that writing to cookie fails after headers are sent and these results remain
72
        // invalidated
73
        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...
74
        $store->write($session, serialize($data1));
75
        $result = $store->read($session);
76
        $this->assertEmpty($result);
77
    }
78
}
79