Duration only mode #131 (#134)

This commit is contained in:
Kevin Papst
2018-02-10 22:19:49 +01:00
committed by GitHub
parent 93e1cd5095
commit 5a4bb0d081
19 changed files with 671 additions and 135 deletions

View File

@@ -0,0 +1,128 @@
<?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\Entity\Timesheet;
use App\Twig\Extensions;
use PHPUnit\Framework\TestCase;
use Twig\TwigFilter;
/**
* @covers \App\Twig\Extensions
*/
class ExtensionsTest extends TestCase
{
public function testGetFilters()
{
$filters = ['duration', 'durationForEntry', 'money', 'currency', 'country'];
$sut = new Extensions('de');
$twigFilters = $sut->getFilters();
$this->assertCount(count($filters), $twigFilters);
$i = 0;
foreach ($twigFilters as $filter) {
$this->assertInstanceOf(TwigFilter::class, $filter);
$this->assertEquals($filters[$i++], $filter->getName());
}
}
public function testGetFunctions()
{
$functions = ['locales'];
$sut = new Extensions('de');
$twigFunctions = $sut->getFunctions();
$this->assertCount(count($functions), $twigFunctions);
$i = 0;
foreach ($twigFunctions as $filter) {
$this->assertInstanceOf(\Twig_SimpleFunction::class, $filter);
$this->assertEquals($functions[$i++], $filter->getName());
}
}
public function testLocales()
{
$locales = [
['code' => 'en', 'name' => 'English'],
['code' => 'de', 'name' => 'Deutsch'],
['code' => 'ru', 'name' => 'русский'],
];
$sut = new Extensions('en|de|ru');
$this->assertEquals($locales, $sut->getLocales());
}
public function testCurrency()
{
$symbols = [
'EUR' => '€',
'USD' => '$',
'RUB' => 'RUB',
];
$sut = new Extensions('en');
foreach ($symbols as $name => $symbol) {
$this->assertEquals($symbol, $sut->currency($name));
}
}
public function testCountry()
{
$countries = [
'DE' => 'Germany',
'RU' => 'Russia',
'ES' => 'Spain',
];
$sut = new Extensions('en');
foreach ($countries as $locale => $name) {
$this->assertEquals($name, $sut->country($locale));
}
}
public function testMoney()
{
$money = [
[2222, 'EUR', '2,222.00 €'],
[13.75, 'USD', '13.75 $'],
];
$sut = new Extensions('en');
foreach ($money as $entry) {
$amount = $entry[0];
$currency = $entry[1];
$expected = $entry[2];
$this->assertEquals($expected, $sut->money($amount, $currency));
}
}
public function testDuration()
{
$record = $this->getTimesheet(9437);
$sut = new Extensions('en');
$this->assertEquals('02:37 h', $sut->duration($record->getDuration()));
$this->assertEquals('02:37:17 h', $sut->duration($record->getDuration(), true));
$this->assertEquals('02:37 h', $sut->durationForEntry($record));
$this->assertEquals('02:37:17 h', $sut->durationForEntry($record, true));
}
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,94 @@
<?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\Utils\Duration;
use PHPUnit\Framework\TestCase;
/**
* @covers \App\Utils\Duration
*/
class DurationTest extends TestCase
{
public function testFormat()
{
$sut = new Duration();
$this->assertEquals('02:38', $sut->format(9494));
$this->assertEquals('02:38:14', $sut->format(9494, true));
}
/**
* @dataProvider getParseDurationTestData
*/
public function testParseDurationString($expected, $duration, $mode)
{
$sut = new Duration();
$this->assertEquals($expected, $sut->parseDurationString($duration));
}
/**
* @dataProvider getParseDurationTestData
*/
public function testParseDuration($expected, $duration, $mode)
{
$sut = new Duration();
$this->assertEquals($expected, $sut->parseDuration($duration, $mode));
}
public function getParseDurationTestData()
{
return [
[0, '', Duration::FORMAT_SECONDS],
[0, 0, Duration::FORMAT_SECONDS],
[0, -12, Duration::FORMAT_SECONDS],
[3600, 3600, Duration::FORMAT_SECONDS],
[0, '', Duration::FORMAT_NATURAL],
[0, 0, Duration::FORMAT_NATURAL],
[7200, '2h', Duration::FORMAT_NATURAL],
[2280, '38m', Duration::FORMAT_NATURAL],
[9480, '2h38m', Duration::FORMAT_NATURAL],
[9497, '2h38m17s', Duration::FORMAT_NATURAL],
[9497, '1h96m137s', Duration::FORMAT_NATURAL],
[0, '', Duration::FORMAT_COLON],
[0, 0, Duration::FORMAT_COLON],
[48420, '13:27', Duration::FORMAT_COLON],
[48474, '13:27:54', Duration::FORMAT_COLON],
[48474, '12:87:54', Duration::FORMAT_COLON],
];
}
public function getParseDurationInvalidData()
{
return [
// invalid input
['13', Duration::FORMAT_COLON],
['13-13', Duration::FORMAT_COLON],
['13.13', Duration::FORMAT_COLON],
[1111, 1111, Duration::FORMAT_NATURAL],
// invalid modes
[17, 'foo'],
[12, ''],
];
}
/**
* @dataProvider getParseDurationInvalidData
* @expectedException \InvalidArgumentException
*/
public function testParseDurationThrowsInvalidArgumentException($duration, $mode)
{
$sut = new Duration();
$sut->parseDuration($duration, $mode);
}
}