Completed
Pull Request — master (#14)
by Helpful
03:02
created

HybridSessionAbstractTest::testExpiry()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 1
eloc 11
nc 1
nop 0
dl 0
loc 17
rs 9.4285
1
<?php
2
3
abstract class HybridSessionAbstractTest extends SapphireTest {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
5
	public function setUp() {
6
		parent::setUp();
7
8
		HybridSessionAbstractTest_TestCookieBackend::$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...
9
10
		Injector::nest();
11
		Injector::inst()->registerService(
12
			new HybridSessionAbstractTest_TestCookieBackend(),
0 ignored issues
show
Documentation introduced by
new \HybridSessionAbstractTest_TestCookieBackend() is of type object<HybridSessionAbst...Test_TestCookieBackend>, but the function expects a object<stdClass>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
13
			'HybridSessionStore_Cookie'
14
		);
15
16
		SS_Datetime::set_mock_now('2010-03-15 12:00:00');
17
18
		if(get_class() === get_class($this)) {
19
			$this->markTestSkipped("Skipping abstract test");
0 ignored issues
show
Bug introduced by
The method markTestSkipped() does not seem to exist on object<HybridSessionAbstractTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
20
			$this->skipTest = true;
21
		}
22
	}
23
24
	public function tearDown() {
25
		Injector::unnest();
26
		SS_Datetime::clear_mock_now();
27
28
		parent::tearDown();
29
	}
30
31
	/**
32
	 * @return HybridSessionStore_Base
33
	 */
34
	abstract protected function getStore();
35
36
	/**
37
	 * Test how this store handles large volumes of data (>1000 characters)
38
	 */
39 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...
40
		$session = uniqid();
41
		$store = $this->getStore();
42
43
		// Test new session is blank
44
		$result = $store->read($session);
45
		$this->assertEmpty($result);
0 ignored issues
show
Bug introduced by
The method assertEmpty() does not seem to exist on object<HybridSessionAbstractTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
46
47
		// Save data against session
48
		$data1 = array(
49
			'Large' => str_repeat('A', 600),
50
			'Content' => str_repeat('B', 600)
51
		);
52
		$store->write($session, serialize($data1));
53
		$result = $store->read($session);
54
		$this->assertEquals($data1, unserialize($result));
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<HybridSessionAbstractTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
55
	}
56
57
	/**
58
	 * Test storage of data
59
	 */
60
	public function testStoreData() {
61
		$session = uniqid();
62
		$store = $this->getStore();
63
64
		// Test new session is blank
65
		$result = $store->read($session);
66
		$this->assertEmpty($result);
0 ignored issues
show
Bug introduced by
The method assertEmpty() does not seem to exist on object<HybridSessionAbstractTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
67
68
		// Save data against session
69
		$data1 = array(
70
			'Color' => 'red',
71
			'Animal' => 'elephant'
72
		);
73
		$store->write($session, serialize($data1));
74
		$result = $store->read($session);
75
		$this->assertEquals($data1, unserialize($result));
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<HybridSessionAbstractTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
76
77
		// Save larger data
78
		$data2 = array(
79
			'Color' => 'blue',
80
			'Animal' => str_repeat('bat', 100)
81
		);
82
		$store->write($session, serialize($data2));
83
		$result = $store->read($session);
84
		$this->assertEquals($data2, unserialize($result));
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<HybridSessionAbstractTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
85
	}
86
87
	/**
88
	 * Test expiry of data
89
	 */
90
	public function testExpiry() {
91
		$session1 = uniqid();
92
		$store = $this->getStore();
93
94
		// Store data now
95
		$data1 = array(
96
			'Food' => 'Pizza'
97
		);
98
		$store->write($session1, serialize($data1));
99
		$result1 = $store->read($session1);
100
		$this->assertEquals($data1, unserialize($result1));
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<HybridSessionAbstractTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
101
102
		// Go to the future and test that the expiry is accurate
103
		SS_Datetime::set_mock_now('2040-03-16 12:00:00');
104
		$result2 = $store->read($session1);
105
		$this->assertEmpty($result2);
0 ignored issues
show
Bug introduced by
The method assertEmpty() does not seem to exist on object<HybridSessionAbstractTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
106
	}
107
}
108
109
class HybridSessionAbstractTest_TestCookieBackend extends HybridSessionStore_Cookie {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
110
111
112
	/**
113
	 * Override value of 'headers_sent' but only if running tests.
114
	 *
115
	 * Set to true or false, or null to not override
116
	 *
117
	 * @var string
118
	 */
119
	public static $override_headers_sent = null;
120
121
	protected function canWrite() {
122
		if(self::$override_headers_sent !== null) {
123
			return !self::$override_headers_sent;
124
		}
125
		parent::canWrite();
126
	}
127
}