1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Vectorface\Tests\Auth; |
4
|
|
|
|
5
|
|
|
use Vectorface\Auth\Auth; |
6
|
|
|
use Vectorface\Auth\AuthException; |
7
|
|
|
use Vectorface\Auth\Plugin\SuccessPlugin; |
8
|
|
|
use Vectorface\Auth\Plugin\NullPlugin; |
9
|
|
|
use Monolog\Logger; |
10
|
|
|
use Monolog\Handler\NullHandler; |
11
|
|
|
use SplFixedArray; |
12
|
|
|
use Exception; |
13
|
|
|
use PHPUnit\Framework\TestCase; |
14
|
|
|
use Vectorface\Tests\Auth\Helpers\HardcodedUserPlugin; |
15
|
|
|
use Vectorface\Tests\Auth\Helpers\TestPlugin; |
16
|
|
|
|
17
|
|
|
class AuthTest extends TestCase |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var Auth |
21
|
|
|
*/ |
22
|
|
|
private $auth; |
23
|
|
|
|
24
|
|
|
protected function setUp() |
25
|
|
|
{ |
26
|
|
|
$logger = new Logger('auth'); |
27
|
|
|
$logger->pushHandler(new NullHandler()); |
28
|
|
|
|
29
|
|
|
$this->auth = new Auth(); |
30
|
|
|
$this->auth->setLogger($logger); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @throws AuthException |
35
|
|
|
*/ |
36
|
|
View Code Duplication |
public function testNothingDoesNothing() |
|
|
|
|
37
|
|
|
{ |
38
|
|
|
$this->assertTrue($this->auth->addPlugin(new NullPlugin())); |
39
|
|
|
$this->assertFalse($this->auth->login('a', 'b')); |
40
|
|
|
$this->assertFalse($this->auth->verify()); |
41
|
|
|
$this->assertFalse($this->auth->logout()); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @throws AuthException |
46
|
|
|
*/ |
47
|
|
View Code Duplication |
public function testSuccess() |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
$this->assertTrue($this->auth->addPlugin(new SuccessPlugin())); |
50
|
|
|
$this->assertTrue($this->auth->login('a', 'b')); |
51
|
|
|
$this->assertTrue($this->auth->verify()); |
52
|
|
|
$this->assertTrue($this->auth->logout()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @throws AuthException |
57
|
|
|
*/ |
58
|
|
|
public function testLogin() |
59
|
|
|
{ |
60
|
|
|
$this->assertTrue($this->auth->addPlugin(new HardcodedUserPlugin())); |
61
|
|
|
$this->assertFalse($this->auth->login('u', 'p')); |
62
|
|
|
$this->assertTrue($this->auth->login('foo', 'bar')); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @throws AuthException |
67
|
|
|
*/ |
68
|
|
|
public function testForce() |
69
|
|
|
{ |
70
|
|
|
$testFail = new TestPlugin(); |
71
|
|
|
$testForce = new TestPlugin(); |
72
|
|
|
$testFail->setResult(Auth::RESULT_FAILURE); |
73
|
|
|
$testForce->setResult(Auth::RESULT_FORCE); |
74
|
|
|
$this->assertTrue($this->auth->addPlugin($testForce)); // Force before fail. |
75
|
|
|
$this->assertTrue($this->auth->addPlugin($testFail)); |
76
|
|
|
$this->assertTrue($this->auth->verify()); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testArrayAccess() |
80
|
|
|
{ |
81
|
|
|
$this->assertArrayNotHasKey('foo', $this->auth); // offsetExists |
82
|
|
|
$this->assertNull($this->auth['foo']); // offsetGet |
83
|
|
|
$this->auth['foo'] = 'bar'; // offsetSet |
84
|
|
|
$this->assertEquals('bar', $this->auth['foo']); // offsetGet |
85
|
|
|
unset($this->auth['foo']); // offsetUnset |
86
|
|
|
$this->assertNull($this->auth['foo']); // offsetGet |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @noinspection PhpUndefinedMethodInspection |
91
|
|
|
* @noinspection PhpRedundantCatchClauseInspection |
92
|
|
|
*/ |
93
|
|
|
public function testFunctionPassthrough() |
94
|
|
|
{ |
95
|
|
|
$test = new TestPlugin(); |
96
|
|
|
$this->auth->addPlugin($test); |
97
|
|
|
|
98
|
|
|
$this->assertEquals($this->auth, $test->getAuthObject()); |
99
|
|
|
$this->assertTrue($this->auth->returnTrue()); |
|
|
|
|
100
|
|
|
|
101
|
|
|
try { |
102
|
|
|
$this->assertNull($this->auth->throwAuthException()); |
|
|
|
|
103
|
|
|
$this->fail('Expected to pass up the AuthException'); |
104
|
|
|
} catch (AuthException $e) { |
105
|
|
|
// Expected |
106
|
|
|
} |
107
|
|
|
$this->assertNull($this->auth->throwException()); // Gets caught and causes action failure. |
|
|
|
|
108
|
|
|
$this->assertNull($this->auth->notDefined()); // Method not implemented |
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function testAddPlugin() |
112
|
|
|
{ |
113
|
|
|
$this->assertTrue($this->auth->addPlugin(new TestPlugin())); |
114
|
|
|
$this->assertFalse($this->auth->addPlugin(new SplFixedArray()), 'Not an Auth plugin'); |
115
|
|
|
$this->assertFalse($this->auth->addPlugin('SplFixedArray'), 'Not an Auth plugin'); |
116
|
|
|
$this->assertFalse($this->auth->addPlugin(1.2), 'Not an Auth plugin'); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @throws AuthException |
121
|
|
|
*/ |
122
|
|
|
public function testEdgeCases() |
123
|
|
|
{ |
124
|
|
|
$test = new TestPlugin(); |
125
|
|
|
$this->auth->addPlugin($test); |
126
|
|
|
$test->setResult(new SplFixedArray()); // This isn't a valid result. |
127
|
|
|
|
128
|
|
|
try { |
129
|
|
|
$this->auth->login('u', 'p'); |
130
|
|
|
$this->fail("An invalid result should have triggered an AuthException"); |
131
|
|
|
} catch (AuthException $e) { |
132
|
|
|
// Expected |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$this->setUp(); |
136
|
|
|
$this->assertTrue($this->auth->addPlugin('Vectorface\\Auth\\Plugin\\SuccessPlugin')); |
137
|
|
|
$this->auth->addPlugin($test); |
138
|
|
|
$test->setResult(new Exception("Exception added on purpose by test case.")); // Causes a log entry and failure. |
139
|
|
|
$this->assertFalse($this->auth->verify()); |
140
|
|
|
|
141
|
|
|
$test->setResult(new AuthException()); |
142
|
|
|
try { |
143
|
|
|
$this->auth->verify(); |
144
|
|
|
$this->fail("Expected AuthException to be passed up."); |
145
|
|
|
} catch (AuthException $e) { |
146
|
|
|
// Expected |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/* Loading by class name should work. */ |
150
|
|
|
$this->assertTrue($this->auth->addPlugin('Vectorface\\Auth\\Plugin\\SuccessPlugin')); |
151
|
|
|
$this->assertFalse($this->auth->addPlugin(new SplFixedArray())); // Fails for obvious reasons. |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
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.