1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NuvoleWeb\Drupal\DrupalExtension\Context; |
4
|
|
|
|
5
|
|
|
use Behat\Mink\Exception\ExpectationException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class TaxonomyTermContext. |
9
|
|
|
* |
10
|
|
|
* @package NuvoleWeb\Drupal\DrupalExtension\Context |
11
|
|
|
*/ |
12
|
|
|
class TaxonomyTermContext extends RawDrupalContext { |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Visit taxonomy term page given its type and name. |
16
|
|
|
* |
17
|
|
|
* @Given I visit the :type term :title |
18
|
|
|
* @Given I am visiting the :type term :title |
19
|
|
|
*/ |
20
|
|
|
public function iAmViewingTheTerm($type, $title) { |
21
|
|
|
$this->visitTermPage('view', $type, $title); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Visit taxonomy term edit page given its type and name. |
26
|
|
|
* |
27
|
|
|
* @Given I am editing the :type term :title |
28
|
|
|
* @Given I edit the :type term :title |
29
|
|
|
*/ |
30
|
|
|
public function iAmEditingTheTerm($type, $title) { |
31
|
|
|
$this->visitTermPage('edit', $type, $title); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Visit taxonomy term delete page given its type and name. |
36
|
|
|
* |
37
|
|
|
* @Given I am deleting the :type term :title |
38
|
|
|
* @Given I delete the :type term :title |
39
|
|
|
*/ |
40
|
|
|
public function iAmDeletingTheTerm($type, $title) { |
41
|
|
|
$this->visitTermPage('delete', $type, $title); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Provides a common step definition callback for taxonomy term pages. |
46
|
|
|
* |
47
|
|
|
* @param string $op |
48
|
|
|
* The operation being performed: 'view', 'edit', 'delete'. |
49
|
|
|
* @param string $type |
50
|
|
|
* The term's vocabulary, either as machine name or label. |
51
|
|
|
* @param string $name |
52
|
|
|
* The term name. |
53
|
|
|
* |
54
|
|
|
* @throws ExpectationException |
55
|
|
|
* When the term does not exist. |
56
|
|
|
*/ |
57
|
|
|
protected function visitTermPage($op, $type, $name) { |
58
|
|
|
$type = $this->getCore()->convertLabelToTermTypeId($type); |
|
|
|
|
59
|
|
|
$term = $this->getCore()->loadTaxonomyTermByName($type, $name); |
|
|
|
|
60
|
|
|
|
61
|
|
|
if (!empty($term)) { |
62
|
|
|
$path = [ |
63
|
|
|
'view' => "taxonomy/term/{$this->getCore()->getTaxonomyTermId($term)}", |
|
|
|
|
64
|
|
|
'edit' => "taxonomy/term/{$this->getCore()->getTaxonomyTermId($term)}/edit", |
|
|
|
|
65
|
|
|
'delete' => "taxonomy/term/{$this->getCore()->getTaxonomyTermId($term)}/delete", |
|
|
|
|
66
|
|
|
]; |
67
|
|
|
$this->visitPath($path[$op]); |
68
|
|
|
} |
69
|
|
|
else { |
70
|
|
|
throw new ExpectationException("No taxonomy term with vocabulary '$type' and name '$name' has been found.", $this->getSession()); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
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: