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
|
|
|
namespace Sylius\Behat\Context\Api\Admin; |
13
|
|
|
|
14
|
|
|
use Behat\Behat\Context\Context; |
15
|
|
|
use Sylius\Component\Core\Model\ProductInterface; |
16
|
|
|
use Symfony\Component\BrowserKit\Client; |
17
|
|
|
use Symfony\Component\BrowserKit\Cookie; |
18
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface; |
19
|
|
|
use Webmozart\Assert\Assert; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author Jan Góralski <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
final class ManagingProductVariantsContext implements Context |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var Client |
28
|
|
|
*/ |
29
|
|
|
private $client; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var SessionInterface |
33
|
|
|
*/ |
34
|
|
|
private $session; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param Client $client |
38
|
|
|
* @param SessionInterface $session |
39
|
|
|
*/ |
40
|
|
|
public function __construct(Client $client, SessionInterface $session) |
41
|
|
|
{ |
42
|
|
|
$this->client = $client; |
43
|
|
|
$this->session = $session; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @When I look for a variant with :phrase in descriptor within the :product product |
48
|
|
|
*/ |
49
|
|
|
public function iLookForVariantWithDescriptorWithinProduct($phrase, ProductInterface $product) |
50
|
|
|
{ |
51
|
|
|
$this->client->getCookieJar()->set(new Cookie($this->session->getName(), $this->session->getId())); |
52
|
|
|
$this->client->request( |
53
|
|
|
'GET', |
54
|
|
|
'/admin/ajax/product-variants/search', |
55
|
|
|
['phrase' => $phrase, 'productCode' => $product->getCode()], |
56
|
|
|
[], |
57
|
|
|
['ACCEPT' => 'application/json'] |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @Then /^I should see (\d+) product variants? on the list$/ |
63
|
|
|
*/ |
64
|
|
|
public function iShouldSeeProductVariantsInTheList($number) |
65
|
|
|
{ |
66
|
|
|
Assert::eq(count($this->getJSONResponse()), $number); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @Then I should see the product variant named :firstName on the list |
71
|
|
|
* @Then I should see the product variants named :firstName and :secondName on the list |
72
|
|
|
* @Then I should see the product variants named :firstName, :secondName and :thirdName on the list |
73
|
|
|
* @Then I should see the product variants named :firstName, :secondName, :thirdName and :fourthName on the list |
74
|
|
|
*/ |
75
|
|
|
public function iShouldSeeTheProductVariantNamedAnd(...$names) |
76
|
|
|
{ |
77
|
|
|
$itemsNames = array_map(function ($item) { |
78
|
|
|
return strstr($item['descriptor'], ' ', true); |
79
|
|
|
}, $this->getJSONResponse()); |
80
|
|
|
|
81
|
|
|
Assert::allOneOf($itemsNames, $names); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @Then I should see the product variant labeled :label on the list |
86
|
|
|
*/ |
87
|
|
|
public function iShouldSeeTheProductVariantLabeledAs($label) |
88
|
|
|
{ |
89
|
|
|
$itemsLabels = array_map(function ($item) { |
90
|
|
|
return $item['descriptor']; |
91
|
|
|
}, $this->getJSONResponse()); |
92
|
|
|
|
93
|
|
|
Assert::oneOf($label, $itemsLabels, 'Expected "%s" to be on the list, found: %s.'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return mixed |
98
|
|
|
*/ |
99
|
|
|
private function getJSONResponse() |
100
|
|
|
{ |
101
|
|
|
return json_decode($this->client->getResponse()->getContent(), true); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|