Passed
Push — master ( 998827...3c47cb )
by Yaroslav
02:35
created

TestWithDuskReport::setUpReportFileForClass()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 12
ccs 0
cts 7
cp 0
rs 10
cc 3
nc 3
nop 1
crap 12
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 ]);
0 ignored issues
show
Bug introduced by
It seems like $initialisationCallback can also be of type null; however, parameter $callback of call_user_func_array() does only seem to accept callable, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

32
                call_user_func_array(/** @scrutinizer ignore-type */ $initialisationCallback, [ &$this->globalDuskTestReportFile ]);
Loading history...
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++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
97
                $backPath = "../{$backPath}";
98
            }
99
            $file->link($backPath, trans('dusk-reporter::report.back_to_home_link'))->br(2, PHP_EOL)
0 ignored issues
show
Bug introduced by
It seems like trans('dusk-reporter::report.back_to_home_link') can also be of type array and array; however, parameter $text of LaravelDuskReporter\Gene...ortFileContract::link() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

99
            $file->link($backPath, /** @scrutinizer ignore-type */ trans('dusk-reporter::report.back_to_home_link'))->br(2, PHP_EOL)
Loading history...
100
                 ->listItem(trans('dusk-reporter::report.date_of_test', [ 'date' => Carbon::now()->format('Y-m-d H:i:s') ]));
0 ignored issues
show
Bug introduced by
It seems like trans('dusk-reporter::re...format('Y-m-d H:i:s'))) can also be of type array and array; however, parameter $content of LaravelDuskReporter\Gene...ileContract::listItem() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

100
                 ->listItem(/** @scrutinizer ignore-type */ trans('dusk-reporter::report.date_of_test', [ 'date' => Carbon::now()->format('Y-m-d H:i:s') ]));
Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like getName() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

155
                $this->duskReportSetHeadingFromTestMethod($this->/** @scrutinizer ignore-call */ getName(), $testHeading);
Loading history...
156
            }
157
158
            return $reporter;
159
        }
160
161
        return null;
162
    }
163
}
164