Create::seeActionSuccess()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * Finance module for HiPanel
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-module-finance
6
 * @package   hipanel-module-finance
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\finance\tests\_support\Page\bill;
12
13
use hipanel\tests\_support\Page\Authenticated;
14
use hipanel\tests\_support\Page\Widget\Input\Dropdown;
15
use hipanel\tests\_support\Page\Widget\Input\Input;
16
use hipanel\tests\_support\Page\Widget\Input\Select2;
17
18
class Create extends Authenticated
19
{
20
    /**
21
     * @param array $billData
22
     * @throws \Exception
23
     */
24
    public function fillMainBillFields(array $billData): void
25
    {
26
        $I = $this->tester;
27
28
        (new Select2($I, '#billform-0-client_id'))->setValue($billData['login']);
29
        (new Dropdown($I, '#billform-0-type'))->setValue($billData['type']);
30
        (new Input($I, '#billform-0-sum'))->setValue($billData['sum']);
31
32
        $I->click('//div[@class=\'input-group-btn\']/button[2]');
33
        $I->click('//li/a[contains(text(),\'$\')]');
34
35
        (new Input($I, '#billform-0-quantity'))->setValue($billData['quantity']);
36
    }
37
38
    /**
39
     * @param int $sum
40
     */
41
    public function setBillTotalSum(int $sum): void
42
    {
43
        $this->tester->fillField(['billform-0-sum'], $sum);
44
    }
45
46
    /**
47
     * @param array $chargesData
48
     * @throws \Exception
49
     */
50
    public function addCharges(array $chargesData)
51
    {
52
        foreach ($chargesData as $chargeData) {
53
            $this->addCharge($chargeData);
54
        }
55
    }
56
57
    /**
58
     * @param array $chargeData
59
     * @throws \Exception
60
     */
61
    public function addCharge(array $chargeData): void
62
    {
63
        $this->clickAddChargeButton();
64
        if (!empty($chargeData)) {
65
            $this->fillChargeFields($chargeData);
66
        }
67
    }
68
69
    protected function clickAddChargeButton(): void
70
    {
71
        $this->tester->click('button.add-charge');
72
    }
73
74
    /**
75
     * @param array $chargeData
76
     * @throws \Exception
77
     */
78
    protected function fillChargeFields(array $chargeData): void
79
    {
80
        $I = $this->tester;
81
82
        $base = 'div.bill-charges>div:last-child ';
83
        $classSelector = $base . 'div[class=row] select[id*=class]';
84
        (new Dropdown($I, $classSelector))->setValue($chargeData['class']);
85
86
        $objectIdSelector = $base . 'div[class=row] select[id*=object_id]';
87
        (new Select2($I, $objectIdSelector))->setValue($chargeData['objectId']);
88
89
        $typeSelector = $base . 'div[class*=type] select';
90
        (new Dropdown($I, $typeSelector))->setValue($chargeData['type']);
91
92
        $sumSelector = $base . 'div[class*=sum] input';
93
        (new Input($I, $sumSelector))->setValue($chargeData['sum']);
94
95
        $qtySelector = $base . 'div[class*=quantity] input';
96
        (new Input($I, $qtySelector))->setValue($chargeData['quantity']);
97
    }
98
99
    /**
100
     * Checks whether a page contains the specified quantity of charges.
101
     *
102
     * @param int $n - quantity of charges
103
     */
104
    public function containsCharges(int $n): void
105
    {
106
        $selector = 'div.bill-charges div[class*=input-row]';
107
        $this->tester->seeNumberOfElements($selector, $n);
108
    }
109
110
    /**
111
     * Adds sum of each charge on page and returns it.
112
     *
113
     * @return int - total sum of charges
114
     */
115
    public function getChargesTotalSum(): int
116
    {
117
        $sum = $this->tester->executeJS(<<<JS
118
            var sum = 0;
119
            var selector = 'div.bill-charges div[class*=sum] input';
120
            var chargesSum = document.querySelectorAll(selector);
121
            chargesSum.forEach(function(chargeSum) {
122
               sum += parseInt(chargeSum.value); 
123
            });
124
            return sum
125
JS
126
        );
127
128
        return $sum;
129
    }
130
131
    public function deleteLastCharge(): void
132
    {
133
        $this->tester->click('div.bill-charges>div:last-child button');
134
    }
135
136
    public function deleteChargeByName(string $chargeName): void
137
    {
138
        $this->tester->executeJS(
139
            ';let charge = $("div.bill-charges :contains(\'" + arguments[0] + "\')");
140
            charge.parents(".charge-item").find("button").click();',
141
            [$chargeName]
142
        );
143
    }
144
145
    /**
146
     * Checks whether a bill was created successfully and returns its id.
147
     *
148
     * @return string - id of created bill
149
     */
150
    public function seeActionSuccess(): ?string
151
    {
152
        $I = $this->tester;
153
154
        $I->closeNotification('Bill was created successfully');
155
        $I->seeInCurrentUrl('/finance/bill?id');
156
157
        return $I->grabFromCurrentUrl('~id_in%5B0%5D=(\d+)~');
158
    }
159
160
    /**
161
     * Looking for blank errors for the given fields.
162
     *
163
     * @param array $fieldsList
164
     * @throws \Exception
165
     */
166
    public function containsBlankFieldsError(array $fieldsList): void
167
    {
168
        foreach ($fieldsList as $field) {
169
            $this->tester->waitForText("$field cannot be blank.");
170
        }
171
    }
172
173
    /**
174
     * Looking for sum mismatch errors.
175
     *
176
     * @throws \Exception
177
     */
178
    public function containsSumMismatch(): void
179
    {
180
        $this->tester->waitForText('Bill sum must match charges sum:');
181
    }
182
}
183