Completed
Push — master ( 78fd01...e243d3 )
by Paweł
11:06
created

iCheckTheShippingCategory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
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\Ui\Admin;
15
16
use Behat\Behat\Context\Context;
17
use Sylius\Behat\Page\Admin\Crud\IndexPageInterface;
18
use Sylius\Behat\Page\Admin\Crud\UpdatePageInterface;
19
use Sylius\Behat\Page\Admin\ShippingCategory\CreatePageInterface;
20
use Sylius\Component\Shipping\Model\ShippingCategoryInterface;
21
use Webmozart\Assert\Assert;
22
23
class ManagingShippingCategoriesContext implements Context
24
{
25
    /**
26
     * @var IndexPageInterface
27
     */
28
    private $indexPage;
29
30
    /**
31
     * @var CreatePageInterface
32
     */
33
    private $createPage;
34
35
    /**
36
     * @var UpdatePageInterface
37
     */
38
    private $updatePage;
39
40
    /**
41
     * @param IndexPageInterface $indexPage
42
     * @param CreatePageInterface $createPage
43
     * @param UpdatePageInterface $updatePage
44
     */
45
    public function __construct(
46
        IndexPageInterface $indexPage,
47
        CreatePageInterface $createPage,
48
        UpdatePageInterface $updatePage
49
    ) {
50
        $this->indexPage = $indexPage;
51
        $this->createPage = $createPage;
52
        $this->updatePage = $updatePage;
53
    }
54
55
    /**
56
     * @Given I want to create a new shipping category
57
     */
58
    public function iWantToCreateANewShippingCategory()
59
    {
60
        $this->createPage->open();
61
    }
62
63
    /**
64
     * @When /^I browse shipping categories$/
65
     */
66
    public function iWantToBrowseShippingCategories()
67
    {
68
        $this->indexPage->open();
69
    }
70
71
    /**
72
     * @Then I should see a single shipping category in the list
73
     * @Then I should see :numberOfShippingCategories shipping categories in the list
74
     */
75
    public function iShouldSeeShippingCategoriesInTheList(int $numberOfShippingCategories = 1): void
76
    {
77
        Assert::same($this->indexPage->countItems(), $numberOfShippingCategories);
78
    }
79
80
    /**
81
     * @When I specify its description as :shippingCategoryDescription
82
     */
83
    public function iSpecifyItsDescriptionAs($shippingCategoryDescription)
84
    {
85
        $this->createPage->specifyDescription($shippingCategoryDescription);
86
    }
87
88
    /**
89
     * @When I add it
90
     * @When I try to add it
91
     */
92
    public function iAddIt()
93
    {
94
        $this->createPage->create();
95
    }
96
97
    /**
98
     * @Then I should be notified that :element is required
99
     */
100
    public function iShouldBeNotifiedThatCodeIsRequired($element)
101
    {
102
        Assert::same(
103
            $this->updatePage->getValidationMessage($element),
104
            sprintf('Please enter shipping category %s.', $element)
105
        );
106
    }
107
108
    /**
109
     * @When I do not specify its code
110
     * @When I specify its code as :shippingCategoryCode
111
     */
112
    public function iSpecifyItsCodeAs($shippingCategoryCode = null)
113
    {
114
        $this->createPage->specifyCode($shippingCategoryCode);
115
    }
116
117
    /**
118
     * @When I name it :shippingCategoryName
119
     * @When I do not specify its name
120
     */
121
    public function iNameIt($shippingCategoryName = null)
122
    {
123
        $this->createPage->nameIt($shippingCategoryName);
124
    }
125
126
    /**
127
     * @Then I should see the shipping category :shippingCategoryName in the list
128
     */
129
    public function iShouldSeeTheShippingCategoryInTheList(string $shippingCategoryName): void
130
    {
131
        Assert::true($this->indexPage->isSingleResourceOnPage(['name' => $shippingCategoryName]));
132
    }
133
134
    /**
135
     * @Then /^the (shipping category "([^"]+)") should be in the registry$/
136
     * @Then /^the (shipping category "([^"]+)") should appear in the registry$/
137
     */
138
    public function theShippingCategoryShouldAppearInTheRegistry(ShippingCategoryInterface $shippingCategory)
139
    {
140
        $this->iWantToBrowseShippingCategories();
141
142
        Assert::true($this->indexPage->isSingleResourceOnPage(['code' => $shippingCategory->getCode()]));
143
    }
144
145
    /**
146
     * @When I delete shipping category :shippingCategoryName
147
     */
148
    public function iDeleteShippingCategory($shippingCategoryName)
149
    {
150
        $this->iWantToBrowseShippingCategories();
151
        $this->indexPage->deleteResourceOnPage(['name' => $shippingCategoryName]);
152
    }
153
154
    /**
155
     * @Then /^(this shipping category) should no longer exist in the registry$/
156
     */
157
    public function thisShippingCategoryShouldNoLongerExistInTheRegistry(ShippingCategoryInterface $shippingCategory)
158
    {
159
        Assert::false($this->indexPage->isSingleResourceOnPage(['code' => $shippingCategory->getCode()]));
160
    }
161
162
    /**
163
     * @Then shipping category with name :shippingCategoryName should not be added
164
     */
165
    public function shippingCategoryWithNameShouldNotBeAdded($shippingCategoryName)
166
    {
167
        Assert::false($this->indexPage->isSingleResourceOnPage(['name' => $shippingCategoryName]));
168
    }
169
170
    /**
171
     * @When /^I modify a (shipping category "([^"]+)")$/
172
     * @Given /^I want to modify a (shipping category "([^"]+)")$/
173
     */
174
    public function iWantToModifyAShippingCategory(ShippingCategoryInterface $shippingCategory)
175
    {
176
        $this->updatePage->open(['id' => $shippingCategory->getId()]);
177
    }
178
179
    /**
180
     * @When I rename it to :name
181
     */
182
    public function iNameItIn($name)
183
    {
184
        $this->createPage->nameIt($name);
185
    }
186
187
    /**
188
     * @When I save my changes
189
     */
190
    public function iSaveMyChanges()
191
    {
192
        $this->updatePage->saveChanges();
193
    }
194
195
    /**
196
     * @When I check (also) the :shippingCategoryName shipping category
197
     */
198
    public function iCheckTheShippingCategory(string $shippingCategoryName): void
199
    {
200
        $this->indexPage->checkResourceOnPage(['name' => $shippingCategoryName]);
201
    }
202
203
    /**
204
     * @When I delete them
205
     */
206
    public function iDeleteThem(): void
207
    {
208
        $this->indexPage->bulkDelete();
209
    }
210
211
    /**
212
     * @Then the code field should be disabled
213
     */
214
    public function theCodeFieldShouldBeDisabled()
215
    {
216
        Assert::true($this->updatePage->isCodeDisabled());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Sylius\Behat\Page\Admin\Crud\UpdatePageInterface as the method isCodeDisabled() does only exist in the following implementations of said interface: Sylius\Behat\Page\Admin\Channel\UpdatePage, Sylius\Behat\Page\Admin\CustomerGroup\UpdatePage, Sylius\Behat\Page\Admin\PaymentMethod\UpdatePage, Sylius\Behat\Page\Admin\...ociationType\UpdatePage, Sylius\Behat\Page\Admin\...uctAttribute\UpdatePage, Sylius\Behat\Page\Admin\ProductOption\UpdatePage, Sylius\Behat\Page\Admin\ProductVariant\UpdatePage, Sylius\Behat\Page\Admin\...ConfigurableProductPage, Sylius\Behat\Page\Admin\...UpdateSimpleProductPage, Sylius\Behat\Page\Admin\PromotionCoupon\UpdatePage, Sylius\Behat\Page\Admin\Promotion\UpdatePage, Sylius\Behat\Page\Admin\...pingCategory\UpdatePage, Sylius\Behat\Page\Admin\ShippingMethod\UpdatePage, Sylius\Behat\Page\Admin\TaxCategory\UpdatePage, Sylius\Behat\Page\Admin\TaxRate\UpdatePage, Sylius\Behat\Page\Admin\Taxon\UpdatePage, Sylius\Behat\Page\Admin\Zone\UpdatePage.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
217
    }
218
219
    /**
220
     * @Then this shipping category name should be :shippingCategoryName
221
     */
222
    public function thisShippingCategoryNameShouldBe($shippingCategoryName)
223
    {
224
        Assert::true($this->updatePage->hasResourceValues(['name' => $shippingCategoryName]));
225
    }
226
227
    /**
228
     * @Then I should be notified that shipping category with this code already exists
229
     */
230
    public function iShouldBeNotifiedThatShippingCategoryWithThisCodeAlreadyExists()
231
    {
232
        Assert::same(
233
            $this->createPage->getValidationMessage('code'),
234
            'The shipping category with given code already exists.'
235
        );
236
    }
237
238
    /**
239
     * @Then there should still be only one shipping category with code :code
240
     */
241
    public function thereShouldStillBeOnlyOneShippingCategoryWith($code)
242
    {
243
        $this->iWantToBrowseShippingCategories();
244
245
        Assert::true($this->indexPage->isSingleResourceOnPage(['code' => $code]));
246
    }
247
}
248