PageRenderTime 36ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/tests/PHPUnit/Extensions/Story/TestCase.php

https://github.com/asakusuma/sugarcrm_dev
PHP | 217 lines | 65 code | 18 blank | 134 comment | 2 complexity | eab44456e3cf007ae3cfe51a042ec4b4 MD5 | raw file
  1. <?php
  2. /**
  3. * PHPUnit
  4. *
  5. * Copyright (c) 2002-2009, Sebastian Bergmann <sb@sebastian-bergmann.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. * @category Testing
  38. * @package PHPUnit
  39. * @author Mattis Stordalen Flister <mattis@xait.no>
  40. * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
  41. * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
  42. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  43. * @link http://www.phpunit.de/
  44. * @since File available since Release 3.3.0
  45. */
  46. require_once 'PHPUnit/Extensions/Story/Scenario.php';
  47. require_once 'PHPUnit/Framework.php';
  48. require_once 'PHPUnit/Util/Filter.php';
  49. PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
  50. /**
  51. * A story test case.
  52. *
  53. * @category Testing
  54. * @package PHPUnit
  55. * @author Mattis Stordalen Flister <mattis@xait.no>
  56. * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
  57. * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
  58. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  59. * @version Release: 3.3.17
  60. * @link http://www.phpunit.de/
  61. * @since Class available since Release 3.3.0
  62. * @abstract
  63. */
  64. abstract class PHPUnit_Extensions_Story_TestCase extends PHPUnit_Framework_TestCase
  65. {
  66. /**
  67. * @var PHPUnit_Extensions_Story_Scenario
  68. */
  69. protected $scenario;
  70. /**
  71. * @var array
  72. */
  73. protected $world = array();
  74. /**
  75. * Constructs a test case with the given name.
  76. *
  77. * @param string $name
  78. * @param array $data
  79. * @param string $dataName
  80. */
  81. public function __construct($name = NULL, array $data = array(), $dataName = '')
  82. {
  83. parent::__construct($name, $data, $dataName);
  84. $this->scenario = new PHPUnit_Extensions_Story_Scenario($this);
  85. }
  86. /**
  87. * @method PHPUnit_Extensions_Story_Step and($contextOrOutcome)
  88. */
  89. public function __call($command, $arguments)
  90. {
  91. switch ($command) {
  92. case 'and': {
  93. return $this->scenario->_and($arguments);
  94. }
  95. break;
  96. default: {
  97. throw new BadMethodCallException(
  98. "Method $command not defined."
  99. );
  100. }
  101. }
  102. }
  103. /**
  104. * Returns this test's scenario.
  105. *
  106. * @return PHPUnit_Extensions_Story_Scenario
  107. */
  108. public function getScenario()
  109. {
  110. return $this->scenario;
  111. }
  112. /**
  113. *
  114. *
  115. */
  116. protected function notImplemented($action)
  117. {
  118. if (strstr($action, ' ')) {
  119. $this->markTestIncomplete("step: $action not implemented.");
  120. }
  121. throw new BadMethodCallException("Method $action not defined.");
  122. }
  123. /**
  124. * Adds a "Given" step to the scenario.
  125. *
  126. * @param array $arguments
  127. * @return PHPUnit_Extensions_Story_TestCase
  128. */
  129. protected function given($context)
  130. {
  131. return $this->scenario->given(func_get_args());
  132. }
  133. /**
  134. * Adds a "When" step to the scenario.
  135. *
  136. * @param array $arguments
  137. * @return PHPUnit_Extensions_Story_TestCase
  138. */
  139. protected function when($event)
  140. {
  141. return $this->scenario->when(func_get_args());
  142. }
  143. /**
  144. * Adds a "Then" step to the scenario.
  145. *
  146. * @param array $arguments
  147. * @return PHPUnit_Extensions_Story_TestCase
  148. */
  149. protected function then($outcome)
  150. {
  151. return $this->scenario->then(func_get_args());
  152. }
  153. /**
  154. * Add another step of the same type as the step that was added before.
  155. *
  156. * @param array $arguments
  157. * @return PHPUnit_Extensions_Story_TestCase
  158. */
  159. protected function _and($contextOrOutcome)
  160. {
  161. return $this->scenario->_and(func_get_args());
  162. }
  163. /**
  164. * Run this test's scenario.
  165. *
  166. * @throws RuntimeException
  167. */
  168. protected function runTest()
  169. {
  170. parent::runTest();
  171. $this->scenario->run($this->world);
  172. }
  173. /**
  174. * Implementation for "Given" steps.
  175. *
  176. * @param array $world
  177. * @param string $action
  178. * @param array $arguments
  179. */
  180. abstract protected function runGiven(&$world, $action, $arguments);
  181. /**
  182. * Implementation for "When" steps.
  183. *
  184. * @param array $world
  185. * @param string $action
  186. * @param array $arguments
  187. */
  188. abstract protected function runWhen(&$world, $action, $arguments);
  189. /**
  190. * Implementation for "Then" steps.
  191. *
  192. * @param array $world
  193. * @param string $action
  194. * @param array $arguments
  195. */
  196. abstract protected function runThen(&$world, $action, $arguments);
  197. }
  198. ?>