CreditCest::save()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 1
eloc 10
c 2
b 1
f 0
nc 1
nop 2
dl 0
loc 12
rs 9.9332
1
<?php
2
3
namespace models;
4
5
use app\{fixtures\CreditWorkLogFixture, models\Credit};
6
use Codeception\Example;
7
use UnitTester;
8
9
class CreditCest
10
{
11
    public function fixtures(UnitTester $I)
12
    {
13
        $I->haveFixtures([
14
            'credit_worklog' => [
15
                'class' => CreditWorkLogFixture::class,
16
            ],
17
        ]);
18
    }
19
20
    /**
21
     * @dataprovider failedValidationData
22
     * @depends fixtures
23
     */
24
    public function failedValidation(UnitTester $I, Example $example)
25
    {
26
        $credit = new Credit();
27
        $credit->load($example['data'], '');
28
        $I->assertFalse($credit->validate());
29
        foreach ($example['errors'] as $attribute => $message) {
30
            $I->assertTrue($credit->hasErrors($attribute));
31
            $I->assertEquals($message, $credit->getFirstError($attribute));
32
        }
33
    }
34
35
    protected function failedValidationData()
36
    {
37
        return [
38
            [
39
                'data' => [],
40
                'errors' => [
41
                    'workflow_id' => 'Workflow ID cannot be blank.',
42
                    'stage_id' => 'Stage ID cannot be blank.',
43
                ],
44
            ],
45
            [
46
                'data' => [
47
                    'workflow_id' => 10,
48
                    'stage_id' => 10,
49
                ],
50
                'errors' => [
51
                    'workflow_id' => 'Workflow ID is invalid.',
52
                    'stage_id' => 'Not an initial stage for the workflow.',
53
                ],
54
            ],
55
        ];
56
    }
57
58
    /**
59
     * @dataprovider saveData
60
     */
61
    public function save(UnitTester $I, Example $example)
62
    {
63
        $credit = new Credit();
64
        $credit->load($example['data'], '');
65
        $credit->save();
66
        $I->assertEmpty($credit->getFirstErrors());
67
        $I->assertTrue($credit->save());
68
        $I->assertNotEmpty($credit->workLogs);
69
        $I->assertNotEmpty($credit->activeWorkLog);
70
        $I->assertEquals(
71
            $example['data']['stage_id'],
72
            $credit->activeWorkLog->stage_id
73
        );
74
    }
75
76
    protected function saveData()
77
    {
78
        return [
79
            [
80
                'data' => [
81
                    'workflow_id' => 1,
82
                    'stage_id' => 1,
83
                ],
84
            ],
85
        ];
86
    }
87
}
88