Completed
Push — master ( 4dbd44...4341e2 )
by Dmitry
05:09
created

Create::getChargesQuantity()   A

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
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
        $selector = 'div.bill-charges div[class*=input-row]';
121
        $this->tester->seeNumberOfElements($selector, $n);
122
    }
123
124
    /**
125
     * Adds sum of each charge on page and returns it.
126
     *
127
     * @return int - total sum of charges
128
     */
129
    public function getChargesTotalSum(): int
130
    {
131
        $sum = $this->tester->executeJS(<<<JS
132
            var sum = 0;
133
            var selector = 'div.bill-charges div[class*=sum] input';
134
            var chargesSum = document.querySelectorAll(selector);
135
            chargesSum.forEach(function(chargeSum) {
136
               sum += parseInt(chargeSum.value); 
137
            });
138
            return sum
139
JS
140
        );
141
        return $sum;
142
    }
143
144
    public function deleteLastCharge()
145
    {
146
        $this->tester->click('div.bill-charges>div:last-child button');
147
    }
148
149
    /**
150
     * Checks whether a bill was created successfully and returns its id.
151
     *
152
     * @return string - id of created bill.
153
     */
154
    public function seeBillWasCreated(): string
155
    {
156
        $I = $this->tester;
157
158
        $I->closeNotification('Bill was created successfully');
159
        $I->seeInCurrentUrl('/finance/bill?id');
160
161
        return $I->grabFromCurrentUrl('~id_in%5B0%5D=(\d+)~');
162
    }
163
164
    /**
165
     * Looking for blank errors for the given fields.
166
     *
167
     * @param array $fieldsList
168
     * @throws \Exception
169
     */
170
    public function containsBlankFieldsError(array $fieldsList): void
171
    {
172
        foreach ($fieldsList as $field) {
173
            $this->tester->waitForText("$field cannot be blank.");
174
        }
175
    }
176
177
    /**
178
     * Looking for sum mismatch errors.
179
     *
180
     * @throws \Exception
181
     */
182
    public function containsSumMismatch(): void
183
    {
184
        $this->tester->waitForText('Bill sum must match charges sum:');
185
    }
186
187
    /**
188
     *  Saves created bill.
189
     */
190
    public function save(): void
191
    {
192
        $this->tester->click('//button[contains(@type,\'submit\')]');
193
    }
194
}
195