1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelDuskReporter; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use LaravelDuskReporter\Generation\ReportFile; |
8
|
|
|
use LaravelDuskReporter\Generation\ReportFileContract; |
9
|
|
|
use LaravelDuskReporter\Generation\ReportScreenshot; |
10
|
|
|
use LaravelDuskReporter\Generation\ReportScreenshotContract; |
11
|
|
|
|
12
|
|
|
class Reporter |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Report files extension. |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
public static string $fileExt = 'md'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Index file base name. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
public static string $indexFileBaseName = 'index'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The directory that will contain any build files. |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
public static string $storeBuildAt = ''; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The directory that will contain any screenshots. |
37
|
|
|
* If null or empty than will be used "$storeBuildAt" field. |
38
|
|
|
* |
39
|
|
|
* @var ?string |
40
|
|
|
*/ |
41
|
|
|
public static ?string $storeScreenshotAt = null; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Use relative path to screenshot. |
45
|
|
|
* |
46
|
|
|
* @var bool |
47
|
|
|
*/ |
48
|
|
|
public static bool $screenshotRelativePath = true; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Disable screenshots making. |
52
|
|
|
* |
53
|
|
|
* @var bool |
54
|
|
|
*/ |
55
|
|
|
public static bool $disableScreenshots = false; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Fully disable reporting. |
59
|
|
|
* |
60
|
|
|
* @var bool |
61
|
|
|
*/ |
62
|
|
|
public static bool $stopReporting = false; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Closure tio find body element. |
66
|
|
|
* |
67
|
|
|
* @var Closure|null |
68
|
|
|
*/ |
69
|
|
|
public static ?Closure $getBodyElementCallback = null; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Check is report file name valid, and if not - amend it. |
73
|
|
|
* |
74
|
|
|
* @param string $name |
75
|
|
|
* |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
|
|
public static function getValidFileName(string $name): string |
79
|
|
|
{ |
80
|
|
|
$fileExt = static::$fileExt; |
81
|
|
|
if (empty($name)) { |
82
|
|
|
$name = Str::random(); |
83
|
|
|
} |
84
|
|
|
if (Str::endsWith($name, ".{$fileExt}")) { |
85
|
|
|
$name .= ".{$fileExt}"; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $name; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get new report file. |
93
|
|
|
* |
94
|
|
|
* @param string $name |
95
|
|
|
* |
96
|
|
|
* @return ReportFileContract |
97
|
|
|
*/ |
98
|
|
|
public function newFile(string $name): ReportFileContract |
99
|
|
|
{ |
100
|
|
|
return new ReportFile($this, $name); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get new screenshot manager. |
105
|
|
|
* |
106
|
|
|
* @return ReportScreenshotContract |
107
|
|
|
*/ |
108
|
|
|
public function screenshoter(): ReportScreenshotContract |
109
|
|
|
{ |
110
|
|
|
return new ReportScreenshot($this); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param Closure|null $getBodyElementCallback |
115
|
|
|
* |
116
|
|
|
* @return $this |
117
|
|
|
*/ |
118
|
|
|
public function setBodyElementSearchCallback(?Closure $getBodyElementCallback): self |
119
|
|
|
{ |
120
|
|
|
static::$getBodyElementCallback = $getBodyElementCallback; |
121
|
|
|
|
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Get store build folder |
127
|
|
|
* |
128
|
|
|
* @param string $path - File path, optional |
129
|
|
|
* |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
|
|
public function storeBuildAt(string $path = ''): string |
133
|
|
|
{ |
134
|
|
|
return rtrim(static::$storeBuildAt, '/') . ($path ? ('/' . ltrim($path, '/')) : ''); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Get store screenshots folder |
139
|
|
|
* |
140
|
|
|
* @param string $path - File path, optional |
141
|
|
|
* |
142
|
|
|
* @return string |
143
|
|
|
*/ |
144
|
|
|
public function storeScreenshotAt(string $path = ''): string |
145
|
|
|
{ |
146
|
|
|
$screenshotDir = static::$storeScreenshotAt; |
147
|
|
|
if (!$screenshotDir) { |
148
|
|
|
$screenshotDir = $this->storeBuildAt(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return rtrim($screenshotDir, '/') . ($path ? ('/' . ltrim($path, '/')) : ''); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Check if need use relative path |
156
|
|
|
* |
157
|
|
|
* @return bool |
158
|
|
|
*/ |
159
|
|
|
public function useScreenshotRelativePath(): bool |
160
|
|
|
{ |
161
|
|
|
return static::$screenshotRelativePath && ($this->storeScreenshotAt() == $this->storeBuildAt()); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Check is screenshots disabled |
166
|
|
|
* |
167
|
|
|
* @return bool |
168
|
|
|
*/ |
169
|
|
|
public function isScreenshotsDisabled(): bool |
170
|
|
|
{ |
171
|
|
|
return static::$disableScreenshots; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Check is reporting disabled |
176
|
|
|
* |
177
|
|
|
* @return bool |
178
|
|
|
*/ |
179
|
|
|
public function isReportingDisabled(): bool |
180
|
|
|
{ |
181
|
|
|
return static::$stopReporting; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Add file to table of contents. |
186
|
|
|
* |
187
|
|
|
* @param string $name |
188
|
|
|
* |
189
|
|
|
* @return void |
190
|
|
|
*/ |
191
|
|
|
public function addToTableOfContents(string $name): void |
192
|
|
|
{ |
193
|
|
|
if (!$this->isReportingDisabled()) { |
194
|
|
|
$reportFile = $this->reportFileName($name, true); |
195
|
|
|
|
196
|
|
|
$filePath = static::getValidFileName("{$this->storeBuildAt()}/" . static::$indexFileBaseName); |
197
|
|
|
|
198
|
|
|
$directoryPath = dirname($filePath); |
199
|
|
|
|
200
|
|
|
if (!is_dir($directoryPath)) { |
201
|
|
|
mkdir($directoryPath, 0777, true); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
if (!file_exists($filePath)) { |
205
|
|
|
file_put_contents($filePath, '# ' . trans('dusk-reporter::report.table_of_contents') . PHP_EOL . PHP_EOL); |
|
|
|
|
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
if (!str_contains(file_get_contents($filePath), $reportFile)) { |
209
|
|
|
file_put_contents($filePath, "- [{$reportFile}]({$reportFile})" . PHP_EOL, FILE_APPEND | LOCK_EX); |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Get report file name |
216
|
|
|
* |
217
|
|
|
* @param string $name |
218
|
|
|
* @param bool $relative |
219
|
|
|
* |
220
|
|
|
* @return string |
221
|
|
|
*/ |
222
|
|
|
public function reportFileName(string $name, bool $relative = false): string |
223
|
|
|
{ |
224
|
|
|
return ($relative ? '' : "{$this->storeBuildAt()}/") . static::getValidFileName($name); |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|