Completed
Push — component-bundle ( 0f1f3e )
by Kamil
35:06
created

ManagingTaxonsContext::iWantToGetTaxonWithCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
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\TaxonInterface;
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 Arkadiusz Krakowiak <[email protected]>
23
 */
24
final class ManagingTaxonsContext 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 taxon with :phrase in name
48
     */
49
    public function iTypeIn($phrase)
50
    {
51
        $this->client->getCookieJar()->set(new Cookie($this->session->getName(), $this->session->getId()));
52
        $this->client->request('GET', '/admin/ajax/taxons/search', ['phrase' => $phrase], [], ['ACCEPT' => 'application/json']);
53
    }
54
55
    /**
56
     * @When I want to get taxon with :code code
57
     */
58
    public function iWantToGetTaxonWithCode($code)
59
    {
60
        $this->client->getCookieJar()->set(new Cookie($this->session->getName(), $this->session->getId()));
61
        $this->client->request('GET', '/admin/ajax/taxons/leaf', ['code' => $code], [], ['ACCEPT' => 'application/json']);
62
    }
63
64
    /**
65
     * @When /^I want to get ("[^"]+" taxon) leafs$/
66
     */
67
    public function iWantToGetTaxonLeafs(TaxonInterface $taxon)
68
    {
69
        $this->client->getCookieJar()->set(new Cookie($this->session->getName(), $this->session->getId()));
70
        $this->client->request('GET', '/admin/ajax/taxons/leafs', ['parentCode' => $taxon->getCode()], [], ['ACCEPT' => 'application/json']);
71
    }
72
73
    /**
74
     * @When I want to get taxon root
75
     */
76
    public function iWantToGetTaxonRoot()
77
    {
78
        $this->client->getCookieJar()->set(new Cookie($this->session->getName(), $this->session->getId()));
79
        $this->client->request('GET', '/admin/ajax/taxons/root-nodes', [], [], ['ACCEPT' => 'application/json']);
80
    }
81
82
    /**
83
     * @Then /^I should see (\d+) taxons on the list$/
84
     */
85
    public function iShouldSeeTaxonsInTheList($number)
86
    {
87
        $response = json_decode($this->client->getResponse()->getContent(), true);
88
89
        Assert::eq($number, count($response));
90
    }
91
92
    /**
93
     * @Then I should see the taxon named :firstName in the list
94
     * @Then I should see the taxon named :firstName and :secondName in the list
95
     * @Then I should see the taxon named :firstName, :secondName and :thirdName in the list
96
     * @Then I should see the taxon named :firstName, :secondName, :thirdName and :fourthName in the list
97
     */
98
    public function iShouldSeeTheTaxonNamedAnd(... $taxonNames)
99
    {
100
        foreach ($taxonNames as $name) {
101
            $response = json_decode($this->client->getResponse()->getContent(), true);
102
103
            $taxonNames = array_map(function ($item) {
104
                return $item['name'];
105
            }, $response);
106
107
            if (!in_array($name, $taxonNames)) {
108
                throw new \InvalidArgumentException(
109
                    sprintf('There should be taxon named "%s", but got only "%s"', $name, $this->client->getResponse()->getContent())
110
                );
111
            }
112
        }
113
    }
114
}
115