Completed
Push — master ( 0a1951...1a638f )
by Paweł
22:32 queued 13:24
created

Behat/Context/Setup/ProductReviewContext.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\Setup;
13
14
use Behat\Behat\Context\Context;
15
use SM\Factory\FactoryInterface as StateMachineFactoryInterface;
16
use Sylius\Component\Core\Model\CustomerInterface;
17
use Sylius\Component\Core\Model\ProductInterface;
18
use Sylius\Behat\Service\SharedStorageInterface;
19
use Sylius\Component\Core\ProductReviewTransitions;
20
use Sylius\Component\Resource\Factory\FactoryInterface;
21
use Sylius\Component\Resource\Repository\RepositoryInterface;
22
use Sylius\Component\Review\Model\ReviewInterface;
23
24
/**
25
 * @author Magdalena Banasiak <[email protected]>
26
 */
27
final class ProductReviewContext implements Context
28
{
29
    /**
30
     * @var SharedStorageInterface
31
     */
32
    private $sharedStorage;
33
34
    /**
35
     * @var FactoryInterface
36
     */
37
    private $productReviewFactory;
38
39
    /**
40
     * @var RepositoryInterface
41
     */
42
    private $productReviewRepository;
43
44
    /**
45
     * @var StateMachineFactoryInterface
46
     */
47
    private $stateMachineFactory;
48
49
    /**
50
     * @param SharedStorageInterface $sharedStorage
51
     * @param FactoryInterface $productReviewFactory
52
     * @param RepositoryInterface $productReviewRepository
53
     * @param StateMachineFactoryInterface $stateMachineFactory
54
     */
55
    public function __construct(
56
        SharedStorageInterface $sharedStorage,
57
        FactoryInterface $productReviewFactory,
58
        RepositoryInterface $productReviewRepository,
59
        StateMachineFactoryInterface $stateMachineFactory
60
    ) {
61
        $this->sharedStorage = $sharedStorage;
62
        $this->productReviewFactory = $productReviewFactory;
63
        $this->productReviewRepository = $productReviewRepository;
64
        $this->stateMachineFactory = $stateMachineFactory;
65
    }
66
67
    /**
68
     * @Given /^(this product) has one review$/
69
     */
70
    public function productHasAReview(ProductInterface $product)
71
    {
72
        $review = $this->createProductReview($product, 'Title', 5, 'Comment');
73
74
        $this->productReviewRepository->add($review);
75
    }
76
77
    /**
78
     * @Given /^(this product) has(?:| also) a review titled "([^"]+)" and rated (\d+) added by (customer "[^"]+")(?:|, created (\d+) days ago)$/
79
     */
80
    public function thisProductHasAReviewTitledAndRatedAddedByCustomer(
81
        ProductInterface $product,
82
        $title,
83
        $rating,
84
        CustomerInterface $customer,
85
        $daysSinceCreation = null
86
    ) {
87
        $review = $this->createProductReview($product, $title, $rating, $title, $customer);
88
        if (null !== $daysSinceCreation) {
89
            $review->setCreatedAt(new \DateTime('-'.$daysSinceCreation.' days'));
90
        }
91
92
        $this->productReviewRepository->add($review);
93
    }
94
95
    /**
96
     * @Given /^(this product) has(?:| also) a review titled "([^"]+)" and rated (\d+) with a comment "([^"]+)" added by (customer "[^"]+")$/
97
     */
98
    public function thisProductHasAReviewTitledAndRatedWithACommentAddedByCustomer(
99
        ProductInterface $product,
100
        $title,
101
        $rating,
102
        $comment,
103
        CustomerInterface $customer
104
    ) {
105
        $review = $this->createProductReview($product, $title, $rating, $comment, $customer);
106
107
        $this->productReviewRepository->add($review);
108
    }
109
110
    /**
111
     * @Given /^(this product) has(?:| also) a new review titled "([^"]+)" and rated (\d+) added by (customer "[^"]+")$/
112
     */
113
    public function thisProductHasAReviewTitledAndRatedAddedByCustomerWhichIsNotAcceptedYet(
114
        ProductInterface $product,
115
        $title,
116
        $rating,
117
        CustomerInterface $customer
118
    ) {
119
        $review = $this->createProductReview($product, $title, $rating, $title, $customer, null);
120
121
        $this->productReviewRepository->add($review);
122
    }
123
124
    /**
125
     * @Given /^(this product)(?:| also) has accepted reviews rated (\d+), (\d+), (\d+), (\d+) and (\d+)$/
126
     * @Given /^(this product)(?:| also) has accepted reviews rated (\d+), (\d+) and (\d+)$/
127
     */
128
    public function thisProductHasAcceptedReviewsRated(ProductInterface $product, ...$rates)
129
    {
130
        $customer = $this->sharedStorage->get('customer');
131
        foreach ($rates as $key => $rate) {
132
            $review = $this->createProductReview($product, 'Title '.$key, $rate, 'Comment '.$key, $customer);
133
            $this->productReviewRepository->add($review);
134
        }
135
    }
136
137
    /**
138
     * @Given /^(this product)(?:| also) has review rated (\d+) which is not accepted yet$/
139
     */
140
    public function itAlsoHasReviewRatedWhichIsNotAcceptedYet(ProductInterface $product, $rate)
141
    {
142
        $customer = $this->sharedStorage->get('customer');
143
        $review = $this->createProductReview($product, 'Title', $rate, 'Comment', $customer, null);
144
        $this->productReviewRepository->add($review);
145
    }
146
147
    /**
148
     * @Given /^(this product) also has review rated (\d+) which is rejected$/
149
     */
150
    public function itAlsoHasReviewRatedWhichIsRejected(ProductInterface $product, $rate)
151
    {
152
        $customer = $this->sharedStorage->get('customer');
153
        $review = $this->createProductReview($product, 'Title', $rate, 'Comment', $customer, ProductReviewTransitions::TRANSITION_REJECT);
154
        $this->productReviewRepository->add($review);
155
    }
156
157
    /**
158
     * @param ProductInterface $product
159
     * @param string $title
160
     * @param int $rating
161
     * @param string $comment
162
     * @param CustomerInterface|null $customer
163
     * @param string $transition
164
     *
165
     * @return ReviewInterface
166
     */
167
    private function createProductReview(
168
        ProductInterface $product,
169
        $title,
170
        $rating,
171
        $comment,
172
        CustomerInterface $customer = null,
173
        $transition = ProductReviewTransitions::TRANSITION_ACCEPT
174
    ) {
175
        /** @var ReviewInterface $review */
176
        $review = $this->productReviewFactory->createNew();
177
        $review->setTitle($title);
178
        $review->setRating($rating);
179
        $review->setComment($comment);
180
        $review->setReviewSubject($product);
181
        $review->setAuthor($customer);
0 ignored issues
show
It seems like $customer defined by parameter $customer on line 172 can also be of type object<Sylius\Component\...odel\CustomerInterface>; however, Sylius\Component\Review\...wInterface::setAuthor() does only seem to accept null|object<Sylius\Compo...odel\ReviewerInterface>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
182
183
        $product->addReview($review);
184
185
        if (null !== $transition) {
186
            $stateMachine = $this->stateMachineFactory->get($review, ProductReviewTransitions::GRAPH);
187
            $stateMachine->apply($transition);
188
        }
189
190
        $this->sharedStorage->set('product_review', $review);
191
192
        return $review;
193
    }
194
}
195