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