|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Vivait\AuthBundle\Features\Context; |
|
4
|
|
|
|
|
5
|
|
|
use Behat\Behat\Context\ClosuredContextInterface, |
|
6
|
|
|
Behat\Behat\Context\TranslatedContextInterface, |
|
7
|
|
|
Behat\Behat\Context\BehatContext, |
|
8
|
|
|
Behat\Behat\Exception\PendingException; |
|
9
|
|
|
use Behat\Gherkin\Node\PyStringNode, |
|
10
|
|
|
Behat\Gherkin\Node\TableNode; |
|
11
|
|
|
use Behat\Mink\Driver\BrowserKitDriver; |
|
12
|
|
|
use Behat\Mink\Driver\GoutteDriver; |
|
13
|
|
|
use Behat\Mink\Exception\UnsupportedDriverActionException; |
|
14
|
|
|
use Behat\MinkExtension\Context\MinkContext; |
|
15
|
|
|
use Behat\Behat\Context\Step; |
|
16
|
|
|
use Behat\Symfony2Extension\Context\KernelDictionary; |
|
17
|
|
|
use Doctrine\ORM\EntityRepository; |
|
18
|
|
|
use Nelmio\Alice\Loader\Base; |
|
19
|
|
|
use Nelmio\Alice\Loader\Yaml; |
|
20
|
|
|
use Nelmio\Alice\ORM\Doctrine; |
|
21
|
|
|
use PHPUnit_Framework_Assert; |
|
22
|
|
|
use Symfony\Component\BrowserKit\Cookie; |
|
23
|
|
|
use Viva\AuthBundle\Entity\User; |
|
24
|
|
|
use Viva\AuthBundle\Entity\UserRepository; |
|
25
|
|
|
use Vivait\AuthBundle\Features\Context\AuthContextTrait; |
|
26
|
|
|
use Vivait\BehatAliceLoader\Behat; |
|
27
|
|
|
use Symfony\Component\Config\Definition\Exception\Exception; |
|
28
|
|
|
|
|
29
|
|
|
// |
|
30
|
|
|
// Require 3rd-party libraries here: |
|
31
|
|
|
// |
|
32
|
|
|
// require_once 'PHPUnit/Autoload.php'; |
|
33
|
|
|
// require_once 'PHPUnit/Framework/Assert/Functions.php'; |
|
34
|
|
|
// |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Features context. |
|
38
|
|
|
*/ |
|
39
|
|
|
class FeatureContext extends MinkContext { |
|
40
|
|
|
use KernelDictionary; |
|
41
|
|
|
use AuthContextTrait; |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @Given /^(.*) without redirection$/ |
|
46
|
|
|
*/ |
|
47
|
|
|
public function theRedirectionsAreIntercepted($step) { |
|
48
|
|
|
$this->canIntercept(); |
|
49
|
|
|
$this->getSession() |
|
|
|
|
|
|
50
|
|
|
->getDriver() |
|
51
|
|
|
->getClient() |
|
52
|
|
|
->followRedirects(false); |
|
53
|
|
|
|
|
54
|
|
|
return new Step\Given($step); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @When /^I follow the redirection$/ |
|
59
|
|
|
* @Then /^I should be redirected$/ |
|
60
|
|
|
*/ |
|
61
|
|
|
public function iFollowTheRedirection() { |
|
62
|
|
|
$this->canIntercept(); |
|
63
|
|
|
$client = $this->getSession()->getDriver()->getClient(); |
|
|
|
|
|
|
64
|
|
|
$client->followRedirects(true); |
|
65
|
|
|
$client->followRedirect(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function canIntercept() { |
|
69
|
|
|
$driver = $this->getSession()->getDriver(); |
|
70
|
|
|
if (!$driver instanceof GoutteDriver) { |
|
71
|
|
|
throw new UnsupportedDriverActionException( |
|
72
|
|
|
'You need to tag the scenario with ' . |
|
73
|
|
|
'"@mink:goutte" or "@mink:symfony". ' . |
|
74
|
|
|
'Intercepting the redirections is not ' . |
|
75
|
|
|
'supported by %s', $driver |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
} |
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: