|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jasny\Auth; |
|
4
|
|
|
|
|
5
|
|
|
use Jasny\Auth; |
|
6
|
|
|
use PHPUnit_Framework_TestCase as TestCase; |
|
7
|
|
|
use PHPUnit_Framework_MockObject_MockObject as MockObject; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @covers Jasny\Auth\Sessions |
|
11
|
|
|
*/ |
|
12
|
|
|
class SessionsAuth extends TestCase |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var Auth\Sessions|MockObject |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $auth; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $sessionModule; |
|
23
|
|
|
|
|
24
|
|
|
protected function mockSessionHandling() |
|
25
|
|
|
{ |
|
26
|
|
|
// Mock sessions |
|
27
|
|
|
session_cache_limiter(''); |
|
28
|
|
|
ini_set('session.use_cookies', 0); |
|
29
|
|
|
ini_set('session.use_only_cookies', 0); |
|
30
|
|
|
|
|
31
|
|
|
$this->sessionModule = session_module_name(); |
|
32
|
|
|
session_set_save_handler($this->createMock(\SessionHandlerInterface::class)); |
|
33
|
|
|
|
|
34
|
|
|
session_start(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
protected function restoreSessionHandling() |
|
38
|
|
|
{ |
|
39
|
|
|
session_abort(); |
|
40
|
|
|
session_module_name($this->sessionModule); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function setUp() |
|
44
|
|
|
{ |
|
45
|
|
|
$this->auth = $this->getMockForTrait(Auth\Sessions::class); |
|
46
|
|
|
$this->mockSessionHandling(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function tearDown() |
|
50
|
|
|
{ |
|
51
|
|
|
$this->restoreSessionHandling(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Call a protected method |
|
57
|
|
|
* |
|
58
|
|
|
* @param object $object |
|
59
|
|
|
* @param string $method |
|
60
|
|
|
* @param array $args |
|
61
|
|
|
* @return mixed |
|
62
|
|
|
*/ |
|
63
|
|
|
protected function callProtectedMethod($object, $method, array $args = []) |
|
64
|
|
|
{ |
|
65
|
|
|
$refl = new \ReflectionMethod(get_class($object), $method); |
|
66
|
|
|
$refl->setAccessible(true); |
|
67
|
|
|
|
|
68
|
|
|
return $refl->invokeArgs($object, $args); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
public function testGetCurrentUserIdWithUser() |
|
73
|
|
|
{ |
|
74
|
|
|
$_SESSION['auth_uid'] = 123; |
|
75
|
|
|
|
|
76
|
|
|
$id = $this->callProtectedMethod($this->auth, 'getCurrentUserId'); |
|
77
|
|
|
$this->assertEquals(123, $id); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function testGetCurrentUserIdWithoutUser() |
|
81
|
|
|
{ |
|
82
|
|
|
$id = $this->callProtectedMethod($this->auth, 'getCurrentUserId'); |
|
83
|
|
|
$this->assertNull($id); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
public function testPersistCurrentUserWithUser() |
|
88
|
|
|
{ |
|
89
|
|
|
$_SESSION['foo'] = 'bar'; |
|
90
|
|
|
|
|
91
|
|
|
$user = $this->createMock(Auth\User::class); |
|
92
|
|
|
$user->method('getId')->willReturn(123); |
|
93
|
|
|
|
|
94
|
|
|
$this->auth->expects($this->once())->method('user')->willReturn($user); |
|
95
|
|
|
|
|
96
|
|
|
$this->callProtectedMethod($this->auth, 'persistCurrentUser'); |
|
97
|
|
|
|
|
98
|
|
|
$this->assertEquals(['foo' => 'bar', 'auth_uid' => 123], $_SESSION); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function testPersistCurrentUserWithoutUser() |
|
102
|
|
|
{ |
|
103
|
|
|
$_SESSION['auth_uid'] = 123; |
|
104
|
|
|
$_SESSION['foo'] = 'bar'; |
|
105
|
|
|
|
|
106
|
|
|
$this->auth->expects($this->once())->method('user')->willReturn(null); |
|
107
|
|
|
|
|
108
|
|
|
$this->callProtectedMethod($this->auth, 'persistCurrentUser'); |
|
109
|
|
|
|
|
110
|
|
|
$this->assertEquals(['foo' => 'bar'], $_SESSION); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|