|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace LaravelDuskReporter; |
|
5
|
|
|
|
|
6
|
|
|
use Carbon\Carbon; |
|
7
|
|
|
use Illuminate\Support\Str; |
|
8
|
|
|
use LaravelDuskReporter\Exceptions\LaravelDuskReporterException; |
|
9
|
|
|
use LaravelDuskReporter\Generation\ReportFileContract; |
|
10
|
|
|
|
|
11
|
|
|
trait TestWithDuskReport |
|
12
|
|
|
{ |
|
13
|
|
|
protected ?ReportFileContract $globalDuskTestReportFile = null; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Create report file for global usage |
|
17
|
|
|
* |
|
18
|
|
|
* @param string|null $initialisationFilename |
|
19
|
|
|
* @param \Closure|null $initialisationCallback |
|
20
|
|
|
* |
|
21
|
|
|
* @return ReportFileContract |
|
22
|
|
|
* @throws LaravelDuskReporterException |
|
23
|
|
|
*/ |
|
24
|
|
|
protected function duskReportFile(?string $initialisationFilename = null, ?\Closure $initialisationCallback = null): ReportFileContract |
|
25
|
|
|
{ |
|
26
|
|
|
if (!$this->globalDuskTestReportFile) { |
|
27
|
|
|
if (!$initialisationFilename) { |
|
28
|
|
|
throw new LaravelDuskReporterException('On initialisation you should specify $initialisationFilename'); |
|
29
|
|
|
} |
|
30
|
|
|
$this->globalDuskTestReportFile = $this->newDuskReportFile($initialisationFilename); |
|
31
|
|
|
if (is_callable($initialisationCallback) && $this->globalDuskTestReportFile->isEmpty()) { |
|
32
|
|
|
call_user_func_array($initialisationCallback, [ &$this->globalDuskTestReportFile ]); |
|
|
|
|
|
|
33
|
|
|
} |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
return $this->globalDuskTestReportFile; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Create new report file |
|
41
|
|
|
* |
|
42
|
|
|
* @param string|null $filename |
|
43
|
|
|
* |
|
44
|
|
|
* @return ReportFileContract |
|
45
|
|
|
*/ |
|
46
|
|
|
protected function newDuskReportFile(?string $filename = null): ReportFileContract |
|
47
|
|
|
{ |
|
48
|
|
|
return LaravelDuskReporter::newFile($filename ?? $this->duskReportFileName()); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Get File Name for Report |
|
53
|
|
|
* |
|
54
|
|
|
* @return string |
|
55
|
|
|
*/ |
|
56
|
|
|
protected function duskReportFileName(): string |
|
57
|
|
|
{ |
|
58
|
|
|
$names = array_reverse(explode('\\', get_class($this))); |
|
59
|
|
|
$reportFolderName = rtrim(Str::ucfirst(Str::camel(($names[1] ?? '') . $names[0])), '/'); |
|
60
|
|
|
|
|
61
|
|
|
if (method_exists($this, 'getDuskReportFileName')) { |
|
62
|
|
|
$name = $this->getDuskReportFileName(); |
|
63
|
|
|
} elseif (method_exists($this, 'getName')) { |
|
64
|
|
|
$name = $this->getName(); |
|
65
|
|
|
} else { |
|
66
|
|
|
$name = Carbon::now()->format('Y_m_d_h_i_s-') . Str::random(60); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return "{$reportFolderName}/{$name}"; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Set up report file with initial data. |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $name |
|
76
|
|
|
* @param string|null $title |
|
77
|
|
|
* |
|
78
|
|
|
* @return ReportFileContract |
|
79
|
|
|
* @throws LaravelDuskReporterException |
|
80
|
|
|
*/ |
|
81
|
|
|
protected function duskReportSetUp(string $name, ?string $title = null): ReportFileContract |
|
82
|
|
|
{ |
|
83
|
|
|
$name = trim($name, '/'); |
|
84
|
|
|
|
|
85
|
|
|
return $this->duskReportFile($name, function (ReportFileContract $file) use ($name, $title) { |
|
86
|
|
|
$file->h1($title ?? Str::title(Str::snake(Str::afterLast($name, '/'), ' '))); |
|
87
|
|
|
|
|
88
|
|
|
if (method_exists($this, 'duskReportFileInitialisationContent')) { |
|
89
|
|
|
$this->duskReportFileInitialisationContent($file); |
|
90
|
|
|
|
|
91
|
|
|
return; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$parts = explode('/', $name); |
|
95
|
|
|
$backPath = Reporter::getValidFileName(Reporter::$indexFileBaseName); |
|
96
|
|
|
for ($i = 1; $i < count($parts); $i++) { |
|
|
|
|
|
|
97
|
|
|
$backPath = "../{$backPath}"; |
|
98
|
|
|
} |
|
99
|
|
|
$file->link($backPath, trans('dusk-reporter::report.back_to_home_link'))->br(2, PHP_EOL) |
|
|
|
|
|
|
100
|
|
|
->listItem(trans('dusk-reporter::report.date_of_test', [ 'date' => Carbon::now()->format('Y-m-d H:i:s') ])); |
|
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
if (method_exists($this, 'duskReportFileInitialisationAdditionalContent')) { |
|
103
|
|
|
$this->duskReportFileInitialisationAdditionalContent($file); |
|
104
|
|
|
} |
|
105
|
|
|
}); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Set up report file with initial data using class name. |
|
110
|
|
|
* |
|
111
|
|
|
* @param string|null $path |
|
112
|
|
|
* @param string|null $title |
|
113
|
|
|
* |
|
114
|
|
|
* @return ReportFileContract |
|
115
|
|
|
* @throws LaravelDuskReporterException |
|
116
|
|
|
*/ |
|
117
|
|
|
protected function duskReportSetUpUsingTestClassName(?string $path = null, ?string $title = null): ReportFileContract |
|
118
|
|
|
{ |
|
119
|
|
|
$path = $path ? (rtrim($path, '/') . '/') : ''; |
|
120
|
|
|
$name = $path . trim(Str::beforeLast(class_basename(get_class($this)), 'Test'), '/'); |
|
121
|
|
|
|
|
122
|
|
|
return $this->duskReportSetUp($name, $title); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Add new heading to file using method name. |
|
127
|
|
|
* |
|
128
|
|
|
* @param string $methodName |
|
129
|
|
|
* @param string $headerType |
|
130
|
|
|
* |
|
131
|
|
|
* @return ReportFileContract |
|
132
|
|
|
* @throws LaravelDuskReporterException |
|
133
|
|
|
*/ |
|
134
|
|
|
protected function duskReportSetHeadingFromTestMethod(string $methodName, string $headerType = 'h2'): ReportFileContract |
|
135
|
|
|
{ |
|
136
|
|
|
return call_user_func([ |
|
137
|
|
|
$this->duskReportFile(), |
|
138
|
|
|
$headerType, |
|
139
|
|
|
], Str::title(Str::snake(Str::camel(Str::after($methodName, 'test')), ' '))); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Set up report file for entry class. |
|
144
|
|
|
* |
|
145
|
|
|
* @param string|null $testHeading |
|
146
|
|
|
* |
|
147
|
|
|
* @return ReportFileContract|null |
|
148
|
|
|
* @throws LaravelDuskReporterException |
|
149
|
|
|
*/ |
|
150
|
|
|
protected function setUpReportFileForClass(?string $testHeading = 'h2'): ?ReportFileContract |
|
151
|
|
|
{ |
|
152
|
|
|
if (property_exists($this, 'duskReportClassFilePath')) { |
|
153
|
|
|
$reporter = $this->duskReportSetUpUsingTestClassName($this->duskReportClassFilePath); |
|
154
|
|
|
if ($testHeading) { |
|
155
|
|
|
$this->duskReportSetHeadingFromTestMethod($this->getName(), $testHeading); |
|
|
|
|
|
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
return $reporter; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
return null; |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|