Scenario.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace Codeception;
  3. use Codeception\Event\StepEvent;
  4. use Codeception\Exception\ConditionalAssertionFailed;
  5. use Codeception\Test\Metadata;
  6. class Scenario
  7. {
  8. /**
  9. * @var TestInterface
  10. */
  11. protected $test;
  12. /**
  13. * @var Metadata
  14. */
  15. protected $metadata;
  16. /**
  17. * @var array
  18. */
  19. protected $steps = [];
  20. /**
  21. * @var string
  22. */
  23. protected $feature;
  24. protected $metaStep;
  25. /**
  26. * Constructor
  27. *
  28. * @param TestInterface $test
  29. */
  30. public function __construct(TestInterface $test)
  31. {
  32. $this->metadata = $test->getMetadata();
  33. $this->test = $test;
  34. }
  35. public function setFeature($feature)
  36. {
  37. $this->metadata->setFeature($feature);
  38. }
  39. public function getFeature()
  40. {
  41. return $this->metadata->getFeature();
  42. }
  43. public function getGroups()
  44. {
  45. return $this->metadata->getGroups();
  46. }
  47. public function current($key)
  48. {
  49. return $this->metadata->getCurrent($key);
  50. }
  51. public function runStep(Step $step)
  52. {
  53. $step->saveTrace();
  54. if ($this->metaStep instanceof Step\Meta) {
  55. $step->setMetaStep($this->metaStep);
  56. }
  57. $this->steps[] = $step;
  58. $result = null;
  59. $this->metadata->getService('dispatcher')->dispatch(Events::STEP_BEFORE, new StepEvent($this->test, $step));
  60. try {
  61. $result = $step->run($this->metadata->getService('modules'));
  62. } catch (ConditionalAssertionFailed $f) {
  63. $result = $this->test->getTestResultObject();
  64. if (is_null($result)) {
  65. $this->metadata->getService('dispatcher')->dispatch(Events::STEP_AFTER, new StepEvent($this->test, $step));
  66. throw $f;
  67. } else {
  68. $result->addFailure(clone($this->test), $f, $result->time());
  69. }
  70. } catch (\Exception $e) {
  71. $this->metadata->getService('dispatcher')->dispatch(Events::STEP_AFTER, new StepEvent($this->test, $step));
  72. throw $e;
  73. }
  74. $this->metadata->getService('dispatcher')->dispatch(Events::STEP_AFTER, new StepEvent($this->test, $step));
  75. $step->executed = true;
  76. return $result;
  77. }
  78. public function addStep(Step $step)
  79. {
  80. $this->steps[] = $step;
  81. }
  82. /**
  83. * Returns the steps of this scenario.
  84. *
  85. * @return array
  86. */
  87. public function getSteps()
  88. {
  89. return $this->steps;
  90. }
  91. public function getHtml()
  92. {
  93. $text = '';
  94. foreach ($this->getSteps() as $step) {
  95. /** @var Step $step */
  96. if ($step->getName() !== 'Comment') {
  97. $text .= $step->getHtml() . '<br/>';
  98. } else {
  99. $text .= trim($step->getHumanizedArguments(), '"') . '<br/>';
  100. }
  101. }
  102. $text = str_replace(['"\'', '\'"'], ["'", "'"], $text);
  103. $text = "<h3>" . mb_strtoupper('I want to ' . $this->getFeature(), 'utf-8') . "</h3>" . $text;
  104. return $text;
  105. }
  106. public function getText()
  107. {
  108. $text = '';
  109. foreach ($this->getSteps() as $step) {
  110. $text .= $step->getPrefix() . "$step \r\n";
  111. }
  112. $text = trim(str_replace(['"\'', '\'"'], ["'", "'"], $text));
  113. $text = mb_strtoupper('I want to ' . $this->getFeature(), 'utf-8') . "\r\n\r\n" . $text . "\r\n\r\n";
  114. return $text;
  115. }
  116. public function comment($comment)
  117. {
  118. $this->runStep(new \Codeception\Step\Comment($comment, []));
  119. }
  120. public function skip($message = '')
  121. {
  122. throw new \PHPUnit\Framework\SkippedTestError($message);
  123. }
  124. public function incomplete($message = '')
  125. {
  126. throw new \PHPUnit\Framework\IncompleteTestError($message);
  127. }
  128. public function __call($method, $args)
  129. {
  130. // all methods were deprecated and removed from here
  131. trigger_error("Codeception: \$scenario->$method() has been deprecated and removed. Use annotations to pass scenario params", E_USER_DEPRECATED);
  132. }
  133. /**
  134. * @param Step\Meta $metaStep
  135. */
  136. public function setMetaStep($metaStep)
  137. {
  138. $this->metaStep = $metaStep;
  139. }
  140. /**
  141. * @return Step\Meta
  142. */
  143. public function getMetaStep()
  144. {
  145. return $this->metaStep;
  146. }
  147. }