1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelDuskReporter\Generation; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use Laravel\Dusk\Browser; |
7
|
|
|
use LaravelDuskReporter\Reporter; |
8
|
|
|
|
9
|
|
|
class ReportFile implements ReportFileContract { |
10
|
|
|
protected Reporter $reporter; |
11
|
|
|
|
12
|
|
|
protected string $name; |
13
|
|
|
|
14
|
|
|
protected string $newLine; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* ReportFile constructor. |
18
|
|
|
* |
19
|
|
|
* @param Reporter $reporter |
20
|
|
|
* @param string $name |
21
|
|
|
*/ |
22
|
|
|
public function __construct( Reporter $reporter, string $name ) { |
23
|
|
|
$this->reporter = $reporter; |
24
|
|
|
$this->name = $name; |
25
|
|
|
$this->setNewLine(); |
26
|
|
|
|
27
|
|
|
$this->reporter->addToTableOfContents( $this->fileName() ); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param string|null $newLine |
32
|
|
|
* |
33
|
|
|
* @return $this |
34
|
|
|
*/ |
35
|
|
|
public function setNewLine( ?string $newLine = null ): static { |
36
|
|
|
$this->newLine = is_string( $newLine ) ? $newLine : '<br>' . PHP_EOL; |
37
|
|
|
|
38
|
|
|
return $this; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @inheritDoc |
43
|
|
|
*/ |
44
|
|
|
public function isEmpty(): bool { |
45
|
|
|
$filePath = $this->reporter->reportFileName( $this->fileName() ); |
46
|
|
|
clearstatcache(); |
47
|
|
|
|
48
|
|
|
return ! ( file_exists( $filePath ) && filesize( $filePath ) ); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @inheritDoc |
53
|
|
|
*/ |
54
|
|
|
public function raw( string $content = '', bool|string $newLine = false ): static { |
55
|
|
|
return $this->addContent( $content, $newLine ); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @inheritDoc |
60
|
|
|
*/ |
61
|
|
|
public function h1( string $content = '', bool|string $newLine = PHP_EOL . PHP_EOL ): static { |
62
|
|
|
return $this->addContent( "# {$content}", $newLine ); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @inheritDoc |
67
|
|
|
*/ |
68
|
|
|
public function h2( string $content = '', bool|string $newLine = PHP_EOL . PHP_EOL ): static { |
69
|
|
|
return $this->addContent( "## {$content}", $newLine ); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @inheritDoc |
74
|
|
|
*/ |
75
|
|
|
public function h3( string $content = '', bool|string $newLine = PHP_EOL . PHP_EOL ): static { |
76
|
|
|
return $this->addContent( "### {$content}", $newLine ); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @inheritDoc |
81
|
|
|
*/ |
82
|
|
|
public function h4( string $content = '', bool|string $newLine = PHP_EOL . PHP_EOL ): static { |
83
|
|
|
return $this->addContent( "#### {$content}", $newLine ); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @inheritDoc |
88
|
|
|
*/ |
89
|
|
|
public function h5( string $content = '', bool|string $newLine = PHP_EOL . PHP_EOL ): static { |
90
|
|
|
return $this->addContent( "##### {$content}", $newLine ); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @inheritDoc |
95
|
|
|
*/ |
96
|
|
|
public function h6( string $content = '', bool|string $newLine = PHP_EOL . PHP_EOL ): static { |
97
|
|
|
return $this->addContent( "###### {$content}", $newLine ); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @inheritDoc |
102
|
|
|
*/ |
103
|
|
|
public function p( string $content = '', bool|string $newLine = true ): static { |
104
|
|
|
return $this->addContent( $content, $newLine ); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @inheritDoc |
109
|
|
|
*/ |
110
|
|
|
public function br( int $count = 1, ?string $lineString = null ): static { |
111
|
|
|
foreach ( range( 1, $count ) as $num ) { |
112
|
|
|
$this->addContent( '', $lineString ?? true ); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @inheritDoc |
120
|
|
|
*/ |
121
|
|
|
public function list( array|\ArrayAccess $items = [], bool|string $newLine = PHP_EOL, string $styleType = '-' ): static { |
122
|
|
|
if ( count( $items ) ) { |
123
|
|
|
foreach ( $items as $item ) { |
124
|
|
|
$this->listItem( $item, true, $styleType ); |
125
|
|
|
} |
126
|
|
|
if ( $newLine ) { |
127
|
|
|
$this->br( lineString: is_string( $newLine ) ? $newLine : null ); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $this; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @inheritDoc |
136
|
|
|
*/ |
137
|
|
|
public function listItem( string $content = '', bool|string $newLine = PHP_EOL, string $styleType = '-' ): static { |
138
|
|
|
return $this->addContent( "{$styleType} {$content}", $newLine ); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @inheritDoc |
143
|
|
|
*/ |
144
|
|
|
public function image( string $url, string $alt = '', bool|string $newLine = true ): static { |
145
|
|
|
return $this->addContent( "", $newLine ); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @inheritDoc |
150
|
|
|
*/ |
151
|
|
|
public function link( string $url, string $text = '', bool|string $newLine = false ): static { |
152
|
|
|
return $this->addContent( "[{$text}]({$url})", $newLine ); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @inheritDoc |
157
|
|
|
*/ |
158
|
|
|
public function screenshot( Browser $browser, ?string $resize = null, ?string $suffix = null, bool|string $newLine = true ): static { |
159
|
|
|
if ( $this->reporter->isScreenshotsDisabled() ) { |
160
|
|
|
return $this; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
$filename = $filepath = $this->reporter->screenshoter()->make( $browser, $this->fileName(), $resize, $suffix ); |
164
|
|
|
|
165
|
|
|
if ( $this->reporter->useScreenshotRelativePath() ) { |
166
|
|
|
$filepath = $this->filePrefix() . Str::afterLast( $filepath, $this->filePrefix() ); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return $this->addContent( "", $newLine ); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @inheritDoc |
174
|
|
|
*/ |
175
|
|
|
public function screenshotWithVisibleScreen( Browser $browser, ?string $suffix = null, bool|string $newLine = true ): static { |
176
|
|
|
return $this->screenshot( $browser, null, $suffix, $newLine ); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @inheritDoc |
181
|
|
|
*/ |
182
|
|
|
public function screenshotWithFitScreen( Browser $browser, ?string $suffix = null, bool|string $newLine = true ): static { |
183
|
|
|
return $this->screenshot( $browser, ReportScreenshotContract::RESIZE_FIT, $suffix, $newLine ); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @inheritDoc |
188
|
|
|
*/ |
189
|
|
|
public function screenshotWithCombineScreen( Browser $browser, ?string $suffix = null, bool|string $newLine = true ): static { |
190
|
|
|
return $this->screenshot( $browser, ReportScreenshotContract::RESIZE_COMBINE, $suffix, $newLine ); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @return string |
195
|
|
|
*/ |
196
|
|
|
public function fileName(): string { |
197
|
|
|
return $this->name; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @return string |
202
|
|
|
*/ |
203
|
|
|
public function filePrefix(): string { |
204
|
|
|
return Str::afterLast( $this->fileName(), '/' ); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Save content. |
209
|
|
|
* |
210
|
|
|
* @param string $content |
211
|
|
|
* @param bool|string $newLine |
212
|
|
|
* |
213
|
|
|
* @return static |
214
|
|
|
*/ |
215
|
|
|
protected function addContent( string $content = '', bool|string $newLine = true ): static { |
216
|
|
|
return $this->appendToFile( $content . ( is_string( $newLine ) ? $newLine : ( $newLine ? $this->newLine : '' ) ) ); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Append content to file. |
221
|
|
|
* |
222
|
|
|
* @param string $content |
223
|
|
|
* |
224
|
|
|
* @return static |
225
|
|
|
*/ |
226
|
|
|
protected function appendToFile( string $content ): static { |
227
|
|
|
if ( ! $this->reporter->isReportingDisabled() ) { |
228
|
|
|
$filePath = $this->reporter->reportFileName( $this->fileName() ); |
229
|
|
|
|
230
|
|
|
if ( ! is_dir( $directoryPath = dirname( $filePath ) ) ) { |
231
|
|
|
mkdir( $directoryPath, 0777, true ); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
file_put_contents( $filePath, $content, FILE_APPEND | LOCK_EX ); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
return $this; |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|