Completed
Push — wtf-removal ( 4585f3 )
by Kamil
18:08
created

ShowPage::hasCustomerPlacedAnyOrders()   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 0
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\Page\Admin\Customer;
13
14
use Behat\Mink\Element\NodeElement;
15
use Sylius\Behat\Page\SymfonyPage;
16
use Webmozart\Assert\Assert;
17
18
/**
19
 * @author Magdalena Banasiak <[email protected]>
20
 */
21
class ShowPage extends SymfonyPage implements ShowPageInterface
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function isRegistered()
27
    {
28
        $username = $this->getDocument()->find('css', '#username');
29
30
        return null !== $username;
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function deleteAccount()
37
    {
38
        $deleteButton = $this->getElement('delete_account_button');
39
        $deleteButton->pressButton('Delete');
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function getCustomerEmail()
46
    {
47
        return $this->getElement('customer_email')->getText();
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function getCustomerName()
54
    {
55
        return $this->getElement('customer_name')->getText();
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function getRegistrationDate()
62
    {
63
        return new \DateTime(str_replace('Customer since ', '', $this->getElement('registration_date')->getText()));
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function getDefaultAddress()
70
    {
71
        return $this->getElement('default_address')->getText();
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function hasAccount()
78
    {
79
        return $this->hasElement('no_account');
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function isSubscribedToNewsletter()
86
    {
87
        $subscribedToNewsletter = $this->getElement('subscribed_to_newsletter');
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $subscribedToNewsletter 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...
88
        if ($subscribedToNewsletter->find('css', 'i.green')) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return (bool) $subscribe...find('css', 'i.green');.
Loading history...
89
            return true;
90
        }
91
92
        return false;
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function hasDefaultAddressProvinceName($provinceName)
99
    {
100
        $defaultAddressProvince = $this->getElement('default_address')->getText();
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $defaultAddressProvince 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...
101
102
        return false !== stripos($defaultAddressProvince, $provinceName);
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function hasVerifiedEmail()
109
    {
110
        $verifiedEmail = $this->getElement('verified_email');
111
        if ($verifiedEmail->find('css', 'i.green')) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return (bool) $verifiedE...find('css', 'i.green');.
Loading history...
112
            return true;
113
        }
114
115
        return false;
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121
    public function getGroupName()
122
    {
123
        $group = $this->getElement('group');
124
125
        Assert::notNull($group, 'There should be element group on page.');
126
127
        list($text, $groupName) = explode(':', $group->getText());
0 ignored issues
show
Unused Code introduced by
The assignment to $text is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
128
129
        return trim($groupName);
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135
    public function hasEmailVerificationInformation()
136
    {
137
        return null === $this->getDocument()->find('css', '#verified-email');
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143
    public function hasImpersonateButton()
144
    {
145
        return $this->hasElement('impersonate_button');
146
    }
147
148
    /**
149
     * {@inheritdoc}
150
     */
151
    public function impersonate()
152
    {
153
        $this->getElement('impersonate_button')->click();
154
    }
155
156
    /**
157
     * {@inheritdoc}
158
     */
159
    public function hasCustomerPlacedAnyOrders()
160
    {
161
        return null !== $this->getElement('statistics')->find('css', '.sylius-orders-overall-count');
162
    }
163
164
    /**
165
     * {@inheritdoc}
166
     */
167
    public function getOverallOrdersCount()
168
    {
169
        $overallOrders = $this
170
            ->getElement('statistics')
171
            ->find('css', '.sylius-orders-overall-count')
172
            ->getText()
173
        ;
174
175
        return (int) preg_replace('/[^0-9]/', '',$overallOrders);
176
    }
177
178
    /**
179
     * {@inheritdoc}
180
     */
181
    public function getOrdersCountInChannel($channelName)
182
    {
183
        $ordersCountStatistic = $this
184
            ->getStatisticsForChannel($channelName)
185
            ->find('css', '.sylius-orders-count')
186
            ->getText()
187
        ;
188
189
        return (int) $this->getStatisticValue($ordersCountStatistic);
190
    }
191
192
    /**
193
     * {@inheritdoc}
194
     */
195
    public function getOrdersTotalInChannel($channelName)
196
    {
197
        $ordersCountStatistic = $this
198
            ->getStatisticsForChannel($channelName)
199
            ->find('css', '.sylius-orders-total')
200
            ->getText()
201
        ;
202
203
        return $this->getStatisticValue($ordersCountStatistic);
204
    }
205
206
    /**
207
     * {@inheritdoc}
208
     */
209
    public function getAverageTotalInChannel($channelName)
210
    {
211
        $averageTotalStatistic = $this
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $averageTotalStatistic 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...
212
            ->getStatisticsForChannel($channelName)
213
            ->find('css', '.sylius-order-average-total')
214
            ->getText()
215
        ;
216
217
        return $this->getStatisticValue($averageTotalStatistic);
218
    }
219
220
    /**
221
     * {@inheritdoc}
222
     */
223
    public function getSuccessFlashMessage()
224
    {
225
        return trim($this->getElement('flash_message')->getText());
226
    }
227
228
    /**
229
     * {@inheritdoc}
230
     */
231
    public function getRouteName()
232
    {
233
        return 'sylius_admin_customer_show';
234
    }
235
236
    /**
237
     * {@inheritdoc}
238
     */
239
    protected function getDefinedElements()
240
    {
241
        return array_merge(parent::getDefinedElements(), [
242
            'customer_email' => '#info .content.extra > a',
243
            'customer_name' => '#info .content > a',
244
            'default_address' => '#defaultAddress address',
245
            'delete_account_button' => '#actions',
246
            'flash_message' => '.ui.icon.positive.message .content p',
247
            'group' => '.group',
248
            'impersonate_button' => '#impersonate',
249
            'no_account' => '#no-account',
250
            'statistics' => '#statistics',
251
            'registration_date' => '#info .content .date',
252
            'subscribed_to_newsletter' => '#subscribed-to-newsletter',
253
            'verified_email' => '#verified-email',
254
        ]);
255
    }
256
257
    /**
258
     * @param string $channelName
259
     *
260
     * @return NodeElement
261
     *
262
     * @throws \InvalidArgumentException
263
     */
264
    private function getStatisticsForChannel($channelName)
265
    {
266
        $statisticsRibs = $this->getElement('statistics')->findAll('css', '.accordion > .title');
267
268
        $statisticsRibs = array_filter($statisticsRibs, function (NodeElement $statistic) use ($channelName) {
269
            return $channelName === trim($statistic->getText());
270
        });
271
272
        $actualStatisticsCount = count($statisticsRibs);
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $actualStatisticsCount 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...
273
        Assert::same(
274
            1,
275
            $actualStatisticsCount,
276
            sprintf(
277
                'Expected a single statistic for channel "%s", but %d were found.',
278
                $channelName,
279
                $actualStatisticsCount
280
            )
281
        );
282
283
        $statisticsContents = $this->getElement('statistics')->findAll('css', '.accordion > .content');
284
        $contentIndexes = array_keys($statisticsRibs);
285
286
        return $statisticsContents[reset($contentIndexes)];
287
    }
288
289
    /**
290
     * @param string $statistic
291
     *
292
     * @return string
293
     */
294
    private function getStatisticValue($statistic)
295
    {
296
        return trim(substr($statistic, strpos($statistic, ':') + 1));
297
    }
298
}
299