|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Sylius package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Paweł Jędrzejewski |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Sylius\Behat\Service\Setter; |
|
15
|
|
|
|
|
16
|
|
|
use Behat\Mink\Driver\Selenium2Driver; |
|
17
|
|
|
use Behat\Mink\Session; |
|
18
|
|
|
use DMore\ChromeDriver\ChromeDriver; |
|
19
|
|
|
use FriendsOfBehat\SymfonyExtension\Driver\SymfonyDriver; |
|
20
|
|
|
use Symfony\Component\BrowserKit\Cookie; |
|
21
|
|
|
|
|
22
|
|
|
final class CookieSetter implements CookieSetterInterface |
|
23
|
|
|
{ |
|
24
|
|
|
/** @var Session */ |
|
25
|
|
|
private $minkSession; |
|
26
|
|
|
|
|
27
|
|
|
/** @var array */ |
|
28
|
|
|
private $minkParameters; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct(Session $minkSession, $minkParameters) |
|
31
|
|
|
{ |
|
32
|
|
|
if (!is_array($minkParameters) && !$minkParameters instanceof \ArrayAccess) { |
|
33
|
|
|
throw new \InvalidArgumentException(sprintf( |
|
34
|
|
|
'"$minkParameters" passed to "%s" has to be an array or implement "%s".', |
|
35
|
|
|
self::class, |
|
36
|
|
|
\ArrayAccess::class |
|
37
|
|
|
)); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
$this->minkSession = $minkSession; |
|
41
|
|
|
$this->minkParameters = $minkParameters; |
|
|
|
|
|
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* {@inheritdoc} |
|
46
|
|
|
*/ |
|
47
|
|
|
public function setCookie($name, $value) |
|
48
|
|
|
{ |
|
49
|
|
|
$driver = $this->minkSession->getDriver(); |
|
50
|
|
|
|
|
51
|
|
|
if ($driver instanceof ChromeDriver) { |
|
|
|
|
|
|
52
|
|
|
if (!$driver->isStarted()) { |
|
53
|
|
|
$driver->start(); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$this->prepareMinkSessionIfNeeded($this->minkSession); |
|
58
|
|
|
|
|
59
|
|
|
if ($driver instanceof SymfonyDriver) { |
|
|
|
|
|
|
60
|
|
|
$driver->getClient()->getCookieJar()->set( |
|
61
|
|
|
new Cookie($name, $value, null, null, parse_url($this->minkParameters['base_url'], \PHP_URL_HOST)) |
|
62
|
|
|
); |
|
63
|
|
|
|
|
64
|
|
|
return; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$this->minkSession->setCookie($name, $value); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
private function prepareMinkSessionIfNeeded(Session $session): void |
|
71
|
|
|
{ |
|
72
|
|
|
if ($this->shouldMinkSessionBePrepared($session)) { |
|
73
|
|
|
$session->visit(rtrim($this->minkParameters['base_url'], '/') . '/'); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
private function shouldMinkSessionBePrepared(Session $session): bool |
|
78
|
|
|
{ |
|
79
|
|
|
$driver = $session->getDriver(); |
|
80
|
|
|
|
|
81
|
|
|
if ($driver instanceof SymfonyDriver) { |
|
|
|
|
|
|
82
|
|
|
return false; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
if ($driver instanceof Selenium2Driver && $driver->getWebDriverSession() === null) { |
|
86
|
|
|
return true; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
if ($driver instanceof ChromeDriver && $driver->isStarted() === false) { |
|
|
|
|
|
|
90
|
|
|
return true; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
if (false !== strpos($session->getCurrentUrl(), $this->minkParameters['base_url'])) { |
|
|
|
|
|
|
94
|
|
|
return false; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return true; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.