Completed
Branch develop (0bf7ae)
by Jonathan
02:46
created

AuthTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 137
Duplicated Lines 10.95 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 9
dl 15
loc 137
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 8 1
A testNothingDoesNothing() 7 7 1
A testSuccess() 7 7 1
A testLogin() 0 6 1
A testForce() 0 10 1
A testArrayAccess() 0 9 1
A testFunctionPassthrough() 0 17 2
A testAddPlugin() 0 7 1
A testEdgeCases() 0 31 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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());
0 ignored issues
show
Documentation Bug introduced by
The method returnTrue does not exist on object<Vectorface\Auth\Auth>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
100
101
        try {
102
            $this->assertNull($this->auth->throwAuthException());
0 ignored issues
show
Documentation Bug introduced by
The method throwAuthException does not exist on object<Vectorface\Auth\Auth>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
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.
0 ignored issues
show
Documentation Bug introduced by
The method throwException does not exist on object<Vectorface\Auth\Auth>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
108
        $this->assertNull($this->auth->notDefined()); // Method not implemented
0 ignored issues
show
Documentation Bug introduced by
The method notDefined does not exist on object<Vectorface\Auth\Auth>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
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