|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright (c) 2011-present Mediasift Ltd |
|
5
|
|
|
* All rights reserved. |
|
6
|
|
|
* |
|
7
|
|
|
* Redistribution and use in source and binary forms, with or without |
|
8
|
|
|
* modification, are permitted provided that the following conditions |
|
9
|
|
|
* are met: |
|
10
|
|
|
* |
|
11
|
|
|
* * Redistributions of source code must retain the above copyright |
|
12
|
|
|
* notice, this list of conditions and the following disclaimer. |
|
13
|
|
|
* |
|
14
|
|
|
* * Redistributions in binary form must reproduce the above copyright |
|
15
|
|
|
* notice, this list of conditions and the following disclaimer in |
|
16
|
|
|
* the documentation and/or other materials provided with the |
|
17
|
|
|
* distribution. |
|
18
|
|
|
* |
|
19
|
|
|
* * Neither the names of the copyright holders nor the names of his |
|
20
|
|
|
* contributors may be used to endorse or promote products derived |
|
21
|
|
|
* from this software without specific prior written permission. |
|
22
|
|
|
* |
|
23
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
24
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
25
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
|
26
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
|
27
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
|
28
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
|
29
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
|
30
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
|
31
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
|
32
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
|
33
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
34
|
|
|
* POSSIBILITY OF SUCH DAMAGE. |
|
35
|
|
|
* |
|
36
|
|
|
* @category Libraries |
|
37
|
|
|
* @package Storyplayer/Cli |
|
38
|
|
|
* @author Stuart Herbert <[email protected]> |
|
39
|
|
|
* @copyright 2011-present Mediasift Ltd www.datasift.com |
|
40
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
|
41
|
|
|
* @link http://datasift.github.io/storyplayer |
|
42
|
|
|
*/ |
|
43
|
|
|
|
|
44
|
|
|
namespace DataSift\Storyplayer\Cli; |
|
45
|
|
|
|
|
46
|
|
|
use Exception; |
|
47
|
|
|
use Phix_Project\CliEngine; |
|
48
|
|
|
use Phix_Project\CliEngine\CliCommand; |
|
49
|
|
|
use Phix_Project\CliEngine\CliResult; |
|
50
|
|
|
use Phix_Project\ExceptionsLib1\Legacy_ErrorHandler; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* A command to create a new story to fill in |
|
54
|
|
|
* |
|
55
|
|
|
* @category Libraries |
|
56
|
|
|
* @package Storyplayer/Cli |
|
57
|
|
|
* @author Stuart Herbert <[email protected]> |
|
58
|
|
|
* @copyright 2011-present Mediasift Ltd www.datasift.com |
|
59
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
|
60
|
|
|
* @link http://datasift.github.io/storyplayer |
|
61
|
|
|
*/ |
|
62
|
|
|
class CreateStory_Command extends CliCommand |
|
63
|
|
|
{ |
|
64
|
|
|
public function __construct() |
|
65
|
|
|
{ |
|
66
|
|
|
// define the command |
|
67
|
|
|
$this->setName('create-story'); |
|
68
|
|
|
$this->setShortDescription('create a new story'); |
|
69
|
|
|
$this->setLongDescription( |
|
70
|
|
|
"Use this command to create a new Story.php file, complete with " |
|
71
|
|
|
."the necessary PHP 'use' statement and comments to help guide you " |
|
72
|
|
|
."as you bring your story to life." |
|
73
|
|
|
.PHP_EOL |
|
74
|
|
|
); |
|
75
|
|
|
$this->setArgsList(array( |
|
76
|
|
|
"[<story.php|list.json>]" => "the story.php file to create" |
|
77
|
|
|
)); |
|
78
|
|
|
$this->setSwitches(array( |
|
79
|
|
|
new CreateStory_BasedOnSwitch, |
|
80
|
|
|
new CreateStory_ForceSwitch |
|
81
|
|
|
)); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* |
|
86
|
|
|
* @param CliEngine $engine |
|
87
|
|
|
* @param array $params |
|
88
|
|
|
* @param mixed $additionalContext |
|
89
|
|
|
* @return int |
|
|
|
|
|
|
90
|
|
|
*/ |
|
91
|
|
|
public function processCommand(CliEngine $engine, $params = array(), $additionalContext = null) |
|
92
|
|
|
{ |
|
93
|
|
|
// do we have the name of the file to create? |
|
94
|
|
|
if (!isset($params[0])) { |
|
95
|
|
|
echo "*** error: you must specify which story to create\n"; |
|
96
|
|
|
exit(1); |
|
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
// we're going to be dealing with some prehistoric parts of PHP |
|
100
|
|
|
$legacyHandler = new Legacy_ErrorHandler(); |
|
101
|
|
|
|
|
102
|
|
|
// create the path to the story |
|
103
|
|
|
$storyFolder = dirname($params[0]); |
|
104
|
|
|
if (!file_exists($storyFolder)) { |
|
105
|
|
|
try { |
|
106
|
|
|
$legacyHandler->run(function() use ($storyFolder) { |
|
107
|
|
|
mkdir($storyFolder, 0755, true); |
|
108
|
|
|
}); |
|
109
|
|
|
} |
|
110
|
|
|
catch (Exception $e) { |
|
111
|
|
|
echo "*** error: unable to create folder '{$storyFolder}'\n"; |
|
112
|
|
|
exit(1); |
|
|
|
|
|
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
// create the story inside the folder |
|
117
|
|
|
$story = <<<EOS |
|
118
|
|
|
<?php |
|
119
|
|
|
|
|
120
|
|
|
use Storyplayer\SPv2\Modules\Asserts; |
|
121
|
|
|
use Storyplayer\SPv2\Modules\Checkpoint; |
|
122
|
|
|
use Storyplayer\SPv2\Modules\Log; |
|
123
|
|
|
use Storyplayer\SPv2\Stories\BuildStory; |
|
124
|
|
|
|
|
125
|
|
|
EOS; |
|
126
|
|
|
|
|
127
|
|
|
if (isset($engine->options->basedOn)) { |
|
128
|
|
|
foreach ($engine->options->basedOn as $templateClass) { |
|
129
|
|
|
$story .= "use {$templateClass};\n"; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
$story .= <<<EOS |
|
133
|
|
|
|
|
134
|
|
|
// ======================================================================== |
|
135
|
|
|
// |
|
136
|
|
|
// STORY DETAILS |
|
137
|
|
|
// |
|
138
|
|
|
// ------------------------------------------------------------------------ |
|
139
|
|
|
|
|
140
|
|
|
\$story = BuildStory::newStory(); |
|
141
|
|
|
EOS; |
|
142
|
|
|
|
|
143
|
|
|
if (isset($engine->options->basedOn)) { |
|
144
|
|
|
foreach ($engine->options->basedOn as $templateClass) { |
|
145
|
|
|
$story .= "\n\$story->basedOn(new " . basename(str_replace('\\', '/', $templateClass)) . ");"; |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
$story .= <<<EOS |
|
150
|
|
|
|
|
151
|
|
|
|
|
152
|
|
|
// ======================================================================== |
|
153
|
|
|
// |
|
154
|
|
|
// TEST SETUP / TEAR-DOWN |
|
155
|
|
|
// |
|
156
|
|
|
// ------------------------------------------------------------------------ |
|
157
|
|
|
|
|
158
|
|
|
/* |
|
159
|
|
|
\$story->addTestSetup(function() { |
|
160
|
|
|
// what are we doing? |
|
161
|
|
|
\$log = Log::usingLog()->startAction("describe what we are doing"); |
|
162
|
|
|
|
|
163
|
|
|
// setup the conditions for this specific test |
|
164
|
|
|
\$checkpoint = Checkpoint::getCheckpoint(); |
|
165
|
|
|
|
|
166
|
|
|
// all done |
|
167
|
|
|
\$log->endAction(); |
|
168
|
|
|
}); |
|
169
|
|
|
*/ |
|
170
|
|
|
|
|
171
|
|
|
/* |
|
172
|
|
|
\$story->addTestTeardown(function() { |
|
173
|
|
|
// what are we doing? |
|
174
|
|
|
\$log = Log::usingLog()->startAction("describe what we are doing"); |
|
175
|
|
|
|
|
176
|
|
|
// undo anything that you did in addTestSetup() |
|
177
|
|
|
|
|
178
|
|
|
// all done |
|
179
|
|
|
\$log->endAction(); |
|
180
|
|
|
}); |
|
181
|
|
|
*/ |
|
182
|
|
|
|
|
183
|
|
|
// ======================================================================== |
|
184
|
|
|
// |
|
185
|
|
|
// PRE-TEST PREDICTION |
|
186
|
|
|
// |
|
187
|
|
|
// ------------------------------------------------------------------------ |
|
188
|
|
|
|
|
189
|
|
|
/* |
|
190
|
|
|
\$story->addPreTestPrediction(function() { |
|
191
|
|
|
// what are we doing? |
|
192
|
|
|
\$log = Log::usingLog()->startAction("describe what we are doing"); |
|
193
|
|
|
|
|
194
|
|
|
// if it is okay for your story to fail, detect that here |
|
195
|
|
|
|
|
196
|
|
|
// all done |
|
197
|
|
|
\$log->endAction(); |
|
198
|
|
|
}); |
|
199
|
|
|
*/ |
|
200
|
|
|
|
|
201
|
|
|
// ======================================================================== |
|
202
|
|
|
// |
|
203
|
|
|
// PRE-TEST INSPECTION |
|
204
|
|
|
// |
|
205
|
|
|
// ------------------------------------------------------------------------ |
|
206
|
|
|
|
|
207
|
|
|
/* |
|
208
|
|
|
\$story->addPreTestInspection(function() { |
|
209
|
|
|
// what are we doing? |
|
210
|
|
|
\$log = Log::usingLog()->startAction("describe what we are doing"); |
|
211
|
|
|
|
|
212
|
|
|
// get the checkpoint - we're going to store data in here |
|
213
|
|
|
\$checkpoint = Checkpoint::getCheckpoint(); |
|
214
|
|
|
|
|
215
|
|
|
// store any data that your story is about to change, so that you |
|
216
|
|
|
// can do a before and after comparison |
|
217
|
|
|
|
|
218
|
|
|
// all done |
|
219
|
|
|
\$log->endAction(); |
|
220
|
|
|
}); |
|
221
|
|
|
*/ |
|
222
|
|
|
|
|
223
|
|
|
// ======================================================================== |
|
224
|
|
|
// |
|
225
|
|
|
// ACTIONS |
|
226
|
|
|
// |
|
227
|
|
|
// ------------------------------------------------------------------------ |
|
228
|
|
|
|
|
229
|
|
|
/* |
|
230
|
|
|
\$story->addAction(function() { |
|
231
|
|
|
// what are we doing? |
|
232
|
|
|
\$log = Log::usingLog()->startAction("describe what we are doing"); |
|
233
|
|
|
|
|
234
|
|
|
// use the checkpoint to store any data collected during the action |
|
235
|
|
|
// this data will be examined in the postTestInspection phase |
|
236
|
|
|
\$checkpoint = Checkpoint::getCheckpoint(); |
|
237
|
|
|
|
|
238
|
|
|
// this is where you perform the steps of your user story |
|
239
|
|
|
|
|
240
|
|
|
// all done |
|
241
|
|
|
\$log->endAction(); |
|
242
|
|
|
}); |
|
243
|
|
|
*/ |
|
244
|
|
|
|
|
245
|
|
|
// ======================================================================== |
|
246
|
|
|
// |
|
247
|
|
|
// POST-TEST INSPECTION |
|
248
|
|
|
// |
|
249
|
|
|
// ------------------------------------------------------------------------ |
|
250
|
|
|
|
|
251
|
|
|
\$story->addPostTestInspection(function() { |
|
252
|
|
|
// what are we doing? |
|
253
|
|
|
\$log = Log::usingLog()->startAction("describe what we are doing"); |
|
254
|
|
|
|
|
255
|
|
|
// the information to guide our checks is in the checkpoint |
|
256
|
|
|
\$checkpoint = Checkpoint::getCheckpoint(); |
|
257
|
|
|
|
|
258
|
|
|
// gather new data, and make sure that your action actually changed |
|
259
|
|
|
// something. never assume that the action worked just because it |
|
260
|
|
|
// completed to the end with no errors or exceptions! |
|
261
|
|
|
|
|
262
|
|
|
// all done |
|
263
|
|
|
\$log->endAction(); |
|
264
|
|
|
}); |
|
265
|
|
|
|
|
266
|
|
|
EOS; |
|
267
|
|
|
|
|
268
|
|
|
// does the file already exist? |
|
269
|
|
|
if (file_exists($params[0])) { |
|
270
|
|
|
// has the user used --force? |
|
271
|
|
|
if (!isset($engine->options->force) || !$engine->options->force) { |
|
272
|
|
|
echo "*** error: file '{$params[0]}' already exists\n"; |
|
273
|
|
|
echo "use --force to replace this file with the new story file\n"; |
|
274
|
|
|
exit(1); |
|
|
|
|
|
|
275
|
|
|
} |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
try { |
|
279
|
|
|
$legacyHandler->run(function() use($params, $story) { |
|
280
|
|
|
file_put_contents($params[0], $story); |
|
281
|
|
|
}); |
|
282
|
|
|
} |
|
283
|
|
|
catch (Exception $e) { |
|
284
|
|
|
echo "*** error: " . $e->getMessage() . "\n"; |
|
285
|
|
|
exit(1); |
|
|
|
|
|
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
// all done |
|
289
|
|
|
return 0; |
|
290
|
|
|
} |
|
291
|
|
|
} |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.