Passed
Branch master (71c281)
by Wanderson
01:19
created

UserDAOTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 76
Duplicated Lines 51.32 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 2
cbo 3
dl 39
loc 76
rs 10
c 0
b 0
f 0

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 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
	public static function setUpBeforeClass() {
19
		static::$pdo = MySQLTest::startMySQLConnection();
20
		static::$pdo->query('DROP TABLE ' . UserDAO::TABLE);
21
		static::$pdo->query('CREATE TABLE `user` (
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);
39
	}
40
41
	private function insertExample() {
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();
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();
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
	public function testUpdatePassword() {
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