Completed
Push — master ( 08f7b1...ea4e28 )
by Eric
10:49
created

LocaleWebContext::selectGridBatchAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
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 Lug package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Lug\Bundle\LocaleBundle\Behat\Context;
13
14
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
15
use Behat\Gherkin\Node\TableNode;
16
use Lug\Bundle\GridBundle\Behat\Context\GridWebContext;
17
use Lug\Bundle\ResourceBundle\Behat\Context\ResourceWebContext;
18
use Lug\Component\Behat\Context\RoutingContext;
19
20
/**
21
 * @author GeLo <[email protected]>
22
 */
23
class LocaleWebContext extends AbstractLocaleContext
24
{
25
    /**
26
     * @var GridWebContext
27
     */
28
    private $gridContext;
29
30
    /**
31
     * @var RoutingContext
32
     */
33
    private $routingContext;
34
35
    /**
36
     * @BeforeScenario
37
     */
38
    public function init(BeforeScenarioScope $scope)
39
    {
40
        $environment = $scope->getEnvironment();
41
42
        $this->gridContext = $environment->getContext(GridWebContext::class);
43
        $this->resourceContext = $environment->getContext(ResourceWebContext::class);
44
        $this->routingContext = $environment->getContext(RoutingContext::class);
45
    }
46
47
    /**
48
     * @Given I am on the locale listing page
49
     */
50
    public function visitIndex()
51
    {
52
        $this->visit('index');
53
    }
54
55
    /**
56
     * @Given I am on the locale creation page
57
     */
58
    public function visitCreate()
59
    {
60
        $this->visit('create');
61
    }
62
63
    /**
64
     * @param string $code
65
     *
66
     * @Given I am on the locale ":code" page
67
     */
68
    public function visitShow($code)
69
    {
70
        $this->visit('show', $code);
71
    }
72
73
    /**
74
     * @param string $code
75
     *
76
     * @Given I am on the locale ":code" edition page
77
     */
78
    public function visitUpdate($code)
79
    {
80
        $this->visit('update', $code);
81
    }
82
83
    /**
84
     * @Given I toggle the locale grid batches
85
     */
86
    public function toggleGridBatch()
87
    {
88
        $this->gridContext->toggleBatch();
89
    }
90
91
    /**
92
     * @param string $code
93
     *
94
     * @Given I select the locale grid batch ":code"
95
     */
96
    public function selectGridBatch($code)
97
    {
98
        $this->gridContext->selectBatch($code);
99
    }
100
101
    /**
102
     * @Given I select the "all" locale grid batch
103
     */
104
    public function selectGridBatchAll()
105
    {
106
        $this->gridContext->selectBatchAll();
107
    }
108
109
    /**
110
     * @param string $code
111
     * @param string $sorting
112
     *
113
     * @Given I follow the locale grid sorting link ":code" ":sorting"
114
     */
115
    public function followGridSortingLink($code, $sorting)
116
    {
117
        $this->gridContext->followSortingLink($code, $sorting);
118
    }
119
120
    /**
121
     * @param string $code
122
     *
123
     * @Given I follow the locale grid show link ":code"
124
     */
125
    public function followGridShowLink($code)
126
    {
127
        $this->gridContext->followColumnActionLink($code, 'Show');
128
    }
129
130
    /**
131
     * @param string $code
132
     *
133
     * @Given I follow the locale grid edition link ":code"
134
     */
135
    public function followGridEditionLink($code)
136
    {
137
        $this->gridContext->followColumnActionLink($code, 'Update');
138
    }
139
140
    /**
141
     * @param string $code
142
     *
143
     * @Given I follow the locale grid deletion link ":code"
144
     */
145
    public function followGridDeletionLink($code)
146
    {
147
        $this->gridContext->followColumnActionLink($code, 'Delete');
148
    }
149
150
    /**
151
     * @Given I wait the locale grid filters
152
     */
153
    public function waitGridFilters()
154
    {
155
        $this->gridContext->waitFilters();
156
    }
157
158
    /**
159
     * @Given I wait the locale grid filters refresh
160
     */
161
    public function waitGridFiltersRefresh()
162
    {
163
        $this->gridContext->waitFiltersRefresh();
164
    }
165
166
    /**
167
     * @param TableNode $table
168
     *
169
     * @Then the locale grid should be:
170
     */
171
    public function assertGrid(TableNode $table)
172
    {
173
        $this->gridContext->assertGrid($table->getRows());
174
    }
175
176
    /**
177
     * @param string $column
178
     * @param string $order
179
     *
180
     * @Then the locale grid column ":column" should be sorted ":order"
181
     */
182
    public function assertGridSorting($column, $order)
183
    {
184
        $this->gridContext->assertSorting($column, $order);
185
    }
186
187
    /**
188
     * @param string $column
189
     * @param string $cells
190
     *
191
     * @Given the locale grid for the column ":column" should be ":cells"
192
     */
193
    public function assertGridCells($column, $cells)
194
    {
195
        $this->gridContext->assertColumnCells(
196
            $column,
0 ignored issues
show
Documentation introduced by
$column is of type string, but the function expects a array<integer,string>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
197
            array_map('trim', !empty($cells) ? explode(';', $cells) : [])
198
        );
199
    }
200
201
    /**
202
     * @Then all locale grid batches should be checked
203
     */
204
    public function assertGridBatchesChecked()
205
    {
206
        $this->gridContext->assertBatchesState(true);
207
    }
208
209
    /**
210
     * @Then all locale grid batches should not be checked
211
     */
212
    public function assertGridBatchesUnchecked()
213
    {
214
        $this->gridContext->assertBatchesState(false);
215
    }
216
217
    /**
218
     * @Then I should be on the locale listing page
219
     */
220
    public function assertIndexAddress()
221
    {
222
        $this->assertAddress('index');
223
    }
224
225
    /**
226
     * @Then I should be on the locale batching page
227
     */
228
    public function assertBatchAddress()
229
    {
230
        $this->assertAddress('batch');
231
    }
232
233
    /**
234
     * @Then I should be on the locale creation page
235
     */
236
    public function assertCreateAddress()
237
    {
238
        $this->assertAddress('create');
239
    }
240
241
    /**
242
     * @param string $code
243
     *
244
     * @Then I should be on the locale ":code" page
245
     */
246
    public function assertShowAddress($code)
247
    {
248
        $this->assertAddress('show', $code);
249
    }
250
251
    /**
252
     * @param string $code
253
     *
254
     * @Then I should be on the locale ":code" edition page
255
     */
256
    public function assertUpdateAddress($code)
257
    {
258
        $this->assertAddress('update', $code);
259
    }
260
261
    /**
262
     * @param string      $action
263
     * @param string|null $code
264
     */
265 View Code Duplication
    private function visit($action, $code = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
266
    {
267
        $parameters = [];
268
269
        if ($code !== null) {
270
            $parameters['code'] = $code;
271
        }
272
273
        $this->resourceContext->visit('locale', $action, $parameters);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Lug\Bundle\ResourceBundl...AbstractResourceContext as the method visit() does only exist in the following sub-classes of Lug\Bundle\ResourceBundl...AbstractResourceContext: Lug\Bundle\ResourceBundl...text\ResourceWebContext. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
274
    }
275
276
    /**
277
     * @param string      $action
278
     * @param string|null $code
279
     */
280 View Code Duplication
    private function assertAddress($action, $code = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
281
    {
282
        $parameters = [];
283
284
        if ($code !== null) {
285
            $parameters['code'] = $code;
286
        }
287
288
        $this->resourceContext->assertAddress('locale', $action, $parameters);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Lug\Bundle\ResourceBundl...AbstractResourceContext as the method assertAddress() does only exist in the following sub-classes of Lug\Bundle\ResourceBundl...AbstractResourceContext: Lug\Bundle\ResourceBundl...text\ResourceWebContext. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
289
    }
290
}
291