Completed
Pull Request — 3.4 (#6961)
by Daniel
09:45
created

CookieTest::tearDown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
class CookieTest extends SapphireTest {
4
5
	private $cookieInst;
6
7
	public function setUp() {
0 ignored issues
show
Coding Style introduced by
setUp uses the super-global variable $_COOKIE which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
8
		parent::setUp();
9
		Injector::inst()->registerService(new CookieJar($_COOKIE), 'Cookie_Backend');
10
	}
11
12
	/**
13
	 * Check a new cookie inst will be loaded with the superglobal by default
14
	 */
15
	public function testCheckNewInstTakesSuperglobal() {
0 ignored issues
show
Coding Style introduced by
testCheckNewInstTakesSuperglobal uses the super-global variable $_COOKIE which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
16
		//store the superglobal state
17
		$existingCookies = $_COOKIE;
18
19
		//set a mock state for the superglobal
20
		$_COOKIE = array(
21
			'cookie1' => 1,
22
			'cookie2' => 'cookies',
23
			'cookie3' => 'test',
24
			'cookie_4' => 'value',
25
		);
26
27
		Injector::inst()->unregisterNamedObject('Cookie_Backend');
28
29
		$this->assertEquals($_COOKIE['cookie1'], Cookie::get('cookie1'));
30
		$this->assertEquals($_COOKIE['cookie2'], Cookie::get('cookie2'));
31
		$this->assertEquals($_COOKIE['cookie3'], Cookie::get('cookie3'));
32
		$this->assertEquals($_COOKIE['cookie_4'], Cookie::get('cookie.4'));
33
		$this->assertEquals($_COOKIE['cookie_4'], Cookie::get('cookie_4'));
34
35
		//for good measure check the CookieJar hasn't stored anything extra
36
		$this->assertEquals($_COOKIE, Cookie::get_inst()->getAll(false));
37
38
		//restore the superglobal state
39
		$_COOKIE = $existingCookies;
40
	}
41
42
	/**
43
	 * Check we don't mess with super globals when manipulating cookies
44
	 *
45
	 * State should be managed sperately to the super global
46
	 */
47
	public function testCheckSuperglobalsArentTouched() {
0 ignored issues
show
Coding Style introduced by
testCheckSuperglobalsArentTouched uses the super-global variable $_COOKIE which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
48
49
		//store the current state
50
		$before = $_COOKIE;
51
52
		//change some cookies
53
		Cookie::set('cookie', 'not me');
54
		Cookie::force_expiry('cookie2');
55
56
		//assert it hasn't changed
57
		$this->assertEquals($before, $_COOKIE);
58
59
	}
60
61
	/**
62
	 * Check we can actually change a backend
63
	 */
64
	public function testChangeBackend() {
65
66
		Cookie::set('test', 'testvalue');
67
68
		$this->assertEquals('testvalue', Cookie::get('test'));
69
70
		Injector::inst()->registerService(new CookieJar(array()), 'Cookie_Backend');
71
72
		$this->assertEmpty(Cookie::get('test'));
73
74
	}
75
76
	/**
77
	 * Check we can actually get the backend inst out
78
	 */
79
	public function testGetInst() {
80
81
		$inst = new CookieJar(array('test' => 'testvalue'));
82
83
		Injector::inst()->registerService($inst, 'Cookie_Backend');
84
85
		$this->assertEquals($inst, Cookie::get_inst());
86
87
		$this->assertEquals('testvalue', Cookie::get('test'));
88
89
	}
90
91
	/**
92
	 * Test that we can set and get cookies
93
	 */
94
	public function testSetAndGet() {
95
		$this->assertEmpty(Cookie::get('testCookie'));
96
97
		//set a test cookie
98
		Cookie::set('testCookie', 'testVal');
99
100
		//make sure it was set
101
		$this->assertEquals('testVal', Cookie::get('testCookie'));
102
103
		//make sure we can distinguise it from ones that were "existing"
104
		$this->assertEmpty(Cookie::get('testCookie', false));
105
	}
106
107
	/**
108
	 * Test that we can distinguish between vars that were loaded on instantiation
109
	 * and those added later
110
	 */
111
	public function testExistingVersusNew() {
112
		//load with a cookie
113
		$cookieJar = new CookieJar(array(
114
			'cookieExisting' => 'i woz here',
115
		));
116
		Injector::inst()->registerService($cookieJar, 'Cookie_Backend');
117
118
		//set a new cookie
119
		Cookie::set('cookieNew', 'i am new');
120
121
		//check we can fetch new and old cookie values
122
		$this->assertEquals('i woz here', Cookie::get('cookieExisting'));
123
		$this->assertEquals('i woz here', Cookie::get('cookieExisting', false));
124
		$this->assertEquals('i am new', Cookie::get('cookieNew'));
125
		//there should be no original value for the new cookie
126
		$this->assertEmpty(Cookie::get('cookieNew', false));
127
128
		//change the existing cookie, can we fetch the new and old value
129
		Cookie::set('cookieExisting', 'i woz changed');
130
131
		$this->assertEquals('i woz changed', Cookie::get('cookieExisting'));
132
		$this->assertEquals('i woz here', Cookie::get('cookieExisting', false));
133
134
		//check we can get all cookies
135
		$this->assertEquals(array(
136
			'cookieExisting' => 'i woz changed',
137
			'cookieNew' => 'i am new',
138
		), Cookie::get_all());
139
140
		//check we can get all original cookies
141
		$this->assertEquals(array(
142
			'cookieExisting' => 'i woz here',
143
		), Cookie::get_all(false));
144
	}
145
146
	/**
147
	 * Check we can remove cookies and we can access their original values
148
	 */
149
	public function testForceExpiry() {
150
		//load an existing cookie
151
		$cookieJar = new CookieJar(array(
152
			'cookieExisting' => 'i woz here',
153
		));
154
		Injector::inst()->registerService($cookieJar, 'Cookie_Backend');
155
156
		//make sure it's available
157
		$this->assertEquals('i woz here', Cookie::get('cookieExisting'));
158
159
		//remove the cookie
160
		Cookie::force_expiry('cookieExisting');
161
162
		//check it's gone
163
		$this->assertEmpty(Cookie::get('cookieExisting'));
164
165
		//check we can get it's original value
166
		$this->assertEquals('i woz here', Cookie::get('cookieExisting', false));
167
168
169
		//check we can add a new cookie and remove it and it doesn't leave any phantom values
170
		Cookie::set('newCookie', 'i am new');
171
172
		//check it's set by not recieved
173
		$this->assertEquals('i am new', Cookie::get('newCookie'));
174
		$this->assertEmpty(Cookie::get('newCookie', false));
175
176
		//remove it
177
		Cookie::force_expiry('newCookie');
178
179
		//check it's neither set nor reveived
180
		$this->assertEmpty(Cookie::get('newCookie'));
181
		$this->assertEmpty(Cookie::get('newCookie', false));
182
	}
183
184
}
185