Failed Conditions
Pull Request — master (#102)
by
unknown
03:22
created

SharedSessionStrategy   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 97.62%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 6
dl 0
loc 157
ccs 41
cts 42
cp 0.9762
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getSubscribedEvents() 0 7 1
A setEventDispatcher() 0 4 1
A session() 0 15 3
A stopSession() 0 9 2
A _switchToMainWindow() 0 4 1
A onTestFailed() 0 10 3
A onTestSuiteEnd() 0 12 4
A _isEventForMe() 0 4 1
1
<?php
2
/**
3
 * This file is part of the phpunit-mink library.
4
 * For the full copyright and license information, please view
5
 * the LICENSE file that was distributed with this source code.
6
 *
7
 * @copyright Alexander Obuhovich <[email protected]>
8
 * @link      https://github.com/aik099/phpunit-mink
9
 */
10
11
namespace aik099\PHPUnit\Session;
0 ignored issues
show
introduced by
Expected 2 blank line(-s) after namespace declaration; 1 found
Loading history...
12
13
use aik099\PHPUnit\BrowserConfiguration\BrowserConfiguration;
14
use aik099\PHPUnit\BrowserTestCase;
15
use aik099\PHPUnit\Event\TestEvent;
16
use aik099\PHPUnit\Event\TestFailedEvent;
17
use Behat\Mink\Session;
18
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
19
20
/**
21
 * Keeps a Session object shared between test runs to save time.
22
 *
23
 * @method \Mockery\Expectation shouldReceive(string $name)
24
 */
25
class SharedSessionStrategy implements ISessionStrategy
26
{
27
28
    /**
29
     * Original session strategy.
30
     *
31
     * @var ISessionStrategy
32
     */
33
    private $_originalStrategy;
34
35
    /**
36
     * Reference to created session.
37
     *
38
     * @var Session
39
     */
40
    private $_session;
41
42
    /**
43
     * Remembers if last test failed.
44
     *
45
     * @var boolean
46
     */
47
    private $_lastTestFailed = false;
48
49
    /**
50
     * Remembers original session strategy upon shared strategy creation.
51
     *
52
     * @param ISessionStrategy $original_strategy Original session strategy.
53
     */
54 7
    public function __construct(ISessionStrategy $original_strategy)
55
    {
56 7
        $this->_originalStrategy = $original_strategy;
57 7
    }
58
59
    /**
60
     * Returns an array of event names this subscriber wants to listen to.
61
     *
62
     * @return array The event names to listen to
63
     */
64 7
    public static function getSubscribedEvents()
65
    {
66
        return array(
67 7
            BrowserTestCase::TEST_FAILED_EVENT => array('onTestFailed', 0),
68 7
            BrowserTestCase::TEST_SUITE_ENDED_EVENT => array('onTestSuiteEnd', 0),
69
        );
70
    }
71
72
    /**
73
     * Sets event dispatcher.
74
     *
75
     * @param EventDispatcherInterface $event_dispatcher Event dispatcher.
76
     *
77
     * @return void
78
     */
79 7
    public function setEventDispatcher(EventDispatcherInterface $event_dispatcher)
80
    {
81 7
        $event_dispatcher->addSubscriber($this);
82 7
    }
83
84
    /**
85
     * Returns Mink session with given browser configuration.
86
     *
87
     * @param BrowserConfiguration $browser Browser configuration for a session.
88
     *
89
     * @return Session
90
     */
91 5
    public function session(BrowserConfiguration $browser)
92
    {
93 5
        if ($this->_lastTestFailed) {
0 ignored issues
show
introduced by
Expected 1 spaces after "if" opening bracket; 0 found
Loading history...
introduced by
Expected 1 spaces before "if" closing bracket; 0 found
Loading history...
94 2
            $this->stopSession();
95 2
            $this->_lastTestFailed = false;
96
        }
97
98 5
        if ($this->_session === null) {
0 ignored issues
show
introduced by
Expected 1 spaces after "if" opening bracket; 0 found
Loading history...
introduced by
Expected 1 spaces before "if" closing bracket; 0 found
Loading history...
99 5
            $this->_session = $this->_originalStrategy->session($browser);
100
        } else {
0 ignored issues
show
introduced by
Beginning of the "else" control structure must be first content on the line
Loading history...
introduced by
Expected "}\nelse \n"; found " else {\n"
Loading history...
101 4
            $this->_switchToMainWindow();
102
        }
103
104 5
        return $this->_session;
105
    }
106
107
    /**
108
     * Stops session.
109
     *
110
     * @return void
111
     */
112 2
    protected function stopSession()
113
    {
114 2
        if ($this->_session === null) {
0 ignored issues
show
introduced by
Expected 1 spaces after "if" opening bracket; 0 found
Loading history...
introduced by
Expected 1 spaces before "if" closing bracket; 0 found
Loading history...
115 1
            return;
116
        }
117
118 1
        $this->_session->stop();
119 1
        $this->_session = null;
120 1
    }
121
122
    /**
123
     * Switches to window, that was created upon session creation.
124
     *
125
     * @return void
126
     */
127 4
    private function _switchToMainWindow()
128
    {
129 4
        $this->_session->switchToWindow(null);
130 4
    }
131
132
    /**
133
     * Called, when test fails.
134
     *
135
     * @param TestFailedEvent $event Test failed event.
136
     *
137
     * @return void
138
     */
139 4
    public function onTestFailed(TestFailedEvent $event)
140
    {
141 4
        if ($event->getException() instanceof \PHPUnit_Framework_IncompleteTestError) {
0 ignored issues
show
introduced by
Expected 1 spaces after "if" opening bracket; 0 found
Loading history...
introduced by
Expected 1 spaces before "if" closing bracket; 0 found
Loading history...
142 1
            return;
143 3
        } elseif ($event->getException() instanceof \PHPUnit_Framework_SkippedTestError) {
0 ignored issues
show
introduced by
Beginning of the "elseif" control structure must be first content on the line
Loading history...
introduced by
Expected "}\nelseif (...) \n"; found " elseif (...) {\n"
Loading history...
introduced by
Expected 1 spaces after "elseif" opening bracket; 0 found
Loading history...
introduced by
Expected 1 spaces before "elseif" closing bracket; 0 found
Loading history...
144 1
            return;
145
        }
146
147 2
        $this->_lastTestFailed = true;
148 2
    }
149
150
    /**
151
     * Called, when test case ends.
152
     *
153
     * @param TestEvent $event Test event.
154
     *
155
     * @return void
156
     */
157 1
    public function onTestSuiteEnd(TestEvent $event)
158
    {
159 1
        if (!$this->_isEventForMe($event)) {
0 ignored issues
show
introduced by
Expected 1 spaces after "if" opening bracket; 0 found
Loading history...
introduced by
Expected 1 spaces before "if" closing bracket; 0 found
Loading history...
160
            return;
161
        }
162
163 1
        $session = $event->getSession();
164
165 1
        if ($session !== null && $session->isStarted()) {
0 ignored issues
show
introduced by
Expected 1 spaces after "if" opening bracket; 0 found
Loading history...
introduced by
Expected 1 spaces before "if" closing bracket; 0 found
Loading history...
166 1
            $session->stop();
167
        }
168 1
    }
169
170
    /**
171
     * Checks, that event can be handled by this class.
172
     *
173
     * @param TestEvent $event Test event.
174
     *
175
     * @return boolean
176
     */
177 1
    private function _isEventForMe(TestEvent $event)
178
    {
179 1
        return $event->getTestCase()->getSessionStrategy() instanceof self;
180
    }
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line after function; 0 found
Loading history...
181
}
182