Completed
Pull Request — master (#33)
by Dmitry
12:31
created

Create::createDetailedBill()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 1
nc 1
nop 1
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
    /**
22
     * Tries to create a new simple bill without any data.
23
     *
24
     * Expects blank field errors.
25
     * @throws \Exception
26
     */
27
    public function createBillWithoutData(): void
28
    {
29
        $I = $this->tester;
30
31
        $I->needPage(Url::to('@bill/create'));
32
        $this->clickSaveButton();
33
34
        $this->seeBlankFieldsError(['Client', 'Sum', 'Currency', 'Quantity']);
35
    }
36
37
    /**
38
     * Tries to create a new simple bill with all the necessary data.
39
     *
40
     * Expects successful bill creation.
41
     *
42
     * @param array $billData
43
     */
44
    public function createBill(array $billData): void
45
    {
46
        $I = $this->tester;
47
48
        $I->needPage(Url::to('@bill/create'));
49
        $this->fillBillFields($billData);
50
        $this->clickSaveButton();
51
        $this->seeBillWasCreated();
52
    }
53
54
    /**
55
     * Tries to create a new detailed bill without detailed data.
56
     *
57
     * Expects blank field errors.
58
     *
59
     * @param array $billData
60
     * @throws \Exception
61
     */
62
    public function createDetailedBillWithoutDetailedData(array $billData): void
63
    {
64
        $I = $this->tester;
65
66
        $I->needPage(Url::to('@bill/create'));
67
        $this->fillBillFields($billData);
68
        $this->clickDetailingButton();
69
        $this->clickSaveButton();
70
        $this->seeBlankFieldsError(['Object', 'Sum', 'Quantity']);
71
    }
72
73
    /**
74
     * Tries to create a new detailed bill with all the necessary data.
75
     *
76
     * Expects successful bill creation.
77
     * Also checks Sum field mismatch error.
78
     *
79
     * @param array $billData
80
     * @throws \Exception
81
     */
82
    public function createDetailedBill(array $billData): void
83
    {
84
        $I = $this->tester;
85
86
        $I->needPage(Url::to('@bill/create'));
87
        $this->fillBillFields($billData);
88
89
        $this->fillDetailingFields($billData, 1);
90
        $this->clickDetailingButton();
91
        $this->fillDetailingFields($billData, 2);
92
        $this->clickSaveButton();
93
94
        $I->waitForText('Bill sum must match charges sum:');
95
        $I->fillField(['name' => 'Charge[0][1][sum]'], -$billData['sum'] / 2);
96
        $I->fillField(['name' => 'Charge[0][2][sum]'], -$billData['sum'] / 2);
97
98
        $this->clickSaveButton();
99
        $this->seeBillWasCreated();
100
    }
101
102
    /**
103
     * Fills basic bill fields.
104
     *
105
     * @param array $billData
106
     */
107
    protected function fillBillFields(array $billData): void
108
    {
109
        $I = $this->tester;
110
111
        $this->select2->open('#billform-0-client_id');
112
        $this->select2->fillSearchField($billData['login']);
113
        $this->select2->chooseOption($billData['login']);
114
115
        $I->selectOption('#billform-0-type', ['value' => $billData['type']]);
116
117
        $I->fillField(['name' => 'BillForm[0][sum]'], $billData['sum']);
118
119
        $I->click('//div[contains(@class,\'input-group-btn\')]//button[2]');
120
        $I->click('//a[contains(text(),\'$\')]');
121
122
        $I->fillField(['name' => 'BillForm[0][quantity]'], $billData['quantity']);
123
    }
124
125
    /**
126
     * Fills detailed bill fields.
127
     *
128
     * @param array $billData
129
     * @param int $n number of detailed block
130
     */
131
    protected function fillDetailingFields(array $billData, $n): void
132
    {
133
        $I = $this->tester;
134
135
        $I->selectOption("#charge-0-$n-class", ['value' => 'Server']);
136
137
        $this->select2->open("#charge-0-$n-object_id");
138
        $this->select2->fillSearchField("TEST01");
139
        $this->select2->chooseOption("TEST01");
140
141
        $I->selectOption("#charge-0-$n-type", ['value' => $billData['type']]);
142
143
        $I->fillField(['name' => "Charge[0][$n][sum]"], $billData['sum']);
144
145
        $I->fillField(['name' => "Charge[0][$n][quantity]"], $billData['quantity']);
146
    }
147
148
    protected function clickSaveButton(): void
149
    {
150
        $this->tester->click('//button[contains(@type,\'submit\')]');
151
    }
152
153
    protected function clickDetailingButton(): void
154
    {
155
        $this->tester->click('//div[@class=\'col-md-12 margin-bottom\']' .
156
                                '//button[@type=\'button\']');
157
    }
158
159
    /**
160
     * Checks whether a bill was successfully created.
161
     */
162
    protected function seeBillWasCreated(): void
163
    {
164
        $I = $this->tester;
165
166
        $I->closeNotification('Bill was created successfully');
167
        $I->seeInCurrentUrl('/finance/bill?id');
168
    }
169
170
    /**
171
     * Looking for blank errors for the given fields.
172
     *
173
     * @param array $fieldsList
174
     * @throws \Exception
175
     */
176
    protected function seeBlankFieldsError(array $fieldsList): void
177
    {
178
        foreach ($fieldsList as $field) {
179
            $this->tester->waitForText("$field cannot be blank.");
180
        }
181
    }
182
}
183