Completed
Push — pull-request/7609 ( 4ccf63...393b85 )
by Kamil
146:59 queued 125:08
created

ShowPage::getAverageTotalInChannel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
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
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 getCustomerPhoneNumber()
54
    {
55
        return $this->getElement('customer_phone_number')->getText();
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function getCustomerName()
62
    {
63
        return $this->getElement('customer_name')->getText();
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function getRegistrationDate()
70
    {
71
        return new \DateTime(str_replace('Customer since ', '', $this->getElement('registration_date')->getText()));
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function getDefaultAddress()
78
    {
79
        return $this->getElement('default_address')->getText();
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function hasAccount()
86
    {
87
        return $this->hasElement('no_account');
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function isSubscribedToNewsletter()
94
    {
95
        $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...
96
        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...
97
            return true;
98
        }
99
100
        return false;
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106
    public function hasDefaultAddressProvinceName($provinceName)
107
    {
108
        $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...
109
110
        return false !== stripos($defaultAddressProvince, $provinceName);
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116
    public function hasVerifiedEmail()
117
    {
118
        $verifiedEmail = $this->getElement('verified_email');
119
        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...
120
            return true;
121
        }
122
123
        return false;
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129
    public function getGroupName()
130
    {
131
        $group = $this->getElement('group');
132
133
        Assert::notNull($group, 'There should be element group on page.');
134
135
        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...
136
137
        return trim($groupName);
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143
    public function hasEmailVerificationInformation()
144
    {
145
        return null === $this->getDocument()->find('css', '#verified-email');
146
    }
147
148
    /**
149
     * {@inheritdoc}
150
     */
151
    public function hasImpersonateButton()
152
    {
153
        return $this->hasElement('impersonate_button');
154
    }
155
156
    /**
157
     * {@inheritdoc}
158
     */
159
    public function impersonate()
160
    {
161
        $this->getElement('impersonate_button')->click();
162
    }
163
164
    /**
165
     * {@inheritdoc}
166
     */
167
    public function hasCustomerPlacedAnyOrders()
168
    {
169
        return null !== $this->getElement('statistics')->find('css', '.sylius-orders-overall-count');
170
    }
171
172
    /**
173
     * {@inheritdoc}
174
     */
175
    public function getOrdersCountInChannel($channelName)
176
    {
177
        return (int) $this
178
            ->getStatisticsForChannel($channelName)
179
            ->find('css', '.sylius-orders-count')
180
            ->getText()
181
        ;
182
    }
183
184
    /**
185
     * {@inheritdoc}
186
     */
187
    public function getOrdersTotalInChannel($channelName)
188
    {
189
        return $this
190
            ->getStatisticsForChannel($channelName)
191
            ->find('css', '.sylius-orders-total')
192
            ->getText()
193
        ;
194
    }
195
196
    /**
197
     * {@inheritdoc}
198
     */
199
    public function getAverageTotalInChannel($channelName)
200
    {
201
        return $this
202
            ->getStatisticsForChannel($channelName)
203
            ->find('css', '.sylius-order-average-total')
204
            ->getText()
205
        ;
206
    }
207
208
    /**
209
     * {@inheritdoc}
210
     */
211
    public function getSuccessFlashMessage()
212
    {
213
        return trim($this->getElement('flash_message')->getText());
214
    }
215
216
    /**
217
     * {@inheritdoc}
218
     */
219
    public function getRouteName()
220
    {
221
        return 'sylius_admin_customer_show';
222
    }
223
224
    /**
225
     * {@inheritdoc}
226
     */
227
    protected function getDefinedElements()
228
    {
229
        return array_merge(parent::getDefinedElements(), [
230
            'customer_email' => '#info .content.extra > a',
231
            'customer_name' => '#info .content > a',
232
            'customer_phone_number' => '#phone-number',
233
            'default_address' => '#default-address',
234
            'delete_account_button' => '#actions',
235
            'flash_message' => '.ui.icon.positive.message .content p',
236
            'group' => '.group',
237
            'impersonate_button' => '#impersonate',
238
            'no_account' => '#no-account',
239
            'statistics' => '#statistics',
240
            'registration_date' => '#info .content .date',
241
            'subscribed_to_newsletter' => '#subscribed-to-newsletter',
242
            'verified_email' => '#verified-email',
243
        ]);
244
    }
245
246
    /**
247
     * @param string $channelName
248
     *
249
     * @return NodeElement
250
     *
251
     * @throws \InvalidArgumentException
252
     */
253
    private function getStatisticsForChannel($channelName)
254
    {
255
        $statisticsRibs = $this
256
            ->getElement('statistics')
257
            ->findAll('css', '.row > .column > .statistic > .sylius-channel-name')
258
        ;
259
260
        $statisticsRibs = array_filter($statisticsRibs, function (NodeElement $statistic) use ($channelName) {
261
            return $channelName === trim($statistic->getText());
262
        });
263
264
        $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...
265
        Assert::same(
266
            1,
267
            $actualStatisticsCount,
268
            sprintf(
269
                'Expected a single statistic for channel "%s", but %d were found.',
270
                $channelName,
271
                $actualStatisticsCount
272
            )
273
        );
274
275
        $statisticsContents = $this->getElement('statistics')->findAll('css', '.row');
276
        $contentIndexes = array_keys($statisticsRibs);
277
278
        return $statisticsContents[reset($contentIndexes)];
279
    }
280
}
281