-
Notifications
You must be signed in to change notification settings - Fork 676
/
TestOfUserMySQLDAO.php
207 lines (182 loc) · 8.13 KB
/
TestOfUserMySQLDAO.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
/**
*
* ThinkUp/tests/TestOfUserMySQLDAO.php
*
* Copyright (c) 2009-2013 Gina Trapani
*
* LICENSE:
*
* This file is part of ThinkUp (http://thinkup.com).
*
* ThinkUp is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any
* later version.
*
* ThinkUp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with ThinkUp. If not, see
* <http://www.gnu.org/licenses/>.
*
*
* Test of UserDAO
*
* @license http://www.gnu.org/licenses/gpl.html
* @copyright 2009-2013 Gina Trapani
* @author Gina Trapani <ginatrapani[at]gmail[dot]com>
*
*/
require_once dirname(__FILE__).'/init.tests.php';
require_once THINKUP_WEBAPP_PATH.'_lib/extlib/simpletest/autorun.php';
require_once THINKUP_WEBAPP_PATH.'config.inc.php';
class TestOfUserMySQLDAO extends ThinkUpUnitTestCase {
public function setUp() {
parent::setUp();
$this->builders = self::buildData();
}
protected function buildData() {
//Insert test data into test table
$builders[] = FixtureBuilder::build('users', array('user_id'=>12, 'user_name'=>'jack',
'full_name'=>'Jack Dorsey', 'avatar'=>'avatar.jpg', 'location'=>'San Francisco',
'network'=>'twitter'));
//Insert test data into test table
$builders[] = FixtureBuilder::build('users', array('user_id'=>13, 'user_name'=>'zuck',
'full_name'=>'Mark Zuckerberg', 'avatar'=>'avatar.jpg', 'location'=>'San Francisco',
'network'=>'facebook'));
$this->logger = Logger::getInstance();
return $builders;
}
/**
* Tear down
*/
public function tearDown() {
$this->builders = null;
parent::tearDown();
$this->logger->close();
}
/**
* Test DAO constructor
*/
public function testCreateNewUserDAO() {
$dao = DAOFactory::getDAO('UserDAO');
$this->assertTrue(isset($dao));
}
/**
* Test isUserInDB
*/
public function testIsUserInDB() {
$user_dao = DAOFactory::getDAO('UserDAO');
$this->assertTrue($user_dao->isUserInDB(12, 'twitter'));
$this->assertFalse($user_dao->isUserInDB(13, 'twitter'));
$this->assertTrue($user_dao->isUserInDB(13, 'facebook'));
}
/**
* Test isUserInDBByName
*/
public function testIsUserInDBByName() {
$user_dao = DAOFactory::getDAO('UserDAO');
$this->assertTrue($user_dao->isUserInDBByName('jack', 'twitter'));
$this->assertFalse($user_dao->isUserInDBByName('gina', 'twitter'));
$this->assertTrue($user_dao->isUserInDBByName('zuck', 'facebook'));
$this->assertFalse($user_dao->isUserInDBByName('zuck', 'twitter'));
}
/**
* Test getDetails when the user exists
*/
public function testGetDetailsUserExists() {
$user_dao = DAOFactory::getDAO('UserDAO');
$user = $user_dao->getDetails(12, 'twitter');
$this->assertEqual($user->id, 1);
$this->assertEqual($user->user_id, 12);
$this->assertEqual($user->username, 'jack');
$this->assertEqual($user->network, 'twitter');
$user = $user_dao->getDetails(13, 'facebook');
$this->assertEqual($user->id, 2);
$this->assertEqual($user->user_id, 13);
$this->assertEqual($user->username, 'zuck');
$this->assertEqual($user->network, 'facebook');
$user = $user_dao->getDetails(13, 'twitter');
$this->assertTrue(!isset($user));
}
/**
* Test getDetails when user does not exist
*/
public function testGetDetailsUserDoesNotExist() {
$user_dao = DAOFactory::getDAO('UserDAO');
$user = $user_dao->getDetails(13, 'twitter');
$this->assertTrue(!isset($user));
}
/**
* Test update individual user
*/
public function testUpdateUser() {
$user_dao = DAOFactory::getDAO('UserDAO');
$user_array = array('user_id'=>'13', 'user_name'=>'ginatrapani', 'full_name'=>'Gina Trapani',
'avatar'=>'avatar.jpg', 'location'=>'NYC', 'description'=>'Blogger', 'url'=>'http://ginatrapani.org',
'is_protected'=>0, 'follower_count'=>5000, 'post_count'=>1000, 'joined'=>'2007-03-06 13:48:05', 'network'=>'twitter',
'last_post_id'=>'abc102');
$user = new User($user_array, 'Test Insert');
$this->assertEqual($user_dao->updateUser($user), 1, "1 user inserted");
$user_from_db = $user_dao->getDetails('13', 'twitter');
$this->assertEqual($user_from_db->user_id, '13');
$this->assertEqual($user_from_db->username, 'ginatrapani');
$this->assertEqual($user_from_db->avatar, 'avatar.jpg');
$this->assertEqual($user_from_db->location, 'NYC');
$user_array = array('user_id'=>13, 'user_name'=>'ginatrapanichanged', 'full_name'=>'Gina Trapani ',
'avatar'=>'avatara.jpg', 'location'=>'San Diego', 'description'=>'Blogger', 'url'=>'http://ginatrapani.org',
'is_protected'=>0, 'follower_count'=>5000, 'post_count'=>1000, 'joined'=>'2007-03-06 13:48:05', 'network'=>'twitter');
$user1 = new User($user_array, 'Test Update');
$this->assertEqual($user_dao->updateUser($user1), 1, "1 row updated");
$user_from_db = $user_dao->getDetails(13, 'twitter');
$this->assertEqual($user_from_db->user_id, 13);
$this->assertEqual($user_from_db->username, 'ginatrapanichanged');
$this->assertEqual($user_from_db->avatar, 'avatara.jpg');
$this->assertEqual($user_from_db->location, 'San Diego');
//Test no username set
$user_array = array('user_id'=>13, 'user_name'=>null, 'full_name'=>'Gina Trapani ',
'avatar'=>'avatara.jpg', 'location'=>'San Diego', 'description'=>'Blogger', 'url'=>'http://ginatrapani.org',
'is_protected'=>0, 'follower_count'=>5000, 'post_count'=>1000, 'joined'=>'2007-03-06 13:48:05', 'network'=>'twitter');
$user1 = new User($user_array, 'Test Update');
$this->assertEqual($user_dao->updateUser($user1), 0);
}
/**
* Test updateUsers for multiple user update
*/
public function testUpdateUsers() {
$user_dao = DAOFactory::getDAO('UserDAO');
$user_array1 = array('id'=>2, 'user_id'=>'13', 'user_name'=>'ginatrapani', 'full_name'=>'Gina Trapani',
'avatar'=>'avatar.jpg', 'location'=>'NYC', 'description'=>'Blogger', 'url'=>'http://ginatrapani.org',
'is_protected'=>0, 'follower_count'=>5000, 'post_count'=>1000, 'joined'=>'2007-03-06 13:48:05', 'network'=>'twitter',
'last_post_id'=>'abc123');
$user1 = new User($user_array1, 'Test');
$user_array2 = array('id'=>3, 'user_id'=>'14', 'user_name'=>'anildash', 'full_name'=>'Anil Dash',
'avatar'=>'avatar.jpg', 'location'=>'NYC', 'description'=>'Blogger', 'url'=>'http://ginatrapani.org',
'is_protected'=>0, 'follower_count'=>5000, 'post_count'=>1000, 'joined'=>'2007-03-06 13:48:05', 'network'=>'twitter',
'last_post_id'=>'abc456');
$user2 = new User($user_array2, 'Test');
$users_to_update = array($user1, $user2);
$this->assertTrue($user_dao->updateUsers($users_to_update) == 2);
}
/**
* Test getUserByName when user exists
*/
public function testGetUserByNameUserExists() {
$user_dao = DAOFactory::getDAO('UserDAO');
$user = $user_dao->getUserByName('jack', 'twitter');
$this->assertEqual($user->id, 1);
$this->assertEqual($user->user_id, 12);
$this->assertEqual($user->username, 'jack');
$this->assertEqual($user->full_name, 'Jack Dorsey');
$this->assertEqual($user->location, 'San Francisco');
}
/**
* Test getUserByName when user does not exist
*/
public function testGetUserByNameUserDoesNotExist() {
$user_dao = DAOFactory::getDAO('UserDAO');
$user = $user_dao->getUserByName('gina', 'twitter');
$this->assertEqual($user, null);
}
}