Completed
Push — master ( 888950...2eb122 )
by Andrii
05:25
created

FeatureContext::ensureNo()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
/**
3
 * PHP Billing Library
4
 *
5
 * @link      https://github.com/hiqdev/php-billing
6
 * @package   php-billing
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2017-2018, HiQDev (http://hiqdev.com/)
9
 */
10
use Behat\Behat\Context\Context;
11
use hiqdev\php\billing\action\Action;
12
use hiqdev\php\billing\charge\Charge;
13
use hiqdev\php\billing\customer\Customer;
14
use hiqdev\php\billing\formula\FormulaEngine;
15
use hiqdev\php\billing\price\SinglePrice;
16
use hiqdev\php\billing\target\Target;
17
use hiqdev\php\billing\type\Type;
18
use hiqdev\php\units\Quantity;
19
use Money\Currency;
20
use Money\Money;
21
use PHPUnit\Framework\Assert;
22
23
/**
24
 * Defines application features from the specific context.
25
 */
26
class FeatureContext implements Context
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
27
{
28
    protected $engine;
29
30
    protected $customer;
31
    protected $price;
32
    protected $action;
33
    protected $charges;
34
    protected $date;
35
36
    /**
37
     * Initializes context.
38
     */
39
    public function __construct()
40
    {
41
        $this->customer = new Customer(null, 'somebody');
42
    }
43
44
    /**
45
     * @Given /(\S+) (\S+) price is ([0-9.]+) (\w+) per ([0-9.]+) (\w+)/
46
     */
47
    public function priceIs($target, $type, $sum, $currency, $amount, $unit)
48
    {
49
        $type = new Type(Type::ANY, $type);
50
        $target = new Target(Target::ANY, $target);
51
        $quantity = Quantity::create($unit, $amount);
52
        $sum = new Money($sum*100, new Currency($currency));
53
        $this->price = new SinglePrice(null, $type, $target, null, $quantity, $sum);
54
    }
55
56
    /**
57
     * @Given /action is (\S+) (\w+) ([0-9.]+) (\S+)/
58
     */
59
    public function actionIs($target, $type, $amount, $unit)
60
    {
61
        $type = new Type(Type::ANY, $type);
62
        $target = new Target(Target::ANY, $target);
63
        $quantity = Quantity::create($unit, $amount);
64
        $time = new DateTimeImmutable();
65
        $this->action = new Action(null, $type, $target, $quantity, $this->customer, $time);
66
    }
67
68
    /**
69
     * @Given /formula is (.+)/
70
     */
71
    public function formulaIs($formula)
72
    {
73
        $this->price->setFormula($this->getFormulaEngine()->build($formula));
74
    }
75
76
    protected function getFormulaEngine()
77
    {
78
        if ($this->engine === null) {
79
            $this->engine = new FormulaEngine();
80
        }
81
82
        return $this->engine;
83
    }
84
85
    /**
86
     * @When /date is ([0-9.-]+)/
87
     */
88
    public function dateIs($date)
89
    {
90
        $this->date = $date;
91
    }
92
93
    /**
94
     * @Then /(\w+) charge is (\S+)? ?([0-9.]+)? ?([A-Z]{3})?/
95
     */
96
    public function chargeIs($numeral, $type = null, $sum = null, $currency = null)
97
    {
98
        $no = $this->ensureNo($numeral);
99
        if ($no === 0) {
100
            $this->charges = $this->price->calculateCharges($this->action);
101
        }
102
        //var_dump($this->charges); die;
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
103
        $this->assertCharge($type, $sum, $currency, $this->charges[$no]);
104
    }
105
106
    public function assertCharge($type, $sum, $currency, $charge)
107
    {
108
        if (empty($type) && empty($sum) && empty($currency)) {
109
            return;
110
        }
111
        //var_dump($charge);die;
0 ignored issues
show
Unused Code Comprehensibility introduced by
86% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
112
        Assert::assertInstanceOf(Charge::class, $charge);
113
        Assert::assertSame($type, $charge->getPrice()->getType()->getName());
114
        $money = new Money($sum*100, new Currency($currency));
0 ignored issues
show
Unused Code introduced by
$money is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
115
        //Assert::assertEquals($money, $charge->getSum());
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
116
        //var_dump($this->formula);
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
117
        //var_dump($this->date);
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
118
    }
119
120
    protected $numerals = [
121
        'first'     => 1,
122
        'second'    => 2,
123
        'third'     => 3,
124
        'fourth'    => 4,
125
        'fifth'     => 5,
126
    ];
127
128
    public function ensureNo($numeral)
129
    {
130
        if (empty($this->numerals[$numeral])) {
131
            throw new Exception("wrong numeral '$numeral'");
132
        }
133
134
        return $this->numerals[$numeral] - 1;
135
    }
136
}
137