Completed
Push — master ( 60b5cc...359431 )
by Michał
458:10 queued 444:50
created

ShowPage   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 12
c 1
b 0
f 1
lcom 1
cbo 5
dl 0
loc 115
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A addToCartWithVariant() 0 9 1
A visit() 0 5 1
A getName() 0 4 1
A addToCartWithOption() 0 7 1
A addToCart() 0 4 1
A addToCartWithQuantity() 0 5 1
A hasAttributeWithValue() 0 15 2
A isOutOfStock() 0 4 1
A hasAddToCartButton() 0 4 1
A getRouteName() 0 4 1
A getDefinedElements() 0 8 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 Sylius\Component\Product\Model\OptionInterface;
15
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
16
use Symfony\Component\Routing\RouterInterface;
17
use Sylius\Behat\Page\SymfonyPage;
18
19
/**
20
 * @author Arkadiusz Krakowiak <[email protected]>
21
 * @author Anna Walasek <[email protected]>
22
 */
23
class ShowPage extends SymfonyPage implements ShowPageInterface
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function addToCart()
29
    {
30
        $this->getDocument()->pressButton('Add to cart');
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function addToCartWithQuantity($quantity)
37
    {
38
        $this->getDocument()->fillField('Quantity', $quantity);
39
        $this->getDocument()->pressButton('Add to cart');
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function addToCartWithVariant($variant)
46
    {
47
        $item = $this->getDocument()->find('css', sprintf('#sylius-product-variants tbody tr:contains("%s")', $variant));
48
        $radio = $item->find('css', 'input');
49
50
        $this->getDocument()->fillField($radio->getAttribute('name'), $radio->getAttribute('value'));
51
52
        $this->getDocument()->pressButton('Add to cart');
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function addToCartWithOption(OptionInterface $option, $optionValue)
59
    {
60
        $select = $this->getDocument()->find('css', sprintf('select#sylius_cart_item_variant_%s', $option->getCode()));
61
62
        $this->getDocument()->selectFieldOption($select->getAttribute('name'), $optionValue);
63
        $this->getDocument()->pressButton('Add to cart');
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function visit($url)
70
    {
71
        $absoluteUrl = $this->makePathAbsolute($url);
72
        $this->getDriver()->visit($absoluteUrl);
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function getName()
79
    {
80
        return $this->getElement('name')->getText();
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function hasAttributeWithValue($name, $value)
87
    {
88
        $tableWithAttributes = $this->getElement('attributes');
89
90
        $nameTdSelector = sprintf('tr > td.sylius-product-attribute-name:contains("%s")', $name);
91
        $nameTd = $tableWithAttributes->find('css', $nameTdSelector);
92
93
        if (null === $nameTd) {
94
            return false;
95
        }
96
97
        $row = $nameTd->getParent();
98
99
        return $value === $row->find('css', 'td.sylius-product-attribute-value')->getText();
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    public function isOutOfStock()
106
    {
107
        return $this->hasElement('out-of-stock');
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113
    public function hasAddToCartButton()
114
    {
115
        return $this->getDocument()->hasButton('Add to cart');
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121
    public function getRouteName()
122
    {
123
        return 'sylius_shop_product_show';
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129
    protected function getDefinedElements()
130
    {
131
        return array_merge(parent::getDefinedElements(), [
132
            'name' => '#sylius-product-name',
133
            'attributes' => '#sylius-product-attributes',
134
            'out-of-stock' => '#sylius-product-out-of-stock',
135
        ]);
136
    }
137
}
138