SilverStripeAwareInitializer   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 176
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 9
Bugs 0 Features 1
Metric Value
c 9
b 0
f 1
dl 0
loc 176
rs 10
wmc 19
lcom 1
cbo 1

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __destruct() 0 10 2
A initialize() 0 10 1
A supports() 0 4 1
A __construct() 0 20 1
A setAjaxSteps() 0 6 2
A getAjaxSteps() 0 4 1
A setAjaxTimeout() 0 4 1
A getAjaxTimeout() 0 4 1
A setAdminUrl() 0 4 1
A getAdminUrl() 0 4 1
A setLoginUrl() 0 4 1
A getLoginUrl() 0 4 1
A setScreenshotPath() 0 4 1
A getScreenshotPath() 0 4 1
A getRegionMap() 0 4 1
A setRegionMap() 0 4 1
A bootstrap() 0 12 1
1
<?php
2
3
namespace SilverStripe\BehatExtension\Context\Initializer;
4
5
use Behat\Behat\Context\Initializer\InitializerInterface;
6
use Behat\Behat\Context\ContextInterface;
7
8
use SilverStripe\BehatExtension\Context\SilverStripeAwareContextInterface;
9
10
/*
11
 * This file is part of the Behat/SilverStripeExtension
12
 *
13
 * (c) Michał Ochman <[email protected]>
14
 *
15
 * For the full copyright and license information, please view the LICENSE
16
 * file that was distributed with this source code.
17
 */
18
19
/**
20
 * SilverStripe aware contexts initializer.
21
 * Sets SilverStripe instance to the SilverStripeAware contexts.
22
 *
23
 * @author Michał Ochman <[email protected]>
24
 */
25
class SilverStripeAwareInitializer implements InitializerInterface
26
{
27
    
28
    private $databaseName;
29
    
30
    /**
31
     * @var Array
32
     */
33
    protected $ajaxSteps;
34
35
    /**
36
     * @var Int Timeout in milliseconds
37
     */
38
    protected $ajaxTimeout;
39
40
    /**
41
     * @var String {@link see SilverStripeContext}
42
     */
43
    protected $adminUrl;
44
45
    /**
46
     * @var String {@link see SilverStripeContext}
47
     */
48
    protected $loginUrl;
49
50
    /**
51
     * @var String {@link see SilverStripeContext}
52
     */
53
    protected $screenshotPath;
54
55
    /**
56
     * @var object {@link TestSessionEnvironment}
57
     */
58
    protected $testSessionEnvironment;
59
60
    /**
61
     * Initializes initializer.
62
     */
63
    public function __construct($frameworkPath)
64
    {
65
        $this->bootstrap($frameworkPath);
66
67
        file_put_contents('php://stdout', "Creating test session environment" . PHP_EOL);
68
69
        $testEnv = \Injector::inst()->get('TestSessionEnvironment');
70
        $testEnv->startTestSession(array(
71
            'createDatabase' => true
72
        ));
73
74
        $state = $testEnv->getState();
75
76
        $this->databaseName = $state->database;
77
        $this->testSessionEnvironment = $testEnv;
78
79
        file_put_contents('php://stdout', "Temp Database: $this->databaseName" . PHP_EOL . PHP_EOL);
80
81
        register_shutdown_function(array($this, '__destruct'));
82
    }
83
84
    public function __destruct()
85
    {
86
        // Add condition here as register_shutdown_function() also calls this in __construct()
87
        if ($this->testSessionEnvironment) {
88
            file_put_contents('php://stdout', "Killing test session environment...");
89
            $this->testSessionEnvironment->endTestSession();
90
            $this->testSessionEnvironment = null;
91
            file_put_contents('php://stdout', " done!" . PHP_EOL);
92
        }
93
    }
94
95
    /**
96
     * Checks if initializer supports provided context.
97
     *
98
     * @param ContextInterface $context
99
     *
100
     * @return Boolean
101
     */
102
    public function supports(ContextInterface $context)
103
    {
104
        return $context instanceof SilverStripeAwareContextInterface;
105
    }
106
107
    /**
108
     * Initializes provided context.
109
     *
110
     * @param ContextInterface $context
111
     */
112
    public function initialize(ContextInterface $context)
113
    {
114
        $context->setDatabase($this->databaseName);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Behat\Behat\Context\ContextInterface as the method setDatabase() does only exist in the following implementations of said interface: SilverStripe\BehatExtens...ext\SilverStripeContext.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
115
        $context->setAjaxSteps($this->ajaxSteps);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Behat\Behat\Context\ContextInterface as the method setAjaxSteps() does only exist in the following implementations of said interface: SilverStripe\BehatExtens...ext\SilverStripeContext.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
116
        $context->setAjaxTimeout($this->ajaxTimeout);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Behat\Behat\Context\ContextInterface as the method setAjaxTimeout() does only exist in the following implementations of said interface: SilverStripe\BehatExtens...ext\SilverStripeContext.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
117
        $context->setScreenshotPath($this->screenshotPath);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Behat\Behat\Context\ContextInterface as the method setScreenshotPath() does only exist in the following implementations of said interface: SilverStripe\BehatExtens...ext\SilverStripeContext.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
118
        $context->setRegionMap($this->regionMap);
0 ignored issues
show
Bug introduced by
The property regionMap does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
It seems like you code against a concrete implementation and not the interface Behat\Behat\Context\ContextInterface as the method setRegionMap() does only exist in the following implementations of said interface: SilverStripe\BehatExtens...ext\SilverStripeContext.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
119
        $context->setAdminUrl($this->adminUrl);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Behat\Behat\Context\ContextInterface as the method setAdminUrl() does only exist in the following implementations of said interface: SilverStripe\BehatExtens...ext\SilverStripeContext.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
120
        $context->setLoginUrl($this->loginUrl);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Behat\Behat\Context\ContextInterface as the method setLoginUrl() does only exist in the following implementations of said interface: SilverStripe\BehatExtens...ext\SilverStripeContext.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
121
    }
122
123
    public function setAjaxSteps($ajaxSteps)
124
    {
125
        if ($ajaxSteps) {
126
            $this->ajaxSteps = $ajaxSteps;
127
        }
128
    }
129
130
    public function getAjaxSteps()
131
    {
132
        return $this->ajaxSteps;
133
    }
134
135
    public function setAjaxTimeout($ajaxTimeout)
136
    {
137
        $this->ajaxTimeout = $ajaxTimeout;
138
    }
139
140
    public function getAjaxTimeout()
141
    {
142
        return $this->ajaxTimeout;
143
    }
144
145
    public function setAdminUrl($adminUrl)
146
    {
147
        $this->adminUrl = $adminUrl;
148
    }
149
150
    public function getAdminUrl()
151
    {
152
        return $this->adminUrl;
153
    }
154
155
    public function setLoginUrl($loginUrl)
156
    {
157
        $this->loginUrl = $loginUrl;
158
    }
159
160
    public function getLoginUrl()
161
    {
162
        return $this->loginUrl;
163
    }
164
165
    public function setScreenshotPath($screenshotPath)
166
    {
167
        $this->screenshotPath = $screenshotPath;
168
    }
169
170
    public function getScreenshotPath()
171
    {
172
        return $this->screenshotPath;
173
    }
174
175
    public function getRegionMap()
176
    {
177
        return $this->regionMap;
178
    }
179
180
    public function setRegionMap($regionMap)
181
    {
182
        $this->regionMap = $regionMap;
183
    }
184
185
    /**
186
     * @param String Absolute path to 'framework' module
187
     */
188
    protected function bootstrap($frameworkPath)
189
    {
190
        file_put_contents('php://stdout', 'Bootstrapping' . PHP_EOL);
191
192
        // Connect to database and build manifest
193
        $_GET['flush'] = 1;
194
        require_once $frameworkPath . '/core/Core.php';
195
        unset($_GET['flush']);
196
197
        // Remove the error handler so that PHPUnit can add its own
198
        restore_error_handler();
199
    }
200
}
201