/tests/UnitCheckTests/testTests.php

# · PHP · 447 lines · 235 code · 134 blank · 78 comment · 24 complexity · e67339a8bc7fa700776dbc520ebae4b4 MD5 · raw file

  1. <?php
  2. /**
  3. * This is the tests test file
  4. *
  5. * Copyright (C) 2011 Tom Kaczocha
  6. *
  7. * This file is part of UnitCheck.
  8. *
  9. * UnitCheck is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * UnitCheck is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with UnitCheck. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. require_once('../includes/initialise.php');
  24. // test ensures that the new test name was added
  25. // to the database successfully
  26. function newTestNameAddedTest() {
  27. $database = new UnitCheckDatabase();
  28. $unitCheck = new UnitCheck();
  29. $test = new UnitCheckTest("TEST - New Test Name Added");
  30. $unitCheck->addTest($test);
  31. $result = $database->createFullDatabase('UnitTesting');
  32. if ($result) {
  33. //$finalUnitCheck = new UnitCheck();
  34. //$finalTest = new UnitCheckTest("Test - New User Created");
  35. //$finalUnitCheck->addTest($finalTest);
  36. $database->dropDatabase("UnitTesting");
  37. }
  38. $test->failUnless($testResult = FALSE,
  39. "Error: Failed to Add New Name to Database");
  40. }
  41. // test the successful addition of
  42. // new tests
  43. function addNewTestTest() {
  44. $database = new UnitCheckDatabase();
  45. $unitCheck = new UnitCheck();
  46. $helper = new UnitCheckHelper();
  47. $testResult = 0;
  48. $testName = "Test - New Super Power Added";
  49. $temp = "function NewSuperPowerAddedTest() {\$test = new UnitCheckTest(\"TEST - Blah blah\");}";
  50. $testBody = $database->escapeValue($temp);
  51. $functionName = "NewSuperPowerAddedTest()";
  52. $errorMessage = "Error - New Super Power Not Added";
  53. $testAuthor = "Tom Kaczocha";
  54. $projectID = 1;
  55. $active = 1;
  56. $comments = "This is a test of a test";
  57. //echo ($testBody);
  58. $test = new UnitCheckTest("TEST - New Test Added");
  59. $unitCheck->addTest($test);
  60. $result = $database->createFullDatabase('tests');
  61. if ($result) {
  62. //mysql_select_db('tests', $database->getConnection());
  63. $project = new UnitCheckProject();
  64. $project->createNewProject("UnitTesting");
  65. $tID = $test->addNewTest(
  66. $testName,
  67. $testBody,
  68. $functionName,
  69. $errorMessage,
  70. $testAuthor,
  71. $projectID,
  72. $active,
  73. $comments);
  74. $data = $test->getTestDataSetByID($tID);
  75. //$helper->printArray($data);
  76. if ($data != FALSE) {
  77. if (($data['test_id'] == $tID) &&
  78. ($data['test_name'] == $testName) &&
  79. ($data['project_id'] == 1)) {
  80. $testResult = 1;
  81. }
  82. else {
  83. $testResult = 0;
  84. }
  85. }
  86. else {
  87. $testResult = 0;
  88. }
  89. //$database->dropDatabase('tests');
  90. }
  91. $test->failUnless($testResult,
  92. "Error: Addition of New Test Failed");
  93. }
  94. // test the successful retrieval
  95. // of test dataset
  96. function testDataSetRetrievedTest() {
  97. global $database;
  98. global $unitCheck;
  99. $test = new UnitCheckTest("TEST - Test DataSet Retrieved");
  100. $unitCheck->addTest($test);
  101. $test->failUnless($result,
  102. "Error: Failed to Retrieve Test DataSet");
  103. }
  104. // test the successful retrieval
  105. // of test dataset
  106. function testResultSetRetrievedTest() {
  107. global $database;
  108. global $unitCheck;
  109. $test = new UnitCheckTest("TEST - Test ResultSet Retrieved");
  110. $unitCheck->addTest($test);
  111. $test->failUnless($result,
  112. "Error: Failed to Retrieve Test ResultSet");
  113. }
  114. // test the successful retrieval
  115. // of last test date
  116. function lastTestDateRetrievedTest() {
  117. global $database;
  118. global $unitCheck;
  119. $test = new UnitCheckTest("TEST - Last Test Date Retrieved");
  120. $unitCheck->addTest($test);
  121. $test->failUnless($result,
  122. "Error: Failed to Retrieve Last Test Date");
  123. }
  124. // test the successful addition of test
  125. // dependencies
  126. function addedTestDependenciesTest() {
  127. global $database;
  128. global $unitCheck;
  129. $test = new UnitCheckTest("TEST - Added Test Dependencies");
  130. $unitCheck->addTest($test);
  131. $test->failUnless($result,
  132. "Error: Failed to Add Test Dependencies");
  133. }
  134. // test the successful addition of test
  135. // body
  136. function addedTestBodyTest() {
  137. global $database;
  138. global $unitCheck;
  139. $test = new UnitCheckTest("TEST - Added Test Body");
  140. $unitCheck->addTest($test);
  141. $test->failUnless($result,
  142. "Error: Failed to Add Test Body");
  143. }
  144. // test the successful addition of test
  145. // error message
  146. function addedTestErrorMessageTest() {
  147. global $database;
  148. global $unitCheck;
  149. $test = new UnitCheckTest("TEST - Added Test Error Message");
  150. $unitCheck->addTest($test);
  151. $test->failUnless($result,
  152. "Error: Failed to Add Test Error Message");
  153. }
  154. // test the successful addition of test
  155. // name
  156. function addedTestNameTest() {
  157. global $database;
  158. global $unitCheck;
  159. $test = new UnitCheckTest("TEST - Added Test Name");
  160. $unitCheck->addTest($test);
  161. $test->failUnless($result,
  162. "Error: Failed to Add Test Name");
  163. }
  164. // test the successful addition of test
  165. // project id
  166. function addedTestProjectIDTest() {
  167. global $database;
  168. global $unitCheck;
  169. $test = new UnitCheckTest("TEST - Added Test Project ID");
  170. $unitCheck->addTest($test);
  171. $test->failUnless($result,
  172. "Error: Failed to Add Test Project ID");
  173. }
  174. // test the successful addition of test
  175. // author
  176. function addedTestAuthorTest() {
  177. global $database;
  178. global $unitCheck;
  179. $test = new UnitCheckTest("TEST - Added Test Author");
  180. $unitCheck->addTest($test);
  181. $test->failUnless($result,
  182. "Error: Failed to Add Test Author");
  183. }
  184. // test the successful update of test
  185. // name
  186. function updatedTestNameTest() {
  187. global $database;
  188. global $unitCheck;
  189. $test = new UnitCheckTest("TEST - Test Name Updated");
  190. $unitCheck->addTest($test);
  191. $test->failUnless($result,
  192. "Error: Failed to Update Test Name");
  193. }
  194. // test the successful update of test
  195. // dependencies
  196. function updatedTestDependenciesTest() {
  197. global $database;
  198. global $unitCheck;
  199. $test = new UnitCheckTest("TEST - Test Dependencies Updated");
  200. $unitCheck->addTest($test);
  201. $test->failUnless($result,
  202. "Error: Failed to Update Test Dependencies");
  203. }
  204. // test the successful update of test
  205. // error message
  206. function updatedTestErrorMessageTest() {
  207. global $database;
  208. global $unitCheck;
  209. $test = new UnitCheckTest("TEST - Test Error Message Updated");
  210. $unitCheck->addTest($test);
  211. $test->failUnless($result,
  212. "Error: Failed to Update Test Error Message");
  213. }
  214. // test the successful update of test
  215. // body
  216. function updatedTestBodyTest() {
  217. global $database;
  218. global $unitCheck;
  219. $test = new UnitCheckTest("TEST - Test Body Updated");
  220. $unitCheck->addTest($test);
  221. $test->failUnless($result,
  222. "Error: Failed to Update Test Body");
  223. }
  224. // test the successful update of test
  225. // author
  226. function updatedTestAuthorTest() {
  227. global $database;
  228. global $unitCheck;
  229. $test = new UnitCheckTest("TEST - Test Author Updated");
  230. $unitCheck->addTest($test);
  231. $test->failUnless($result,
  232. "Error: Failed to Update Test Author");
  233. }
  234. // test the successful update of test
  235. // project ID
  236. function updatedTestProjectIDTest() {
  237. global $database;
  238. global $unitCheck;
  239. $test = new UnitCheckTest("TEST - Test Project ID Updated");
  240. $unitCheck->addTest($test);
  241. $test->failUnless($result,
  242. "Error: Failed to Update Test Project ID");
  243. }
  244. // test validates test body according to
  245. // criteria
  246. // criter to be tested for
  247. // * contains the word 'function'
  248. // * number of braces is greater then 0 &
  249. // even number
  250. function validateTestBody() {
  251. $database = new UnitCheckDatabase();
  252. $unitCheck = new UnitCheck();
  253. $testResult = 0;
  254. $test = new UnitCheckTest("TEST - Test Function Body Valid");
  255. $unitCheck->addTest($test);
  256. $temp1 = "function testFunctionBody() {\$test = new UnitCheckTest(\"test name\")}";
  257. $temp2 = "testFunctionBody() {\$test = new UnitCheckTest(\"test name\")}";
  258. $temp3 = "function testFunctionBody() {\$test = new UnitCheckTest(\"test name\")";
  259. $temp4 = "function testFunctionBody {\$test = new UnitCheckTest(\"test name\")}";
  260. $functionBody1 = $database->escapeValue($temp1);
  261. $functionBody2 = $database->escapeValue($temp2);
  262. $functionBody3 = $database->escapeValue($temp3);
  263. $functionBody4 = $database->escapeValue($temp4);
  264. $result1 = $test->validateTestBody($functionBody1);
  265. $result2 = $test->validateTestBody($functionBody2);
  266. $result3 = $test->validateTestBody($functionBody3);
  267. $result4 = $test->validateTestBody($functionBody4);
  268. // echo "<br />Result 1: " . $result1 . "<br />";
  269. // echo "Result 2: " . $result2 . "<br />";
  270. // echo "Result 3: " . $result3 . "<br />";
  271. // echo "Result 4: " . $result4 . "<br />";
  272. if (($result1 == TRUE) && ($result2 == FALSE) && ($result3 == FALSE) && ($result4 == FALSE)) {
  273. $testResult = TRUE;
  274. }
  275. else {
  276. $testResult = FALSE;
  277. }
  278. $test->failUnless($testResult,
  279. "Error: Failed to Validate Function Test Body");
  280. }
  281. // test checks for the successful
  282. // extraction of function name from
  283. // function body
  284. function extractFunctionNameTest() {
  285. global $database;
  286. global $unitCheck;
  287. $testResult = 0;
  288. $test = new UnitCheckTest("TEST - Function Name Retrieved");
  289. $unitCheck->addTest($test);
  290. $temp1 = "function testFunctionBody1() {\$test = new UnitCheckTest(\"test name\")}";
  291. $temp2 = "function testFunctionBody2 () {\$test = new UnitCheckTest(\"test name\")}";
  292. $temp3 = "function testFunctionBody3( ) {\$test = new UnitCheckTest(\"test name\")}";
  293. $functionBody1 = $database->escapeValue($temp1);
  294. $functionBody2 = $database->escapeValue($temp2);
  295. $functionBody3 = $database->escapeValue($temp3);
  296. $fName1 = $test->extractFunctionName($functionBody1);
  297. $fName2 = $test->extractFunctionName($functionBody2);
  298. $fName3 = $test->extractFunctionName($functionBody3);
  299. // echo "Function name is: '" . $fName1 . "'<br />";
  300. // echo "Function name is: '" . $fName2 . "'<br />";
  301. // echo "Function name is: '" . $fName3 . "'<br />";
  302. if (($fName1 == "testFunctionBody1();") &&
  303. ($fName2 == "testFunctionBody2();") &&
  304. ($fName3 == "testFunctionBody3();")) {
  305. $testResult = TRUE;
  306. }
  307. else {
  308. $testResult = 0;
  309. }
  310. $test->failUnless($testResult,
  311. "Error: Failed to Retrieve Function Name");
  312. }
  313. ?>