Completed
Push — master ( c8110c...3d2236 )
by Dmitry
19:54 queued 16:15
created

Create::fillBillFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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