Passed
Push — master ( 445708...0db764 )
by Steffen
08:17
created

LengthConverterTest::testConversion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
dl 8
loc 8
rs 9.4285
c 0
b 0
f 0
nc 1
cc 1
eloc 5
nop 6
1
<?php
2
/*
3
 * This file is part of the unicorn project
4
 *
5
 * (c) Philipp Braeutigam <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Xynnn\Unicorn\Tests\Converter;
12
13
use Xynnn\Unicorn\Converter\LengthConverter;
14
use Xynnn\Unicorn\Model\ConvertibleValue;
15
use Xynnn\Unicorn\Model\Unit;
16
17
class LengthConverterTest extends AbstractConverterTest
18
{
19
    public function testIsInstantiable()
20
    {
21
        $converter = $this->getConverter();
22
23
        $this->assertInstanceOf(LengthConverter::class, $converter);
24
    }
25
26
    public function testGetName()
27
    {
28
        $converter = $this->getConverter();
29
30
        $this->assertEquals('unicorn.converter.length', $converter->getName());
31
    }
32
33
    /**
34
     * @expectedException \Xynnn\Unicorn\Exception\UnsupportedUnitException
35
     * @expectedExceptionMessage The conversion of "noUnit" is not possible. Make sure to add it to the converters units array first.
36
     */
37
    public function testWrongTypePassed()
38
    {
39
        $converter = $this->getConverter();
40
        $converter->convert(new ConvertibleValue('10000', $converter::$nanometer), new Unit('noUnit', 'nu', '1'));
41
    }
42
43 View Code Duplication
    public function testOwnTypePassed()
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...
44
    {
45
        $converter = $this->getConverter();
46
        $converter->addUnit(new Unit('myUnit', 'mu', '5'));
47
        $result = $converter->convert(new ConvertibleValue('1', $converter::$meter), new Unit('myUnit', 'mu', '5'));
48
49
        $this->assertEquals('5', $result->getValue());
50
        $this->assertEquals(new Unit('myUnit', 'mu', '5'), $result->getUnit());
51
        $this->assertEquals('mu', $result->getUnit()->getAbbreviation());
52
    }
53
54
    /**
55
     * @return array
56
     */
57
    public function dataProvider()
58
    {
59
        $converter = $this->getConverter();
60
61
        return [
62
            [$converter, new ConvertibleValue('0.000000001', $converter::$meter), $converter::$nanometer, '1', 'nanometer', 'nm'],
63
            [$converter, new ConvertibleValue('0.000001', $converter::$meter), $converter::$micrometer, '1', 'micrometer', 'µm'],
64
            [$converter, new ConvertibleValue('0.001', $converter::$meter), $converter::$millimeter, '1', 'millimeter', 'mm'],
65
            [$converter, new ConvertibleValue('0.01', $converter::$meter), $converter::$centimeter, '1', 'centimeter', 'cm'],
66
            [$converter, new ConvertibleValue('0.1', $converter::$meter), $converter::$decimeter, '1', 'decimeter', 'dm'],
67
            [$converter, new ConvertibleValue('1', $converter::$meter), $converter::$meter, '1', 'meter', 'm'],
68
            [$converter, new ConvertibleValue('1000', $converter::$meter), $converter::$kilometer, '1', 'kilometer', 'km'],
69
            [$converter, new ConvertibleValue('0.0254', $converter::$meter), $converter::$inch, '1', 'inch', 'in'],
70
            [$converter, new ConvertibleValue('0.3048', $converter::$meter), $converter::$feet, '1', 'feet', 'ft'],
71
            [$converter, new ConvertibleValue('0.9144', $converter::$meter), $converter::$yard, '1', 'yard', 'yd'],
72
            [$converter, new ConvertibleValue('1609.344', $converter::$meter), $converter::$mile, '1', 'mile', 'm'],
73
            [$converter, new ConvertibleValue(bcdiv('1', '1609344000000', $converter::MAX_DECIMALS), $converter::$mile), $converter::$nanometer, '1', 'nanometer', 'nm'],
74
            [$converter, new ConvertibleValue(bcdiv('1', '1609344000', $converter::MAX_DECIMALS), $converter::$mile), $converter::$micrometer, '1', 'micrometer', 'µm'],
75
            [$converter, new ConvertibleValue(bcdiv('1', '1609344', $converter::MAX_DECIMALS), $converter::$mile), $converter::$millimeter, '1', 'millimeter', 'mm'],
76
            [$converter, new ConvertibleValue(bcdiv('1', '160934.4', $converter::MAX_DECIMALS), $converter::$mile), $converter::$centimeter, '1', 'centimeter', 'cm'],
77
            [$converter, new ConvertibleValue(bcdiv('1', '16093.44', $converter::MAX_DECIMALS), $converter::$mile), $converter::$decimeter, '1', 'decimeter', 'dm'],
78
            [$converter, new ConvertibleValue(bcdiv('1', '1609.344', $converter::MAX_DECIMALS), $converter::$mile), $converter::$meter, '1', 'meter', 'm'],
79
            [$converter, new ConvertibleValue(bcdiv('1', '1.609344', $converter::MAX_DECIMALS), $converter::$mile), $converter::$kilometer, '1', 'kilometer', 'km'],
80
            [$converter, new ConvertibleValue(bcdiv('1', '63360', $converter::MAX_DECIMALS), $converter::$mile), $converter::$inch, '1', 'inch', 'in'],
81
            [$converter, new ConvertibleValue(bcdiv('1', '5280', $converter::MAX_DECIMALS), $converter::$mile), $converter::$feet, '1', 'feet', 'ft'],
82
            [$converter, new ConvertibleValue(bcdiv('1', '1760', $converter::MAX_DECIMALS), $converter::$mile), $converter::$yard, '1', 'yard', 'yd'],
83
            [$converter, new ConvertibleValue('1', $converter::$mile), $converter::$mile, '1', 'mile', 'm']
84
        ];
85
    }
86
87
    public function testNestedConversion()
88
    {
89
        $converter = $this->getConverter();
90
        $result = $converter->convert(
91
            $converter->convert(new ConvertibleValue('10000', $converter::$nanometer), $converter::$micrometer),
92
            $converter::$nanometer
93
        );
94
95
        $this->assertEquals('10000', $result->getValue());
96
        $this->assertEquals($converter::$nanometer, $result->getUnit());
97
        $this->assertEquals('nm', $result->getUnit()->getAbbreviation());
98
    }
99
100 View Code Duplication
    public function testConversionWithAddition()
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...
101
    {
102
        $converter = $this->getConverter();
103
        $result = $converter->convert(
104
            $converter->add(
105
                new ConvertibleValue('1', $converter::$kilometer),
106
                new ConvertibleValue('1000', $converter::$meter)
107
            ),
108
            $converter::$meter
109
        );
110
111
        $this->assertEquals('2000', $result->getValue());
112
        $this->assertEquals($converter::$meter, $result->getUnit());
113
        $this->assertEquals('m', $result->getUnit()->getAbbreviation());
114
    }
115
116
    public function testConversionWithNestedAddition()
117
    {
118
        $converter = $this->getConverter();
119
        $result = $converter->convert(
120
            $converter->add(
121
                $converter->add(
122
                    new ConvertibleValue('10000', $converter::$nanometer),
123
                    new ConvertibleValue('10', $converter::$micrometer)
124
                ),
125
                new ConvertibleValue('30000', $converter::$nanometer)
126
            ),
127
            $converter::$micrometer
128
        );
129
130
        $this->assertEquals('50', $result->getValue());
131
        $this->assertEquals($converter::$micrometer, $result->getUnit());
132
        $this->assertEquals('µm', $result->getUnit()->getAbbreviation());
133
    }
134
135 View Code Duplication
    public function testConversionWithSubtraction()
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...
136
    {
137
        $converter = $this->getConverter();
138
        $result = $converter->convert(
139
            $converter->sub(
140
                new ConvertibleValue('20000', $converter::$nanometer),
141
                new ConvertibleValue('10', $converter::$micrometer)
142
            ),
143
            $converter::$micrometer
144
        );
145
146
        $this->assertEquals('10', $result->getValue());
147
        $this->assertEquals($converter::$micrometer, $result->getUnit());
148
        $this->assertEquals('µm', $result->getUnit()->getAbbreviation());
149
    }
150
151
    public function testConversionWithNestedSubtraction()
152
    {
153
        $converter = $this->getConverter();
154
        $result = $converter->convert(
155
            $converter->sub(
156
                $converter->sub(
157
                    new ConvertibleValue('100000', $converter::$nanometer),
158
                    new ConvertibleValue('10', $converter::$micrometer)
159
                ),
160
                new ConvertibleValue('30000', $converter::$nanometer)
161
            ),
162
            $converter::$micrometer
163
        );
164
165
        $this->assertEquals('60', $result->getValue());
166
        $this->assertEquals($converter::$micrometer, $result->getUnit());
167
        $this->assertEquals('µm', $result->getUnit()->getAbbreviation());
168
    }
169
170 View Code Duplication
    public function testAdditionMustNotChangeUnit()
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...
171
    {
172
        $converter = $this->getConverter();
173
        $result = $converter->add(
174
            new ConvertibleValue('2', $converter::$kilometer),
175
            new ConvertibleValue('1000', $converter::$meter)
176
        );
177
178
        $this->assertEquals('3', $result->getValue());
179
        $this->assertEquals($converter::$kilometer, $result->getUnit());
180
        $this->assertEquals('km', $result->getUnit()->getAbbreviation());
181
        $this->assertEquals('kilometer', $result->getUnit()->getName());
182
    }
183
184 View Code Duplication
    public function testSubtractionMustNotChangeUnit()
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...
185
    {
186
        $converter = $this->getConverter();
187
        $result = $converter->sub(
188
            new ConvertibleValue('2', $converter::$kilometer),
189
            new ConvertibleValue('1000', $converter::$meter)
190
        );
191
192
        $this->assertEquals('1', $result->getValue());
193
        $this->assertEquals($converter::$kilometer, $result->getUnit());
194
        $this->assertEquals('km', $result->getUnit()->getAbbreviation());
195
        $this->assertEquals('kilometer', $result->getUnit()->getName());
196
    }
197
198
    /**
199
     * @return LengthConverter
200
     */
201
    private function getConverter() : LengthConverter
202
    {
203
        return new LengthConverter();
204
    }
205
}
206