|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* YAWIK |
|
4
|
|
|
* |
|
5
|
|
|
* @filesource |
|
6
|
|
|
* @license MIT |
|
7
|
|
|
* @copyright 2013 - 2017 Cross Solution <http://cross-solution.de> |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Yawik\Behat; |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
use Auth\Entity\User; |
|
14
|
|
|
use Behat\Behat\Hook\Scope\BeforeScenarioScope; |
|
15
|
|
|
use Behat\MinkExtension\Context\MinkContext; |
|
16
|
|
|
use Core\Repository\RepositoryInterface; |
|
17
|
|
|
use Zend\View\Helper\Url; |
|
18
|
|
|
|
|
19
|
|
|
trait CommonContextTrait |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var MinkContext |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $minkContext; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var CoreContext |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $coreContext; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var UserContext |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $userContext; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var SummaryFormContext |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $summaryFormContext; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @BeforeScenario |
|
43
|
|
|
* |
|
44
|
|
|
* @param BeforeScenarioScope $scope |
|
45
|
|
|
*/ |
|
46
|
|
|
final public function gatherContexts(BeforeScenarioScope $scope) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->minkContext = $scope->getEnvironment()->getContext(MinkContext::class); |
|
|
|
|
|
|
49
|
|
|
$this->coreContext = $scope->getEnvironment()->getContext(CoreContext::class); |
|
|
|
|
|
|
50
|
|
|
$this->userContext = $scope->getEnvironment()->getContext(UserContext::class); |
|
|
|
|
|
|
51
|
|
|
$this->summaryFormContext = $scope->getEnvironment()->getContext(SummaryFormContext::class); |
|
|
|
|
|
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function buildUrl($name, array $params=array(), array $options=array()) |
|
55
|
|
|
{ |
|
56
|
|
|
$defaults = ['lang'=>'en']; |
|
57
|
|
|
$params = array_merge($defaults,$params); |
|
58
|
|
|
/* @var Url $urlHelper */ |
|
59
|
|
|
$urlHelper = $this |
|
60
|
|
|
->getService('ViewHelperManager') |
|
61
|
|
|
->get('url') |
|
62
|
|
|
; |
|
63
|
|
|
$url = $urlHelper($name,$params,$options); |
|
64
|
|
|
|
|
65
|
|
|
return $this->coreContext->generateUrl($url); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function visit($url) |
|
69
|
|
|
{ |
|
70
|
|
|
$this->coreContext->iVisit($url); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param $id |
|
75
|
|
|
* @return mixed|object |
|
76
|
|
|
*/ |
|
77
|
|
|
public function getService($id) |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->coreContext->getServiceManager()->get($id); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param $id |
|
84
|
|
|
* |
|
85
|
|
|
* @return RepositoryInterface |
|
86
|
|
|
*/ |
|
87
|
|
|
public function getRepository($id) |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->coreContext->getRepositories()->get($id); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @return UserContext |
|
94
|
|
|
*/ |
|
95
|
|
|
public function getUserContext() |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->userContext; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
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: