/test/php_unit_test_framework/text_test_runner.php

https://github.com/slatronica/hyperpublic_php · PHP · 339 lines · 249 code · 28 blank · 62 comment · 0 complexity · 188e81fa7fab5f35e086fb46410dee71 MD5 · raw file

  1. <?php
  2. //////////////////////////////////////////////////////////
  3. ///
  4. /// $Author: edheal $
  5. /// $Date: 2011-04-05 13:41:43 +0100 (Tue, 05 Apr 2011) $
  6. /// $Id: text_test_runner.php 9 2011-04-05 12:41:43Z edheal $
  7. ///
  8. /// \file
  9. /// \brief Runs the test and produces ASCII output.
  10. ///
  11. /// \details
  12. ///
  13. /// This test runner performs the tests and generates ASCII output.
  14. ///
  15. /// \section License
  16. ///
  17. /// A PHP Unit testing framework
  18. ///
  19. /// Copyright (C) 2011 Ed Heal (ed.heal@yahoo.co.uk)
  20. ///
  21. /// This program is free software: you can redistribute it and/or modify
  22. /// it under the terms of the GNU General Public License as published by
  23. /// the Free Software Foundation, either version 3 of the License, or
  24. /// (at your option) any later version.
  25. ///
  26. /// This program is distributed in the hope that it will be useful,
  27. /// but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29. /// GNU General Public License for more details.
  30. ///
  31. /// You should have received a copy of the GNU General Public License
  32. /// along with this program. If not, see <http://www.gnu.org/licenses/>.
  33. ///
  34. //////////////////////////////////////////////////////////
  35. require_once 'xml_test_runner_with_style_sheet.php';
  36. //////////////////////////////////////////////////////////
  37. /// \brief This test runner generates the report in ASCII.
  38. ///
  39. /// The class defines a XSLT style sheet and then
  40. /// uses XMLTestRunnerWithStyleSheet to generate
  41. /// the XML report and apply the style sheet.
  42. //////////////////////////////////////////////////////////
  43. class TextTestRunner extends XMLTestRunnerWithStyleSheet
  44. {
  45. //////////////////////////////////////////////////////////
  46. /// \brief Sets up the style sheet.
  47. ///
  48. /// This constructs the style sheet to create the ASCII
  49. /// report.
  50. //////////////////////////////////////////////////////////
  51. public function __construct()
  52. {
  53. parent::__construct();
  54. $this->styleSheet .= <<<END_OF_STYLE_SHEET
  55. <xsl:output method="text" />
  56. <xsl:template match="/testresults">
  57. <xsl:text>
  58. ******************************************************************************
  59. * *
  60. * TEST REPORT *
  61. * *
  62. ******************************************************************************
  63. +----------------------------------------------------------------------------+
  64. | SUMMARY |
  65. +----------------------------------------------------------------------------+
  66. </xsl:text>
  67. <xsl:for-each select="testcase">
  68. <xsl:text>
  69. Name: </xsl:text><xsl:value-of select="name"/>
  70. <xsl:text>
  71. Test ID: </xsl:text><xsl:value-of select="id"/>
  72. <xsl:text>
  73. Description:
  74. </xsl:text>
  75. <xsl:value-of select="description"/>
  76. <xsl:text>
  77. Test Passed: </xsl:text>
  78. <xsl:if test="passed='Yes'">Passed</xsl:if>
  79. <xsl:if test="passed='No'">Failed</xsl:if>
  80. </xsl:for-each>
  81. <xsl:text>
  82. +----------------------------------------------------------------------------+
  83. | DETAILS |
  84. +----------------------------------------------------------------------------+
  85. </xsl:text>
  86. <xsl:for-each select="testcase">
  87. <xsl:text>
  88. Name: </xsl:text>
  89. <xsl:value-of select="name"/>
  90. <xsl:text>
  91. Test ID: </xsl:text>
  92. <xsl:value-of select="id"/>
  93. <xsl:text>
  94. Test Passed: </xsl:text>
  95. <xsl:if test="passed='Yes'"><xsl:text>Passed</xsl:text></xsl:if>
  96. <xsl:if test="passed='No'"><xsl:text>Failed</xsl:text></xsl:if>
  97. <xsl:text>
  98. TEST EVENTS
  99. -----------
  100. </xsl:text>
  101. <xsl:for-each select="listOfEvents/event">
  102. <xsl:text>
  103. </xsl:text>
  104. <xsl:value-of select="time" />
  105. <xsl:text>
  106. </xsl:text>
  107. <xsl:choose>
  108. <xsl:when test="type='PASS'">
  109. <xsl:text>Test step success
  110. Reason: </xsl:text>
  111. <xsl:value-of select="reason" />
  112. <xsl:text>
  113. Message: </xsl:text>
  114. <xsl:value-of select="message" />
  115. <xsl:text>
  116. Actual value type:
  117. </xsl:text>
  118. <xsl:value-of select="actual/type" />
  119. <xsl:text>
  120. Actual value:
  121. </xsl:text>
  122. <xsl:value-of select="actual/value" />
  123. <xsl:text>
  124. Comparison value type:
  125. </xsl:text>
  126. <xsl:value-of select="comparison/type" />
  127. <xsl:text>
  128. Comparison value:
  129. </xsl:text>
  130. <xsl:value-of select="comparison/value" />
  131. <xsl:text>
  132. File: </xsl:text>
  133. <xsl:value-of select="file" />
  134. <xsl:text> at line </xsl:text>
  135. <xsl:value-of select="line" />
  136. <xsl:text>
  137. </xsl:text>
  138. </xsl:when>
  139. <xsl:when test="type='PASS_MSG'">
  140. <xsl:text>Test step success
  141. Message: </xsl:text>
  142. <xsl:value-of select="message" />
  143. <xsl:text>
  144. File: </xsl:text>
  145. <xsl:value-of select="file" />
  146. <xsl:text> at line </xsl:text>
  147. <xsl:value-of select="line" />
  148. <xsl:text>
  149. </xsl:text>
  150. </xsl:when>
  151. <xsl:when test="type='FAIL'">
  152. <xsl:text>Test step failed
  153. Reason: </xsl:text>
  154. <xsl:value-of select="reason" />
  155. <xsl:text>
  156. Message: </xsl:text>
  157. <xsl:value-of select="message" />
  158. <xsl:text>
  159. Actual value type:
  160. </xsl:text>
  161. <xsl:value-of select="actual/type" />
  162. <xsl:text>
  163. Actual value:
  164. </xsl:text>
  165. <xsl:value-of select="actual/value" />
  166. <xsl:text>
  167. Comparison value type:
  168. </xsl:text>
  169. <xsl:value-of select="comparison/type" />
  170. <xsl:text>
  171. Comparison value:
  172. </xsl:text>
  173. <xsl:value-of select="comparison/value" />
  174. <xsl:text>
  175. File: </xsl:text>
  176. <xsl:value-of select="file" />
  177. <xsl:text> at line </xsl:text>
  178. <xsl:value-of select="line" />
  179. <xsl:text>
  180. </xsl:text>
  181. </xsl:when>
  182. <xsl:when test="type='FAIL_MSG'">
  183. <xsl:text>Test step failed
  184. Message: </xsl:text>
  185. <xsl:value-of select="message" />
  186. <xsl:text>
  187. File: </xsl:text>
  188. <xsl:value-of select="file" />
  189. <xsl:text> at line </xsl:text>
  190. <xsl:value-of select="line" />
  191. <xsl:text>
  192. </xsl:text>
  193. </xsl:when>
  194. <xsl:when test="type='ERROR'">
  195. <xsl:text>Test step in error
  196. Reason: </xsl:text>
  197. <xsl:value-of select="reason" />
  198. <xsl:text>
  199. Message: </xsl:text>
  200. <xsl:value-of select="message" />
  201. <xsl:text>
  202. Actual value type:
  203. </xsl:text>
  204. <xsl:value-of select="actual/type" />
  205. <xsl:text>
  206. Actual value:
  207. </xsl:text>
  208. <xsl:value-of select="actual/value" />
  209. <xsl:text>
  210. Comparison value type:
  211. </xsl:text>
  212. <xsl:value-of select="comparison/type" />
  213. <xsl:text>
  214. Comparison value:
  215. </xsl:text>
  216. <xsl:value-of select="comparison/value" />
  217. <xsl:text>
  218. File: </xsl:text>
  219. <xsl:value-of select="file" />
  220. <xsl:text>at line </xsl:text>
  221. <xsl:value-of select="line" />
  222. <xsl:text>
  223. </xsl:text>
  224. </xsl:when>
  225. <xsl:when test="type='USER_MSG'">
  226. <xsl:text>Message: </xsl:text>
  227. <xsl:value-of select="message" />
  228. <xsl:text>
  229. File: </xsl:text>
  230. <xsl:value-of select="file" />
  231. <xsl:text> at line </xsl:text>
  232. <xsl:value-of select="line" />
  233. <xsl:text>
  234. </xsl:text>
  235. </xsl:when>
  236. <xsl:when test="type='SYS_MSG'">
  237. <xsl:value-of select="reason" /><xsl:text>
  238. </xsl:text>
  239. </xsl:when>
  240. <xsl:when test="type='EXCEPTION_THROWN'">
  241. <xsl:text>Exception occured at: </xsl:text>
  242. <xsl:value-of select="message" />
  243. <xsl:text>
  244. Exceptions details: </xsl:text>
  245. <xsl:value-of select="reason" />
  246. <xsl:text>
  247. File: </xsl:text>
  248. <xsl:value-of select="file" />
  249. <xsl:text> at line </xsl:text>
  250. <xsl:value-of select="line" />
  251. <xsl:text>
  252. </xsl:text>
  253. </xsl:when>
  254. <xsl:when test="type='START_SETUP'">
  255. <xsl:text>Set up phase started.
  256. </xsl:text>
  257. </xsl:when>
  258. <xsl:when test="type='END_SETUP'">
  259. <xsl:text>Set up phase finished.
  260. </xsl:text>
  261. </xsl:when>
  262. <xsl:when test="type='START_RUN'">
  263. <xsl:text>Run phase started.
  264. </xsl:text>
  265. </xsl:when>
  266. <xsl:when test="type='END_RUN'">
  267. <xsl:text>Run phase finished.
  268. </xsl:text>
  269. </xsl:when>
  270. <xsl:when test="type='START_TEAR_DOWN'">
  271. <xsl:text>Tear down phase started.
  272. </xsl:text>
  273. </xsl:when>
  274. <xsl:when test="type='END_TEAR_DOWN'">
  275. <xsl:text>Tear down phase finished.
  276. </xsl:text>
  277. </xsl:when>
  278. </xsl:choose>
  279. </xsl:for-each>
  280. <xsl:text>
  281. ==============================================================================
  282. </xsl:text>
  283. </xsl:for-each>
  284. </xsl:template>
  285. </xsl:stylesheet>
  286. END_OF_STYLE_SHEET;
  287. }
  288. //////////////////////////////////////////////////////
  289. /// Runs all the test cases in the $suite, with the
  290. /// option of storing the results in a file.
  291. /// \param[in] TestSuite &$suite The suite of tests to run.
  292. /// \param[in] string $filename Filename is either:
  293. /// - null - <em>stdout</em> is used;
  294. /// - '' - A temporary filename is used, with
  295. /// the filename written to <em>stdout</em>;
  296. /// - any other string - The given filename is used.
  297. /// \param[in] string $extension The extension to use for the file. Defaults to 'txt'
  298. /// \return Returns one of the following values:
  299. /// - 0 - All tests passed;
  300. /// - -1 - One or more tests failed;
  301. /// - -2 - Unable to open file;
  302. /// - -3 - Unable to write to file.
  303. //////////////////////////////////////////////////////
  304. public function Run(TestSuite &$suite,
  305. $filename = null,
  306. $extension = 'txt')
  307. {
  308. return parent::Run($suite, $filename, $extension);
  309. }
  310. }
  311. ?>