PriceCest::_before()   A
last analyzed

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 1
1
<?php
2
3
namespace advancedhosters\hipanel\tests\acceptance\module\finance\manager;
4
5
use Codeception\Example;
6
use hipanel\helpers\Url;
7
use hipanel\modules\finance\tests\_support\Page\price\Create as PriceCreatePage;
8
use hipanel\modules\finance\tests\acceptance\manager\PriceCest as BasePriceCest;
9
use hipanel\tests\_support\Page\IndexPage;
10
use hipanel\tests\_support\Page\Widget\Input\Dropdown;
11
use hipanel\tests\_support\Page\Widget\Input\Input;
12
use hipanel\tests\_support\Page\Widget\Input\Select2;
13
use hipanel\tests\_support\Step\Acceptance\Manager;
14
15
/**
16
 * Class PriceCest
17
 *
18
 * @author Dmytro Naumenko <[email protected]>
19
 */
20
class PriceCest extends BasePriceCest
21
{
22
    /**
23
     * @var string
24
     */
25
    private $templateName;
26
27
    /**
28
     * @var IndexPage
29
     */
30
    private $index;
31
32
    public function _before(Manager $I)
33
    {
34
        $this->index = new IndexPage($I);
35
        $this->ensureIHaveTestTemplate($I);
36
    }
37
38 View Code Duplication
    public function ensureIndexPageWorks(Manager $I)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
39
    {
40
        $I->login();
41
        $I->needPage(Url::to('@price'));
42
        $I->see('Price', 'h1');
43
        $this->ensureICanSeeAdvancedSearchBox($I);
44
        $this->ensureICanSeeBulkBillSearchBox();
45
    }
46
47
    private function ensureICanSeeAdvancedSearchBox(Manager $I)
48
    {
49
        $this->index->containsFilters([
50
            Input::asAdvancedSearch($I, 'Tariff plan buyer'),
51
            Input::asAdvancedSearch($I, 'Tariff plan owner'),
52
            Input::asAdvancedSearch($I, 'Tariff plan name'),
53
            Input::asAdvancedSearch($I, 'Object name'),
54
            Input::asAdvancedSearch($I, 'Group model name'),
55
            Input::asAdvancedSearch($I, 'Model partno'),
56
            Input::asAdvancedSearch($I, 'Price'),
57
            Dropdown::asAdvancedSearch($I, 'Type')->withItems([
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class hipanel\tests\_support\P...get\Input\TestableInput as the method withItems() does only exist in the following sub-classes of hipanel\tests\_support\P...get\Input\TestableInput: hipanel\tests\_support\Page\Widget\Input\Dropdown. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
58
                'Monthly fee',
59
                'Server traffic monthly fee',
60
                'IP addresses monthly fee',
61
                'Rack unit monthly fee',
62
                'Server traffic 95% overuse',
63
                'Domain traffic',
64
                'Number of domains',
65
                'Backup disk usage monthly fee',
66
                'Backup traffic monthly fee',
67
                'IP traffic',
68
                'Number of IP addresses',
69
                'Support time monthly fee',
70
                'Account traffic',
71
                'Number of accounts',
72
                'Account disk usage',
73
                'Biggest directory',
74
                'CDN disk usage',
75
                'Backup traffic',
76
                'Number of mailboxes',
77
                'Support time',
78
                'Quantity',
79
            ]),
80
            Select2::asAdvancedSearch($I, 'Currency')
81
        ]);
82
    }
83
84
    private function ensureICanSeeBulkBillSearchBox()
85
    {
86
        $this->index->containsBulkButtons([
87
            'Update',
88
            'Delete',
89
        ]);
90
        $this->index->containsColumns([
91
            'Object',
92
            'Details',
93
            'Price',
94
            'Type',
95
            'Note',
96
            'Tariff plan',
97
        ]);
98
    }
99
100
    /**
101
     * @return array of types that should be included in template plan
102
     *
103
     * ```php
104
     * return [
105
     *     ['vCDN'],
106
     *     ['pCDN']
107
     * ];
108
     * ```
109
     */
110
    protected function templatePriceTypesProvider(): array
111
    {
112
        return [
113
            ['Model groups'],
114
            ['Dedicated Server'],
115
            ['vCDN'],
116
            ['pCDN']
117
        ];
118
    }
119
120
    /**
121
     * @dataProvider templatePriceTypesProvider
122
     * @param Manager $I
123
     * @param Example $example
124
     */
125
    public function ensureICanCreateTemplatePlan(Manager $I, Example $example): void
126
    {
127
        $page = new PriceCreatePage($I, $this->id);
128
        $page->openModal();
129
        $page->choosePriceType($example[0]);
130
        $page->proceedToCreation();
131
        $page->fillRandomPrices('templateprice');
132
        $page->saveForm();
133
        $page->seeRandomPrices();
134
    }
135
136
    private function ensureIHaveTestTemplate(Manager $I): void
137
    {
138
        if (!$this->templateName) {
139
            $this->templateName = uniqid('TemplatePlan', true);
140
            $this->id = $this->createPlan($I, $this->templateName, 'Template');
141
            $I->needPage(Url::to(['@plan/view', 'id' => $this->id]));
142
            $I->see('No prices found');
143
        }
144
    }
145
146
    protected function suggestedPricesOptionsProvider(Manager $I): array
147
    {
148
        return [
149
            [
150
                'type' => 'Server',
151
                'templateName' => $this->templateName,
152
                'priceTypes' => ['Main prices', 'Parts prices'],
153
                'object' => 'TEST-DS-01',
154
            ],
155
            [
156
                'type' => 'vCDN',
157
                'templateName' => $this->templateName,
158
                'priceTypes' => ['Main prices'],
159
                'object' => 'vCDN-soltest',
160
            ],
161
        ];
162
    }
163
}
164