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

IsolatedSessionStrategy::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 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 Behat\Mink\Session;
17
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
18
19
/**
20
 * Produces a new Session object shared for each test.
21
 *
22
 * @method \Mockery\Expectation shouldReceive(string $name)
23
 */
24
class IsolatedSessionStrategy implements ISessionStrategy
25
{
26
27
    /**
28
     * Session factory.
29
     *
30
     * @var ISessionFactory
31
     */
32
    private $_sessionFactory;
33
34
    /**
35
     * Creates isolated session strategy instance.
36
     *
37
     * @param ISessionFactory $session_factory Session factory.
38
     */
39 7
    public function __construct(ISessionFactory $session_factory)
40
    {
41 7
        $this->_sessionFactory = $session_factory;
42 7
    }
43
44
    /**
45
     * Returns an array of event names this subscriber wants to listen to.
46
     *
47
     * @return array The event names to listen to
48
     */
49 7
    public static function getSubscribedEvents()
50
    {
51
        return array(
52 7
            BrowserTestCase::TEST_ENDED_EVENT => array('onTestEnd', 0),
53
        );
54
    }
55
56
    /**
57
     * Sets event dispatcher.
58
     *
59
     * @param EventDispatcherInterface $event_dispatcher Event dispatcher.
60
     *
61
     * @return void
62
     */
63 7
    public function setEventDispatcher(EventDispatcherInterface $event_dispatcher)
64
    {
65 7
        $event_dispatcher->addSubscriber($this);
66 7
    }
67
68
    /**
69
     * Returns Mink session with given browser configuration.
70
     *
71
     * @param BrowserConfiguration $browser Browser configuration for a session.
72
     *
73
     * @return Session
74
     */
75 1
    public function session(BrowserConfiguration $browser)
76
    {
77 1
        return $this->_sessionFactory->createSession($browser);
78
    }
79
80
    /**
81
     * Called, when test ends.
82
     *
83
     * @param TestEvent $event Test event.
84
     *
85
     * @return void
86
     */
87 3
    public function onTestEnd(TestEvent $event)
88
    {
89 3
        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...
90
            return;
91
        }
92
93 3
        $session = $event->getSession();
94
95 3
        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...
96 2
            $session->stop();
97
        }
98 3
    }
99
100
    /**
101
     * Checks, that event can be handled by this class.
102
     *
103
     * @param TestEvent $event Test event.
104
     *
105
     * @return boolean
106
     */
107 3
    private function _isEventForMe(TestEvent $event)
108
    {
109 3
        return $event->getTestCase()->getSessionStrategy() instanceof self;
110
    }
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line after function; 0 found
Loading history...
111
}
112