FeatureContext::theRedirectionsAreIntercepted()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 9
rs 9.6666
c 1
b 0
f 1
cc 1
eloc 7
nc 1
nop 1
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()
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Behat\Mink\Driver\DriverInterface as the method getClient() does only exist in the following implementations of said interface: Behat\Mink\Driver\BrowserKitDriver, Behat\Symfony2Extension\Driver\KernelDriver.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

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

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
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();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Behat\Mink\Driver\DriverInterface as the method getClient() does only exist in the following implementations of said interface: Behat\Mink\Driver\BrowserKitDriver, Behat\Symfony2Extension\Driver\KernelDriver.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

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

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
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
}