PageRenderTime 132ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/models/eimadmin/JobTitleTest.php

https://bitbucket.org/wildanm/orangehrm
PHP | 214 lines | 119 code | 32 blank | 63 comment | 4 complexity | 6b667db0e0ee8f8ccdc49907a41eb36c 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 JobTitleTest::main() if this source file is executed directly.
  21. if (!defined("PHPUnit_MAIN_METHOD")) {
  22. define("PHPUnit_MAIN_METHOD", "JobTitleTest::main");
  23. }
  24. require_once "PHPUnit/Framework/TestCase.php";
  25. require_once "PHPUnit/Framework/TestSuite.php";
  26. require_once "testConf.php";
  27. require_once 'JobTitle.php';
  28. require_once ROOT_PATH . '/lib/common/UniqueIDGenerator.php';
  29. /**
  30. * Test class for JobTitle.
  31. */
  32. class JobTitleTest extends PHPUnit_Framework_TestCase {
  33. /**
  34. * Runs the test methods of this class.
  35. *
  36. * @access public
  37. * @static
  38. */
  39. public static function main() {
  40. require_once "PHPUnit/TextUI/TestRunner.php";
  41. $suite = new PHPUnit_Framework_TestSuite("JobTitleTest");
  42. $result = PHPUnit_TextUI_TestRunner::run($suite);
  43. }
  44. /**
  45. * Sets up the fixture, for example, open a network connection.
  46. * This method is called before a test is executed.
  47. *
  48. * @access protected
  49. */
  50. protected function setUp() {
  51. $conf = new Conf();
  52. mysql_connect($conf->dbhost.":".$conf->dbport, $conf->dbuser, $conf->dbpass);
  53. mysql_select_db($conf->dbname);
  54. $this->_deleteTables();
  55. $this->_runQuery("INSERT INTO hs_hr_job_spec(jobspec_id, jobspec_name, jobspec_desc, jobspec_duties) " .
  56. "VALUES(1, 'Spec 1', 'Desc 1', 'duties 1')");
  57. $this->_runQuery("INSERT INTO hs_hr_job_spec(jobspec_id, jobspec_name, jobspec_desc, jobspec_duties) " .
  58. "VALUES(2, 'Spec 2', 'Desc 2', 'duties 2')");
  59. $this->_runQuery("INSERT INTO hs_pr_salary_grade(sal_grd_code, sal_grd_name) " .
  60. "VALUES('SAL001', 'Director grade')");
  61. $this->_runQuery("INSERT INTO hs_pr_salary_grade(sal_grd_code, sal_grd_name) " .
  62. "VALUES('SAL002', 'Other grade')");
  63. $this->_runQuery("INSERT INTO hs_hr_job_title(jobtit_code, jobtit_name, jobtit_desc,jobtit_comm, " .
  64. "sal_grd_code, jobspec_id) " .
  65. "VALUES('JOB001', 'Driver', 'Driver Desc', 'Driver comments', 'SAL002', null)");
  66. $this->_runQuery("INSERT INTO hs_hr_job_title(jobtit_code, jobtit_name, jobtit_desc,jobtit_comm, " .
  67. "sal_grd_code, jobspec_id) " .
  68. "VALUES('JOB002', 'Typist', 'Typist Desc', 'Typist comments', 'SAL002', 1)");
  69. UniqueIDGenerator::getInstance()->initTable();
  70. }
  71. /**
  72. * Tears down the fixture, for example, close a network connection.
  73. * This method is called after a test is executed.
  74. *
  75. * @access protected
  76. */
  77. protected function tearDown() {
  78. $this->_deleteTables();
  79. UniqueIDGenerator::getInstance()->initTable();
  80. }
  81. private function _deleteTables() {
  82. $this->_runQuery("TRUNCATE TABLE `hs_hr_job_title`");
  83. $this->_runQuery("TRUNCATE TABLE `hs_pr_salary_grade`");
  84. $this->_runQuery("TRUNCATE TABLE `hs_hr_job_spec`");
  85. }
  86. /**
  87. * Test case for addJobTitles() and updateJobTitles()
  88. */
  89. public function testAddUpdateJobTitles() {
  90. $before = $this->_getCount();
  91. // Add job title without spec
  92. $jobTitle = new JobTitle();
  93. $jobTitle->setJobName('Director');
  94. $jobTitle->setJobDesc('Director Description');
  95. $jobTitle->setJobComm('Director comments');
  96. $jobTitle->setJobSalGrd('SAL001');
  97. $jobTitle->addJobTitles();
  98. $id = $jobTitle->getJobId();
  99. $this->assertEquals($before + 1, $this->_getCount());
  100. $this->assertEquals(1, $this->_getCount("jobtit_code='$id' AND jobtit_name='Director'" .
  101. "AND jobtit_desc = 'Director Description' AND jobtit_comm = 'Director comments' AND " .
  102. "sal_grd_code = 'SAL001' AND jobspec_id IS NULL"));
  103. // Update job title values (specify job spec id)
  104. $jobTitle->setJobSpecId('1');
  105. $jobTitle->updateJobTitles();
  106. $this->assertEquals($before + 1, $this->_getCount());
  107. $this->assertEquals(1, $this->_getCount("jobtit_code='$id' AND jobtit_name='Director'" .
  108. "AND jobtit_desc = 'Director Description' AND jobtit_comm = 'Director comments' AND " .
  109. "sal_grd_code = 'SAL001' AND jobspec_id = 1"));
  110. // Add job title with spec
  111. $jobTitle = new JobTitle();
  112. $jobTitle->setJobName('Manager');
  113. $jobTitle->setJobDesc('Manager Description');
  114. $jobTitle->setJobComm('Manager comments');
  115. $jobTitle->setJobSalGrd('SAL001');
  116. $jobTitle->setJobSpecId('1');
  117. $jobTitle->addJobTitles();
  118. $id = $jobTitle->getJobId();
  119. $this->assertEquals($before + 2, $this->_getCount());
  120. $this->assertEquals(1, $this->_getCount("jobtit_code='$id' AND jobtit_name='Manager'" .
  121. "AND jobtit_desc = 'Manager Description' AND jobtit_comm = 'Manager comments' AND " .
  122. "sal_grd_code = 'SAL001' AND jobspec_id = 1"));
  123. // Update job title values (clear job spec id)
  124. $jobTitle->setJobSpecId(null);
  125. $jobTitle->updateJobTitles();
  126. $this->assertEquals($before + 2, $this->_getCount());
  127. $this->assertEquals(1, $this->_getCount("jobtit_code='$id' AND jobtit_name='Manager'" .
  128. "AND jobtit_desc = 'Manager Description' AND jobtit_comm = 'Manager comments' AND " .
  129. "sal_grd_code = 'SAL001' AND jobspec_id IS NULL"));
  130. }
  131. /**
  132. * Test case for filterJobTitles()
  133. */
  134. public function testFilterJobTitles() {
  135. // retrieve job title with job spec defined
  136. $jobTitle = new JobTitle();
  137. $result = $jobTitle->filterJobTitles('JOB002');
  138. $this->_validateJobTitle($result, 'JOB002', 'Typist', 'Typist Desc', 'Typist comments', 'SAL002', 1);
  139. // retrieve job title without job spec defined
  140. $result = $jobTitle->filterJobTitles('JOB001');
  141. $this->_validateJobTitle($result, 'JOB001', 'Driver', 'Driver Desc', 'Driver comments', 'SAL002', null);
  142. }
  143. /**
  144. * Validate job title
  145. * asserts if not valid
  146. */
  147. private function _validateJobTitle($result, $id, $name, $desc, $comments, $salaryCode, $jobSpecId) {
  148. $this->assertEquals(1, count($result));
  149. $this->assertEquals(6, count($result[0]));
  150. $this->assertEquals($result[0][0], $id);
  151. $this->assertEquals($result[0][1], $name);
  152. $this->assertEquals($result[0][2], $desc);
  153. $this->assertEquals($result[0][3], $comments);
  154. $this->assertEquals($result[0][4], $salaryCode);
  155. $this->assertEquals($result[0][5], $jobSpecId);
  156. }
  157. /**
  158. * Return the count from job title table (with given where statement)
  159. */
  160. private function _getCount($whereClause = null) {
  161. $sql = 'SELECT COUNT(*) FROM hs_hr_job_title';
  162. if (!empty($whereClause)) {
  163. $sql .= ' WHERE ' . $whereClause;
  164. }
  165. $result = mysql_query($sql);
  166. $this->assertTrue($result !== false);
  167. $row = mysql_fetch_array($result, MYSQL_NUM);
  168. $count = $row[0];
  169. return $count;
  170. }
  171. /**
  172. * Run given sql query, checking the return value
  173. */
  174. private function _runQuery($sql) {
  175. $this->assertTrue(mysql_query($sql), mysql_error());
  176. }
  177. }
  178. // Call JobTitleTest::main() if this source file is executed directly.
  179. if (PHPUnit_MAIN_METHOD == "JobTitleTest::main") {
  180. JobTitleTest::main();
  181. }
  182. ?>