Passed
Push — master ( 4ee9ec...849354 )
by San
04:18 queued 03:37
created

HttpCallListener::afterStep()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 30
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 30
rs 8.439
c 1
b 0
f 0
cc 5
eloc 14
nc 5
nop 1
1
<?php
2
3
namespace Behatch\HttpCall;
4
5
use Behat\Behat\EventDispatcher\Event\StepTested;
6
use Behat\Behat\EventDispatcher\Event\AfterStepTested;
7
use Behat\Behat\Tester\Result\ExecutedStepResult;
8
use Behat\Mink\Mink;
9
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
10
11
class HttpCallListener implements EventSubscriberInterface
12
{
13
    private $contextSupportedVoter;
14
15
    private $httpCallResultPool;
16
17
    private $mink;
18
19
    public function __construct(ContextSupportedVoter $contextSupportedVoter, HttpCallResultPool $httpCallResultPool, Mink $mink)
20
    {
21
        $this->contextSupportedVoter = $contextSupportedVoter;
22
        $this->httpCallResultPool = $httpCallResultPool;
23
        $this->mink = $mink;
24
    }
25
26
    public static function getSubscribedEvents()
27
    {
28
        return array(
29
           StepTested::AFTER => 'afterStep'
30
        );
31
    }
32
33
    public function afterStep(AfterStepTested $event)
34
    {
35
        $testResult = $event->getTestResult();
36
37
        if (!$testResult instanceof ExecutedStepResult) {
38
            return;
39
        }
40
41
        $httpCallResult = new HttpCallResult(
42
            $testResult->getCallResult()->getReturn()
43
        );
44
45
        if ($this->contextSupportedVoter->vote($httpCallResult)) {
46
            $this->httpCallResultPool->store($httpCallResult);
47
48
            return true;
49
        }
50
51
        // For now to avoid modification on MinkContext
52
        // We add fallback on Mink
53
        try {
54
            $this->httpCallResultPool->store(
55
                new HttpCallResult($this->mink->getSession()->getPage()->getContent())
56
            );
57
        } catch (\LogicException $e) {
58
            // Mink has no response
59
        } catch (\Behat\Mink\Exception\DriverException $e) {
60
            // No Mink
61
        }
62
    }
63
}
64