Completed
Branch feature/multi-screenshot (3b2450)
by Geza
12:02
created

ScreenshotListener::cleanupAfterScenario()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Bex\Behat\ScreenshotExtension\Listener;
4
5
use Behat\Behat\EventDispatcher\Event\AfterScenarioTested;
6
use Behat\Behat\EventDispatcher\Event\AfterStepTested;
7
use Behat\Behat\EventDispatcher\Event\ScenarioTested;
8
use Behat\Behat\EventDispatcher\Event\StepTested;
9
use Behat\Testwork\Tester\Result\TestResult;
10
use Bex\Behat\ScreenshotExtension\Service\ScreenshotTaker;
11
use Bex\Behat\ScreenshotExtension\Service\ScreenshotUploader;
12
use Bex\Behat\ScreenshotExtension\Service\StepFilenameGenerator;
13
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
14
15
/**
16
 * This class is responsible to decide when to make a screenshot
17
 *
18
 * @license http://opensource.org/licenses/MIT The MIT License
19
 */
20
final class ScreenshotListener implements EventSubscriberInterface
21
{
22
    /**
23
     * @var ScreenshotTaker
24
     */
25
    private $screenshotTaker;
26
27
    /**
28
     * @var StepFilenameGenerator
29
     */
30
    private $filenameGenerator;
31
    
32
    /**
33
     * @var ScreenshotUploader
34
     */
35
    private $screenshotUploader;
36
37
    /**
38
     * Constructor
39
     *
40
     * @param ScreenshotTaker $screenshotTaker
41
     * @param StepFilenameGenerator $filenameGenerator
42
     * @param ScreenshotUploader $screenshotUploader
43
     */
44
    public function __construct(
45
        ScreenshotTaker $screenshotTaker, 
46
        StepFilenameGenerator $filenameGenerator, 
47
        ScreenshotUploader $screenshotUploader
48
    ) {
49
        $this->screenshotTaker = $screenshotTaker;
50
        $this->filenameGenerator = $filenameGenerator;
51
        $this->screenshotUploader = $screenshotUploader;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public static function getSubscribedEvents()
58
    {
59
        return [
60
            StepTested::AFTER => 'checkAfterStep',
61
            ScenarioTested::AFTER => 'cleanupAfterScenario',
62
        ];
63
    }
64
65
    /**
66
     * Take screenshot after a failed step
67
     *
68
     * @param AfterStepTested $event
69
     */
70
    public function checkAfterStep(AfterStepTested $event)
71
    {
72
        $this->screenshotTaker->takeScreenshot();
73
        if ($event->getTestResult()->getResultCode() === TestResult::FAILED) {
74
            $stepFileName = $this->filenameGenerator->convertStepToFileName($event->getStep());
75
            $image = $this->screenshotTaker->getCombinedImage();
76
            $this->screenshotUploader->upload($image, $stepFileName);
77
        }
78
    }
79
    
80
    public function cleanupAfterScenario()
81
    {
82
        $this->screenshotTaker->reset();
83
    }
84
}
85