Completed
Push — api ( cecea0...116a36 )
by Kamil
29:57 queued 30s
created

iShouldSeeTheProductReviewTitleInTheList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
rs 10
cc 1
nc 1
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
declare(strict_types=1);
13
14
namespace Sylius\Behat\Context\Api\Admin;
15
16
use Behat\Behat\Context\Context;
17
use Sylius\Behat\Client\ApiClientInterface;
18
use Sylius\Behat\Service\SharedStorageInterface;
19
use Sylius\Component\Review\Model\ReviewInterface;
20
use Webmozart\Assert\Assert;
21
22
final class ManagingProductReviewsContext implements Context
23
{
24
    /** @var ApiClientInterface */
25
    private $client;
26
27
    /** @var SharedStorageInterface */
28
    private $sharedStorage;
29
30
    public function __construct(
31
        ApiClientInterface $client,
32
        SharedStorageInterface $sharedStorage
33
    ) {
34
        $this->client = $client;
35
        $this->sharedStorage = $sharedStorage;
36
    }
37
38
    /**
39
     * @When I browse product reviews
40
     * @When I want to browse product reviews
41
     */
42
    public function iWantToBrowseProductReviews(): void
43
    {
44
        $this->client->index('product_reviews');
45
    }
46
47
    /**
48
     * @When I want to modify the :productReview product review
49
     */
50
    public function iWantToModifyTheProductReview(ReviewInterface $productReview): void
51
    {
52
        $this->client->buildUpdateRequest('product_reviews', (string) $productReview->getId());
53
    }
54
55
    /**
56
     * @When I change its title to :title
57
     * @when I remove its title
58
     */
59
    public function iChangeItsTitleTo(?string $title = ''): void
60
    {
61
        $this->client->addRequestData('title', $title);
62
    }
63
64
    /**
65
     * @When I change its comment to :comment
66
     * @when I remove its comment
67
     */
68
    public function iChangeItsCommentTo(?string $comment = ''): void
69
    {
70
        $this->client->updateRequestData(['comment' => $comment]);
71
    }
72
73
    /**
74
     * @When I save my changes
75
     * @When I try to save my changes
76
     */
77
    public function iSaveMyChanges(): void
78
    {
79
        $this->client->update();
80
    }
81
82
    /**
83
     * @When I choose :rating as its rating
84
     */
85
    public function iChooseAsItsRating(int $rating): void
86
    {
87
        $this->client->updateRequestData(['rating' => $rating]);
88
    }
89
90
    /**
91
     * @When /^I (accept|reject) the ("([^"]+)" product review)$/
92
     */
93
    public function iChangeStateTheProductReview(string $state, ReviewInterface $productReview): void
94
    {
95
        $this->client->applyTransition('product_reviews', (string) $productReview->getId(), $state);
96
    }
97
98
    /**
99
     * @When I delete the :productReview product review
100
     */
101
    public function iDeleteTheProductReview(ReviewInterface $productReview): void
102
    {
103
        $this->sharedStorage->set('product_review_id', $productReview->getId());
104
105
        $this->client->delete('product_reviews', (string) $productReview->getId());
106
    }
107
108
    /**
109
     * @Then I should (also) see the product review :title in the list
110
     */
111
    public function iShouldSeeTheProductReviewTitleInTheList(string $title): void
112
    {
113
        Assert::true(
114
            $this->isItemOnIndex('title', $title),
115
            sprintf('Product review with title %s does not exist', $title)
116
        );
117
    }
118
119
    /**
120
     * @Then I should see a single product review in the list
121
     * @Then I should see :amount reviews in the list
122
     */
123
    public function iShouldSeeReviewsInTheList(int $amount = 1): void
124
    {
125
        Assert::same($this->client->countCollectionItems(), $amount);
126
    }
127
128
    /**
129
     * @Then /^(this product review) (comment|title) should be "([^"]+)"$/
130
     */
131
    public function thisProductReviewElementShouldBeValue(ReviewInterface $productReview, string $element, string $value): void
132
    {
133
        $this->assertIfReviewHasElementWithValue($productReview, $element, $value);
134
    }
135
136
    /**
137
     * @Then /^(this product review) rating should be (\d+)$/
138
     */
139
    public function thisProductReviewRatingShouldBe(ReviewInterface $productReview, int $rating): void
140
    {
141
        $this->assertIfReviewHasElementWithValue($productReview, 'rating', $rating);
142
    }
143
144
    /**
145
     * @Then /^(this product review) status should be "([^"]+)"$/
146
     */
147
    public function thisProductReviewStatusShouldBe(ReviewInterface $productReview, string $status): void
148
    {
149
        $this->assertIfReviewHasElementWithValue($productReview, 'status', $status);
150
    }
151
152
    /**
153
     * @Then /^I should be notified that it has been successfully (accepted|rejected)$/
154
     */
155
    public function iShouldBeNotifiedThatItHasBeenSuccessfullyUpdated(string $action): void
156
    {
157
        $this->assertIfReviewHasElementWithValue($this->sharedStorage->get('product_review'), 'status', $action);
158
    }
159
160
    /**
161
     * @Then this product review should no longer exist in the registry
162
     */
163
    public function thisProductReviewShouldNoLongerExistInTheRegistry(): void
164
    {
165
       $id = (string) $this->sharedStorage->get('product_review_id');
166
       Assert::false(
167
           $this->isItemOnIndex('id', $id),
168
           sprintf('Product review with id %s exist', $id)
169
       );
170
    }
171
172
    /**
173
     * @Then I should be notified that :element is required
174
     */
175
    public function iShouldBeNotifiedThatElementIsRequired(string $element): void
176
    {
177
        Assert::contains($this->client->getError(), sprintf('%s: Review %s should not be blank', $element, $element));
178
    }
179
180
    /**
181
     * @Then /^(this product review) should still be titled "([^"]+)"$/
182
     */
183
    public function thisProductReviewTitleShouldBeTitled(ReviewInterface $productReview, string $title): void
184
    {
185
        $this->assertIfReviewHasElementWithValue($productReview, 'title', $title);
186
    }
187
188
    /**
189
     * @Then /^(this product review) should still have a comment "([^"]+)"$/
190
     */
191
    public function thisProductReviewShouldStillHaveAComment(ReviewInterface $productReview, string $comment): void
192
    {
193
        $this->assertIfReviewHasElementWithValue($productReview, 'comment', $comment);
194
    }
195
196
    private function isItemOnIndex(string $property, string $value): bool
197
    {
198
        $this->client->index('product_reviews');
199
200
        return $this->client->hasItemWithValue($property, $value);
201
    }
202
203
    /** @param string|int $value */
204
    private function assertIfReviewHasElementWithValue(ReviewInterface $productReview, string $element, $value): void
205
    {
206
        $this->client->show('product_reviews', (string) $productReview->getId());
207
        Assert::true(
208
            $this->client->responseHasValue($element, $value),
209
            sprintf('Product review %s is not %s', $element, $value)
210
        );
211
    }
212
}
213