1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NuvoleWeb\Drupal\DrupalExtension\Context; |
4
|
|
|
|
5
|
|
|
use Behat\Behat\Context\SnippetAcceptingContext; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class ContentContext. |
9
|
|
|
* |
10
|
|
|
* @package NuvoleWeb\Drupal\DrupalExtension\Context |
11
|
|
|
*/ |
12
|
|
|
class ContentContext extends RawDrupalContext implements SnippetAcceptingContext { |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Assert viewing content given its type and title. |
16
|
|
|
* |
17
|
|
|
* @param string $type |
18
|
|
|
* Content type machine name. |
19
|
|
|
* @param string $title |
20
|
|
|
* Content title. |
21
|
|
|
* |
22
|
|
|
* @Given I am visiting the :type content :title |
23
|
|
|
* @Given I visit the :type content :title |
24
|
|
|
*/ |
25
|
|
|
public function iAmViewingTheContent($type, $title) { |
26
|
|
|
$this->visitContentPage('view', $type, $title); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Assert editing content given its type and title. |
31
|
|
|
* |
32
|
|
|
* @param string $type |
33
|
|
|
* Content type machine name. |
34
|
|
|
* @param string $title |
35
|
|
|
* Content title. |
36
|
|
|
* |
37
|
|
|
* @Given I am editing the :type content :title |
38
|
|
|
* @Given I edit the :type content :title |
39
|
|
|
*/ |
40
|
|
|
public function iAmEditingTheContent($type, $title) { |
41
|
|
|
$this->visitContentPage('edit', $type, $title); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Assert deleting content given its type and title. |
46
|
|
|
* |
47
|
|
|
* @param string $type |
48
|
|
|
* Content type machine name. |
49
|
|
|
* @param string $title |
50
|
|
|
* Content title. |
51
|
|
|
* |
52
|
|
|
* @Given I am deleting the :type content :title |
53
|
|
|
* @Given I delete the :type content :title |
54
|
|
|
*/ |
55
|
|
|
public function iAmDeletingTheContent($type, $title) { |
56
|
|
|
$this->visitContentPage('delete', $type, $title); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Provides a common step definition callback for node pages. |
61
|
|
|
* |
62
|
|
|
* @param string $op |
63
|
|
|
* The operation being performed: 'view', 'edit', 'delete'. |
64
|
|
|
* @param string $type |
65
|
|
|
* The node type either as id or as label. |
66
|
|
|
* @param string $title |
67
|
|
|
* The node title. |
68
|
|
|
*/ |
69
|
|
|
protected function visitContentPage($op, $type, $title) { |
70
|
|
|
$nid = $this->getCore()->getEntityIdByLabel('node', $type, $title); |
|
|
|
|
71
|
|
|
$path = [ |
72
|
|
|
'view' => "node/$nid", |
73
|
|
|
'edit' => "node/$nid/edit", |
74
|
|
|
'delete' => "node/$nid/delete", |
75
|
|
|
]; |
76
|
|
|
$this->visitPath($path[$op]); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
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: