PageRenderTime 56ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/models/hrfunct/EmpLocationTest.php

https://bitbucket.org/wildanm/orangehrm
PHP | 419 lines | 249 code | 62 blank | 108 comment | 4 complexity | d4f0cc67598dbbf1ee19fde0d54b7c9a MD5 | raw file
Possible License(s): CC-BY-SA-3.0, AGPL-3.0, BSD-3-Clause, AGPL-1.0, GPL-2.0, LGPL-2.1, LGPL-3.0
  1. <?php
  2. /**
  3. * OrangeHRM is a comprehensive Human Resource Management (HRM) System that captures
  4. * all the essential functionalities required for any enterprise.
  5. * Copyright (C) 2006 OrangeHRM Inc., http://www.orangehrm.com
  6. *
  7. * OrangeHRM is free software; you can redistribute it and/or modify it under the terms of
  8. * the GNU General Public License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * OrangeHRM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  12. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. * See the GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with this program;
  16. * if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  17. * Boston, MA 02110-1301, USA
  18. *
  19. */
  20. // Call EmpLocationTest::main() if this source file is executed directly.
  21. if (!defined("PHPUnit_MAIN_METHOD")) {
  22. define("PHPUnit_MAIN_METHOD", "EmpLocationTest::main");
  23. }
  24. require_once "PHPUnit/Framework/TestCase.php";
  25. require_once "PHPUnit/Framework/TestSuite.php";
  26. require_once "testConf.php";
  27. require_once ROOT_PATH."/lib/confs/Conf.php";
  28. require_once ROOT_PATH."/lib/confs/sysConf.php";
  29. require_once ROOT_PATH."/lib/models/hrfunct/EmpLocation.php";
  30. require_once ROOT_PATH."/lib/common/UniqueIDGenerator.php";
  31. /**
  32. * Test class for EmpLocation
  33. */
  34. class EmpLocationTest extends PHPUnit_Framework_TestCase {
  35. private $empLocations;
  36. private $locations;
  37. private $errorLevel;
  38. private $errorStr;
  39. /**
  40. * Runs the test methods of this class.
  41. *
  42. * @access public
  43. * @static
  44. */
  45. public static function main() {
  46. require_once "PHPUnit/TextUI/TestRunner.php";
  47. $suite = new PHPUnit_Framework_TestSuite("EmpLocationTest");
  48. $result = PHPUnit_TextUI_TestRunner::run($suite);
  49. }
  50. /**
  51. * Sets up the fixture, making sure table is empty and creating database
  52. * entries needed during test.
  53. *
  54. * @access protected
  55. */
  56. protected function setUp() {
  57. $conf = new Conf();
  58. $this->connection = mysql_connect($conf->dbhost.":".$conf->dbport, $conf->dbuser, $conf->dbpass);
  59. mysql_select_db($conf->dbname);
  60. $this->_deleteTables();
  61. // Insert employees
  62. $this->_runQuery("INSERT INTO hs_hr_employee(emp_number, employee_id, emp_lastname, emp_firstname, emp_middle_name) " .
  63. "VALUES(11, '0011', 'Rajasinghe', 'Saman', 'Marlon')");
  64. $this->_runQuery("INSERT INTO hs_hr_employee(emp_number, employee_id, emp_lastname, emp_firstname, emp_middle_name) " .
  65. "VALUES(12, '0022', 'Jayasinghe', 'Aruna', 'Shantha')");
  66. $this->_runQuery("INSERT INTO hs_hr_employee(emp_number, employee_id, emp_lastname, emp_firstname, emp_middle_name) " .
  67. "VALUES(13, '0042', 'Jayaweera', 'Nimal', 'T')");
  68. // Insert locations
  69. $this->locations[] = array('LOC002', 'Colombo');
  70. $this->locations[] = array('LOC001', 'Kandy');
  71. $this->locations[] = array('LOC003', 'Matara');
  72. $this->locations[] = array('LOC004', 'Nuwara Eliya');
  73. foreach ($this->locations as $loc) {
  74. $this->_insertLocation($loc[0], $loc[1], 'LK', '111 Main St', '1111');
  75. }
  76. // Assign locations to employees
  77. $this->empLocations[0] = new EmpLocation(11, 'LOC001');
  78. $this->empLocations[0]->setLocationName('Kandy');
  79. $this->empLocations[1] = new EmpLocation(11, 'LOC002');
  80. $this->empLocations[1]->setLocationName('Colombo');
  81. $this->empLocations[2] = new EmpLocation(11, 'LOC003');
  82. $this->empLocations[2]->setLocationName('Matara');
  83. $this->empLocations[3] = new EmpLocation(11, 'LOC004');
  84. $this->empLocations[3]->setLocationName('Nuwara Eliya');
  85. $this->empLocations[4] = new EmpLocation(12, 'LOC002');
  86. $this->empLocations[4]->setLocationName('Colombo');
  87. $this->empLocations[5] = new EmpLocation(12, 'LOC003');
  88. $this->empLocations[5]->setLocationName('Matara');
  89. foreach ($this->empLocations as $loc) {
  90. $this->_assignLocation($loc);
  91. }
  92. UniqueIDGenerator::getInstance()->resetIDs();
  93. }
  94. /**
  95. * Tears down the fixture, removed database entries created during test.
  96. *
  97. * @access protected
  98. */
  99. protected function tearDown() {
  100. $this->_deleteTables();
  101. UniqueIDGenerator::getInstance()->resetIDs();
  102. }
  103. /**
  104. * Delete data created during test
  105. */
  106. private function _deleteTables() {
  107. $this->_runQuery("TRUNCATE TABLE `hs_hr_emp_locations`");
  108. $this->_runQuery("TRUNCATE TABLE `hs_hr_location`");
  109. $this->_runQuery("TRUNCATE TABLE `hs_hr_employee`");
  110. }
  111. /**
  112. * Test case for save function
  113. */
  114. public function testSave() {
  115. // Invalid emp number
  116. $empLoc = new EmpLocation('a1', 'LOC001');
  117. try {
  118. $empLoc->save();
  119. $this->fail("Exception expected");
  120. } catch (EmpLocationException $e) {
  121. $this->assertEquals(EmpLocationException::INVALID_PARAMETER, $e->getCode());
  122. }
  123. // Invalid location code
  124. $empLoc = new EmpLocation(11, 'LOCX001');
  125. try {
  126. $empLoc->save();
  127. $this->fail("Exception expected");
  128. } catch (EmpLocationException $e) {
  129. $this->assertEquals(EmpLocationException::INVALID_PARAMETER, $e->getCode());
  130. }
  131. // Emp Number not belonging to any employee
  132. $this->_clearError();
  133. set_error_handler(array($this, 'errorHandler'));
  134. $empLoc = new EmpLocation(112, 'LOC011');
  135. try {
  136. $empLoc->save();
  137. $this->fail("Exception expected");
  138. } catch (EmpLocationException $e) {
  139. $this->assertEquals(EmpLocationException::DB_ERROR, $e->getCode());
  140. }
  141. restore_error_handler();
  142. $this->assertNotNull($this->errorLevel);
  143. // Location code not belonging to any location
  144. $this->_clearError();
  145. set_error_handler(array($this, 'errorHandler'));
  146. $empLoc = new EmpLocation(11, 'LOC011');
  147. try {
  148. $empLoc->save();
  149. $this->fail("Exception expected");
  150. } catch (EmpLocationException $e) {
  151. $this->assertEquals(EmpLocationException::DB_ERROR, $e->getCode());
  152. }
  153. restore_error_handler();
  154. $this->assertNotNull($this->errorLevel);
  155. // Emp Number and location ok
  156. $this->assertEquals(0, $this->_getNumRows('emp_number = 13'));
  157. $empLoc = new EmpLocation(13, 'LOC002');
  158. $empLoc->save();
  159. $this->assertEquals(1, $this->_getNumRows('emp_number = 13'));
  160. // Reassign same location
  161. $empLoc = new EmpLocation(13, 'LOC002');
  162. $empLoc->save();
  163. $this->assertEquals(1, $this->_getNumRows('emp_number = 13'));
  164. // Assign different location
  165. $empLoc = new EmpLocation(13, 'LOC004');
  166. $empLoc->save();
  167. $this->assertEquals(2, $this->_getNumRows('emp_number = 13'));
  168. }
  169. /**
  170. * Test case for delete function
  171. */
  172. public function testDelete() {
  173. // Invalid emp number
  174. $empLoc = new EmpLocation('a1', 'LOC001');
  175. try {
  176. $empLoc->delete();
  177. $this->fail("Exception expected");
  178. } catch (EmpLocationException $e) {
  179. $this->assertEquals(EmpLocationException::INVALID_PARAMETER, $e->getCode());
  180. }
  181. // Invalid location code
  182. $empLoc = new EmpLocation(11, 'LOCX001');
  183. try {
  184. $empLoc->delete();
  185. $this->fail("Exception expected");
  186. } catch (EmpLocationException $e) {
  187. $this->assertEquals(EmpLocationException::INVALID_PARAMETER, $e->getCode());
  188. }
  189. // Not assigned location
  190. $this->assertEquals(0, $this->_getNumRows('emp_number = 13'));
  191. $empLoc = new EmpLocation(13, 'LOC002');
  192. $empLoc->delete();
  193. $this->assertEquals(0, $this->_getNumRows('emp_number = 13'));
  194. $this->assertEquals(2, $this->_getNumRows('emp_number = 12'));
  195. $empLoc = new EmpLocation(12, 'LOC004');
  196. $empLoc->delete();
  197. $this->assertEquals(2, $this->_getNumRows('emp_number = 12'));
  198. // Emp Number and location ok
  199. $empLoc = new EmpLocation(12, 'LOC002');
  200. $empLoc->delete();
  201. $this->assertEquals(1, $this->_getNumRows('emp_number = 12'));
  202. $empLoc = new EmpLocation(12, 'LOC003');
  203. $empLoc->delete();
  204. $this->assertEquals(0, $this->_getNumRows('emp_number = 12'));
  205. }
  206. /**
  207. * Test case for getEmpLocations function
  208. */
  209. public function testGetEmpLocations() {
  210. // Invalid emp number
  211. try {
  212. $list = EmpLocation::getEmpLocations('a1');
  213. $this->fail("Exception expected");
  214. } catch (EmpLocationException $e) {
  215. $this->assertEquals(EmpLocationException::INVALID_PARAMETER, $e->getCode());
  216. }
  217. // Emp Number not belonging to any employee
  218. $list = EmpLocation::getEmpLocations(111);
  219. $this->assertTrue(is_array($list));
  220. $this->assertEquals(0, count($list));
  221. // employee without any locations assigned
  222. $list = EmpLocation::getEmpLocations(13);
  223. $this->assertTrue(is_array($list));
  224. $this->assertEquals(0, count($list));
  225. // employee with 2 locations assigned
  226. $list = EmpLocation::getEmpLocations(12);
  227. $this->assertTrue(is_array($list));
  228. $this->assertEquals(2, count($list));
  229. $this->_compareEmpLocations(array($this->empLocations[4], $this->empLocations[5]), $list);
  230. // employee with all location assigned
  231. $list = EmpLocation::getEmpLocations(11);
  232. $this->assertTrue(is_array($list));
  233. $this->assertEquals(4, count($list));
  234. $this->_compareEmpLocations(array($this->empLocations[0], $this->empLocations[1], $this->empLocations[2], $this->empLocations[3]), $list);
  235. }
  236. /**
  237. * Test case for getUnassignedLocations function
  238. */
  239. public function testGetUnassignedLocations() {
  240. // Invalid emp number
  241. try {
  242. $list = EmpLocation::getUnassignedLocations('a1');
  243. $this->fail("Exception expected");
  244. } catch (EmpLocationException $e) {
  245. $this->assertEquals(EmpLocationException::INVALID_PARAMETER, $e->getCode());
  246. }
  247. // Emp Number not belonging to any employee
  248. $list = EmpLocation::getUnassignedLocations(111);
  249. $this->assertTrue(is_array($list));
  250. $this->assertEquals(4, count($list));
  251. $this->_compareLocations($this->locations, $list);
  252. // employee without any locations assigned
  253. $list = EmpLocation::getUnassignedLocations(13);
  254. $this->assertTrue(is_array($list));
  255. $this->assertEquals(4, count($list));
  256. $this->_compareLocations($this->locations, $list);
  257. // employee with 2 locations assigned
  258. $list = EmpLocation::getUnassignedLocations(12);
  259. $this->assertTrue(is_array($list));
  260. $this->assertEquals(2, count($list));
  261. $this->_compareLocations(array($this->locations[1], $this->locations[3]), $list);
  262. // employee with all location assigned
  263. $list = EmpLocation::getUnassignedLocations(11);
  264. $this->assertTrue(is_array($list));
  265. $this->assertEquals(0, count($list));
  266. }
  267. /**
  268. * Returns the number of rows in the hs_hr_emp_locations table
  269. *
  270. * @param string $where where clause
  271. * @return int number of rows
  272. */
  273. private function _getNumRows($where = null) {
  274. $sql = "SELECT COUNT(*) FROM hs_hr_emp_locations";
  275. if (!empty($where)) {
  276. $sql .= " WHERE " . $where;
  277. }
  278. $result = mysql_query($sql);
  279. $row = mysql_fetch_array($result, MYSQL_NUM);
  280. $count = $row[0];
  281. return $count;
  282. }
  283. /**
  284. * Compares two arrays EmpLocation objects
  285. *
  286. * @param array $expected Expected
  287. * @param array $result Result
  288. */
  289. private function _compareEmpLocations($expected, $result) {
  290. $this->assertEquals(count($expected), count($result));
  291. $i = 0;
  292. foreach ($result as $empLocation) {
  293. $this->assertTrue($empLocation instanceof EmpLocation, "Should return EmpLocation objects");
  294. $this->assertEquals($expected[$i], $empLocation);
  295. $this->assertEquals($expected[$i]->getLocationName(), $empLocation->getLocationName());
  296. $i++;
  297. }
  298. }
  299. /**
  300. * Compares two arrays of Locations
  301. *
  302. * @param array $expected Expected
  303. * @param array $result Result
  304. */
  305. private function _compareLocations($expected, $result) {
  306. $this->assertEquals(count($expected), count($result));
  307. $i = 0;
  308. foreach ($result as $location) {
  309. $this->assertTrue(is_array($location));
  310. $this->assertEquals(2, count($location));
  311. $this->assertTrue(is_array($expected[$i]));
  312. $this->assertEquals(2, count($expected[$i]));
  313. $this->assertEquals($expected[$i][0], $location[0]);
  314. $this->assertEquals($expected[$i][1], $location[1]);
  315. $i++;
  316. }
  317. }
  318. /**
  319. * Insert given location into the database
  320. */
  321. private function _insertLocation($code, $name, $country, $address, $zip) {
  322. $sql = sprintf("INSERT INTO hs_hr_location(loc_code, loc_name, loc_country, loc_add, loc_zip) " .
  323. "VALUES('%s', '%s', '%s', '%s', '%s')",
  324. $code, $name, $country, $address, $zip);
  325. $this->_runQuery($sql);
  326. }
  327. /**
  328. * Assign Location
  329. *
  330. * @param EmpLocation $empLocation Employee location to assign
  331. */
  332. private function _assignLocation($empLocation) {
  333. $sql = sprintf("INSERT INTO hs_hr_emp_locations(emp_number, loc_code) " .
  334. "VALUES(%d, '%s')",
  335. $empLocation->getEmpNumber(), $empLocation->getLocation());
  336. $this->_runQuery($sql);
  337. }
  338. /**
  339. * Run given sql query, checking the return value
  340. */
  341. private function _runQuery($sql) {
  342. $this->assertTrue(mysql_query($sql), mysql_error() . ' SQL=' . $sql);
  343. }
  344. public function errorHandler($errlevel, $errstr, $errfile='', $errline='', $errcontext=''){
  345. $this->errorLevel = $errlevel;
  346. $this->errorStr = $errstr;
  347. }
  348. private function _clearError() {
  349. $this->errorLevel = null;
  350. $this->errorStr = null;
  351. }
  352. }
  353. // Call EmpLocationTest::main() if this source file is executed directly.
  354. if (PHPUnit_MAIN_METHOD == "EmpLocationTest::main") {
  355. EmpLocationTest::main();
  356. }
  357. ?>