mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-08-30 14:49:55 +02:00
Added some more tests.
Also changed the behavior of some code to meet the expectation.
This commit is contained in:
parent
322778af68
commit
a97d016740
8 changed files with 523 additions and 12 deletions
108
tests/Services/AmountFormatterTest.php
Normal file
108
tests/Services/AmountFormatterTest.php
Normal file
|
@ -0,0 +1,108 @@
|
|||
<?php
|
||||
/**
|
||||
*
|
||||
* part-db version 0.1
|
||||
* Copyright (C) 2005 Christoph Lechner
|
||||
* http://www.cl-projects.de/
|
||||
*
|
||||
* part-db version 0.2+
|
||||
* Copyright (C) 2009 K. Jacobs and others (see authors.php)
|
||||
* http://code.google.com/p/part-db/
|
||||
*
|
||||
* Part-DB Version 0.4+
|
||||
* Copyright (C) 2016 - 2019 Jan Böhmer
|
||||
* https://github.com/jbtronics
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
*
|
||||
*/
|
||||
|
||||
namespace App\Tests\Services;
|
||||
|
||||
|
||||
use App\Entity\Parts\MeasurementUnit;
|
||||
use App\Services\AmountFormatter;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||
use Symfony\Component\Intl\Locale\Locale;
|
||||
|
||||
class AmountFormatterTest extends WebTestCase
|
||||
{
|
||||
/**
|
||||
* @var AmountFormatter
|
||||
*/
|
||||
protected $service;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp(); // TODO: Change the autogenerated stub
|
||||
|
||||
//Get an service instance.
|
||||
self::bootKernel();
|
||||
$this->service = self::$container->get(AmountFormatter::class);
|
||||
}
|
||||
|
||||
public function testFormatWithoutUnit()
|
||||
{
|
||||
$this->assertEquals("2", $this->service->format(2.321));
|
||||
$this->assertEquals("1002", $this->service->format(1002.356));
|
||||
$this->assertEquals("1000454", $this->service->format(1000454.0));
|
||||
$this->assertEquals("0", $this->service->format(0.01));
|
||||
$this->assertEquals("0", $this->service->format(0));
|
||||
}
|
||||
|
||||
public function testInvalidInput()
|
||||
{
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
$this->service->format("test");
|
||||
}
|
||||
|
||||
public function testFormatUnitWithoutSI()
|
||||
{
|
||||
$meters = new MeasurementUnit();
|
||||
$meters->setIsInteger(false)->setUseSIPrefix(false)->setUnit("m");
|
||||
|
||||
$this->assertEquals('0.32 m', $this->service->format(0.3245, $meters));
|
||||
$this->assertEquals('10003.56 m', $this->service->format(10003.556, $meters));
|
||||
$this->assertEquals('0.00 m', $this->service->format(0.0004, $meters));
|
||||
}
|
||||
|
||||
public function testFormatUnitWithSI()
|
||||
{
|
||||
$meters = new MeasurementUnit();
|
||||
$meters->setIsInteger(false)->setUseSIPrefix(true)->setUnit('m');
|
||||
|
||||
$this->assertEquals('0.32 m', $this->service->format(0.3245, $meters));
|
||||
$this->assertEquals('12.32 m', $this->service->format(12.323, $meters));
|
||||
$this->assertEquals('120.32 km', $this->service->format(120320.45, $meters));
|
||||
|
||||
$this->assertEquals('0.32 mm', $this->service->format(0.00032, $meters));
|
||||
}
|
||||
|
||||
public function testFormatMoreDigits()
|
||||
{
|
||||
$this->assertEquals('12.12345', $this->service->format(12.1234532, null, ['is_integer' => false, 'decimals' => 5]));
|
||||
$this->assertEquals('12.1', $this->service->format(12.1234532, null, ['is_integer' => false, 'decimals' => 1]));
|
||||
}
|
||||
|
||||
public function testFormatOptionsOverride()
|
||||
{
|
||||
$meters = new MeasurementUnit();
|
||||
$meters->setIsInteger(false)->setUseSIPrefix(true)->setUnit('m');
|
||||
|
||||
$this->assertEquals('12.32', $this->service->format(12.323, $meters, ['unit' => '']));
|
||||
$this->assertEquals('12002.32 m', $this->service->format(12002.32, $meters, ['show_prefix' => false]));
|
||||
$this->assertEquals('123 m', $this->service->format(123.234, $meters, ['is_integer' => true]));
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue