1 | <?php |
||
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() |
||
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) |
||
122 | |||
123 | public function setAjaxSteps($ajaxSteps) |
||
124 | { |
||
129 | |||
130 | public function getAjaxSteps() |
||
134 | |||
135 | public function setAjaxTimeout($ajaxTimeout) |
||
139 | |||
140 | public function getAjaxTimeout() |
||
144 | |||
145 | public function setAdminUrl($adminUrl) |
||
149 | |||
150 | public function getAdminUrl() |
||
154 | |||
155 | public function setLoginUrl($loginUrl) |
||
159 | |||
160 | public function getLoginUrl() |
||
164 | |||
165 | public function setScreenshotPath($screenshotPath) |
||
169 | |||
170 | public function getScreenshotPath() |
||
174 | |||
175 | public function getRegionMap() |
||
179 | |||
180 | public function setRegionMap($regionMap) |
||
184 | |||
185 | /** |
||
186 | * @param String Absolute path to 'framework' module |
||
187 | */ |
||
188 | protected function bootstrap($frameworkPath) |
||
200 | } |
||
201 |
Let’s take a look at an example:
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
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: