Completed
Push — pull-request/7612 ( 75b393 )
by Kamil
21:47
created

ManagingTaxonsContext   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 11
c 0
b 0
f 0
lcom 1
cbo 6
dl 0
loc 98
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A iTypeIn() 0 5 1
A iWantToGetTaxonWithCode() 0 5 1
A iWantToGetTaxonLeafs() 0 5 1
A iWantToGetTaxonRoot() 0 5 1
A iShouldSeeTaxonsInTheList() 0 6 1
A iShouldSeeTheTaxonNamedInTheList() 0 14 3
A iShouldSeeTheTaxonNamedAnd() 0 6 2
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 :name in the list
94
     */
95
    public function iShouldSeeTheTaxonNamedInTheList($name)
96
    {
97
        $response = json_decode($this->client->getResponse()->getContent(), true);
98
99
        foreach ($response as $taxon) {
100
            if ($taxon['name'] === $name) {
101
                return;
102
            }
103
        }
104
105
        throw new \InvalidArgumentException(
106
            sprintf('There should be taxon named "%s", but got only "%s"', $name, $this->client->getResponse()->getContent())
107
        );
108
    }
109
110
    /**
111
     * @Then I should see the taxon named :firstName and :secondName in the list
112
     * @Then I should see the taxon named :firstName, :secondName and :thirdName in the list
113
     * @Then I should see the taxon named :firstName, :secondName, :thirdName and :fourthName in the list
114
     */
115
    public function iShouldSeeTheTaxonNamedAnd(... $taxonNames)
116
    {
117
        foreach ($taxonNames as $name) {
118
            $this->iShouldSeeTheTaxonNamedInTheList($name);
119
        }
120
    }
121
}
122