Completed
Push — master ( 59db0f...abafe8 )
by Kamil
18:55
created

TaxonomyContext::taxonHasFollowingChildren()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 42
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 42
rs 8.439
cc 5
eloc 23
nc 6
nop 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\Bundle\TaxonomyBundle\Behat;
13
14
use Behat\Gherkin\Node\TableNode;
15
use Sylius\Bundle\ResourceBundle\Behat\DefaultContext;
16
use Sylius\Component\Taxonomy\Model\TaxonInterface;
17
18
class TaxonomyContext extends DefaultContext
19
{
20
    /**
21
     * @Given /^there are following taxons defined:$/
22
     */
23
    public function thereAreFollowingTaxons(TableNode $table)
24
    {
25
        foreach ($table->getHash() as $data) {
26
            $this->thereIsTaxon($data['name'], $data['code'], false);
27
        }
28
29
        $this->getEntityManager()->flush();
30
    }
31
32
    /**
33
     * @Given /^I created taxon "([^""]*)" with code "([^""]*)"$/
34
     */
35
    public function thereIsTaxon($name, $code, $flush = true)
36
    {
37
        /** @var TaxonInterface $taxon */
38
        $taxon = $this->getFactory('taxon')->createNew();
39
        $taxon->setName($name);
40
        $taxon->setCode($code);
41
42
        $this->getEntityManager()->persist($taxon);
43
        if ($flush) {
44
            $this->getEntityManager()->flush();
45
        }
46
    }
47
48
    /**
49
     * @Given /^taxon "([^""]*)" has following children:$/
50
     */
51
    public function taxonHasFollowingChildren($taxonName, TableNode $childrenTable)
52
    {
53
        /* @var $taxon TaxonInterface */
54
        $taxon = $this->findOneByName('taxon', $taxonName);
55
        $manager = $this->getEntityManager();
56
57
        $children = [];
58
59
        foreach ($childrenTable->getRows() as $node) {
60
            $taxonList = explode('>', $node[0]);
61
62
            /* @var $parent TaxonInterface */
63
            $parent = null;
64
65
            foreach ($taxonList as $item) {
66
                $item = trim($item);
67
                $childData = preg_split('[\\[|\\]]', $item, -1, PREG_SPLIT_NO_EMPTY);
68
69
                if (!isset($children[$childData[0]])) {
70
                    /* @var $child TaxonInterface */
71
                    $child = $this->getFactory('taxon')->createNew();
72
                    $child->setName($childData[0]);
73
                    $child->setCode($childData[1]);
74
75
                    $children[$childData[0]] = $child;
76
                }
77
78
                $child = $children[$childData[0]];
79
80
                if (null !== $parent) {
81
                    $parent->addChild($child);
82
                } else {
83
                    $taxon->addChild($child);
84
                }
85
86
                $parent = $child;
87
            }
88
        }
89
90
        $manager->persist($taxon);
91
        $manager->flush();
92
    }
93
94
    /**
95
     * @Given the following taxon translations exist:
96
     */
97
    public function theFollowingTaxonTranslationsExist(TableNode $table)
98
    {
99
        foreach ($table->getHash() as $data) {
100
            $taxonTranslation = $this->findOneByName('taxon_translation', $data['taxon']);
101
102
            /** @var TaxonInterface $taxon */
103
            $taxon = $taxonTranslation->getTranslatable();
104
            $taxon->setCurrentLocale($data['locale']);
105
            $taxon->setFallbackLocale($data['locale']);
106
107
            $taxon->setName($data['name']);
108
        }
109
110
        $this->getEntityManager()->flush();
111
    }
112
113
    /**
114
     * @Then taxon translation :taxonName should have permalink :expectedPermalink
115
     */
116
    public function taxonForLocaleShouldHavePermalink($taxonName, $expectedPermalink)
117
    {
118
        $taxonTranslation = $this->findOneByName('taxon_translation', $taxonName);
119
        $permalink = $taxonTranslation->getPermalink();
120
121
        \PHPUnit_Framework_Assert::assertEquals($expectedPermalink, $permalink);
122
    }
123
124
    /**
125
     * @When I change then name of taxon translation :taxonName to :newName
126
     */
127
    public function iChangeThenNameOfTaxonTranslationTo($taxonName, $newName)
128
    {
129
        $taxonTranslation = $this->findOneByName('taxon_translation', $taxonName);
130
        $taxonTranslation->setName($newName);
131
132
        $this->getEntityManager()->flush();
133
    }
134
}
135