Completed
Push — master ( 2c8981...1ecbe4 )
by Kamil
23:19
created

ShowPage::addToCartWithVariant()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 9
rs 9.6666
nc 1
cc 1
eloc 5
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\Page\Shop\Product;
13
14
use Behat\Mink\Session;
15
use Sylius\Behat\Service\Accessor\TableAccessorInterface;
16
use Sylius\Component\Product\Model\OptionInterface;
17
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
18
use Symfony\Component\Routing\RouterInterface;
19
use Sylius\Behat\Page\SymfonyPage;
20
21
/**
22
 * @author Arkadiusz Krakowiak <[email protected]>
23
 * @author Anna Walasek <[email protected]>
24
 */
25
class ShowPage extends SymfonyPage implements ShowPageInterface
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function addToCart()
31
    {
32
        $this->getDocument()->pressButton('Add to cart');
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function addToCartWithQuantity($quantity)
39
    {
40
        $this->getDocument()->fillField('Quantity', $quantity);
41
        $this->getDocument()->pressButton('Add to cart');
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function addToCartWithVariant($variant)
48
    {
49
        $item = $this->getDocument()->find('css', sprintf('#product-variants tbody tr:contains("%s")', $variant));
50
        $radio = $item->find('css', 'input');
51
52
        $this->getDocument()->fillField($radio->getAttribute('name'), $radio->getAttribute('value'));
53
54
        $this->getDocument()->pressButton('Add to cart');
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function addToCartWithOption(OptionInterface $option, $optionValue)
61
    {
62
        $select = $this->getDocument()->find('css', sprintf('select#sylius_cart_item_variant_%s', $option->getCode()));
63
64
        $this->getDocument()->selectFieldOption($select->getAttribute('name'), $optionValue);
65
        $this->getDocument()->pressButton('Add to cart');
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function visit($url)
72
    {
73
        $absoluteUrl = $this->makePathAbsolute($url);
74
        $this->getDriver()->visit($absoluteUrl);
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function getName()
81
    {
82
        return $this->getElement('name')->getText();
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function isAttributeWithValueOnPage($attributeName, $attributeValue)
89
    {
90
        return (
91
            $this->isAttributeWith($attributeName, 'tbody > tr > td[id=sylius_attribute_name]') &&
92
            $this->isAttributeWith($attributeValue, 'tbody > tr > td[id=sylius_attribute_value]')
93
        );
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function getRouteName()
100
    {
101
        return 'sylius_shop_product_show';
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    protected function getDefinedElements()
108
    {
109
        return array_merge(parent::getDefinedElements(), [
110
            'name' => '#sylius_product_name',
111
            'attributes' => '#product-attributes'
112
        ]);
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    private function isAttributeWith($fieldValue, $selector)
119
    {
120
        $rows = $this->getElement('attributes')->findAll('css', $selector);
121
        foreach($rows as $row) {
122
            if($fieldValue === $row->getText()) {
123
                return true;
124
            }
125
        }
126
127
        return false;
128
    }
129
}
130