-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathScreenshotCommand.php
223 lines (192 loc) · 7.69 KB
/
ScreenshotCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
namespace DiffyCli\Commands;
use Diffy\Diffy;
use Diffy\Screenshot;
use DiffyCli\Config;
use GuzzleHttp\Exception\InvalidArgumentException;
use Robo\Tasks;
use function GuzzleHttp\json_decode;
class ScreenshotCommand extends Tasks
{
/**
* Create a screenshot from environment
*
* @command screenshot:create
*
* @param int $projectId ID of the project
* @param string $environment Environment of the project. Can be one of "production", "staging", "development", "custom" (short options: "prod", "stage", "dev")
*
* @param array $options
*
* @throws \Diffy\InvalidArgumentsException
*
* @option wait Wait for the screenshot to be completed
* @option max-wait Maximum number of seconds to wait for the screenshot to be completed.
* @option envUrl URL of the custom environment.
*
* @usage screenshot:create 342 production Take screenshot from production on project 342.
* @usage screenshot:create 342 production --wait Take the screenshot and wait till they are completed.
* @usage screenshot:create 342 custom --envUrl="" Take the screenshot and wait till they are completed.
*/
public function createScreenshot($projectId, $environment, array $options = ['wait' => false, 'max-wait' => 1200, 'envUrl' => ''])
{
$apiKey = Config::getConfig()['key'];
Diffy::setApiKey($apiKey);
if ($environment === 'prod') {
$environment = 'production';
} elseif ($environment === 'dev') {
$environment = 'development';
} elseif ($environment === 'stage') {
$environment = 'staging';
}
if ($environment != 'custom') {
$screenshotId = Screenshot::create($projectId, $environment);
}
else {
if (empty($options['envUrl'])) {
$this->io()->write('You need to provide envUrl option if you create screenshots from "custom" environment');
throw new InvalidArgumentException();
}
$optionsCreate = [
'baseUrl' => $options['envUrl']
];
$screenshotId = Screenshot::create($projectId, $environment, $optionsCreate);
}
if (!empty($options['wait']) && $options['wait'] == true) {
$sleep = 10;
$max_wait = (int) $options['max-wait'];
sleep($sleep);
$i = 0;
$screenshot = Screenshot::retrieve($screenshotId);
if ($max_wait !== 1200) {
while ($i < $max_wait / $sleep) {
if ($screenshot->isCompleted()) {
break;
}
sleep($sleep);
$screenshot->refresh();
$i += $sleep;
}
} else {
while (!$screenshot->isCompleted()) {
sleep($sleep);
$screenshot->refresh();
}
}
}
$this->io()->write($screenshotId);
return $screenshotId;
}
/**
* Get the list of screenshots
*
* @command screenshot:list
*
* @param int $projectId ID of the project
*
* @param array $options
*
* @throws \Diffy\InvalidArgumentsException
*
* @option name Filter by the title of the screenshot
* @option limit Number of results to return
*
* @usage screenshot:list 342 return all the screenshots for the project 32.
* @usage screenshot:list 342 --name="feature-branch-5" --limit=1 retrieve details of the last screenshot named "feature-branch-5"
* @usage screenshot:list 18765 --limit=1 --name="Demo screenshot staging" | grep \'id\' | php -r 'print(preg_replace("/[^0-9]/", "", stream_get_contents(STDIN)));'
* Extract ID of the latest screenshot with the name "Demo screenshot staging"
*/
public function listScreenshot($projectId, array $options = ['name' => '', 'limit' => 0])
{
$apiKey = Config::getConfig()['key'];
Diffy::setApiKey($apiKey);
$list = Screenshot::all($projectId);
$screenshots = $list['screenshotsForDiff'];
// Do not include baseline.
array_shift($screenshots);
if (!empty($options['name'])) {
$filtered = [];
foreach ($screenshots as $screenshot) {
if ($screenshot['name'] == $options['name']) {
$filtered[] = $screenshot;
}
}
$screenshots = $filtered;
}
if (!empty($options['limit'])) {
$screenshots = array_slice($screenshots, 0, $options['limit']);
}
$this->io()->write(var_export($screenshots, true));
}
/**
* Create a screenshot from uploade images
*
* @command screenshot:create-uploaded
*
* @param int $projectId ID of the project
* @param string $configurationPath Path to the json config file.
* Json encoded array of snapshotName and arrays "files", "breakpoints", "urls".
*
* @usage screenshot:create-uploaded 342 ./diffy_create_screenshot_upload.json
*/
public function createScreenshotUpload($projectId, string $configurationPath)
{
$apiKey = Config::getConfig()['key'];
Diffy::setApiKey($apiKey);
$configuration = file_get_contents($configurationPath);
if (!$configuration) {
$this->io()->write(sprintf('Configuration not found on path : %s', $configurationPath));
throw new InvalidArgumentException();
}
try {
$configuration = json_decode($configuration, true);
} catch (InvalidArgumentException $exception) {
$this->io()->write('Configuration is not valid JSON ');
throw $exception;
}
$screenshotId = Screenshot::createUpload($projectId, $configuration);
$this->io()->write($screenshotId);
}
/**
* Creates a new baseline from a given environment
*
* @command screenshot:create-baseline
*
* @param int $projectId ID of the project
* @param string $environment Environment of the project. Can be one of "production", "staging", "development", "custom"
*
* @param array $options
*
* @throws \Diffy\InvalidArgumentsException
*
* @option wait Wait for the screenshot to be completed
* @option max-wait Maximum number of seconds to wait for the screenshot to be completed.
*
* @usage screenshot:create-baseline 342 production Take screenshot from production on project 342.
* @usage screenshot:create-baseline 342 production --wait Take the screenshot and wait till they are completed.
*/
public function createScreenshotBaseline($projectId, $environment, array $options = ['wait' => true, 'max-wait' => 1200])
{
$apiKey = Config::getConfig()['key'];
Diffy::setApiKey($apiKey);
$screenshotId = $this->createScreenshot($projectId, $environment, $options);
Screenshot::setBaselineSet($projectId, $screenshotId);
}
/**
* Sets a new baseline from a screenshot ID.
*
* @command screenshot:set-baseline
*
* @param int $projectId ID of the project
* @param int $screenshotId The screenshot ID to be the baseline.
*
* @usage screenshot:set-baseline 342 4325 Set the baseline for project to be screenshot ID.
*/
public function setScreenshotBaseline($projectId, $screenshotId)
{
$apiKey = Config::getConfig()['key'];
Diffy::setApiKey($apiKey);
Screenshot::setBaselineSet($projectId, $screenshotId);
$this->io()->write(sprintf('Baseline for project %d has been updated.', $projectId));
}
}