PageRenderTime 24ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/script/lib/PHPUnit/Extensions/OutputTestCase.php

https://bitbucket.org/chamilo/chamilo-dev/
PHP | 218 lines | 100 code | 23 blank | 95 comment | 12 complexity | 157f8d270f02b908c850b4be5cc7ec6b MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT
  1. <?php
  2. /**
  3. * PHPUnit
  4. *
  5. * Copyright (c) 2002-2011, Sebastian Bergmann <sebastian@phpunit.de>.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * * Neither the name of Sebastian Bergmann nor the names of his
  21. * contributors may be used to endorse or promote products derived
  22. * from this software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  27. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  28. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  29. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  30. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  31. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  34. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35. * POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. * @package PHPUnit
  38. * @subpackage Extensions
  39. * @author Sebastian Bergmann <sebastian@phpunit.de>
  40. * @copyright 2002-2011 Sebastian Bergmann <sebastian@phpunit.de>
  41. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  42. * @link http://www.phpunit.de/
  43. * @since File available since Release 3.0.0
  44. */
  45. /**
  46. * A TestCase that expects a specified output.
  47. *
  48. * @package PHPUnit
  49. * @subpackage Extensions
  50. * @author Sebastian Bergmann <sebastian@phpunit.de>
  51. * @copyright 2002-2011 Sebastian Bergmann <sebastian@phpunit.de>
  52. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  53. * @version Release: 3.5.9
  54. * @link http://www.phpunit.de/
  55. * @since Class available since Release 3.0.0
  56. */
  57. abstract class PHPUnit_Extensions_OutputTestCase extends PHPUnit_Framework_TestCase
  58. {
  59. /**
  60. * @var string
  61. */
  62. protected $expectedRegex = NULL;
  63. /**
  64. * @var string
  65. */
  66. protected $expectedString = NULL;
  67. /**
  68. * @var string
  69. */
  70. protected $output = '';
  71. /**
  72. * @var boolean
  73. */
  74. protected $obActive = FALSE;
  75. /**
  76. * @var mixed
  77. */
  78. protected $outputCallback = FALSE;
  79. /**
  80. * @return bool
  81. */
  82. public function setOutputCallback($callback)
  83. {
  84. if (is_callable($callback))
  85. {
  86. $this->outputCallback = $callback;
  87. return TRUE;
  88. }
  89. return FALSE;
  90. }
  91. /**
  92. * @return string
  93. */
  94. public function normalizeOutput($buffer)
  95. {
  96. return str_replace("\r", '', $buffer);
  97. }
  98. /**
  99. * @return string
  100. */
  101. public function getActualOutput()
  102. {
  103. if (! $this->obActive)
  104. {
  105. return $this->output;
  106. }
  107. else
  108. {
  109. return ob_get_contents();
  110. }
  111. }
  112. /**
  113. * @return string
  114. */
  115. public function expectedRegex()
  116. {
  117. return $this->expectedRegex;
  118. }
  119. /**
  120. * @param string $expectedRegex
  121. */
  122. public function expectOutputRegex($expectedRegex)
  123. {
  124. if ($this->expectedString !== NULL)
  125. {
  126. throw new PHPUnit_Framework_Exception();
  127. }
  128. if (is_string($expectedRegex) || is_null($expectedRegex))
  129. {
  130. $this->expectedRegex = $expectedRegex;
  131. }
  132. }
  133. /**
  134. * @return string
  135. */
  136. public function expectedString()
  137. {
  138. return $this->expectedString;
  139. }
  140. /**
  141. * @param string $expectedString
  142. */
  143. public function expectOutputString($expectedString)
  144. {
  145. if ($this->expectedRegex !== NULL)
  146. {
  147. throw new PHPUnit_Framework_Exception();
  148. }
  149. if (is_string($expectedString) || is_null($expectedString))
  150. {
  151. $this->expectedString = $expectedString;
  152. }
  153. }
  154. /**
  155. * @return mixed
  156. * @throws RuntimeException
  157. */
  158. protected function runTest()
  159. {
  160. ob_start();
  161. $this->obActive = TRUE;
  162. try
  163. {
  164. $testResult = parent :: runTest();
  165. }
  166. catch (Exception $e)
  167. {
  168. ob_end_clean();
  169. $this->obActive = FALSE;
  170. throw $e;
  171. }
  172. if ($this->outputCallback === FALSE)
  173. {
  174. $this->output = ob_get_contents();
  175. }
  176. else
  177. {
  178. $this->output = call_user_func_array($this->outputCallback, array(ob_get_contents()));
  179. }
  180. ob_end_clean();
  181. $this->obActive = FALSE;
  182. if ($this->expectedRegex !== NULL)
  183. {
  184. $this->assertRegExp($this->expectedRegex, $this->output);
  185. $this->expectedRegex = NULL;
  186. }
  187. else
  188. if ($this->expectedString !== NULL)
  189. {
  190. $this->assertEquals($this->expectedString, $this->output);
  191. $this->expectedString = NULL;
  192. }
  193. return $testResult;
  194. }
  195. }