Passed
Push — master ( 99257e...b8bfe3 )
by Yaroslav
02:35
created

TestWithDuskReport::duskReportFile()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
dl 0
loc 13
ccs 0
cts 8
cp 0
rs 9.6111
c 1
b 0
f 0
cc 5
nc 4
nop 2
crap 30
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::camel(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 = 'index.md';
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, 'Go home')->br()
100
                 ->listItem('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