added further timesheet related invoice fields (#411)

This commit is contained in:
Kevin Papst
2018-11-12 12:19:57 +01:00
committed by GitHub
parent f9dc42d1f9
commit e9cbb872d0
4 changed files with 289 additions and 56 deletions

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\Invoice\Renderer;
use App\Entity\InvoiceDocument;
use App\Invoice\Renderer\RendererTrait;
use App\Invoice\RendererInterface;
use App\Model\InvoiceModel;
use Symfony\Component\HttpFoundation\Response;
class DebugRenderer implements RendererInterface
{
use RendererTrait;
/**
* @return string[]
*/
protected function getFileExtensions()
{
return [];
}
/**
* @return string
*/
protected function getContentType()
{
return 'array';
}
/**
* @param \DateTime $date
* @return mixed
*/
protected function getFormattedDateTime(\DateTime $date)
{
return $date->format('d.m.Y');
}
/**
* @param $amount
* @return mixed
*/
protected function getFormattedMoney($amount)
{
return $amount;
}
/**
* @param \DateTime $date
* @return mixed
*/
protected function getFormattedMonthName(\DateTime $date)
{
return $date->format('m');
}
/**
* @param $seconds
* @return mixed
*/
protected function getFormattedDuration($seconds)
{
return $seconds;
}
/**
* Render the given InvoiceDocument with the data from the InvoiceModel into a stupid array for testing only.
*
* @param InvoiceDocument $document
* @param InvoiceModel $model
* @return Response
*/
public function render(InvoiceDocument $document, InvoiceModel $model): Response
{
$result = [
'model' => $this->modelToReplacer($model),
'entries' => [],
];
foreach ($model->getCalculator()->getEntries() as $entry) {
$result['entries'][] = $this->timesheetToArray($entry);
}
return new Response(json_encode($result));
}
}

View File

@@ -0,0 +1,130 @@
<?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\Invoice\Renderer;
use App\Entity\InvoiceDocument;
use App\Model\InvoiceModel;
use Symfony\Component\HttpFoundation\Response;
class DebugRendererTest extends AbstractRendererTest
{
public function getTestModel()
{
yield [$this->getInvoiceModel(), '1,947.99', 5, 5, 1, 2, 2];
yield [$this->getInvoiceModelOneEntry(), '293.27', 1, 1, 0, 1, 0];
}
/**
* @dataProvider getTestModel
*/
public function testRender(InvoiceModel $model, $expectedRate, $expectedRows, $expectedDescriptions, $expectedUser1, $expectedUser2, $expectedUser3)
{
$document = new InvoiceDocument(new \SplFileInfo(__DIR__ . '/DebugRenderer.php'));
$sut = new DebugRenderer();
/** @var Response $response */
$response = $sut->render($document, $model);
$data = json_decode($response->getContent(), true);
$this->assertModelStructure($data['model']);
$rows = $data['entries'];
$this->assertEquals($expectedRows, count($rows));
foreach($rows as $row) {
$this->assertEntryStructure($row);
}
// TODO check values or formats?
}
protected function assertModelStructure(array $model)
{
$keys = [
'invoice.due_date',
'invoice.date',
'invoice.number',
'invoice.currency',
'invoice.vat',
'invoice.tax',
'invoice.total_time',
'invoice.total',
'invoice.subtotal',
'template.name',
'template.company',
'template.address',
'template.title',
'template.payment_terms',
'template.due_days',
'query.begin',
'query.end',
'query.month',
'query.year',
'customer.address',
'customer.name',
'customer.contact',
'customer.company',
'customer.country',
'customer.number',
'customer.homepage',
'customer.comment',
];
foreach($keys as $key) {
$this->assertArrayHasKey($key, $model);
}
$expectedKeys = array_merge([], $keys);
sort($expectedKeys);
$givenKeys = array_keys($model);
sort($givenKeys);
$this->assertEquals(count($keys), count($givenKeys));
$this->assertEquals($expectedKeys, $givenKeys);
}
protected function assertEntryStructure(array $model)
{
$keys = [
'entry.description',
'entry.amount',
'entry.rate',
'entry.total',
'entry.duration',
'entry.duration_minutes',
'entry.begin',
'entry.begin_time',
'entry.begin_timestamp',
'entry.end',
'entry.end_time',
'entry.end_timestamp',
'entry.date',
'entry.user_id',
'entry.user_name',
'entry.user_alias',
'entry.activity',
'entry.activity_id',
'entry.project',
'entry.customer',
'entry.project_id',
'entry.customer_id',
];
foreach($keys as $key) {
$this->assertArrayHasKey($key, $model);
}
$expectedKeys = array_merge([], $keys);
sort($expectedKeys);
$givenKeys = array_keys($model);
sort($givenKeys);
$this->assertEquals(count($keys), count($givenKeys));
$this->assertEquals($expectedKeys, $givenKeys);
}
}