Completed
Push — checkout-consistent-prices ( af49ca )
by Kamil
13:24
created

AssociationsElement::isProductAssociated()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 3
nc 3
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
declare(strict_types=1);
13
14
namespace Sylius\Behat\Element\Product\ShowPage;
15
16
use Behat\Mink\Element\NodeElement;
17
use FriendsOfBehat\PageObjectExtension\Element\Element;
18
19
final class AssociationsElement extends Element implements AssociationsElementInterface
20
{
21
    public function isProductAssociated(string $associationName, string $productName): bool
22
    {
23
        /** @var NodeElement $associations */
24
        $associations = $this->getElement('associations');
25
26
        /** @var NodeElement $product */
27
        foreach ($this->getAssociatedProducts($associations, $associationName) as $product) {
28
            if ($product->getText() === $productName) {
29
                return true;
30
            }
31
        }
32
33
        return false;
34
    }
35
36
    protected function getDefinedElements(): array
37
    {
38
        return array_merge(parent::getDefinedElements(), [
39
            'associations' => '#associations',
40
        ]);
41
    }
42
43
    private function getAssociatedProducts(NodeElement $associations, string $name): array
44
    {
45
        return $associations->findAll(
46
            'css',
47
            sprintf("tr:contains('%s') td:nth-child(2) li", $name)
48
        );
49
    }
50
}
51