Completed
Push — master ( 93e88c...66f8df )
by Andrii
02:53
created

tests/behat/bootstrap/BillingContext.php (1 issue)

1
<?php
2
3
namespace hiqdev\php\billing\tests\behat\bootstrap;
4
5
use hiqdev\php\billing\bill\BillInterface;
6
use hiqdev\php\billing\charge\ChargeInterface;
7
use PHPUnit\Framework\Assert;
8
9
class BillingContext extends BaseContext
10
{
11
    protected $bill;
12
13
    protected array $charges = [];
14
15
    /**
16
     * @Given reseller :reseller
17
     */
18
    public function reseller($reseller)
19
    {
20
        $this->builder->buildReseller($reseller);
21
    }
22
23
    /**
24
     * @Given customer :customer
25
     */
26
    public function customer($customer)
27
    {
28
        $this->builder->buildCustomer($customer);
29
    }
30
31
    /**
32
     * @Given manager :manager
33
     */
34
    public function manager($manager)
35
    {
36
        $this->builder->buildManager($manager);
37
    }
38
39
    /**
40
     * @Given /^(grouping )?(\S+) tariff plan (\S+)/
41
     */
42
    public function plan($grouping, $type, $plan)
43
    {
44
        $this->builder->buildPlan($plan, $type, !empty($grouping));
45
    }
46
47
    protected function fullPrice(array $data)
48
    {
49
        $this->builder->buildPrice($data);
50
    }
51
52
    /**
53
     * @Given /price for (\S+) is +(\S+) (\S+) per (\S+) for target (\S+)/
54
     */
55
    public function priceWithObject($type, $price, $currency, $unit, $target)
56
    {
57
        return $this->fullPrice(compact('type', 'price', 'currency', 'unit', 'target'));
58
    }
59
60
    /**
61
     * @Given /price for (\S+) is +(\S+) (\S+) per (\S+) prepaid (\S+)/
62
     */
63
    public function priceWithOver($type, $price, $currency, $unit, $prepaid)
64
    {
65
        return $this->fullPrice(compact('type', 'price', 'currency', 'unit', 'prepaid'));
66
    }
67
68
    /**
69
     * @Given /price for (\S+) is +(\S+) (\S+) per (\S+)$/
70
     */
71
    public function price($type, $price, $currency, $unit)
72
    {
73
        return $this->fullPrice(compact('type', 'price', 'currency', 'unit'));
74
    }
75
76
    /**
77
     * @Given /price for (\S+) is +(\S+) (\S+) per 1 (\S+) and (\S+) (\S+) per 2 (\S+) for target (\S+)/
78
     */
79
    public function enumPrice($type, $price, $currency, $unit, $price2, $currency2, $unit2, $target)
80
    {
81
        $sums = [1 => $price, 2 => $price2];
82
        return $this->fullPrice(compact('type', 'sums', 'currency', 'unit', 'target'));
83
    }
84
85
    /**
86
     * @Given /^remove and recreate tariff plan (\S+)/
87
     */
88
    public function recreatePlan($plan)
89
    {
90
        $this->builder->recreatePlan($plan);
91
    }
92
93
    /**
94
     * @Given /sale (\S+) for (\S+) plan:(\S+) time:(\S+)/
95
     */
96
    public function sale($id, $target, $plan, $time): void
97
    {
98
        $this->builder->buildSale($id, $target, $plan, $time);
99
    }
100
101
    /**
102
     * @Given /resource consumption for (\S+) is (\d+) (\S+) for target (\S+) at (.+)$/
103
     */
104
    public function setConsumption(string $type, int $amount, string $unit, string $target, string $time): void
105
    {
106
        $this->builder->setConsumption($type, $amount, $unit, $target, $time);
107
    }
108
109
    /**
110
     * @Given /perform billing for time (\S+) for sales/
111
     */
112
    public function performBilling(string $time): void
113
    {
114
        $this->builder->performBilling($time);
115
    }
116
117
    /**
118
     * @Given /bill +for (\S+) is +(\S+) (\S+) per (\d+) (\S+) for target (\S+)$/
119
     */
120
    public function bill($type, $sum, $currency, $quantity, $unit, $target)
121
    {
122
        $bill = $this->findBill([
123
            'type' => $type,
124
            'target' => $target,
125
            'sum' => "$sum $currency",
126
            'quantity' => "$quantity $unit",
127
        ]);
128
        Assert::assertSame($type, $bill->getType()->getName());
129
        Assert::assertSame($target, $bill->getTarget()->getType() . ':' . $bill->getTarget()->getName());
130
        Assert::assertEquals($sum*100, $bill->getSum()->getAmount());
131
        Assert::assertSame($currency, $bill->getSum()->getCurrency()->getCode());
132
        Assert::assertEquals($quantity, $bill->getQuantity()->getQuantity());
133
        Assert::assertSame($unit, $bill->getQuantity()->getUnit()->getName());
134
    }
135
136
    public function findBill(array $params): BillInterface
137
    {
138
        $bills = $this->builder->findBills($params);
139
        $this->bill = reset($bills);
140
        $this->charges = $this->bill->getCharges();
141
142
        return $this->bill;
143
    }
144
145
    /**
146
     * @Given /bills number is (\d+) for (\S+) for target (\S+)/
147
     */
148
    public function billsNumber($number, $type, $target)
149
    {
150
        $count = count($this->builder->findBills([
151
            'type' => $type,
152
            'target' => $target,
153
        ]));
154
155
        Assert::assertEquals($number, $count);
156
    }
157
158
    /**
159
     * @Given /charge for (\S+) is +(\S+) (\S+) per (\d+) (\S+) for target (\S+)$/
160
     */
161
    public function chargeWithTarget($type, $amount, $currency, $quantity, $unit, $target)
162
    {
163
        $charge = $this->findCharge($type, $target);
164
        Assert::assertSame($type, $charge->getType()->getName());
165
        Assert::assertSame($target, $charge->getTarget()->getType() . ':' . $charge->getTarget()->getName());
166
        Assert::assertEquals($amount*100, $charge->getSum()->getAmount());
167
        Assert::assertSame($currency, $charge->getSum()->getCurrency()->getCode());
168
        Assert::assertEquals($quantity, $charge->getUsage()->getQuantity());
169
        Assert::assertSame($unit, $charge->getUsage()->getUnit()->getName());
170
    }
171
172
    /**
173
     * @Given /charge for (\S+) is +(\S+) (\S+) per (\d+) (\S+)$/
174
     */
175
    public function charge($type, $amount, $currency, $quantity, $unit)
176
    {
177
        $this->chargeWithTarget($type, $amount, $currency, $quantity, $unit, null);
178
    }
179
180
    public function findCharge($type, $target): ChargeInterface
181
    {
182
        foreach ($this->charges as $charge) {
183
            if ($charge->getType()->getName() !== $type) {
184
                continue;
185
            }
186
            if ($charge->getTarget()->getFullName() !== $target) {
187
                continue;
188
            }
189
            return $charge;
190
        }
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return hiqdev\php\billing\charge\ChargeInterface. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
191
    }
192
193
    public function getNextCharge(): ChargeInterface
194
    {
195
        $charge = current($this->charges);
196
        next($this->charges);
197
198
        return $charge;
199
    }
200
}
201