AbstractAuthenticationPluginTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
dl 0
loc 52
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 7

1 Method

Rating   Name   Duplication   Size   Complexity  
A testAfterHandlerInvoked() 0 46 4
1
<?php
2
3
namespace Vectorface\SnappyRouterTests\Plugin\Authentication;
4
5
use PHPUnit\Framework\TestCase;
6
7
use Vectorface\SnappyRouter\Authentication\CallbackAuthenticator;
8
use Vectorface\SnappyRouter\Di\Di;
9
use Vectorface\SnappyRouter\Exception\InternalErrorException;
10
use Vectorface\SnappyRouter\Exception\UnauthorizedException;
11
use Vectorface\SnappyRouter\Handler\ControllerHandler;
12
13
/**
14
 * Tests the AbstractAuthenticationPlugin class.
15
 *
16
 * @copyright Copyright (c) 2014, VectorFace, Inc.
17
 * @author J. Anderson <[email protected]>
18
 * @author Dan Bruce   <[email protected]>
19
 */
20
class AbstractAuthenticationPluginTest extends TestCase
21
{
22
    /**
23
     * Authentication of service requests happens by intercepting preInvoke; Validate that.
24
     */
25
    public function testAfterHandlerInvoked()
26
    {
27
        $ignored = new ControllerHandler(array());
28
29
        /* Configure DI */
30
        $bool = false;
31
        $auth = new CallbackAuthenticator(function () use (&$bool) {
32
            return $bool;
33
        });
34
        $di = new Di(array('AuthMechanism' => false));
35
        Di::setDefault($di);
36
37
        /* Direct testing. */
38
        $plugin = new TestAuthenticationPlugin(array());
39
40
        try {
41
            $plugin->afterHandlerSelected($ignored);
42
            $this->fail("An invalid authenticator should yield an internal error");
43
        } catch (InternalErrorException $e) {
44
            $this->assertEquals(500, $e->getAssociatedStatusCode()); /* HTTP 500 ISE */
45
        }
46
47
        /* From here on out, use the "Do whatever I say" authenticator. :) */
48
        $di->set('AuthMechanism', $auth);
49
50
        $plugin->credentials = false;
51
        try {
52
            $plugin->afterHandlerSelected($ignored);
53
            $this->fail("No username and password are available. UnauthorizedException expected.");
54
        } catch (UnauthorizedException $e) {
55
            $this->assertEquals(401, $e->getAssociatedStatusCode()); /* HTTP 401 Unauthorized */
56
        }
57
58
        $plugin->credentials = array('ignored' , 'ignored');
59
        try {
60
            $plugin->afterHandlerSelected($ignored);
61
            $this->fail("Callback expected to return false auth result. UnauthorizedException expected.");
62
        } catch (UnauthorizedException $e) {
63
            // we expect the exception to be thrown
64
        }
65
66
        /* With a true result, preInvoke should pass through. */
67
        $bool = true;
68
        $this->assertTrue($bool);
69
        $plugin->afterHandlerSelected($ignored);
70
    }
71
}
72