]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/PHPUnit/PHPUnit/Extensions/Story/Scenario.php
Release 6.2.0
[Github/sugarcrm.git] / tests / PHPUnit / PHPUnit / Extensions / Story / Scenario.php
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_Story
39  * @author     Mattis Stordalen Flister <mattis@xait.no>
40  * @author     Sebastian Bergmann <sebastian@phpunit.de>
41  * @copyright  2002-2011 Sebastian Bergmann <sebastian@phpunit.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
47 /**
48  * A scenario.
49  *
50  * @package    PHPUnit
51  * @subpackage Extensions_Story
52  * @author     Mattis Stordalen Flister <mattis@xait.no>
53  * @author     Sebastian Bergmann <sebastian@phpunit.de>
54  * @copyright  2002-2011 Sebastian Bergmann <sebastian@phpunit.de>
55  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
56  * @version    Release: 3.5.13
57  * @link       http://www.phpunit.de/
58  * @since      Class available since Release 3.3.0
59  */
60 class PHPUnit_Extensions_Story_Scenario
61 {
62     /**
63      * @var    PHPUnit_Extensions_Story_TestCase
64      */
65     protected $test;
66
67     /**
68      * @var    array
69      */
70     protected $steps = array();
71
72     /**
73      * @var    string
74      */
75     protected $lastCalledMethod;
76
77     /**
78      * Constructor.
79      *
80      * @param  PHPUnit_Extensions_Story_TestCase $caller
81      */
82     public function __construct($test)
83     {
84         if ($test instanceof PHPUnit_Extensions_Story_TestCase ||
85             $test instanceof PHPUnit_Extensions_Story_SeleniumTestCase) {
86             $this->test = $test;
87         } else {
88             throw new Exception('$test must either be PHPUnit_Extensions_Story_TestCase or PHPUnit_Extensions_Story_SeleniumTestCase');
89         }
90     }
91
92     /**
93      * Adds a "Given" step to the scenario.
94      *
95      * @param  array $arguments
96      * @return PHPUnit_Extensions_Story_TestCase
97      */
98     public function given($arguments)
99     {
100         return $this->addStep(new PHPUnit_Extensions_Story_Given($arguments));
101     }
102
103     /**
104      * Adds a "When" step to the scenario.
105      *
106      * @param  array $arguments
107      * @return PHPUnit_Extensions_Story_TestCase
108      */
109     public function when($arguments)
110     {
111         return $this->addStep(new PHPUnit_Extensions_Story_When($arguments));
112     }
113
114     /**
115      * Adds a "Then" step to the scenario.
116      *
117      * @param  array $arguments
118      * @return PHPUnit_Extensions_Story_TestCase
119      */
120     public function then($arguments)
121     {
122         return $this->addStep(new PHPUnit_Extensions_Story_Then($arguments));
123     }
124
125     /**
126      * Add another step of the same type as the step that was added before.
127      *
128      * @param  array $arguments
129      * @return PHPUnit_Extensions_Story_TestCase
130      */
131     public function _and($arguments)
132     {
133         $lastCalledStepClass = get_class($this->steps[count($this->steps)-1]);
134
135         return $this->addStep(new $lastCalledStepClass($arguments));
136     }
137
138     /**
139      * Runs this scenario.
140      *
141      * @param  array $world
142      */
143     public function run(array &$world)
144     {
145         foreach ($this->steps as $step)
146         {
147             if ($step instanceof PHPUnit_Extensions_Story_Given) {
148                 $this->test->runGiven(
149                   $world, $step->getAction(), $step->getArguments()
150                 );
151             }
152
153             else if ($step instanceof PHPUnit_Extensions_Story_When) {
154                 $this->test->runWhen(
155                   $world, $step->getAction(), $step->getArguments()
156                 );
157             }
158
159             else {
160                 $this->test->runThen(
161                   $world, $step->getAction(), $step->getArguments()
162                 );
163             }
164         }
165     }
166
167     /**
168      * Adds a step to the scenario.
169      *
170      * @param  PHPUnit_Extensions_Story_Step $step
171      * @return PHPUnit_Extensions_Story_TestCase
172      */
173     protected function addStep(PHPUnit_Extensions_Story_Step $step)
174     {
175         $this->steps[] = $step;
176
177         return $this->test;
178     }
179
180     /**
181      * Returns the steps of this scenario.
182      *
183      * @return array
184      */
185     public function getSteps()
186     {
187         return $this->steps;
188     }
189 }