Completed
Push — master ( 659231...788af6 )
by Wanderson
05:06
created

UserDAOTest::testUpdateRecoveryHash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
namespace Win\Authentication;
4
5
use Win\Connection\MySQLTest;
6
7
class UserDAOTest extends \PHPUnit_Framework_TestCase {
8
9
	/** @var \PDO */
10
	private static $pdo;
11
12
	/** @var User */
13
	private $user;
14
15
	/** @var UserDAO */
16
	private $dao;
17
18 View Code Duplication
	public static function setUpBeforeClass() {
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...
19
		static::$pdo = MySQLTest::startMySQLConnection();
0 ignored issues
show
Bug introduced by
Since $pdo is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $pdo to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
20
		static::$pdo->query('DROP TABLE ' . UserDAO::TABLE);
0 ignored issues
show
Bug introduced by
Since $pdo is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $pdo to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
21
		static::$pdo->query('CREATE TABLE `user` (
0 ignored issues
show
Bug introduced by
Since $pdo is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $pdo to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
22
					`id` int(11) NOT NULL AUTO_INCREMENT,
23
					`is_enabled` tinyint(1) NOT NULL,
24
					`access_level` smallint(6) NOT NULL,
25
					`group_id` int(11) NOT NULL,
26
					`name` varchar(45) NOT NULL,
27
					`email` varchar(45) NOT NULL,
28
					`password_hash` varchar(32) NOT NULL,
29
					`recovery_hash` varchar(32) DEFAULT NULL,
30
					`image` varchar(45) DEFAULT NULL,
31
					`last_login` datetime DEFAULT NULL,
32
					PRIMARY KEY(`id`)
33
				  );
34
		');
35
	}
36
37
	public function setUp() {
38
		static::$pdo->query('TRUNCATE TABLE ' . UserDAO::TABLE);
0 ignored issues
show
Bug introduced by
Since $pdo is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $pdo to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
39
	}
40
41 View Code Duplication
	private function insertExample() {
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...
42
		$user = new User();
43
		$user->setEnabled(true);
44
		$user->setAccessLevel(User::ACCESS_ADMIN);
45
		$user->setName('My name');
46
		$user->setEmail('[email protected]');
47
		$user->setPassword('123456');
48
		$this->user = $user;
49
50
		$this->dao = new UserDAO();
51
		return $this->dao->save($user);
52
	}
53
54
	public function testUpdateRecoveryHash() {
55
		$this->insertExample();
56
		$oldHash = $this->user->getRecoreryHash();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $oldHash is correct as $this->user->getRecoreryHash() (which targets Win\Authentication\User::getRecoreryHash()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
57
		$this->user->setRecoreryHash('abc');
58
		$this->dao->updateRecoveryHash($this->user);
59
		$this->assertNotEquals($oldHash, $this->user->getRecoreryHash());
60
	}
61
62
	public function testUpdateLastLogin() {
63
		$this->insertExample();
64
		$lastLogin = $this->user->getLastLogin();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $lastLogin is correct as $this->user->getLastLogin() (which targets Win\Authentication\User::getLastLogin()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
65
		$this->user->login();
66
		$this->assertEquals($lastLogin, $this->user->getLastLogin());
67
68
		$this->user->login();
69
		$this->assertNotEquals($lastLogin, $this->user->getLastLogin());
70
	}
71
72 View Code Duplication
	public function testUpdatePassword() {
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...
73
		$this->insertExample();
74
		$oldPasswordHash = $this->user->getPasswordHash();
75
		$this->dao->updatePassword($this->user, 'my-newpass', 'my-newpass');
76
		$this->assertNotEquals($this->user->getPasswordHash(), $oldPasswordHash);
77
78
		$lastUser = $this->dao->fetchById($this->user->getId());
79
		$this->assertEquals($lastUser->getPasswordHash(), $this->user->getPasswordHash());
80
	}
81
82
}
83