-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathScreenshotContextInitializer.php
More file actions
112 lines (100 loc) · 2.88 KB
/
ScreenshotContextInitializer.php
File metadata and controls
112 lines (100 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
declare(strict_types=1);
namespace DrevOps\BehatScreenshotExtension\Context\Initializer;
use Behat\Behat\Context\Context;
use Behat\Behat\Context\Initializer\ContextInitializer;
use DrevOps\BehatScreenshotExtension\Context\ScreenshotAwareContextInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
/**
* Class ScreenshotContextInitializer.
*/
class ScreenshotContextInitializer implements ContextInitializer {
/**
* Flag to purge files in the directory.
*/
protected bool $needsPurging = TRUE;
/**
* ScreenshotContextInitializer constructor.
*
* @param string $dir
* Screenshot dir.
* @param bool $onFailed
* Create screenshot on failed test.
* @param string $failedPrefix
* File name prefix for a failed test.
* @param bool $purge
* Purge dir before start script.
* @param bool $alwaysFullscreen
* Always take fullscreen screenshots.
* @param bool $onEveryStep
* Capture screenshot after every step.
* @param string $filenamePattern
* File name pattern.
* @param string $filenamePatternFailed
* File name pattern failed.
* @param array<int,string> $infoTypes
* Show these info types in the screenshot.
*
* @codeCoverageIgnore
*/
public function __construct(
protected string $dir,
protected bool $onFailed,
private readonly string $failedPrefix,
protected bool $purge,
protected bool $alwaysFullscreen,
protected bool $onEveryStep,
protected string $filenamePattern,
protected string $filenamePatternFailed,
protected array $infoTypes = [],
) {
}
/**
* {@inheritdoc}
*/
public function initializeContext(Context $context): void {
if ($context instanceof ScreenshotAwareContextInterface) {
$dir = getenv('BEHAT_SCREENSHOT_DIR') ?: $this->dir;
if ((getenv('BEHAT_SCREENSHOT_PURGE') || $this->purge) && $this->needsPurging) {
$fs = $this->getFilesystem();
if ($fs->exists($dir)) {
$fs->remove($this->getFinder()->files()->in($dir));
}
$this->needsPurging = FALSE;
}
$context->setScreenshotParameters(
$dir,
$this->onFailed,
$this->failedPrefix,
$this->alwaysFullscreen,
$this->onEveryStep,
$this->filenamePattern,
$this->filenamePatternFailed,
$this->infoTypes
);
}
}
/**
* Get filesystem instance.
*
* @return \Symfony\Component\Filesystem\Filesystem
* Filesystem instance.
*/
protected function getFilesystem(): Filesystem {
// @codeCoverageIgnoreStart
return new Filesystem();
// @codeCoverageIgnoreEnd
}
/**
* Get finder instance.
*
* @return \Symfony\Component\Finder\Finder
* Finder instance.
*/
protected function getFinder(): Finder {
// @codeCoverageIgnoreStart
return new Finder();
// @codeCoverageIgnoreEnd
}
}