FakeAuthUser   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 11
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 10
c 0
b 0
f 0
wmc 2
lcom 0
cbo 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A clearCachedPermission() 0 4 1
A getId() 0 4 1
1
<?php
2
session_start();
3
4
class FakeAuthObserver
5
{
6
}
7
class FakeAuthUser
8
{
9
    function clearCachedPermission()
10
    {
11
        return true;
12
    }
13
    function getId()
14
    {
15
        return 1;
16
    }
17
}
18
19
class FakeAuthAdapter
20
{
21
    function auth()
0 ignored issues
show
Coding Style introduced by
auth uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
22
    {
23
        $_SESSION['intraface_logged_in_user_id'] = 1;
24
        return new FakeAuthUser;
25
    }
26
27
    function getIdentification()
28
    {
29
        return 'fake user';
30
    }
31
}
32
33
class AuthTest extends PHPUnit_Framework_TestCase
34
{
35
    const SESSION_LOGIN = 'thissessionfirstlog';
36
    private $auth;
37
    private $db;
38
    protected $backupGlobals = false;
39
40
    function setUp()
41
    {
42
        $this->db = MDB2::singleton(DB_DSN);
43
44
        $this->auth = new Intraface_Auth(self::SESSION_LOGIN);
45
    }
46
47
    function tearDown()
48
    {
49
        $this->db->exec('TRUNCATE user');
50
        unset($this->auth);
51
    }
52
53
    function testConstructionOfAuth()
54
    {
55
        $this->assertTrue(is_object($this->auth));
56
    }
57
58
    function testAuthMethodReturnsAnObjectOnSuccessFullAuthentication()
59
    {
60
        $this->assertTrue(is_object($this->auth->authenticate(new FakeAuthAdapter)));
61
    }
62
63
    function testAfterAuthenticationTheIdentityCanBeGrappedUsingGetIdentity()
64
    {
65
        $db = MDB2::singleton();
66
        $db->query('INSERT INTO user SET email="[email protected]", session_id="'.self::SESSION_LOGIN.'"');
67
        // @todo lidt dum da der skal v�re en Intraface_User tilg�ngelig.
68
        $this->auth->authenticate(new FakeAuthAdapter);
69
        $identity = $this->auth->getIdentity($this->db);
70
        $this->assertTrue(is_object($identity));
71
    }
72
73
    function createUserInDatabase()
74
    {
75
        // first we add a user.
76
        require_once 'Intraface/modules/intranetmaintenance/UserMaintenance.php';
77
        $u = new UserMaintenance();
78
        $this->assertEquals(1, $u->update(array('email' => '[email protected]', 'password' => 'startup', 'confirm_password' => 'startup')));
79
    }
80
}
81