|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Win\DAO; |
|
4
|
|
|
|
|
5
|
|
|
use Win\Connection\MySQLTest; |
|
6
|
|
|
use Win\Authentication\User; |
|
7
|
|
|
use Win\Authentication\UserDAO; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Este teste usa UserDAO como exemplo |
|
11
|
|
|
* Testa os metodos já existentes em abstract DAO |
|
12
|
|
|
*/ |
|
13
|
|
|
class DAOTest extends \PHPUnit_Framework_TestCase { |
|
14
|
|
|
|
|
15
|
|
|
/** @var \PDO */ |
|
16
|
|
|
private static $pdo; |
|
17
|
|
|
|
|
18
|
|
|
/** @var User */ |
|
19
|
|
|
private $user; |
|
20
|
|
|
|
|
21
|
|
|
/** @var UserDAO */ |
|
22
|
|
|
private $dao; |
|
23
|
|
|
|
|
24
|
|
View Code Duplication |
public static function setUpBeforeClass() { |
|
|
|
|
|
|
25
|
|
|
static::$pdo = MySQLTest::startMySQLConnection(); |
|
|
|
|
|
|
26
|
|
|
static::$pdo->query('DROP TABLE ' . UserDAO::TABLE); |
|
|
|
|
|
|
27
|
|
|
static::$pdo->query('CREATE TABLE `user` ( |
|
|
|
|
|
|
28
|
|
|
`id` int(11) NOT NULL AUTO_INCREMENT, |
|
29
|
|
|
`is_enabled` tinyint(1) NOT NULL, |
|
30
|
|
|
`access_level` smallint(6) NOT NULL, |
|
31
|
|
|
`group_id` int(11) NOT NULL, |
|
32
|
|
|
`name` varchar(45) NOT NULL, |
|
33
|
|
|
`email` varchar(45) NOT NULL, |
|
34
|
|
|
`password_hash` varchar(32) NOT NULL, |
|
35
|
|
|
`recovery_hash` varchar(32) DEFAULT NULL, |
|
36
|
|
|
`image` varchar(45) DEFAULT NULL, |
|
37
|
|
|
`last_login` datetime DEFAULT NULL, |
|
38
|
|
|
PRIMARY KEY(`id`) |
|
39
|
|
|
); |
|
40
|
|
|
'); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function setUp() { |
|
44
|
|
|
static::$pdo->query('TRUNCATE TABLE ' . UserDAO::TABLE); |
|
|
|
|
|
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
View Code Duplication |
private function insertExample() { |
|
|
|
|
|
|
48
|
|
|
$user = new User(); |
|
49
|
|
|
$user->setEnabled(true); |
|
50
|
|
|
$user->setAccessLevel(User::ACCESS_ADMIN); |
|
51
|
|
|
$user->setName('My name'); |
|
52
|
|
|
$user->setEmail('[email protected]'); |
|
53
|
|
|
$user->setPassword('123456'); |
|
54
|
|
|
$this->user = $user; |
|
55
|
|
|
|
|
56
|
|
|
$this->dao = new UserDAO(); |
|
57
|
|
|
return $this->dao->save($user); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function testInsert() { |
|
61
|
|
|
$error = $this->insertExample(); |
|
62
|
|
|
$this->assertNull($error); |
|
63
|
|
|
|
|
64
|
|
|
$lastUser = $this->dao->fetchById($this->user->getId()); |
|
65
|
|
|
$this->assertEquals($this->user->getName(), $lastUser->getName()); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
View Code Duplication |
public function testUpdate() { |
|
|
|
|
|
|
69
|
|
|
$this->insertExample(); |
|
70
|
|
|
|
|
71
|
|
|
$this->user->setName('Name Updated'); |
|
72
|
|
|
$error = $this->dao->save($this->user); |
|
73
|
|
|
$this->assertNull($error); |
|
74
|
|
|
|
|
75
|
|
|
$lastUser = $this->dao->fetchById($this->user->getId()); |
|
76
|
|
|
$this->assertEquals($this->user->getId(), $lastUser->getId()); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function testDelete() { |
|
80
|
|
|
$this->insertExample(); |
|
81
|
|
|
$this->dao->delete($this->user); |
|
82
|
|
|
|
|
83
|
|
|
$lastUser = $this->dao->fetchById($this->user->getId()); |
|
84
|
|
|
$this->assertEquals($lastUser->getId(), 0); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
View Code Duplication |
public function testDeleteById() { |
|
|
|
|
|
|
88
|
|
|
$this->insertExample(); |
|
89
|
|
|
$this->dao->deleteById($this->user->getId()); |
|
90
|
|
|
|
|
91
|
|
|
$lastUser = $this->dao->fetchById($this->user->getId()); |
|
92
|
|
|
$this->assertEquals($lastUser->getId(), 0); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
View Code Duplication |
public function testValidate() { |
|
|
|
|
|
|
96
|
|
|
$this->insertExample(); |
|
97
|
|
|
$this->user->setEmail('invalid'); |
|
98
|
|
|
$this->dao->save($this->user); |
|
99
|
|
|
|
|
100
|
|
|
$lastUser = $this->dao->fetchById($this->user->getId()); |
|
101
|
|
|
$this->assertEquals($lastUser->getEmail(), '[email protected]'); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function testFetch() { |
|
105
|
|
|
$this->insertExample(); |
|
106
|
|
|
$lastUser = $this->dao->fetch(['name = ?' => 'My name']); |
|
107
|
|
|
$this->assertEquals($lastUser->getId(), $this->user->getId()); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function testFetchAll() { |
|
111
|
|
|
$this->dao = new UserDAO(); |
|
112
|
|
|
$userList0 = $this->dao->fetchAll(); |
|
113
|
|
|
$this->assertEquals(count($userList0), 0); |
|
114
|
|
|
|
|
115
|
|
|
/* insert 2 examples */ |
|
116
|
|
|
$this->insertExample(); |
|
117
|
|
|
$this->insertExample(); |
|
118
|
|
|
$userList2 = $this->dao->fetchAll(); |
|
119
|
|
|
$this->assertEquals(count($userList2), 2); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function testNumRows() { |
|
123
|
|
|
$this->dao = new UserDAO(); |
|
124
|
|
|
$numRows0 = $this->dao->numRows(); |
|
125
|
|
|
$this->assertEquals($numRows0, 0); |
|
126
|
|
|
|
|
127
|
|
|
$this->insertExample(); |
|
128
|
|
|
$numRows1 = $this->dao->numRows(); |
|
129
|
|
|
$this->assertEquals($numRows1, 1); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
View Code Duplication |
public function testFoundWithFilter() { |
|
|
|
|
|
|
133
|
|
|
$this->insertExample(); |
|
134
|
|
|
$filters = [ |
|
135
|
|
|
'name = ?' => 'My name', |
|
136
|
|
|
'email = ?' => '[email protected]', |
|
137
|
|
|
'access_level > ?' => 0 |
|
138
|
|
|
]; |
|
139
|
|
|
$user = $this->dao->fetch($filters); |
|
140
|
|
|
$this->assertNotEquals($user->getId(), 0); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
View Code Duplication |
public function testNotFoundWithFilter() { |
|
|
|
|
|
|
144
|
|
|
$this->insertExample(); |
|
145
|
|
|
$filters = [ |
|
146
|
|
|
'name = ?' => 'Name doesnt exist', |
|
147
|
|
|
'email = ?' => '[email protected]', |
|
148
|
|
|
'access_level > ?' => 0 |
|
149
|
|
|
]; |
|
150
|
|
|
$user = $this->dao->fetch($filters); |
|
151
|
|
|
$this->assertEquals($user->getId(), 0); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
} |
|
155
|
|
|
|
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.