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

/lib/models/eimadmin/JobSpecTest.php

https://bitbucket.org/wildanm/orangehrm
PHP | 472 lines | 258 code | 81 blank | 133 comment | 11 complexity | 4eab54a25cd1ec27441a213d49f21df0 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 JobSpecTest::main() if this source file is executed directly.
  21. if (!defined("PHPUnit_MAIN_METHOD")) {
  22. define("PHPUnit_MAIN_METHOD", "JobSpecTest::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/eimadmin/JobSpec.php";
  30. require_once ROOT_PATH."/lib/common/UniqueIDGenerator.php";
  31. /**
  32. * Test class for JobSpec
  33. */
  34. class JobSpecTest extends PHPUnit_Framework_TestCase {
  35. private $jobSpecs;
  36. /**
  37. * Runs the test methods of this class.
  38. *
  39. * @access public
  40. * @static
  41. */
  42. public static function main() {
  43. require_once "PHPUnit/TextUI/TestRunner.php";
  44. $suite = new PHPUnit_Framework_TestSuite("ProjectSpecTest");
  45. $result = PHPUnit_TextUI_TestRunner::run($suite);
  46. }
  47. /**
  48. * Sets up the fixture, making sure table is empty and creating database
  49. * entries needed during test.
  50. *
  51. * @access protected
  52. */
  53. protected function setUp() {
  54. $conf = new Conf();
  55. $this->connection = mysql_connect($conf->dbhost.":".$conf->dbport, $conf->dbuser, $conf->dbpass);
  56. mysql_select_db($conf->dbname);
  57. mysql_query("TRUNCATE TABLE `hs_hr_job_spec`");
  58. // Insert data for tests
  59. $this->jobSpecs[1] = $this->_getJobSpec(1, 'Job Spec 1', 'desc for job spec 1', 'Duties, dutiess 1 etc.');
  60. $this->jobSpecs[2] = $this->_getJobSpec(2, 'Job Spec 2', 'xa for job spec 2', 'Duties, dutiess 2 etc.');
  61. $this->jobSpecs[3] = $this->_getJobSpec(3, 'Job Spec 3', 'qb for job spec 3', 'Duties, dutiess 3 etc.');
  62. $this->jobSpecs[4] = $this->_getJobSpec(4, 'Job Spec 4', 'dd for job spec 4', 'Duties, dutiess 4 etc.');
  63. $this->_createJobSpecs($this->jobSpecs);
  64. UniqueIDGenerator::getInstance()->resetIDs();
  65. }
  66. /**
  67. * Tears down the fixture, removed database entries created during test.
  68. *
  69. * @access protected
  70. */
  71. protected function tearDown() {
  72. mysql_query("TRUNCATE TABLE `hs_hr_job_spec`");
  73. UniqueIDGenerator::getInstance()->resetIDs();
  74. }
  75. /**
  76. * test the JobSpec delete function.
  77. */
  78. public function testDelete() {
  79. $before = $this->_getNumRows();
  80. // invalid params
  81. try {
  82. JobSpec::delete(34);
  83. $this->fail("Exception not thrown");
  84. } catch (JobSpecException $e) {
  85. }
  86. // invalid params
  87. try {
  88. JobSpec::delete(array(1, 'w', 12));
  89. $this->fail("Exception not thrown");
  90. } catch (JobSpecException $e) {
  91. }
  92. // empty array
  93. $res = JobSpec::delete(array());
  94. $this->assertEquals(0, $res);
  95. // no matches
  96. $res = JobSpec::delete(array(12, 22));
  97. $this->assertEquals(0, $res);
  98. // one match
  99. $res = JobSpec::delete(array(1, 21));
  100. $this->assertEquals(1, $res);
  101. $this->assertEquals(1, $before - $this->_getNumRows());
  102. $before = $this->_getNumRows();
  103. // one more the rest
  104. $res = JobSpec::delete(array(3));
  105. $this->assertEquals(1, $res);
  106. $this->assertEquals(1, $before - $this->_getNumRows());
  107. $before = $this->_getNumRows();
  108. // rest
  109. $res = JobSpec::delete(array(4, 2));
  110. $this->assertEquals(2, $res);
  111. $this->assertEquals(2, $before - $this->_getNumRows());
  112. }
  113. /**
  114. * Test the save function
  115. */
  116. public function testSave() {
  117. // no name defined
  118. $before = $this->_getNumRows();
  119. $spec = $this->_getJobSpec(null, null, 'Desc1', 'teach');
  120. try {
  121. $spec->save();
  122. $this->fail('Should throw exception');
  123. } catch (JobSpecException $e) {
  124. }
  125. $this->assertEquals($before, $this->_getNumRows());
  126. // new
  127. $before = $this->_getNumRows();
  128. $spec = $this->_getJobSpec(null, 'A test Spec', 'Desc1', 'teach');
  129. $id = $spec->save();
  130. $this->assertEquals(($before + 1), $this->_getNumRows());
  131. $this->assertEquals(1, $this->_getNumRows("jobspec_name = 'A test Spec' AND jobspec_desc = 'Desc1' AND jobspec_duties='teach'"));
  132. // update
  133. $before = $this->_getNumRows();
  134. $spec = $this->_getJobSpec(1, 'XYZ', 'AAA', 'bbb');
  135. $id = $spec->save();
  136. $this->assertEquals(1, $id);
  137. $this->assertEquals($before, $this->_getNumRows());
  138. $this->assertEquals(1, $this->_getNumRows("jobspec_name = 'XYZ' AND jobspec_desc = 'AAA' AND jobspec_duties='bbb'"));
  139. // update without name
  140. $before = $this->_getNumRows();
  141. $spec = $this->_getJobSpec(2, null, 'AAA', 'bbb');
  142. try {
  143. $spec->save();
  144. $this->fail('Should throw exception');
  145. } catch (JobSpecException $e) {
  146. }
  147. $this->assertEquals($before, $this->_getNumRows());
  148. }
  149. /**
  150. * Test count method
  151. */
  152. public function testCount() {
  153. // Count all
  154. $count = JobSpec::getCount();
  155. $this->assertEquals(4, $count);
  156. // Match of ID
  157. $count = JobSpec::getCount(2, 0);
  158. $this->assertEquals(1, $count);
  159. // ID - no match
  160. $count = JobSpec::getCount(21, 0);
  161. $this->assertEquals(0, $count);
  162. // no match
  163. $count = JobSpec::getCount('XYZ', 1);
  164. $this->assertEquals(0, $count);
  165. // Partial match of name
  166. $count = JobSpec::getCount('Job', 1);
  167. $this->assertEquals(4, $count);
  168. // partial match of desc
  169. $count = JobSpec::getCount('xa', 2);
  170. $this->assertEquals(1, $count);
  171. // Full match of name
  172. $count = JobSpec::getCount('Job Spec 2', 1);
  173. $this->assertEquals(1, $count);
  174. }
  175. /**
  176. * Test the getJobSpec function
  177. */
  178. public function testGetJobSpec() {
  179. // unknown id
  180. $spec = JobSpec::getJobSpec(383);
  181. $this->assertNull($spec);
  182. // invalid id
  183. try {
  184. $spec = JobSpec::getJobSpec('7da');
  185. $this->fail('Should throw exception');
  186. } catch (JobSpecException $e) {
  187. }
  188. // available spec
  189. $spec = JobSpec::getJobSpec(2);
  190. $this->assertNotNull($spec);
  191. $this->assertTrue($this->jobSpecs[2] == $spec);
  192. }
  193. /**
  194. * Test the getListForView function
  195. */
  196. public function testGetListForView() {
  197. // Get all
  198. $list = JobSpec::getListForView();
  199. $this->assertTrue(is_array($list));
  200. $this->assertEquals(4, count($list));
  201. $this->_compareSpecsWithOrder($this->jobSpecs, $list);
  202. // Get all in reverse order by name
  203. $list = JobSpec::getListForView(0, '', -1, 1, 'DESC');
  204. $this->assertTrue(is_array($list));
  205. $this->assertEquals(4, count($list));
  206. $expected = array($this->jobSpecs[4],$this->jobSpecs[3],$this->jobSpecs[2],$this->jobSpecs[1]);
  207. $this->_compareSpecsWithOrder($expected, $list);
  208. // Search by name with exact match
  209. $list = JobSpec::getListForView(0, 'Job Spec 3', 1, 1, 'DESC');
  210. $this->assertTrue(is_array($list));
  211. $this->assertEquals(1, count($list));
  212. $expected = array($this->jobSpecs[3]);
  213. $this->_compareSpecsWithOrder($expected, $list);
  214. // Search by name with multiple matches
  215. $list = JobSpec::getListForView(0, 'Job Spec', 1, 1, 'DESC');
  216. $this->assertTrue(is_array($list));
  217. $this->assertEquals(4, count($list));
  218. $expected = array($this->jobSpecs[4],$this->jobSpecs[3],$this->jobSpecs[2],$this->jobSpecs[1]);
  219. $this->_compareSpecsWithOrder($expected, $list);
  220. // Search by description with one match
  221. $list = JobSpec::getListForView(0, 'qb for job', 2, 1, 'ASC');
  222. $this->assertTrue(is_array($list));
  223. $this->assertEquals(1, count($list));
  224. $expected = array($this->jobSpecs[3]);
  225. $this->_compareSpecsWithOrder($expected, $list);
  226. // Search by id with one match
  227. $list = JobSpec::getListForView(0, '3', 0, 0, 'ASC');
  228. $this->assertTrue(is_array($list));
  229. $this->assertEquals(1, count($list));
  230. $expected = array($this->jobSpecs[3]);
  231. $this->_compareSpecsWithOrder($expected, $list);
  232. // when no job specs available
  233. $this->assertTrue(mysql_query('DELETE from hs_hr_job_spec'), mysql_error());
  234. $list = JobSpec::getAll();
  235. $this->assertTrue(is_array($list));
  236. $this->assertEquals(0, count($list));
  237. // Insert data for tests
  238. for ($i=1; $i<251; $i++) {
  239. $inc = 100 + $i;
  240. if ($i % 2 == 0) {
  241. $desc = "Even ";
  242. $even = true;
  243. } else {
  244. $desc = "Odd ";
  245. $even = false;
  246. }
  247. $spec = $this->_getJobSpec($i, "Spec-$inc", "$desc-$inc", "Duties");
  248. $specs[] = $spec;
  249. if ($even) {
  250. $evenSpecs[] = $spec;
  251. } else {
  252. $oddSpecs[] = $spec;
  253. }
  254. }
  255. $this->_createJobSpecs($specs);
  256. $sysConf = new sysConf();
  257. $pageSize = $sysConf->itemsPerPage;
  258. // check paging - without search
  259. // page 1
  260. $list = JobSpec::getListForView(1, '', -1, 1, 'ASC');
  261. $this->assertTrue(is_array($list));
  262. $this->assertEquals($pageSize, count($list));
  263. $pages = array_chunk($specs, $pageSize);
  264. $this->_compareSpecsWithOrder($pages[0], $list);
  265. // page 3
  266. $list = JobSpec::getListForView(3, '', -1, 1, 'ASC');
  267. $this->assertTrue(is_array($list));
  268. $this->assertEquals($pageSize, count($list));
  269. $this->_compareSpecsWithOrder($pages[2], $list);
  270. // paging with search
  271. // Separate even rows to pages
  272. $pages = array_chunk($evenSpecs, $pageSize);
  273. // Search only for even rows and check page 1
  274. $list = JobSpec::getListForView(1, 'Even', 2, 1, 'ASC');
  275. $this->assertTrue(is_array($list));
  276. $this->assertEquals(count($pages[0]), count($list));
  277. $this->_compareSpecsWithOrder($pages[0], $list);
  278. $list = JobSpec::getListForView(3, 'Even', 2, 1, 'ASC');
  279. $this->assertTrue(is_array($list));
  280. $this->assertEquals(count($pages[2]), count($list));
  281. $this->_compareSpecsWithOrder($pages[2], $list);
  282. }
  283. /**
  284. * test the getAll function
  285. */
  286. public function testGetAll() {
  287. // Get all
  288. $list = JobSpec::getAll();
  289. $this->assertTrue(is_array($list));
  290. $this->assertEquals(4, count($list));
  291. $this->_compareSpecs($this->jobSpecs, $list);
  292. // when no job specs available
  293. $this->assertTrue(mysql_query('DELETE from hs_hr_job_spec'), mysql_error());
  294. $list = JobSpec::getAll();
  295. $this->assertTrue(is_array($list));
  296. $this->assertEquals(0, count($list));
  297. }
  298. /**
  299. * Returns the number of rows in the hs_hr_job_spec table
  300. *
  301. * @param string $where where clause
  302. * @return int number of rows
  303. */
  304. private function _getNumRows($where = null) {
  305. $sql = "SELECT COUNT(*) FROM hs_hr_job_spec";
  306. if (!empty($where)) {
  307. $sql .= " WHERE " . $where;
  308. }
  309. $result = mysql_query($sql);
  310. $row = mysql_fetch_array($result, MYSQL_NUM);
  311. $count = $row[0];
  312. return $count;
  313. }
  314. /**
  315. * Compares two array of JobSpec objects verifing they contain the same
  316. * objects, without considering the order
  317. *
  318. * Objects in first array should be indexed by their id's
  319. *
  320. * @param array $expected Expected
  321. * @param array $result Result
  322. */
  323. private function _compareSpecs($expected, $result) {
  324. $this->assertEquals(count($expected), count($result));
  325. foreach ($result as $spec) {
  326. $this->assertTrue($spec instanceof JobSpec, "Should return JobSpec objects");
  327. $id = $spec->getId();
  328. $this->assertEquals($expected[$id], $spec);
  329. }
  330. }
  331. /**
  332. * Compares two array of JobSpec objects verifing they contain the same
  333. * objects and considering the order
  334. *
  335. *
  336. * @param array $expected Expected
  337. * @param array $result Result
  338. */
  339. private function _compareSpecsWithOrder($expected, $result) {
  340. $this->assertEquals(count($expected), count($result));
  341. $i = 0;
  342. foreach ($expected as $spec) {
  343. $this->assertEquals($spec->getId(), $result[$i][0]);
  344. $this->assertEquals($spec->getName(), $result[$i][1]);
  345. $this->assertEquals($spec->getDesc(), $result[$i][2]);
  346. $i++;
  347. }
  348. }
  349. /**
  350. * Checks that the attributes of the Job Spec object and the database row match.
  351. *
  352. * @param JobSpec $spec
  353. * @param array $row
  354. */
  355. private function _checkRow($spec, $row) {
  356. $this->assertEquals($spec->getName(), $row['jobspec_name'], "Name not correct");
  357. $this->assertEquals($spec->getDescription(), $row['jobspec_desc'], "Description not correct");
  358. $this->assertEquals($spec->getId(), $row['jobspec_id'], "ID not correct");
  359. $this->assertEquals($spec->getDuties(), $row['jobspec_duties'], "Duties not correct");
  360. }
  361. /**
  362. * Create a JobSpec object with the passed parameters
  363. */
  364. private function _getJobSpec($id, $name, $desc, $duties) {
  365. $spec = new JobSpec($id);
  366. $spec->setName($name);
  367. $spec->setDesc($desc);
  368. $spec->setDuties($duties);
  369. return $spec;
  370. }
  371. /**
  372. * Saves the given JobSpec objects in the databas
  373. *
  374. * @param array $specs Array of JobSpec objects to save.
  375. */
  376. private function _createJobSpecs($specs) {
  377. foreach ($specs as $spec) {
  378. $sql = sprintf("INSERT INTO hs_hr_job_spec(jobspec_id, jobspec_name, jobspec_desc, jobspec_duties) " .
  379. "VALUES(%d, '%s', '%s', '%s')",
  380. $spec->getId(), $spec->getName(), $spec->getDesc(),
  381. $spec->getDuties());
  382. $this->assertTrue(mysql_query($sql), mysql_error());
  383. }
  384. UniqueIDGenerator::getInstance()->initTable();
  385. }
  386. }
  387. // Call JobSpecTest::main() if this source file is executed directly.
  388. if (PHPUnit_MAIN_METHOD == "JobSpecTest::main") {
  389. JobSpecTest::main();
  390. }
  391. ?>