invoices: unified money, number and date formats and fully respect configured language (#1814)

This commit is contained in:
Kevin Papst
2020-07-10 15:09:52 +02:00
committed by GitHub
parent 19e4ebf88c
commit 32c1e3258e
67 changed files with 1593 additions and 2335 deletions

View File

@@ -28,6 +28,8 @@ class TimesheetTest extends TestCase
public function testDefaultValues()
{
$sut = new Timesheet();
self::assertEquals('timesheet', $sut->getType());
self::assertEquals('work', $sut->getCategory());
self::assertNull($sut->getId());
self::assertNull($sut->getBegin());
self::assertNull($sut->getEnd());

View File

@@ -29,7 +29,6 @@ use App\Export\ExportRendererInterface;
use App\Export\RendererInterface;
use App\Repository\Query\TimesheetQuery;
use App\Twig\DateExtensions;
use App\Utils\LocaleSettings;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
@@ -60,10 +59,8 @@ abstract class AbstractRendererTest extends KernelTestCase
$request->setLocale('en');
$requestStack->push($request);
$localeSettings = new LocaleSettings($requestStack, new LanguageFormattings($languages));
$translator = $this->createMock(TranslatorInterface::class);
$dateExtension = new DateExtensions($localeSettings);
$dateExtension = new DateExtensions($requestStack, new LanguageFormattings($languages));
$dispatcher = new EventDispatcher();
$dispatcher->addSubscriber(new MetaFieldColumnSubscriber());

View File

@@ -28,7 +28,6 @@ use App\Event\TimesheetMetaDisplayEvent;
use App\Export\TimesheetExportInterface;
use App\Repository\Query\TimesheetQuery;
use App\Twig\DateExtensions;
use App\Utils\LocaleSettings;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
@@ -59,10 +58,8 @@ abstract class AbstractRendererTest extends KernelTestCase
$request->setLocale('en');
$requestStack->push($request);
$localeSettings = new LocaleSettings($requestStack, new LanguageFormattings($languages));
$translator = $this->getMockBuilder(TranslatorInterface::class)->getMock();
$dateExtension = new DateExtensions($localeSettings);
$dateExtension = new DateExtensions($requestStack, new LanguageFormattings($languages));
$dispatcher = new EventDispatcher();
$dispatcher->addSubscriber(new MetaFieldColumnSubscriber());

View File

@@ -32,17 +32,22 @@ class DebugFormatter implements InvoiceFormatter
}
/**
* @param mixed $amount
* @param int|float $amount
* @param string|null $currency
* @return mixed
* @param bool $withCurrency
* @return string
*/
public function getFormattedMoney($amount, $currency)
public function getFormattedMoney($amount, ?string $currency, bool $withCurrency = true)
{
if (null !== $currency) {
if (null === $currency) {
$withCurrency = false;
}
if ($withCurrency) {
return $amount . ' ' . $currency;
}
return $amount;
return (string) $amount;
}
/**

View File

@@ -16,6 +16,7 @@ use Twig\Environment;
use Twig\Loader\FilesystemLoader;
/**
* @covers \App\Invoice\Renderer\AbstractTwigRenderer
* @covers \App\Invoice\Renderer\JsonRenderer
* @group integration
*/

View File

@@ -17,6 +17,7 @@ use Twig\Environment;
use Twig\Loader\FilesystemLoader;
/**
* @covers \App\Invoice\Renderer\AbstractTwigRenderer
* @covers \App\Invoice\Renderer\PdfRenderer
* @group integration
*/

View File

@@ -30,11 +30,9 @@ use App\Invoice\NumberGenerator\DateNumberGenerator;
use App\Invoice\Renderer\AbstractRenderer;
use App\Repository\Query\InvoiceQuery;
use App\Twig\DateExtensions;
use App\Twig\Extensions;
use App\Utils\LocaleSettings;
use App\Twig\LocaleExtensions;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Contracts\Translation\TranslatorInterface;
trait RendererTestTrait
{
@@ -87,13 +85,12 @@ trait RendererTestTrait
$request->setLocale('en');
$requestStack->push($request);
$localeSettings = new LocaleSettings($requestStack, new LanguageFormattings($languages));
$formattings = new LanguageFormattings($languages);
$translator = $this->getMockBuilder(TranslatorInterface::class)->getMock();
$dateExtension = new DateExtensions($localeSettings);
$extensions = new Extensions($localeSettings);
$dateExtension = new DateExtensions($requestStack, $formattings);
$extensions = new LocaleExtensions($requestStack, $formattings);
return new DefaultInvoiceFormatter($translator, $dateExtension, $extensions);
return new DefaultInvoiceFormatter($dateExtension, $extensions);
}
protected function getInvoiceModel(): InvoiceModel

View File

@@ -16,6 +16,7 @@ use Twig\Environment;
use Twig\Loader\FilesystemLoader;
/**
* @covers \App\Invoice\Renderer\AbstractTwigRenderer
* @covers \App\Invoice\Renderer\TextRenderer
* @group integration
*/

View File

@@ -16,6 +16,7 @@ use Twig\Environment;
use Twig\Loader\FilesystemLoader;
/**
* @covers \App\Invoice\Renderer\AbstractTwigRenderer
* @covers \App\Invoice\Renderer\TwigRenderer
* @group integration
*/
@@ -56,6 +57,7 @@ class TwigRendererTest extends KernelTestCase
$sut = new TwigRenderer($twig);
$model = $this->getInvoiceModel();
$model->getTemplate()->setLanguage('de');
$document = $this->getInvoiceDocument('timesheet.html.twig');
$response = $sut->render($document, $model);

View File

@@ -16,6 +16,7 @@ use Twig\Environment;
use Twig\Loader\FilesystemLoader;
/**
* @covers \App\Invoice\Renderer\AbstractTwigRenderer
* @covers \App\Invoice\Renderer\XmlRenderer
* @group integration
*/

View File

@@ -11,7 +11,6 @@ namespace App\Tests\Twig;
use App\Configuration\LanguageFormattings;
use App\Twig\DateExtensions;
use App\Utils\LocaleSettings;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
@@ -35,14 +34,12 @@ class DateExtensionsTest extends TestCase
$requestStack = new RequestStack();
$requestStack->push($request);
$localeSettings = new LocaleSettings($requestStack, new LanguageFormattings($dateSettings));
return new DateExtensions($localeSettings);
return new DateExtensions($requestStack, new LanguageFormattings($dateSettings));
}
public function testGetFilters()
{
$filters = ['month_name', 'date_short', 'date_time', 'date_full', 'date_format', 'time', 'hour24'];
$filters = ['month_name', 'day_name', 'date_short', 'date_time', 'date_full', 'date_format', 'time', 'hour24'];
$sut = $this->getSut('de', []);
$twigFilters = $sut->getFilters();
$this->assertCount(\count($filters), $twigFilters);
@@ -121,22 +118,39 @@ class DateExtensionsTest extends TestCase
}
/**
* @param \DateTime $date
* @param string $result
* @dataProvider getMonthData
* @dataProvider getDayNameTestData
*/
public function testMonthName(\DateTime $date, $result)
public function testDayName(string $locale, string $date, string $expectedName)
{
$sut = $this->getSut('en', []);
$this->assertEquals($result, $sut->monthName($date));
$sut = $this->getSut($locale, []);
self::assertEquals($expectedName, $sut->dayName(new \DateTime($date)));
}
public function getMonthData()
public function getDayNameTestData()
{
return [
[new \DateTime('January 2016'), 'month.1'],
[new \DateTime('2016-06-23'), 'month.6'],
[new \DateTime('2016-12-23'), 'month.12'],
['de', '2020-07-09 12:00:00', 'Donnerstag'],
['en', '2020-07-09 12:00:00', 'Thursday']
];
}
/**
* @dataProvider getMonthNameTestData
*/
public function testMonthName(string $locale, string $date, string $expectedName)
{
$sut = $this->getSut($locale, []);
self::assertEquals($expectedName, $sut->monthName(new \DateTime($date)));
}
public function getMonthNameTestData()
{
return [
['de', '2020-07-09 23:59:59', 'Juli'],
['en', '2020-07-09 23:59:59', 'July'],
['de', 'January 2016', 'Januar'],
['en', 'January 2016', 'January'],
['en', '2016-12-23', 'December'],
];
}

View File

@@ -9,15 +9,9 @@
namespace App\Tests\Twig;
use App\Configuration\LanguageFormattings;
use App\Entity\Timesheet;
use App\Entity\User;
use App\Twig\Extensions;
use App\Utils\LocaleSettings;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Intl\Util\IntlTestHelper;
use Twig\TwigFilter;
use Twig\TwigFunction;
@@ -26,32 +20,15 @@ use Twig\TwigFunction;
*/
class ExtensionsTest extends TestCase
{
private $localeEn = ['en' => ['date' => 'Y-m-d', 'duration' => '%h:%m h']];
private $localeDe = ['de' => ['date' => 'd.m.Y', 'duration' => '%h:%m h']];
private $localeRu = ['ru' => ['date' => 'd.m.Y', 'duration' => '%h:%m h']];
private $localeFake = ['XX' => ['date' => 'd.m.Y', 'duration' => '%h - %m - %s Zeit']];
/**
* @param array $locales
* @param string $locale
* @return Extensions
*/
protected function getSut($locales, $locale = 'en')
protected function getSut(): Extensions
{
$request = new Request();
$request->setLocale($locale);
$requestStack = new RequestStack();
$requestStack->push($request);
$localeSettings = new LocaleSettings($requestStack, new LanguageFormattings($locales));
return new Extensions($localeSettings);
return new Extensions();
}
public function testGetFilters()
{
$filters = ['duration', 'duration_decimal', 'money', 'currency', 'country', 'language', 'amount', 'docu_link', 'multiline_indent'];
$sut = $this->getSut($this->localeDe);
$filters = ['docu_link', 'multiline_indent'];
$sut = $this->getSut();
$twigFilters = $sut->getFilters();
$this->assertCount(\count($filters), $twigFilters);
$i = 0;
@@ -64,8 +41,8 @@ class ExtensionsTest extends TestCase
public function testGetFunctions()
{
$functions = ['locales', 'class_name'];
$sut = $this->getSut($this->localeDe);
$functions = ['class_name'];
$sut = $this->getSut();
$twigFunctions = $sut->getFunctions();
$this->assertCount(\count($functions), $twigFunctions);
$i = 0;
@@ -76,216 +53,6 @@ class ExtensionsTest extends TestCase
}
}
public function testLocales()
{
$locales = [
['code' => 'en', 'name' => 'English'],
['code' => 'de', 'name' => 'Deutsch'],
['code' => 'ru', 'name' => 'русский'],
];
$appLocales = array_merge($this->localeEn, $this->localeDe, $this->localeRu);
$sut = $this->getSut($appLocales);
$this->assertEquals($locales, $sut->getLocales());
}
public function testCurrency()
{
$symbols = [
'EUR' => '€',
'USD' => '$',
'RUB' => 'RUB',
'rub' => 'RUB',
123 => 123,
];
$sut = $this->getSut($this->localeEn);
foreach ($symbols as $name => $symbol) {
$this->assertEquals($symbol, $sut->currency($name));
}
}
public function testCountry()
{
$countries = [
'DE' => 'Germany',
'RU' => 'Russia',
'ES' => 'Spain',
'es' => 'Spain',
'12' => '12',
];
$sut = $this->getSut($this->localeEn);
foreach ($countries as $locale => $name) {
$this->assertEquals($name, $sut->country($locale));
}
}
public function testLanguage()
{
$languages = [
'de' => 'German',
'ru' => 'Russian',
'es' => 'Spanish',
'ES' => 'Spanish',
'12' => '12',
];
$sut = $this->getSut($this->localeEn);
foreach ($languages as $locale => $name) {
$this->assertEquals($name, $sut->language($locale));
}
}
public function testMoneyNull()
{
$sut = $this->getSut($this->localeEn, 'en');
$this->assertEquals('123.75', $sut->money(123.75));
$sut = $this->getSut($this->localeEn, 'de');
$this->assertEquals('123.234,755', $sut->money(123234.7554));
}
/**
* @dataProvider getMoneyData
*/
public function testMoney($result, $amount, $currency, $locale)
{
$sut = $this->getSut($this->localeEn, $locale);
$this->assertEquals($result, $sut->money($amount, $currency));
}
public function getMoneyData()
{
return [
['0,00 €', null, 'EUR', 'de'],
['2.345,00 €', 2345, 'EUR', 'de'],
['€2,345.00', 2345, 'EUR', 'en'],
['€2,345.01', 2345.009, 'EUR', 'en'],
['2.345,01 €', 2345.009, 'EUR', 'de'],
['$13.75', 13.75, 'USD', 'en'],
['13,75 $', 13.75, 'USD', 'de'],
['13,75 RUB', 13.75, 'RUB', 'de'],
['14 ¥', 13.75, 'JPY', 'de'],
['13 933 ¥', 13933.49, 'JPY', 'ru'],
['1.234.567,89 $', 1234567.891234567890000, 'USD', 'de'],
];
}
/**
* @dataProvider getAmountData
*/
public function testAmount($result, $amount, $locale)
{
$sut = $this->getSut($this->localeEn, $locale);
$this->assertEquals($result, $sut->amount($amount));
}
public function getAmountData()
{
return [
['0', null, 'de'],
['2.345,01', 2345.01, 'de'],
['2.345', 2345, 'de'],
['2,345', 2345, 'en'],
['2,345.009', 2345.009, 'en'],
['2.345,009', 2345.009, 'de'],
['13.75', 13.75, 'en'],
['13,75', 13.75, 'de'],
['13 933,49', 13933.49, 'ru'],
['1.234.567,891', 1234567.891234567890000, 'de'],
];
}
/**
* @dataProvider getMoneyData62_1
*/
public function testMoney62_1($result, $amount, $currency, $locale)
{
IntlTestHelper::requireFullIntl($this, '62.1');
$sut = $this->getSut($this->localeEn, $locale);
$this->assertEquals($result, $sut->money($amount, $currency));
}
public function getMoneyData62_1()
{
return [
['RUB 13.50', 13.50, 'RUB', 'en'],
['13,75 ₽', 13.75, 'RUB', 'ru'],
];
}
public function testDuration()
{
$record = $this->getTimesheet(9437);
$sut = $this->getSut($this->localeEn);
$this->assertEquals('02:37 h', $sut->duration($record->getDuration()));
$this->assertEquals('2.62', $sut->duration($record->getDuration(), true));
// test Timesheet object
$this->assertEquals('02:37 h', $sut->duration($record));
$this->assertEquals('2.62', $sut->duration($record, true));
// test extended format
$sut = $this->getSut($this->localeFake, 'XX');
$this->assertEquals('02 - 37 - 17 Zeit', $sut->duration($record->getDuration()));
// test negative duration
$sut = $this->getSut($this->localeEn, 'en');
$this->assertEquals('?', $sut->duration('-1'));
// test zero duration
$sut = $this->getSut($this->localeEn, 'en');
$this->assertEquals('00:00 h', $sut->duration('0'));
$sut = $this->getSut($this->localeEn, 'en');
$this->assertEquals('00:00 h', $sut->duration(null));
$this->assertEquals('0', $sut->duration(null, true));
}
public function testDurationDecimal()
{
$record = $this->getTimesheet(9437);
$sut = $this->getSut($this->localeEn);
$this->assertEquals('2.62', $sut->durationDecimal($record->getDuration()));
// test Timesheet object
$this->assertEquals('2.62', $sut->durationDecimal($record));
// test extended format
$sut = $this->getSut($this->localeDe, 'de');
$this->assertEquals('2,62', $sut->durationDecimal($record->getDuration()));
// test negative duration
$sut = $this->getSut($this->localeEn, 'en');
$this->assertEquals('0', $sut->durationDecimal('-1'));
// test zero duration
$sut = $this->getSut($this->localeEn, 'en');
$this->assertEquals('0', $sut->durationDecimal('0'));
$sut = $this->getSut($this->localeEn, 'en');
$this->assertEquals('0', $sut->durationDecimal(null));
}
protected function getTimesheet($seconds)
{
$begin = new \DateTime();
$end = clone $begin;
$end->setTimestamp($begin->getTimestamp() + $seconds);
$record = new Timesheet();
$record->setBegin($begin);
$record->setEnd($end);
$record->setDuration($seconds);
return $record;
}
public function testDocuLink()
{
$data = [
@@ -295,7 +62,7 @@ class ExtensionsTest extends TestCase
'' => 'https://www.kimai.org/documentation/',
];
$sut = $this->getSut($this->localeEn);
$sut = $this->getSut();
foreach ($data as $input => $expected) {
$result = $sut->documentationLink($input);
$this->assertEquals($expected, $result);
@@ -304,7 +71,7 @@ class ExtensionsTest extends TestCase
public function testGetClassName()
{
$sut = $this->getSut($this->localeEn);
$sut = $this->getSut();
$this->assertEquals('DateTime', $sut->getClassName(new \DateTime()));
$this->assertEquals('stdClass', $sut->getClassName(new \stdClass()));
$this->assertNull($sut->getClassName(''));
@@ -346,7 +113,7 @@ sdfsdf' . PHP_EOL . "\n" .
*/
public function testMultilineIndent($indent, $string, $expected)
{
$sut = $this->getSut($this->localeEn);
$sut = $this->getSut();
self::assertEquals(implode("\n", $expected), $sut->multilineIndent($string, $indent));
}
}

View File

@@ -0,0 +1,321 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Tests\Twig;
use App\Configuration\LanguageFormattings;
use App\Entity\Timesheet;
use App\Twig\LocaleExtensions;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Intl\Util\IntlTestHelper;
use Twig\TwigFilter;
use Twig\TwigFunction;
/**
* @covers \App\Twig\LocaleExtensions
*/
class LocaleExtensionsTest extends TestCase
{
private $localeEn = ['en' => ['date' => 'Y-m-d', 'duration' => '%h:%m h']];
private $localeDe = ['de' => ['date' => 'd.m.Y', 'duration' => '%h:%m h']];
private $localeRu = ['ru' => ['date' => 'd.m.Y', 'duration' => '%h:%m h']];
private $localeFake = ['XX' => ['date' => 'd.m.Y', 'duration' => '%h - %m - %s Zeit']];
/**
* @param array $locales
* @param string $locale
* @return LocaleExtensions
*/
protected function getSut($locales, $locale = 'en')
{
$request = new Request();
$request->setLocale($locale);
$requestStack = new RequestStack();
$requestStack->push($request);
return new LocaleExtensions($requestStack, new LanguageFormattings($locales));
}
public function testGetFilters()
{
$filters = ['duration', 'duration_decimal', 'money', 'currency', 'country', 'language', 'amount'];
$sut = $this->getSut($this->localeDe);
$twigFilters = $sut->getFilters();
$this->assertCount(\count($filters), $twigFilters);
$i = 0;
/** @var TwigFilter $filter */
foreach ($twigFilters as $filter) {
$this->assertInstanceOf(TwigFilter::class, $filter);
$this->assertEquals($filters[$i++], $filter->getName());
}
}
public function testGetFunctions()
{
$functions = ['locales'];
$sut = $this->getSut($this->localeDe);
$twigFunctions = $sut->getFunctions();
$this->assertCount(\count($functions), $twigFunctions);
$i = 0;
/** @var TwigFunction $filter */
foreach ($twigFunctions as $filter) {
$this->assertInstanceOf(TwigFunction::class, $filter);
$this->assertEquals($functions[$i++], $filter->getName());
}
}
public function testLocales()
{
$locales = [
['code' => 'en', 'name' => 'English'],
['code' => 'de', 'name' => 'Deutsch'],
['code' => 'ru', 'name' => 'русский'],
];
$appLocales = array_merge($this->localeEn, $this->localeDe, $this->localeRu);
$sut = $this->getSut($appLocales);
$this->assertEquals($locales, $sut->getLocales());
}
public function testCurrency()
{
$symbols = [
'EUR' => '€',
'USD' => '$',
'RUB' => 'RUB',
'rub' => 'RUB',
123 => 123,
];
$sut = $this->getSut($this->localeEn);
foreach ($symbols as $name => $symbol) {
$this->assertEquals($symbol, $sut->currency($name));
}
}
public function testCountry()
{
$countries = [
'DE' => 'Germany',
'RU' => 'Russia',
'ES' => 'Spain',
'es' => 'Spain',
'12' => '12',
];
$sut = $this->getSut($this->localeEn);
foreach ($countries as $locale => $name) {
$this->assertEquals($name, $sut->country($locale));
}
}
public function testLanguage()
{
$languages = [
'de' => 'German',
'ru' => 'Russian',
'es' => 'Spanish',
'ES' => 'Spanish',
'12' => '12',
];
$sut = $this->getSut($this->localeEn);
foreach ($languages as $locale => $name) {
$this->assertEquals($name, $sut->language($locale));
}
}
public function testMoneyWithoutCurrency()
{
$sut = $this->getSut($this->localeEn, 'en');
$this->assertEquals('123.75', $sut->money(123.75));
$sut = $this->getSut($this->localeEn, 'de');
$this->assertEquals('123.234,76', $sut->money(123234.7554, null, true));
$this->assertEquals('123.234,76', $sut->money(123234.7554, null, false));
$this->assertEquals('123.234,76', $sut->money(123234.7554, 'EUR', false));
}
/**
* @dataProvider getMoneyNoCurrencyData
*/
public function testMoneyNoCurrency($result, $amount, $currency, $locale)
{
$sut = $this->getSut($this->localeEn, $locale);
$this->assertEquals($result, $sut->money($amount, $currency, false));
}
public function getMoneyNoCurrencyData()
{
return [
['0,00', null, 'EUR', 'de'],
['2.345,00', 2345, 'EUR', 'de'],
['2,345.00', 2345, 'EUR', 'en'],
['2,345.01', 2345.009, 'EUR', 'en'],
['2.345,01', 2345.009, 'EUR', 'de'],
['13.75', 13.75, 'USD', 'en'],
['13,75', 13.75, 'USD', 'de'],
['13,75', 13.75, 'RUB', 'de'],
['13,75', 13.75, 'JPY', 'de'],
['13 933,49', 13933.49, 'JPY', 'ru'],
['13,75', 13.75, 'CNY', 'de'],
['13.933,00', 13933, 'CNY', 'de'],
['13 933,00', 13933, 'CNY', 'ru'],
['13,933.00', 13933, 'CNY', 'en'],
['13,933.00', 13933, 'CNY', 'zh_CN'],
['1.234.567,89', 1234567.891234567890000, 'USD', 'de'],
];
}
/**
* @dataProvider getMoneyData
*/
public function testMoney($result, $amount, $currency, $locale)
{
$sut = $this->getSut($this->localeEn, $locale);
$this->assertEquals($result, $sut->money($amount, $currency));
}
public function getMoneyData()
{
return [
['0,00 €', null, 'EUR', 'de'],
['2.345,00 €', 2345, 'EUR', 'de'],
['€2,345.00', 2345, 'EUR', 'en'],
['€2,345.01', 2345.009, 'EUR', 'en'],
['2.345,01 €', 2345.009, 'EUR', 'de'],
['$13.75', 13.75, 'USD', 'en'],
['13,75 $', 13.75, 'USD', 'de'],
['13,75 RUB', 13.75, 'RUB', 'de'],
['14 ¥', 13.75, 'JPY', 'de'],
['13 933 ¥', 13933.49, 'JPY', 'ru'],
['13,75 CN¥', 13.75, 'CNY', 'de'],
['13.933,00 CN¥', 13933, 'CNY', 'de'],
['13 933,00 CN¥', 13933, 'CNY', 'ru'],
['CN¥13,933.00', 13933, 'CNY', 'en'],
['1.234.567,89 $', 1234567.891234567890000, 'USD', 'de'],
];
}
/**
* @dataProvider getAmountData
*/
public function testAmount($result, $amount, $locale)
{
$sut = $this->getSut($this->localeEn, $locale);
$this->assertEquals($result, $sut->amount($amount));
}
public function getAmountData()
{
return [
['0', null, 'de'],
['2.345,01', 2345.01, 'de'],
['2.345', 2345, 'de'],
['2,345', 2345, 'en'],
['2,345.009', 2345.009, 'en'],
['2.345,009', 2345.009, 'de'],
['13.75', 13.75, 'en'],
['13,75', 13.75, 'de'],
['13 933,49', 13933.49, 'ru'],
['1.234.567,891', 1234567.891234567890000, 'de'],
];
}
/**
* @dataProvider getMoneyData62_1
*/
public function testMoney62_1($result, $amount, $currency, $locale)
{
IntlTestHelper::requireFullIntl($this, '62.1');
$sut = $this->getSut($this->localeEn, $locale);
$this->assertEquals($result, $sut->money($amount, $currency));
}
public function getMoneyData62_1()
{
return [
['RUB 13.50', 13.50, 'RUB', 'en'],
['13,75 ₽', 13.75, 'RUB', 'ru'],
];
}
public function testDuration()
{
$record = $this->getTimesheet(9437);
$sut = $this->getSut($this->localeEn);
$this->assertEquals('02:37 h', $sut->duration($record->getDuration()));
$this->assertEquals('2.62', $sut->duration($record->getDuration(), true));
// test Timesheet object
$this->assertEquals('02:37 h', $sut->duration($record));
$this->assertEquals('2.62', $sut->duration($record, true));
// test extended format
$sut = $this->getSut($this->localeFake, 'XX');
$this->assertEquals('02 - 37 - 17 Zeit', $sut->duration($record->getDuration()));
// test negative duration
$sut = $this->getSut($this->localeEn, 'en');
$this->assertEquals('?', $sut->duration('-1'));
// test zero duration
$sut = $this->getSut($this->localeEn, 'en');
$this->assertEquals('00:00 h', $sut->duration('0'));
$sut = $this->getSut($this->localeEn, 'en');
$this->assertEquals('00:00 h', $sut->duration(null));
$this->assertEquals('0', $sut->duration(null, true));
}
public function testDurationDecimal()
{
$record = $this->getTimesheet(9437);
$sut = $this->getSut($this->localeEn);
$this->assertEquals('2.62', $sut->durationDecimal($record->getDuration()));
// test Timesheet object
$this->assertEquals('2.62', $sut->durationDecimal($record));
// test extended format
$sut = $this->getSut($this->localeDe, 'de');
$this->assertEquals('2,62', $sut->durationDecimal($record->getDuration()));
// test negative duration
$sut = $this->getSut($this->localeEn, 'en');
$this->assertEquals('0', $sut->durationDecimal('-1'));
// test zero duration
$sut = $this->getSut($this->localeEn, 'en');
$this->assertEquals('0', $sut->durationDecimal('0'));
$sut = $this->getSut($this->localeEn, 'en');
$this->assertEquals('0', $sut->durationDecimal(null));
}
protected function getTimesheet($seconds)
{
$begin = new \DateTime();
$end = clone $begin;
$end->setTimestamp($begin->getTimestamp() + $seconds);
$record = new Timesheet();
$record->setBegin($begin);
$record->setEnd($end);
$record->setDuration($seconds);
return $record;
}
}

View File

@@ -0,0 +1,191 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Tests\Utils;
use App\Configuration\LanguageFormattings;
use App\Utils\LocaleFormats;
use PHPUnit\Framework\TestCase;
/**
* @covers \App\Utils\LocaleFormats
* @covers \App\Configuration\LanguageFormattings
*/
class LocaleFormatsTest extends TestCase
{
protected function getSut(string $locale, array $settings)
{
return new LocaleFormats(new LanguageFormattings($settings), $locale);
}
protected function getDefaultSettings()
{
return [
'de' => [
'date_time_type' => 'dd.MM.yyyy HH:mm',
'date_type' => 'dd.MM.yyyy',
'date' => 'd.m.Y',
'date_time' => 'd.m. H:i',
'duration' => '%h:%m h',
'time' => 'H:i',
'24_hours' => true,
],
'en' => [
'date_time_type' => 'yyyy-MM-dd HH:mm',
'date_type' => 'yyyy-MM-dd',
'date' => 'Y-m-d',
'date_time' => 'm-d H:i',
'duration' => '%h:%m h',
'time' => 'H:i:s',
'24_hours' => false,
],
'pt_BR' => [
'date_time_type' => 'dd-MM-yyyy HH:mm',
'date_type' => 'dd-MM-yyyy',
'date' => 'd-m-Y',
'duration' => '%h:%m h',
],
'it' => [
'date_time_type' => 'dd.MM.yyyy HH:mm',
'date_type' => 'dd.MM.yyyy',
'date' => 'd.m.Y',
'duration' => '%h:%m h',
],
'fr' => [
'date_time_type' => 'dd/MM/yyyy HH:mm',
'date_type' => 'dd/MM/yyyy',
'date' => 'd/m/Y',
'duration' => '%h h %m',
],
'es' => [
'date_time_type' => 'dd.MM.yyyy HH:mm',
'date_type' => 'dd.MM.yyyy',
'date' => 'd.m.Y',
'duration' => '%h:%m h',
],
'ru' => [
'date_time_type' => 'dd.MM.yyyy HH:mm',
'date_type' => 'dd.MM.yyyy',
'date' => 'd.m.Y',
'duration' => '%h:%m h',
],
'ar' => [
'date_time_type' => 'yyyy-MM-dd HH:mm',
'date_type' => 'yyyy-MM-dd',
'date' => 'Y-m-d',
'duration' => '%h:%m h',
],
'hu' => [
'date_time_type' => 'yyyy.MM.dd HH:mm',
'date_type' => 'yyyy.MM.dd',
'date' => 'Y.m.d.',
'duration' => '%h:%m h',
],
];
}
public function testGetLocale()
{
$sut = $this->getSut('en', []);
$this->assertEquals('en', $sut->getLocale());
$sut = $this->getSut('ar', []);
$this->assertEquals('ar', $sut->getLocale());
}
public function testGetAvailableLanguages()
{
$sut = $this->getSut('en', []);
$this->assertEquals([], $sut->getAvailableLanguages());
$sut = $this->getSut('en', $this->getDefaultSettings());
$this->assertEquals(['de', 'en', 'pt_BR', 'it', 'fr', 'es', 'ru', 'ar', 'hu'], $sut->getAvailableLanguages());
}
public function testInvalidLocaleWithDefaultLocale()
{
$this->expectException(\InvalidArgumentException::class);
$sut = $this->getSut('en', []);
$sut->getDateFormat();
}
public function testInvalidLocaleWithGivenLocale()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Unknown locale given: xx');
$sut = $this->getSut('xx', $this->getDefaultSettings());
$sut->getDateFormat();
}
public function testGetDurationFormat()
{
$sut = $this->getSut('en', $this->getDefaultSettings());
$this->assertEquals('%h:%m h', $sut->getDurationFormat());
}
public function testGetDateFormat()
{
$sut = $this->getSut('de', $this->getDefaultSettings());
$this->assertEquals('d.m.Y', $sut->getDateFormat());
}
public function testGetDateTimeFormat()
{
$sut = $this->getSut('en', $this->getDefaultSettings());
$this->assertEquals('m-d H:i', $sut->getDateTimeFormat());
}
public function testGetDateTypeFormat()
{
$sut = $this->getSut('de', $this->getDefaultSettings());
$this->assertEquals('dd.MM.yyyy', $sut->getDateTypeFormat());
}
public function testGetDatePickerFormat()
{
$sut = $this->getSut('en', $this->getDefaultSettings());
$this->assertEquals('YYYY-MM-DD', $sut->getDatePickerFormat());
}
public function testGetDateTimeTypeFormat()
{
$sut = $this->getSut('en', $this->getDefaultSettings());
$this->assertEquals('yyyy-MM-dd HH:mm', $sut->getDateTimeTypeFormat());
}
public function testGetDateTimePickerFormat()
{
$sut = $this->getSut('en', $this->getDefaultSettings());
$this->assertEquals('YYYY-MM-DD HH:mm', $sut->getDateTimePickerFormat());
}
public function testIs24Hours()
{
$sut = $this->getSut('en', $this->getDefaultSettings());
$this->assertFalse($sut->isTwentyFourHours());
}
public function testGetTimeFormat()
{
$sut = $this->getSut('en', $this->getDefaultSettings());
$this->assertEquals('H:i:s', $sut->getTimeFormat());
}
public function testUnknownSetting()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Unknown setting for locale en: date_time_type');
$sut = $this->getSut('en', ['en' => [
'xxx' => 'dd.MM.yyyy HH:mm',
]]);
$sut->getDateTimePickerFormat();
}
}

View File

@@ -0,0 +1,223 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Tests\Utils;
use App\Entity\Timesheet;
use App\Utils\LocaleHelper;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Intl\Util\IntlTestHelper;
/**
* @covers \App\Utils\LocaleHelper
*/
class LocaleHelperTest extends TestCase
{
protected function getSut(string $locale): LocaleHelper
{
return new LocaleHelper($locale);
}
public function testCurrency()
{
$symbols = [
'EUR' => '€',
'USD' => '$',
'RUB' => 'RUB',
'rub' => 'RUB',
123 => 123,
];
$sut = $this->getSut('en');
foreach ($symbols as $name => $symbol) {
$this->assertEquals($symbol, $sut->currency($name));
}
}
public function testCountry()
{
$countries = [
'DE' => 'Germany',
'RU' => 'Russia',
'ES' => 'Spain',
'es' => 'Spain',
'12' => '12',
];
$sut = $this->getSut('en');
foreach ($countries as $locale => $name) {
$this->assertEquals($name, $sut->country($locale));
}
}
public function testLanguage()
{
$languages = [
'de' => 'German',
'ru' => 'Russian',
'es' => 'Spanish',
'ES' => 'Spanish',
'12' => '12',
];
$sut = $this->getSut('en');
foreach ($languages as $locale => $name) {
$this->assertEquals($name, $sut->language($locale));
}
}
public function testMoneyWithoutCurrency()
{
$sut = $this->getSut('en');
$this->assertEquals('123.75', $sut->money(123.75));
$sut = $this->getSut('de');
$this->assertEquals('123.234,76', $sut->money(123234.7554, null, true));
$this->assertEquals('123.234,76', $sut->money(123234.7554, null, false));
$this->assertEquals('123.234,76', $sut->money(123234.7554, 'EUR', false));
}
/**
* @dataProvider getMoneyNoCurrencyData
*/
public function testMoneyNoCurrency($result, $amount, $currency, $locale)
{
$sut = $this->getSut($locale);
$this->assertEquals($result, $sut->money($amount, $currency, false));
}
public function getMoneyNoCurrencyData()
{
return [
['0,00', null, 'EUR', 'de'],
['2.345,00', 2345, 'EUR', 'de'],
['2,345.00', 2345, 'EUR', 'en'],
['2,345.01', 2345.009, 'EUR', 'en'],
['2.345,01', 2345.009, 'EUR', 'de'],
['13.75', 13.75, 'USD', 'en'],
['13,75', 13.75, 'USD', 'de'],
['13,75', 13.75, 'RUB', 'de'],
['13,75', 13.75, 'JPY', 'de'],
['13 933,49', 13933.49, 'JPY', 'ru'],
['13,75', 13.75, 'CNY', 'de'],
['13.933,00', 13933, 'CNY', 'de'],
['13 933,00', 13933, 'CNY', 'ru'],
['13,933.00', 13933, 'CNY', 'en'],
['13,933.00', 13933, 'CNY', 'zh_CN'],
['1.234.567,89', 1234567.891234567890000, 'USD', 'de'],
];
}
/**
* @dataProvider getMoneyData
*/
public function testMoney($result, $amount, $currency, $locale)
{
$sut = $this->getSut($locale);
$this->assertEquals($result, $sut->money($amount, $currency));
}
public function getMoneyData()
{
return [
['0,00 €', null, 'EUR', 'de'],
['2.345,00 €', 2345, 'EUR', 'de'],
['€2,345.00', 2345, 'EUR', 'en'],
['€2,345.01', 2345.009, 'EUR', 'en'],
['2.345,01 €', 2345.009, 'EUR', 'de'],
['$13.75', 13.75, 'USD', 'en'],
['13,75 $', 13.75, 'USD', 'de'],
['13,75 RUB', 13.75, 'RUB', 'de'],
['14 ¥', 13.75, 'JPY', 'de'],
['13 933 ¥', 13933.49, 'JPY', 'ru'],
['13,75 CN¥', 13.75, 'CNY', 'de'],
['13.933,00 CN¥', 13933, 'CNY', 'de'],
['13 933,00 CN¥', 13933, 'CNY', 'ru'],
['CN¥13,933.00', 13933, 'CNY', 'en'],
['1.234.567,89 $', 1234567.891234567890000, 'USD', 'de'],
];
}
/**
* @dataProvider getAmountData
*/
public function testAmount($result, $amount, $locale)
{
$sut = $this->getSut($locale);
$this->assertEquals($result, $sut->amount($amount));
}
public function getAmountData()
{
return [
['0', null, 'de'],
['2.345,01', 2345.01, 'de'],
['2.345', 2345, 'de'],
['2,345', 2345, 'en'],
['2,345.009', 2345.009, 'en'],
['2.345,009', 2345.009, 'de'],
['13.75', 13.75, 'en'],
['13,75', 13.75, 'de'],
['13 933,49', 13933.49, 'ru'],
['1.234.567,891', 1234567.891234567890000, 'de'],
];
}
/**
* @dataProvider getMoneyData62_1
*/
public function testMoney62_1($result, $amount, $currency, $locale)
{
IntlTestHelper::requireFullIntl($this, '62.1');
$sut = $this->getSut($locale);
$this->assertEquals($result, $sut->money($amount, $currency));
}
public function getMoneyData62_1()
{
return [
['RUB 13.50', 13.50, 'RUB', 'en'],
['13,75 ₽', 13.75, 'RUB', 'ru'],
];
}
public function testDurationDecimal()
{
$record = $this->getTimesheet(9437);
$sut = $this->getSut('en');
$this->assertEquals('2.62', $sut->durationDecimal($record->getDuration()));
// test extended format
$sut = $this->getSut('de');
$this->assertEquals('2,62', $sut->durationDecimal($record->getDuration()));
// test negative duration
$sut = $this->getSut('en');
$this->assertEquals('0', $sut->durationDecimal('-1'));
// test zero duration
$sut = $this->getSut('en');
$this->assertEquals('0', $sut->durationDecimal('0'));
}
protected function getTimesheet($seconds)
{
$begin = new \DateTime();
$end = clone $begin;
$end->setTimestamp($begin->getTimestamp() + $seconds);
$record = new Timesheet();
$record->setBegin($begin);
$record->setEnd($end);
$record->setDuration($seconds);
return $record;
}
}

View File

@@ -11,7 +11,6 @@ namespace App\Tests\Utils;
use App\Configuration\LanguageFormattings;
use App\Utils\LocaleSettings;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
@@ -19,185 +18,15 @@ use Symfony\Component\HttpFoundation\RequestStack;
* @covers \App\Utils\LocaleSettings
* @covers \App\Configuration\LanguageFormattings
*/
class LocaleSettingsTest extends TestCase
class LocaleSettingsTest extends LocaleFormatsTest
{
protected function getRequestStack(string $locale)
protected function getSut(string $locale, array $settings)
{
$request = new Request();
$request->setLocale($locale);
$requestStack = new RequestStack();
$requestStack->push($request);
return $requestStack;
}
protected function getSut(string $locale, array $settings)
{
return new LocaleSettings($this->getRequestStack($locale), new LanguageFormattings($settings));
}
protected function getDefaultSettings()
{
return [
'de' => [
'date_time_type' => 'dd.MM.yyyy HH:mm',
'date_type' => 'dd.MM.yyyy',
'date' => 'd.m.Y',
'date_time' => 'd.m. H:i',
'duration' => '%h:%m h',
'time' => 'H:i',
'24_hours' => true,
],
'en' => [
'date_time_type' => 'yyyy-MM-dd HH:mm',
'date_type' => 'yyyy-MM-dd',
'date' => 'Y-m-d',
'date_time' => 'm-d H:i',
'duration' => '%h:%m h',
'time' => 'H:i:s',
'24_hours' => false,
],
'pt_BR' => [
'date_time_type' => 'dd-MM-yyyy HH:mm',
'date_type' => 'dd-MM-yyyy',
'date' => 'd-m-Y',
'duration' => '%h:%m h',
],
'it' => [
'date_time_type' => 'dd.MM.yyyy HH:mm',
'date_type' => 'dd.MM.yyyy',
'date' => 'd.m.Y',
'duration' => '%h:%m h',
],
'fr' => [
'date_time_type' => 'dd/MM/yyyy HH:mm',
'date_type' => 'dd/MM/yyyy',
'date' => 'd/m/Y',
'duration' => '%h h %m',
],
'es' => [
'date_time_type' => 'dd.MM.yyyy HH:mm',
'date_type' => 'dd.MM.yyyy',
'date' => 'd.m.Y',
'duration' => '%h:%m h',
],
'ru' => [
'date_time_type' => 'dd.MM.yyyy HH:mm',
'date_type' => 'dd.MM.yyyy',
'date' => 'd.m.Y',
'duration' => '%h:%m h',
],
'ar' => [
'date_time_type' => 'yyyy-MM-dd HH:mm',
'date_type' => 'yyyy-MM-dd',
'date' => 'Y-m-d',
'duration' => '%h:%m h',
],
'hu' => [
'date_time_type' => 'yyyy.MM.dd HH:mm',
'date_type' => 'yyyy.MM.dd',
'date' => 'Y.m.d.',
'duration' => '%h:%m h',
],
];
}
public function testGetLocale()
{
$sut = $this->getSut('en', []);
$this->assertEquals('en', $sut->getLocale());
$sut = $this->getSut('ar', []);
$this->assertEquals('ar', $sut->getLocale());
}
public function testGetAvailableLanguages()
{
$sut = $this->getSut('en', []);
$this->assertEquals([], $sut->getAvailableLanguages());
$sut = $this->getSut('en', $this->getDefaultSettings());
$this->assertEquals(['de', 'en', 'pt_BR', 'it', 'fr', 'es', 'ru', 'ar', 'hu'], $sut->getAvailableLanguages());
}
public function testInvalidLocaleWithDefaultLocale()
{
$this->expectException(\InvalidArgumentException::class);
$sut = $this->getSut('en', []);
$sut->getDateFormat();
}
public function testInvalidLocaleWithGivenLocale()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Unknown locale given: xx');
$sut = $this->getSut('xx', $this->getDefaultSettings());
$sut->getDateFormat();
}
public function testGetDurationFormat()
{
$sut = $this->getSut('en', $this->getDefaultSettings());
$this->assertEquals('%h:%m h', $sut->getDurationFormat());
}
public function testGetDateFormat()
{
$sut = $this->getSut('de', $this->getDefaultSettings());
$this->assertEquals('d.m.Y', $sut->getDateFormat());
}
public function testGetDateTimeFormat()
{
$sut = $this->getSut('en', $this->getDefaultSettings());
$this->assertEquals('m-d H:i', $sut->getDateTimeFormat());
}
public function testGetDateTypeFormat()
{
$sut = $this->getSut('de', $this->getDefaultSettings());
$this->assertEquals('dd.MM.yyyy', $sut->getDateTypeFormat());
}
public function testGetDatePickerFormat()
{
$sut = $this->getSut('en', $this->getDefaultSettings());
$this->assertEquals('YYYY-MM-DD', $sut->getDatePickerFormat());
}
public function testGetDateTimeTypeFormat()
{
$sut = $this->getSut('en', $this->getDefaultSettings());
$this->assertEquals('yyyy-MM-dd HH:mm', $sut->getDateTimeTypeFormat());
}
public function testGetDateTimePickerFormat()
{
$sut = $this->getSut('en', $this->getDefaultSettings());
$this->assertEquals('YYYY-MM-DD HH:mm', $sut->getDateTimePickerFormat());
}
public function testIs24Hours()
{
$sut = $this->getSut('en', $this->getDefaultSettings());
$this->assertFalse($sut->isTwentyFourHours());
}
public function testGetTimeFormat()
{
$sut = $this->getSut('en', $this->getDefaultSettings());
$this->assertEquals('H:i:s', $sut->getTimeFormat());
}
public function testUnknownSetting()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Unknown setting for locale en: date_time_type');
$sut = $this->getSut('en', ['en' => [
'xxx' => 'dd.MM.yyyy HH:mm',
]]);
$sut->getDateTimePickerFormat();
return new LocaleSettings($requestStack, new LanguageFormattings($settings));
}
}