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
|
|
|
* @copyright 2016-present Ganbaro Digital Ltd www.ganbarodigital.com |
41
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
42
|
|
|
* @link http://datasift.github.io/storyplayer |
43
|
|
|
*/ |
44
|
|
|
|
45
|
|
|
namespace DataSift\Storyplayer\Cli; |
46
|
|
|
|
47
|
|
|
use Exception; |
48
|
|
|
use Phix_Project\CliEngine; |
49
|
|
|
use Phix_Project\CliEngine\CliCommand; |
50
|
|
|
use Phix_Project\CliEngine\CliResult; |
51
|
|
|
use Phix_Project\ExceptionsLib1\Legacy_ErrorHandler; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* A command to create a new test environment to fill in |
55
|
|
|
* |
56
|
|
|
* @category Libraries |
57
|
|
|
* @package Storyplayer/Cli |
58
|
|
|
* @author Stuart Herbert <[email protected]> |
59
|
|
|
* @copyright 2011-present Mediasift Ltd www.datasift.com |
60
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
61
|
|
|
* @link http://datasift.github.io/storyplayer |
62
|
|
|
*/ |
63
|
|
View Code Duplication |
class CreateTestEnv_Command extends CliCommand |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
public function __construct() |
66
|
|
|
{ |
67
|
|
|
// define the command |
68
|
|
|
$this->setName('create-test-env'); |
69
|
|
|
$this->setShortDescription('create a new test environment file'); |
70
|
|
|
$this->setLongDescription( |
71
|
|
|
"Use this command to create a new Env.php file, complete with " |
72
|
|
|
."the necessary PHP 'use' statement and comments to help guide you " |
73
|
|
|
."as you bring your story to life." |
74
|
|
|
.PHP_EOL |
75
|
|
|
); |
76
|
|
|
$this->setArgsList(array( |
77
|
|
|
"namedEnv.php" => "the Env.php file to create" |
78
|
|
|
)); |
79
|
|
|
$this->setSwitches(array( |
80
|
|
|
new CreateTestEnv_BasedOnSwitch, |
81
|
|
|
new CreateTestEnv_ForceSwitch |
82
|
|
|
)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* |
87
|
|
|
* @param CliEngine $engine |
88
|
|
|
* @param array $params |
89
|
|
|
* @param mixed $additionalContext |
90
|
|
|
* @return int |
|
|
|
|
91
|
|
|
*/ |
92
|
|
|
public function processCommand(CliEngine $engine, $params = array(), $additionalContext = null) |
93
|
|
|
{ |
94
|
|
|
// do we have the name of the file to create? |
95
|
|
|
if (!isset($params[0])) { |
96
|
|
|
echo "*** error: you must specify which Env.php file to create\n"; |
97
|
|
|
exit(1); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
// we're going to be dealing with some prehistoric parts of PHP |
101
|
|
|
$legacyHandler = new Legacy_ErrorHandler(); |
102
|
|
|
|
103
|
|
|
// create the path to the environment |
104
|
|
|
$storyFolder = dirname($params[0]); |
105
|
|
|
if (!file_exists($storyFolder)) { |
106
|
|
|
try { |
107
|
|
|
$legacyHandler->run(function() use ($storyFolder) { |
108
|
|
|
mkdir($storyFolder, 0755, true); |
109
|
|
|
}); |
110
|
|
|
} |
111
|
|
|
catch (Exception $e) { |
112
|
|
|
echo "*** error: unable to create folder '{$storyFolder}'\n"; |
113
|
|
|
exit(1); |
|
|
|
|
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
// create the environment inside the folder |
118
|
|
|
$env = <<<EOS |
119
|
|
|
<?php |
120
|
|
|
|
121
|
|
|
use Storyplayer\SPv3\Modules\Asserts; |
122
|
|
|
use Storyplayer\SPv3\Modules\Checkpoint; |
123
|
|
|
use Storyplayer\SPv3\Modules\Log; |
124
|
|
|
use Storyplayer\SPv3\Stories\BuildTestEnvironment; |
125
|
|
|
|
126
|
|
|
EOS; |
127
|
|
|
|
128
|
|
|
if (isset($engine->options->basedOn)) { |
129
|
|
|
foreach ($engine->options->basedOn as $templateClass) { |
130
|
|
|
$story .= "use {$templateClass};\n"; |
|
|
|
|
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
$env .= <<<EOS |
134
|
|
|
|
135
|
|
|
// ======================================================================== |
136
|
|
|
// |
137
|
|
|
// TEST ENVIRONMENT DETAILS |
138
|
|
|
// |
139
|
|
|
// ------------------------------------------------------------------------ |
140
|
|
|
|
141
|
|
|
\$env = BuildTestEnvironment::newTestEnvironment(); |
142
|
|
|
EOS; |
143
|
|
|
|
144
|
|
|
if (isset($engine->options->basedOn)) { |
145
|
|
|
foreach ($engine->options->basedOn as $templateClass) { |
146
|
|
|
$story .= "\n\$env->basedOn(new " . basename(str_replace('\\', '/', $templateClass)) . ");"; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$env .= <<<EOS |
151
|
|
|
|
152
|
|
|
|
153
|
|
|
// ======================================================================== |
154
|
|
|
// |
155
|
|
|
// TEST ENVIRONMENT SETUP |
156
|
|
|
// |
157
|
|
|
// Add one function per step. This makes it easier to debug and maintain |
158
|
|
|
// your test environment construction. |
159
|
|
|
// |
160
|
|
|
// ------------------------------------------------------------------------ |
161
|
|
|
|
162
|
|
|
\$env->addTestEnvironmentSetup(function() { |
163
|
|
|
// what are we doing? |
164
|
|
|
\$log = Log::usingLog()->startAction("describe what we are doing"); |
165
|
|
|
|
166
|
|
|
// add the instructions required to build the environment |
167
|
|
|
|
168
|
|
|
// all done |
169
|
|
|
\$log->endAction(); |
170
|
|
|
}); |
171
|
|
|
|
172
|
|
|
// ======================================================================== |
173
|
|
|
// |
174
|
|
|
// TEST ENVIRONMENT TEARDOWN |
175
|
|
|
// |
176
|
|
|
// Add one function per step. This makes it easier to debug and maintain |
177
|
|
|
// your test environment cleanup. |
178
|
|
|
// |
179
|
|
|
// ------------------------------------------------------------------------ |
180
|
|
|
|
181
|
|
|
\$env->addTestEnvironmentTeardown(function() { |
182
|
|
|
// what are we doing? |
183
|
|
|
\$log = Log::usingLog()->startAction("describe what we are doing"); |
184
|
|
|
|
185
|
|
|
// undo anything that you did in addTestEnvironmentSetup() |
186
|
|
|
|
187
|
|
|
// all done |
188
|
|
|
\$log->endAction(); |
189
|
|
|
}); |
190
|
|
|
|
191
|
|
|
// ======================================================================== |
192
|
|
|
// |
193
|
|
|
// ALL DONE |
194
|
|
|
// |
195
|
|
|
// Return your constructed test environment object, for Storyplayer |
196
|
|
|
// to execute. |
197
|
|
|
// |
198
|
|
|
// ------------------------------------------------------------------------ |
199
|
|
|
|
200
|
|
|
return \$env; |
201
|
|
|
|
202
|
|
|
EOS; |
203
|
|
|
|
204
|
|
|
// does the file already exist? |
205
|
|
|
if (file_exists($params[0])) { |
206
|
|
|
// has the user used --force? |
207
|
|
|
if (!isset($engine->options->force) || !$engine->options->force) { |
208
|
|
|
echo "*** error: file '{$params[0]}' already exists\n"; |
209
|
|
|
echo "use --force to replace this file with the new Env.php file\n"; |
210
|
|
|
exit(1); |
|
|
|
|
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
try { |
215
|
|
|
$legacyHandler->run(function() use($params, $env) { |
216
|
|
|
file_put_contents($params[0], $env); |
217
|
|
|
}); |
218
|
|
|
} |
219
|
|
|
catch (Exception $e) { |
220
|
|
|
echo "*** error: " . $e->getMessage() . "\n"; |
221
|
|
|
exit(1); |
|
|
|
|
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
// all done |
225
|
|
|
return 0; |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.