Passed
Pull Request — master (#4)
by Francis
02:49
created

AuthTest::testForce()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 0
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() : void
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
    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
    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