Completed
Push — symfony3-fqcn-sylius-3 ( 1d8d47...2ae323 )
by Kamil
38:58 queued 18:19
created

ManagingShippingCategoriesContext::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 3
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\Ui\Admin;
13
14
use Behat\Behat\Context\Context;
15
use Sylius\Behat\Page\Admin\Crud\IndexPageInterface;
16
use Sylius\Behat\Page\Admin\Crud\UpdatePageInterface;
17
use Sylius\Behat\Page\Admin\ShippingCategory\CreatePageInterface;
18
use Sylius\Component\Shipping\Model\ShippingCategoryInterface;
19
use Webmozart\Assert\Assert;
20
21
/**
22
 * @author Anna Walasek <[email protected]>
23
 */
24
class ManagingShippingCategoriesContext implements Context
25
{
26
    /**
27
     * @var IndexPageInterface
28
     */
29
    private $indexPage;
30
31
    /**
32
     * @var CreatePageInterface
33
     */
34
    private $createPage;
35
36
    /**
37
     * @var UpdatePageInterface
38
     */
39
    private $updatePage;
40
41
42
    /**
43
     * @param IndexPageInterface $indexPage
44
     * @param CreatePageInterface $createPage
45
     * @param UpdatePageInterface $updatePage
46
     */
47
    public function __construct(
48
        IndexPageInterface $indexPage,
49
        CreatePageInterface $createPage,
50
        UpdatePageInterface $updatePage
51
    ) {
52
        $this->indexPage = $indexPage;
53
        $this->createPage = $createPage;
54
        $this->updatePage = $updatePage;
55
    }
56
57
    /**
58
     * @Given I want to create a new shipping category
59
     */
60
    public function iWantToCreateANewShippingCategory()
61
    {
62
        $this->createPage->open();
63
    }
64
65
    /**
66
     * @When /^I browse shipping categories$/
67
     */
68
    public function iWantToBrowseShippingCategories()
69
    {
70
        $this->indexPage->open();
71
    }
72
73
    /**
74
     * @Then I should see :numberOfShippingCategories shipping categories in the list
75
     */
76
    public function iShouldSeeShippingCategoriesInTheList($numberOfShippingCategories)
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $numberOfShippingCategories exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
77
    {
78
        $foundRows = $this->indexPage->countItems();
79
80
        Assert::true(
81
            ((int) $numberOfShippingCategories) === $foundRows,
82
            sprintf('%s rows with shipping categories should appear on page, %s rows has been found', $numberOfShippingCategories, $foundRows)
83
        );
84
    }
85
86
    /**
87
     * @When I specify its description as :shippingCategoryDescription
88
     */
89
    public function iSpecifyItsDescriptionAs($shippingCategoryDescription)
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $shippingCategoryDescription exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
90
    {
91
        $this->createPage->specifyDescription($shippingCategoryDescription);
92
    }
93
94
    /**
95
     * @When I add it
96
     * @When I try to add it
97
     */
98
    public function iAddIt()
99
    {
100
        $this->createPage->create();
101
    }
102
103
    /**
104
     * @Then I should be notified that :element is required
105
     */
106
    public function iShouldBeNotifiedThatCodeIsRequired($element)
107
    {
108
        Assert::same(
109
            $this->updatePage->getValidationMessage($element),
110
            sprintf('Please enter shipping category %s.', $element)
111
        );
112
    }
113
114
    /**
115
     * @When I do not specify its code
116
     * @When I specify its code as :shippingCategoryCode
117
     */
118
    public function iSpecifyItsCodeAs($shippingCategoryCode = null)
119
    {
120
        $this->createPage->specifyCode($shippingCategoryCode);
121
    }
122
123
    /**
124
     * @When I name it :shippingCategoryName
125
     * @When I do not specify its name
126
     */
127
    public function iNameIt($shippingCategoryName = null)
128
    {
129
        $this->createPage->nameIt($shippingCategoryName);
130
    }
131
132
    /**
133
     * @Then /^the (shipping category "([^"]+)") should be in the registry$/
134
     * @Then /^the (shipping category "([^"]+)") should appear in the registry$/
135
     */
136
    public function theShippingCategoryShouldAppearInTheRegistry(ShippingCategoryInterface $shippingCategory)
137
    {
138
        $this->iWantToBrowseShippingCategories();
139
140
        Assert::true(
141
            $this->indexPage->isSingleResourceOnPage(['code' => $shippingCategory->getCode()]),
142
            sprintf('The shipping category with code %s has not been found.', $shippingCategory->getCode())
143
        );
144
    }
145
146
    /**
147
     * @When I delete shipping category :shippingCategoryName
148
     */
149
    public function iDeleteShippingCategory($shippingCategoryName)
150
    {
151
        $this->iWantToBrowseShippingCategories();
152
        $this->indexPage->deleteResourceOnPage(['name' => $shippingCategoryName]);
153
    }
154
155
    /**
156
     * @Then /^(this shipping category) should no longer exist in the registry$/
157
     */
158
    public function thisShippingCategoryShouldNoLongerExistInTheRegistry(ShippingCategoryInterface $shippingCategory)
159
    {
160
        Assert::false(
161
            $this->indexPage->isSingleResourceOnPage(['code' => $shippingCategory->getCode()]),
162
            sprintf('Shipping category with code %s exists but should not.', $shippingCategory->getCode())
163
        );
164
    }
165
166
    /**
167
     * @Then shipping category with name :shippingCategoryName should not be added
168
     */
169
    public function shippingCategoryWithNameShouldNotBeAdded($shippingCategoryName)
170
    {
171
        Assert::false(
172
            $this->indexPage->isSingleResourceOnPage(['name' => $shippingCategoryName]),
173
            sprintf('Shipping category with name %s exists but should not.', $shippingCategoryName)
174
        );
175
    }
176
177
    /**
178
     * @When /^I modify a (shipping category "([^"]+)")$/
179
     * @Given /^I want to modify a (shipping category "([^"]+)")$/
180
     */
181
    public function iWantToModifyAShippingCategory(ShippingCategoryInterface $shippingCategory)
182
    {
183
        $this->updatePage->open(['id' => $shippingCategory->getId()]);
184
    }
185
186
    /**
187
     * @When I rename it to :name
188
     */
189
    public function iNameItIn($name)
190
    {
191
        $this->createPage->nameIt($name);
192
    }
193
194
    /**
195
     * @When I save my changes
196
     */
197
    public function iSaveMyChanges()
198
    {
199
        $this->updatePage->saveChanges();
200
    }
201
202
    /**
203
     * @Then the code field should be disabled
204
     */
205
    public function theCodeFieldShouldBeDisabled()
206
    {
207
        Assert::true(
208
            $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...
209
            'Code should be immutable, but it does not.'
210
        );
211
    }
212
213
    /**
214
     * @Then this shipping category name should be :shippingCategoryName
215
     */
216
    public function thisShippingCategoryNameShouldBe($shippingCategoryName)
217
    {
218
        Assert::true(
219
            $this->updatePage->hasResourceValues(['name' => $shippingCategoryName]),
220
            sprintf('Shipping category should have name %s, but it does not.', $shippingCategoryName)
221
        );
222
    }
223
224
    /**
225
     * @Then I should be notified that shipping category with this code already exists
226
     */
227
    public function iShouldBeNotifiedThatShippingCategoryWithThisCodeAlreadyExists()
228
    {
229
        Assert::same(
230
            $this->createPage->getValidationMessage('code'), 
231
            'The shipping category with given code already exists.'
232
        );
233
    }
234
235
    /**
236
     * @Then there should still be only one shipping category with code :code
237
     */
238
    public function thereShouldStillBeOnlyOneShippingCategoryWith($code)
239
    {
240
        $this->iWantToBrowseShippingCategories();
241
242
        Assert::true(
243
            $this->indexPage->isSingleResourceOnPage(['code' => $code]),
244
            sprintf('Shipping method with code %s cannot be found.', $code)
245
        );
246
    }
247
}
248