PageRenderTime 25ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/chamilo/chamilo/
PHP | 202 lines | 84 code | 23 blank | 95 comment | 14 complexity | 235ad66b1265ee2b8288456996f4ccd7 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. $this->outputCallback = $callback;
  86. return TRUE;
  87. }
  88. return FALSE;
  89. }
  90. /**
  91. * @return string
  92. */
  93. public function normalizeOutput($buffer)
  94. {
  95. return str_replace("\r", '', $buffer);
  96. }
  97. /**
  98. * @return string
  99. */
  100. public function getActualOutput()
  101. {
  102. if (!$this->obActive) {
  103. return $this->output;
  104. } else {
  105. return ob_get_contents();
  106. }
  107. }
  108. /**
  109. * @return string
  110. */
  111. public function expectedRegex()
  112. {
  113. return $this->expectedRegex;
  114. }
  115. /**
  116. * @param string $expectedRegex
  117. */
  118. public function expectOutputRegex($expectedRegex)
  119. {
  120. if ($this->expectedString !== NULL) {
  121. throw new PHPUnit_Framework_Exception;
  122. }
  123. if (is_string($expectedRegex) || is_null($expectedRegex)) {
  124. $this->expectedRegex = $expectedRegex;
  125. }
  126. }
  127. /**
  128. * @return string
  129. */
  130. public function expectedString()
  131. {
  132. return $this->expectedString;
  133. }
  134. /**
  135. * @param string $expectedString
  136. */
  137. public function expectOutputString($expectedString)
  138. {
  139. if ($this->expectedRegex !== NULL) {
  140. throw new PHPUnit_Framework_Exception;
  141. }
  142. if (is_string($expectedString) || is_null($expectedString)) {
  143. $this->expectedString = $expectedString;
  144. }
  145. }
  146. /**
  147. * @return mixed
  148. * @throws RuntimeException
  149. */
  150. protected function runTest()
  151. {
  152. ob_start();
  153. $this->obActive = TRUE;
  154. try {
  155. $testResult = parent::runTest();
  156. }
  157. catch (Exception $e) {
  158. ob_end_clean();
  159. $this->obActive = FALSE;
  160. throw $e;
  161. }
  162. if ($this->outputCallback === FALSE) {
  163. $this->output = ob_get_contents();
  164. } else {
  165. $this->output = call_user_func_array($this->outputCallback, array(ob_get_contents()));
  166. }
  167. ob_end_clean();
  168. $this->obActive = FALSE;
  169. if ($this->expectedRegex !== NULL) {
  170. $this->assertRegExp($this->expectedRegex, $this->output);
  171. $this->expectedRegex = NULL;
  172. }
  173. else if ($this->expectedString !== NULL) {
  174. $this->assertEquals($this->expectedString, $this->output);
  175. $this->expectedString = NULL;
  176. }
  177. return $testResult;
  178. }
  179. }