]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/PHPUnit/Extensions/Story/SeleniumTestCase.php
Added unit tests.
[Github/sugarcrm.git] / tests / PHPUnit / Extensions / Story / SeleniumTestCase.php
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
44  * @link       http://www.phpunit.de/
45  * @since      File available since Release 3.3.0
46  */
47
48 require_once 'PHPUnit/Extensions/Story/Scenario.php';
49 require_once 'PHPUnit/Extensions/SeleniumTestCase.php';
50 require_once 'PHPUnit/Extensions/Story/TestCase.php';
51
52 /**
53  *
54  *
55  * @category   Testing
56  * @package    PHPUnit
57  * @author     Mattis Stordalen Flister <mattis@xait.no>
58  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
59  * @copyright  2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
60  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
61  * @version    Release: 3.3.17
62  * @link       http://www.phpunit.de/
63  * @since      Class available since Release 3.3.0
64  * @abstract
65  */
66 abstract class PHPUnit_Extensions_Story_SeleniumTestCase extends PHPUnit_Extensions_SeleniumTestCase
67 {
68     protected $scenario;
69     protected $world = array();
70
71     public function __construct($name = NULL, array $data = array(), $dataName = '', array $browser = array())
72     {
73         parent::__construct($name, $data, $dataName, $browser);
74         $this->scenario = new PHPUnit_Extensions_Story_Scenario($this);
75     }
76
77     /**
78      * @method PHPUnit_Extensions_Story_Step and($contextOrOutcome)
79      */
80     public function __call($command, $arguments)
81     {
82         switch($command) {
83             case 'and': {
84                 return $this->scenario->_and($arguments);
85             }
86             break;
87
88             default: {
89                 return parent::__call($command, $arguments);
90             }
91         }
92     }
93
94     /**
95      * Returns this test's scenario.
96      *
97      * @return PHPUnit_Extensions_Story_Scenario
98      */
99     public function getScenario()
100     {
101         return $this->scenario;
102     }
103
104     /**
105      * This method is used by __call
106      *
107      */
108     protected function notImplemented($action)
109     {
110         if (strstr($action, ' ')) {
111             $this->markTestIncomplete("step: $action not implemented.");
112         }
113
114         throw new BadMethodCallException("Method $action not defined.");
115     }
116
117     /**
118      * Adds a "Given" step to the scenario.
119      *
120      * @param  array $arguments
121      * @return PHPUnit_Extensions_Story_TestCase
122      */
123     protected function given($context)
124     {
125         return $this->scenario->given(func_get_args());
126     }
127
128     /**
129      * Adds a "When" step to the scenario.
130      *
131      * @param  array $arguments
132      * @return PHPUnit_Extensions_Story_TestCase
133      */
134     protected function when($event)
135     {
136         return $this->scenario->when(func_get_args());
137     }
138
139     /**
140      * Adds a "Then" step to the scenario.
141      *
142      * @param  array $arguments
143      * @return PHPUnit_Extensions_Story_TestCase
144      */
145     protected function then($outcome)
146     {
147         return $this->scenario->then(func_get_args());
148     }
149
150     /**
151      * Add another step of the same type as the step that was added before.
152      *
153      * @param  array $arguments
154      * @return PHPUnit_Extensions_Story_TestCase
155      */
156     protected function _and($contextOrOutcome)
157     {
158         return $this->scenario->_and(func_get_args());
159     }
160
161     /**
162      * Run this test's scenario.
163      *
164      * @throws RuntimeException
165      */
166     protected function runTest()
167     {
168         $autostop       = $this->autoStop;
169         $this->autoStop = FALSE;
170
171         try {
172             parent::runTest();
173             $this->scenario->run($this->world);
174             $this->autoStop = $autostop;
175         }
176
177         catch (Exception $e) {
178             $this->autoStop = $autostop;
179             throw $e;
180         }
181     }
182
183     /**
184      * Implementation for "Given" steps.
185      *
186      * @param  array  $world
187      * @param  string $action
188      * @param  array  $arguments
189      */
190     abstract protected function runGiven(&$world, $action, $arguments);
191
192     /**
193      * Implementation for "When" steps.
194      *
195      * @param  array  $world
196      * @param  string $action
197      * @param  array  $arguments
198      */
199     abstract protected function runWhen(&$world, $action, $arguments);
200
201     /**
202      * Implementation for "Then" steps.
203      *
204      * @param  array  $world
205      * @param  string $action
206      * @param  array  $arguments
207      */
208     abstract protected function runThen(&$world, $action, $arguments);
209 }