Failed Conditions
Pull Request — master (#12)
by Tibor
03:44
created

ScreenshotListener::takeScreenshot()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
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\EventDispatcher\Event\AfterTested;
10
use Behat\Testwork\Tester\Result\TestResult;
11
use Bex\Behat\ScreenshotExtension\ServiceContainer\Config;
12
use Bex\Behat\ScreenshotExtension\Service\FilenameGenerator;
13
use Bex\Behat\ScreenshotExtension\Service\ScreenshotTaker;
14
use Bex\Behat\ScreenshotExtension\Service\ScreenshotUploader;
15
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
16
17
/**
18
 * This class is responsible to decide when to make a screenshot
19
 *
20
 * @license http://opensource.org/licenses/MIT The MIT License
21
 */
22
final class ScreenshotListener implements EventSubscriberInterface
23
{
24
    /**
25
     * @var Config
26
     */
27
    private $config;
28
29
    /**
30
     * @var ScreenshotTaker
31
     */
32
    private $screenshotTaker;
33
34
    /**
35
     * @var FilenameGenerator
36
     */
37
    private $filenameGenerator;
38
    
39
    /**
40
     * @var ScreenshotUploader
41
     */
42
    private $screenshotUploader;
43
44
    /**
45
     * @param Config             $config
46
     * @param ScreenshotTaker    $screenshotTaker
47
     * @param FilenameGenerator  $filenameGenerator
48
     * @param ScreenshotUploader $screenshotUploader
49
     */
50
    public function __construct(
51
        Config $config,
52
        ScreenshotTaker $screenshotTaker, 
53
        FilenameGenerator $filenameGenerator, 
54
        ScreenshotUploader $screenshotUploader
55
    ) {
56
        $this->config = $config;
57
        $this->screenshotTaker = $screenshotTaker;
58
        $this->filenameGenerator = $filenameGenerator;
59
        $this->screenshotUploader = $screenshotUploader;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public static function getSubscribedEvents()
66
    {
67
        return [
68
            StepTested::AFTER => 'takeScreenshot',
69
            ScenarioTested::AFTER => 'saveScreenshot'
70
        ];
71
    }
72
73
    /**
74
     * Take screenshot after each real (not skipped) step
75
     *
76
     * @param AfterStepTested $event
77
     */
78
    public function takeScreenshot(AfterStepTested $event)
79
    {
80
        if ($this->shouldTakeScreenshot($event)) {
81
            $this->screenshotTaker->takeScreenshot();
82
        }
83
    }
84
    
85
    /**
86
     * Save screenshot after scanerio if required
87
     * 
88
     * @param  AfterScenarioTested $event
89
     */
90
    public function saveScreenshot(AfterScenarioTested $event)
91
    {
92
        if ($this->shouldSaveScreenshot($event)) {
93
            $fileName = $this->filenameGenerator->generateFileName($event->getFeature(), $event->getScenario());
94
            $image = $this->screenshotTaker->getImage();
95
            $this->screenshotUploader->upload($image, $fileName);
96
        }
97
98
        $this->screenshotTaker->reset();
99
    }
100
101
    /**
102
     * @param  AfterTested $event
103
     *
104
     * @return boolean
105
     */
106
    private function shouldTakeScreenshot(AfterTested $event)
107
    {
108
        return $event->getTestResult()->getResultCode() !== TestResult::SKIPPED;
109
    }
110
111
    /**
112
     * @param  AfterTested $event
113
     *
114
     * @return boolean
115
     */
116
    private function shouldSaveScreenshot(AfterTested $event)
117
    {
118
        $isScanerioFailed = $event->getTestResult()->getResultCode() === TestResult::FAILED;
119
        $shouldRecordAllScenarios = $this->config->shouldRecordAllScenarios();
120
121
        return $isScanerioFailed || $shouldRecordAllScenarios;      
122
    }
123
}
124