Passed
Push — master ( d286b7...24e4a5 )
by Jan
04:36
created

AmountFormatterTest::testFormatUnitWithSI()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4
 *
5
 * Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics)
6
 *
7
 * This program is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU General Public License
9
 * as published by the Free Software Foundation; either version 2
10
 * of the License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
20
 */
21
22
namespace App\Tests\Services;
23
24
use App\Entity\Parts\MeasurementUnit;
25
use App\Services\AmountFormatter;
26
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
27
28
class AmountFormatterTest extends WebTestCase
29
{
30
    /**
31
     * @var AmountFormatter
32
     */
33
    protected $service;
34
35
    public function setUp()
36
    {
37
        parent::setUp(); // TODO: Change the autogenerated stub
38
39
        //Get an service instance.
40
        self::bootKernel();
41
        $this->service = self::$container->get(AmountFormatter::class);
42
    }
43
44
    public function testFormatWithoutUnit()
45
    {
46
        $this->assertEquals('2', $this->service->format(2.321));
47
        $this->assertEquals('1002', $this->service->format(1002.356));
48
        $this->assertEquals('1000454', $this->service->format(1000454.0));
49
        $this->assertEquals('0', $this->service->format(0.01));
50
        $this->assertEquals('0', $this->service->format(0));
51
    }
52
53
    public function testInvalidInput()
54
    {
55
        $this->expectException(\InvalidArgumentException::class);
56
        $this->service->format('test');
57
    }
58
59
    public function testFormatUnitWithoutSI()
60
    {
61
        $meters = new MeasurementUnit();
62
        $meters->setIsInteger(false)->setUseSIPrefix(false)->setUnit('m');
63
64
        $this->assertEquals('0.32 m', $this->service->format(0.3245, $meters));
65
        $this->assertEquals('10003.56 m', $this->service->format(10003.556, $meters));
66
        $this->assertEquals('0.00 m', $this->service->format(0.0004, $meters));
67
    }
68
69
    public function testFormatUnitWithSI()
70
    {
71
        $meters = new MeasurementUnit();
72
        $meters->setIsInteger(false)->setUseSIPrefix(true)->setUnit('m');
73
74
        $this->assertEquals('0.32 m', $this->service->format(0.3245, $meters));
75
        $this->assertEquals('12.32 m', $this->service->format(12.323, $meters));
76
        $this->assertEquals('120.32 km', $this->service->format(120320.45, $meters));
77
78
        $this->assertEquals('0.32 mm', $this->service->format(0.00032, $meters));
79
    }
80
81
    public function testFormatMoreDigits()
82
    {
83
        $this->assertEquals('12.12345', $this->service->format(12.1234532, null, ['is_integer' => false, 'decimals' => 5]));
84
        $this->assertEquals('12.1', $this->service->format(12.1234532, null, ['is_integer' => false, 'decimals' => 1]));
85
    }
86
87
    public function testFormatOptionsOverride()
88
    {
89
        $meters = new MeasurementUnit();
90
        $meters->setIsInteger(false)->setUseSIPrefix(true)->setUnit('m');
91
92
        $this->assertEquals('12.32', $this->service->format(12.323, $meters, ['unit' => '']));
93
        $this->assertEquals('12002.32 m', $this->service->format(12002.32, $meters, ['show_prefix' => false]));
94
        $this->assertEquals('123 m', $this->service->format(123.234, $meters, ['is_integer' => true]));
95
    }
96
}
97