Completed
Push — master ( 1671b1...4e9462 )
by Kamil
23:39
created

iShouldBeNotifiedThatThisVariantIsInUseAndCannotBeDeleted()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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\Domain;
13
14
use Behat\Behat\Context\Context;
15
use Doctrine\DBAL\DBALException;
16
use Sylius\Component\Core\Model\ProductInterface;
17
use Sylius\Component\Core\Model\ProductVariantInterface;
18
use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface;
19
use Sylius\Component\Core\Test\Services\SharedStorageInterface;
20
use Sylius\Component\Resource\Repository\RepositoryInterface;
21
22
/**
23
 * @author Łukasz Chruściel <[email protected]>
24
 * @author Magdalena Banasiak <[email protected]>
25
 */
26
final class ProductContext implements Context
27
{
28
    /**
29
     * @var SharedStorageInterface
30
     */
31
    private $sharedStorage;
32
33
    /**
34
     * @var RepositoryInterface
35
     */
36
    private $productRepository;
37
38
    /**
39
     * @var ProductVariantRepositoryInterface
40
     */
41
    private $productVariantRepository;
42
43
    /**
44
     * @var RepositoryInterface
45
     */
46
    private $reviewRepository;
47
48
    /**
49
     * @param SharedStorageInterface $sharedStorage
50
     * @param RepositoryInterface $productRepository
51
     * @param ProductVariantRepositoryInterface $productVariantRepository
52
     * @param RepositoryInterface $reviewRepository
53
     */
54
    public function __construct(
55
        SharedStorageInterface $sharedStorage,
56
        RepositoryInterface $productRepository,
57
        ProductVariantRepositoryInterface $productVariantRepository,
58
        RepositoryInterface $reviewRepository
59
    ) {
60
        $this->sharedStorage = $sharedStorage;
61
        $this->productRepository = $productRepository;
62
        $this->productVariantRepository = $productVariantRepository;
63
        $this->reviewRepository = $reviewRepository;
64
    }
65
66
    /**
67
     * @When /^I delete the ("[^"]+" variant of product "[^"]+")$/
68
     */
69
    public function iDeleteTheVariantOfProduct(ProductVariantInterface $productVariant)
70
    {
71
        $this->sharedStorage->set('product_variant_id', $productVariant->getId());
72
        $this->productVariantRepository->remove($productVariant);
73
    }
74
75
    /**
76
     * @When /^I try to delete the ("[^"]+" variant of product "[^"]+")$/
77
     */
78
    public function iTryToDeleteTheVariantOfProduct(ProductVariantInterface $productVariant)
79
    {
80
        try {
81
            $this->productVariantRepository->remove($productVariant);
82
        } catch (DBALException $exception) {
83
            $this->sharedStorage->set('last_exception', $exception);
84
        }
85
    }
86
87
    /**
88
     * @When /^I try to delete the ("([^"]+)" product)$/
89
     */
90
    public function iTryToDeleteTheProduct(ProductInterface $product)
91
    {
92
        try {
93
            $this->productRepository->remove($product);
94
        } catch (DBALException $exception) {
95
            $this->sharedStorage->set('last_exception', $exception);
96
        }
97
    }
98
99
    /**
100
     * @When /^I should be notified that this (?:variant|product) is in use and cannot be deleted$/
101
     */
102
    public function iShouldBeNotifiedThatThisProductVariantIsInUseAndCannotBeDeleted()
103
    {
104
        expect($this->sharedStorage->get('last_exception'))->toHaveType(DBALException::class);
105
    }
106
107
    /**
108
     * @Then /^this variant should not exist in the product catalog$/
109
     */
110
    public function productVariantShouldNotExistInTheProductCatalog()
111
    {
112
        $productVariantId = $this->sharedStorage->get('product_variant_id');
113
        $productVariant = $this->productVariantRepository->find($productVariantId);
114
115
        expect($productVariant)->toBe(null);
116
    }
117
118
    /**
119
     * @Then /^(this variant) should still exist in the product catalog$/
120
     */
121
    public function productVariantShouldExistInTheProductCatalog(ProductVariantInterface $productVariant)
122
    {
123
        $productVariant = $this->productVariantRepository->find($productVariant->getId());
124
125
        expect($productVariant)->toNotBe(null);
126
    }
127
128
    /**
129
     * @Then /^(this product) should still exist in the product catalog$/
130
     */
131
    public function productShouldExistInTheProductCatalog(ProductInterface $product)
132
    {
133
        $product = $this->productRepository->find($product->getId());
134
135
        expect($product)->toNotBe(null);
136
    }
137
138
    /**
139
     * @When /^I delete the ("[^"]+" product)$/
140
     */
141
    public function iDeleteTheProduct(ProductInterface $product)
142
    {
143
        try {
144
            $this->sharedStorage->set('deleted_product', $product);
145
            $this->productRepository->remove($product);
146
        } catch (DBALException $exception) {
147
            $this->sharedStorage->set('last_exception', $exception);
148
        }
149
    }
150
151
    /**
152
     * @Then /^there should be no reviews of (this product)$/
153
     */
154
    public function thereAreNoProductReviews(ProductInterface $product)
155
    {
156
        expect($this->reviewRepository->findBy(['reviewSubject' => $product]))->toBe([]);
157
    }
158
159
    /**
160
     * @Then /^there should be no variants of (this product) in the product catalog$/
161
     */
162
    public function thereAreNoVariants(ProductInterface $product)
163
    {
164
        expect($this->productVariantRepository->findBy(['object' => $product]))->toBe([]);
165
    }
166
}
167