1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Behat Symfony2Extension |
5
|
|
|
* |
6
|
|
|
* (c) Konstantin Kudryashov <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Behat\Symfony2Extension\Context\Initializer; |
13
|
|
|
|
14
|
|
|
use Behat\Behat\Context\Context; |
15
|
|
|
use Behat\Behat\Context\Initializer\ContextInitializer; |
16
|
|
|
use Behat\Behat\EventDispatcher\Event\ExampleTested; |
17
|
|
|
use Behat\Behat\EventDispatcher\Event\ScenarioTested; |
18
|
|
|
use Behat\Symfony2Extension\Context\KernelAwareContext; |
19
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
20
|
|
|
use Symfony\Component\HttpKernel\KernelInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Kernel aware contexts initializer. |
24
|
|
|
* Sets Kernel instance to the KernelAware contexts. |
25
|
|
|
* |
26
|
|
|
* @author Konstantin Kudryashov <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
final class KernelAwareInitializer implements ContextInitializer, EventSubscriberInterface |
29
|
|
|
{ |
30
|
|
|
private $kernel; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Initializes initializer. |
34
|
|
|
* |
35
|
|
|
* @param KernelInterface $kernel |
36
|
|
|
*/ |
37
|
|
|
public function __construct(KernelInterface $kernel) |
38
|
|
|
{ |
39
|
|
|
$this->kernel = $kernel; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
|
|
public static function getSubscribedEvents() |
46
|
|
|
{ |
47
|
|
|
return array( |
48
|
|
|
ScenarioTested::AFTER => array('rebootKernel', -15), |
49
|
|
|
ExampleTested::AFTER => array('rebootKernel', -15), |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
|
|
public function initializeContext(Context $context) |
57
|
|
|
{ |
58
|
|
|
if (!$context instanceof KernelAwareContext && !$this->usesKernelDictionary($context)) { |
59
|
|
|
return; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$context->setKernel($this->kernel); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Reboots HttpKernel after each scenario. |
67
|
|
|
*/ |
68
|
|
|
public function rebootKernel() |
69
|
|
|
{ |
70
|
|
|
$this->kernel->shutdown(); |
71
|
|
|
$this->kernel->boot(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Checks whether the context uses the KernelDictionary trait. |
76
|
|
|
* |
77
|
|
|
* @param Context $context |
78
|
|
|
* |
79
|
|
|
* @return boolean |
80
|
|
|
*/ |
81
|
|
|
private function usesKernelDictionary(Context $context) |
82
|
|
|
{ |
83
|
|
|
$refl = new \ReflectionObject($context); |
84
|
|
|
if (method_exists($refl, 'getTraitNames')) { |
85
|
|
|
if (in_array('Behat\\Symfony2Extension\\Context\\KernelDictionary', $refl->getTraitNames())) { |
86
|
|
|
return true; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return false; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
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: