added json, xml and txt invoice renderer (#1576)
This commit is contained in:
@@ -17,11 +17,11 @@ class Constants
|
|||||||
/**
|
/**
|
||||||
* The current release version
|
* The current release version
|
||||||
*/
|
*/
|
||||||
public const VERSION = '1.8';
|
public const VERSION = '1.9';
|
||||||
/**
|
/**
|
||||||
* The current release status, either "stable" or "dev"
|
* The current release status, either "stable" or "dev"
|
||||||
*/
|
*/
|
||||||
public const STATUS = 'stable';
|
public const STATUS = 'dev';
|
||||||
/**
|
/**
|
||||||
* The software name
|
* The software name
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -48,7 +48,8 @@ class LanguageType extends AbstractType
|
|||||||
}
|
}
|
||||||
|
|
||||||
$resolver->setDefaults([
|
$resolver->setDefaults([
|
||||||
'choices' => $choices
|
'choices' => $choices,
|
||||||
|
'label' => 'label.language',
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -75,31 +75,31 @@ final class ConfigurableNumberGenerator implements NumberGeneratorInterface
|
|||||||
|
|
||||||
switch ($tmp) {
|
switch ($tmp) {
|
||||||
case 'Y':
|
case 'Y':
|
||||||
$partialResult = date('Y', $timestamp);
|
$partialResult = $invoiceDate->format('Y');
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'y':
|
case 'y':
|
||||||
$partialResult = date('y', $timestamp);
|
$partialResult = $invoiceDate->format('y');
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'M':
|
case 'M':
|
||||||
$partialResult = date('m', $timestamp);
|
$partialResult = $invoiceDate->format('m');
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'm':
|
case 'm':
|
||||||
$partialResult = date('n', $timestamp);
|
$partialResult = $invoiceDate->format('n');
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'D':
|
case 'D':
|
||||||
$partialResult = date('d', $timestamp);
|
$partialResult = $invoiceDate->format('d');
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'd':
|
case 'd':
|
||||||
$partialResult = date('j', $timestamp);
|
$partialResult = $invoiceDate->format('j');
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'date':
|
case 'date':
|
||||||
$partialResult = date('ymd', $timestamp);
|
$partialResult = $invoiceDate->format('ymd');
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'c':
|
case 'c':
|
||||||
|
|||||||
53
src/Invoice/Renderer/JsonRenderer.php
Normal file
53
src/Invoice/Renderer/JsonRenderer.php
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
<?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\Invoice\Renderer;
|
||||||
|
|
||||||
|
use App\Entity\InvoiceDocument;
|
||||||
|
use App\Invoice\InvoiceFilename;
|
||||||
|
use App\Invoice\InvoiceModel;
|
||||||
|
use App\Invoice\RendererInterface;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
|
||||||
|
use Twig\Environment;
|
||||||
|
|
||||||
|
final class JsonRenderer implements RendererInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var Environment
|
||||||
|
*/
|
||||||
|
private $twig;
|
||||||
|
|
||||||
|
public function __construct(Environment $twig)
|
||||||
|
{
|
||||||
|
$this->twig = $twig;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function supports(InvoiceDocument $document): bool
|
||||||
|
{
|
||||||
|
return stripos($document->getFilename(), '.json.twig') !== false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function render(InvoiceDocument $document, InvoiceModel $model): Response
|
||||||
|
{
|
||||||
|
$content = $this->twig->render('@invoice/' . basename($document->getFilename()), [
|
||||||
|
'model' => $model
|
||||||
|
]);
|
||||||
|
$filename = (string) new InvoiceFilename($model);
|
||||||
|
|
||||||
|
$response = new Response($content);
|
||||||
|
|
||||||
|
$disposition = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $filename . '.json');
|
||||||
|
|
||||||
|
$response->headers->set('Content-Type', 'application/json');
|
||||||
|
$response->headers->set('Content-Disposition', $disposition);
|
||||||
|
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
}
|
||||||
53
src/Invoice/Renderer/TextRenderer.php
Normal file
53
src/Invoice/Renderer/TextRenderer.php
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
<?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\Invoice\Renderer;
|
||||||
|
|
||||||
|
use App\Entity\InvoiceDocument;
|
||||||
|
use App\Invoice\InvoiceFilename;
|
||||||
|
use App\Invoice\InvoiceModel;
|
||||||
|
use App\Invoice\RendererInterface;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
|
||||||
|
use Twig\Environment;
|
||||||
|
|
||||||
|
final class TextRenderer implements RendererInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var Environment
|
||||||
|
*/
|
||||||
|
private $twig;
|
||||||
|
|
||||||
|
public function __construct(Environment $twig)
|
||||||
|
{
|
||||||
|
$this->twig = $twig;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function supports(InvoiceDocument $document): bool
|
||||||
|
{
|
||||||
|
return stripos($document->getFilename(), '.txt.twig') !== false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function render(InvoiceDocument $document, InvoiceModel $model): Response
|
||||||
|
{
|
||||||
|
$content = $this->twig->render('@invoice/' . basename($document->getFilename()), [
|
||||||
|
'model' => $model
|
||||||
|
]);
|
||||||
|
$filename = (string) new InvoiceFilename($model);
|
||||||
|
|
||||||
|
$response = new Response($content);
|
||||||
|
|
||||||
|
$disposition = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $filename . '.txt');
|
||||||
|
|
||||||
|
$response->headers->set('Content-Type', 'text/plain');
|
||||||
|
$response->headers->set('Content-Disposition', $disposition);
|
||||||
|
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
}
|
||||||
53
src/Invoice/Renderer/XmlRenderer.php
Normal file
53
src/Invoice/Renderer/XmlRenderer.php
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
<?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\Invoice\Renderer;
|
||||||
|
|
||||||
|
use App\Entity\InvoiceDocument;
|
||||||
|
use App\Invoice\InvoiceFilename;
|
||||||
|
use App\Invoice\InvoiceModel;
|
||||||
|
use App\Invoice\RendererInterface;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
|
||||||
|
use Twig\Environment;
|
||||||
|
|
||||||
|
final class XmlRenderer implements RendererInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var Environment
|
||||||
|
*/
|
||||||
|
private $twig;
|
||||||
|
|
||||||
|
public function __construct(Environment $twig)
|
||||||
|
{
|
||||||
|
$this->twig = $twig;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function supports(InvoiceDocument $document): bool
|
||||||
|
{
|
||||||
|
return stripos($document->getFilename(), '.xml.twig') !== false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function render(InvoiceDocument $document, InvoiceModel $model): Response
|
||||||
|
{
|
||||||
|
$content = $this->twig->render('@invoice/' . basename($document->getFilename()), [
|
||||||
|
'model' => $model
|
||||||
|
]);
|
||||||
|
$filename = (string) new InvoiceFilename($model);
|
||||||
|
|
||||||
|
$response = new Response($content);
|
||||||
|
|
||||||
|
$disposition = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $filename . '.xml');
|
||||||
|
|
||||||
|
$response->headers->set('Content-Type', 'application/xml');
|
||||||
|
$response->headers->set('Content-Disposition', $disposition);
|
||||||
|
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -71,6 +71,7 @@ class Extensions extends AbstractExtension
|
|||||||
new TwigFilter('language', [$this, 'language']),
|
new TwigFilter('language', [$this, 'language']),
|
||||||
new TwigFilter('amount', [$this, 'amount']),
|
new TwigFilter('amount', [$this, 'amount']),
|
||||||
new TwigFilter('docu_link', [$this, 'documentationLink']),
|
new TwigFilter('docu_link', [$this, 'documentationLink']),
|
||||||
|
new TwigFilter('multiline_indent', [$this, 'multilineIndent']),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -98,6 +99,24 @@ class Extensions extends AbstractExtension
|
|||||||
return get_class($object);
|
return get_class($object);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function multilineIndent(?string $string, string $indent): string
|
||||||
|
{
|
||||||
|
if (null === $string || '' === $string) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
$parts = explode("\r\n", $string);
|
||||||
|
if (count($parts) === 1) {
|
||||||
|
$parts = explode("\n", $string);
|
||||||
|
}
|
||||||
|
|
||||||
|
$parts = array_map(function ($part) use ($indent) {
|
||||||
|
return $indent . $part;
|
||||||
|
}, $parts);
|
||||||
|
|
||||||
|
return implode("\n", $parts);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Transforms seconds into a duration string.
|
* Transforms seconds into a duration string.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -87,6 +87,8 @@
|
|||||||
{%- set logLineClass = '' -%}
|
{%- set logLineClass = '' -%}
|
||||||
{%- if '.CRITICAL' in logLine -%}
|
{%- if '.CRITICAL' in logLine -%}
|
||||||
{%- set logLineClass = 'text-danger text-bold' -%}
|
{%- set logLineClass = 'text-danger text-bold' -%}
|
||||||
|
{%- elseif '.WARNING' in logLine -%}
|
||||||
|
{%- set logLineClass = 'text-warning text-bold' -%}
|
||||||
{%- elseif '.ERROR' in logLine -%}
|
{%- elseif '.ERROR' in logLine -%}
|
||||||
{%- set logLineClass = 'text-warning text-bold' -%}
|
{%- set logLineClass = 'text-warning text-bold' -%}
|
||||||
{%- elseif '.DEBUG' in logLine -%}
|
{%- elseif '.DEBUG' in logLine -%}
|
||||||
|
|||||||
7
templates/invoice/renderer/javascript.json.twig
Normal file
7
templates/invoice/renderer/javascript.json.twig
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
{% set json = model.toArray %}
|
||||||
|
{% set items = [] %}
|
||||||
|
{% for entry in model.calculator.entries %}
|
||||||
|
{% set items = items|merge([model.itemToArray(entry)]) %}
|
||||||
|
{% endfor %}
|
||||||
|
{% set json = json|merge({'items': items}) %}
|
||||||
|
{{ json|json_encode|raw}}
|
||||||
19
templates/invoice/renderer/text.txt.twig
Normal file
19
templates/invoice/renderer/text.txt.twig
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
{% for key, value in model.toArray %}
|
||||||
|
{{ key }}:
|
||||||
|
{% if value is not empty %}
|
||||||
|
{{ value|multiline_indent(' ') }}
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
{% endfor %}
|
||||||
|
{% for entry in model.calculator.entries %}
|
||||||
|
---
|
||||||
|
|
||||||
|
{% set items = model.itemToArray(entry) %}
|
||||||
|
{% for key, value in items %}
|
||||||
|
{{ key }}:
|
||||||
|
{% if value is not empty %}
|
||||||
|
{{ value|multiline_indent(' ') }}
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
{% endfor %}
|
||||||
|
{% endfor %}
|
||||||
19
templates/invoice/renderer/xml.xml.twig
Normal file
19
templates/invoice/renderer/xml.xml.twig
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<kimai version="{{ constant('App\\Constants::VERSION') }}">
|
||||||
|
{% for key, value in model.toArray %}
|
||||||
|
<{{ key }}{% if value is empty %}/>{% else %}>{{ value|escape }}</{{ key }}>{% endif %}
|
||||||
|
|
||||||
|
{% endfor %}
|
||||||
|
<items>
|
||||||
|
{%- for entry in model.calculator.entries %}
|
||||||
|
|
||||||
|
<item>
|
||||||
|
{% for key, value in model.itemToArray(entry) %}
|
||||||
|
<{{ key }}{% if value is empty %}/>{% else %}>{{ value|escape }}</{{ key }}>{% endif %}
|
||||||
|
|
||||||
|
{% endfor %}
|
||||||
|
</item>
|
||||||
|
{%- endfor %}
|
||||||
|
|
||||||
|
</items>
|
||||||
|
</kimai>
|
||||||
@@ -77,20 +77,20 @@ class InvoiceCreateCommandTest extends KernelTestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
'user'
|
* Allowed option: user
|
||||||
'start'
|
* Allowed option: start
|
||||||
'end'
|
* Allowed option: end
|
||||||
'timezone'
|
* Allowed option: timezone
|
||||||
'customer'
|
* Allowed option: customer
|
||||||
'template'
|
* Allowed option: template
|
||||||
'search'
|
* Allowed option: search
|
||||||
'exported'
|
* Allowed option: exported
|
||||||
'by-customer'
|
* Allowed option: by-customer
|
||||||
'by-project'
|
* Allowed option: by-project
|
||||||
'set-exported'
|
* Allowed option: set-exported
|
||||||
'template-meta'
|
* Allowed option: template-meta
|
||||||
* @param $user
|
*
|
||||||
* @param array $params
|
* @param array $options
|
||||||
* @return CommandTester
|
* @return CommandTester
|
||||||
*/
|
*/
|
||||||
protected function createInvoice(array $options = [])
|
protected function createInvoice(array $options = [])
|
||||||
|
|||||||
@@ -51,47 +51,47 @@ class ConfigurableNumberGeneratorTest extends TestCase
|
|||||||
|
|
||||||
public function getTestData()
|
public function getTestData()
|
||||||
{
|
{
|
||||||
$timestamp = time();
|
$invoiceDate = new \DateTime();
|
||||||
|
|
||||||
return [
|
return [
|
||||||
// simple tests for single calls
|
// simple tests for single calls
|
||||||
['{date}', date('ymd'), $timestamp],
|
['{date}', $invoiceDate->format('ymd'), $invoiceDate],
|
||||||
['{Y}', date('Y'), $timestamp],
|
['{Y}', $invoiceDate->format('Y'), $invoiceDate],
|
||||||
['{y}', date('y'), $timestamp],
|
['{y}', $invoiceDate->format('y'), $invoiceDate],
|
||||||
['{M}', date('m'), $timestamp],
|
['{M}', $invoiceDate->format('m'), $invoiceDate],
|
||||||
['{m}', date('n'), $timestamp],
|
['{m}', $invoiceDate->format('n'), $invoiceDate],
|
||||||
['{D}', date('d'), $timestamp],
|
['{D}', $invoiceDate->format('d'), $invoiceDate],
|
||||||
['{d}', date('j'), $timestamp],
|
['{d}', $invoiceDate->format('j'), $invoiceDate],
|
||||||
['{c}', '2', $timestamp],
|
['{c}', '2', $invoiceDate],
|
||||||
['{cy}', '2', $timestamp],
|
['{cy}', '2', $invoiceDate],
|
||||||
['{cm}', '2', $timestamp],
|
['{cm}', '2', $invoiceDate],
|
||||||
['{cd}', '2', $timestamp],
|
['{cd}', '2', $invoiceDate],
|
||||||
// number formatting (not testing the lower case versions, as the tests might break depending on the date)
|
// number formatting (not testing the lower case versions, as the tests might break depending on the date)
|
||||||
['{date,10}', '0000' . date('ymd'), $timestamp],
|
['{date,10}', '0000' . $invoiceDate->format('ymd'), $invoiceDate],
|
||||||
['{Y,6}', '00' . date('Y'), $timestamp],
|
['{Y,6}', '00' . $invoiceDate->format('Y'), $invoiceDate],
|
||||||
['{M,3}', '0' . date('m'), $timestamp],
|
['{M,3}', '0' . $invoiceDate->format('m'), $invoiceDate],
|
||||||
['{D,3}', '0' . date('d'), $timestamp],
|
['{D,3}', '0' . $invoiceDate->format('d'), $invoiceDate],
|
||||||
['{c,2}', '02', $timestamp],
|
['{c,2}', '02', $invoiceDate],
|
||||||
['{cy,2}', '02', $timestamp],
|
['{cy,2}', '02', $invoiceDate],
|
||||||
['{cm,2}', '02', $timestamp],
|
['{cm,2}', '02', $invoiceDate],
|
||||||
['{cd,2}', '02', $timestamp],
|
['{cd,2}', '02', $invoiceDate],
|
||||||
// mixing identifiers
|
// mixing identifiers
|
||||||
['{Y}{cy}', date('Y') . '2', $timestamp],
|
['{Y}{cy}', $invoiceDate->format('Y') . '2', $invoiceDate],
|
||||||
['{Y}{cy}{m}', date('Y') . '2' . date('n'), $timestamp],
|
['{Y}{cy}{m}', $invoiceDate->format('Y') . '2' . $invoiceDate->format('n'), $invoiceDate],
|
||||||
['{Y}-{cy}/{m}', date('Y') . '-2/' . date('n'), $timestamp],
|
['{Y}-{cy}/{m}', $invoiceDate->format('Y') . '-2/' . $invoiceDate->format('n'), $invoiceDate],
|
||||||
['{Y}-{cy}/{m}', date('Y') . '-2/' . date('n'), $timestamp],
|
['{Y}-{cy}/{m}', $invoiceDate->format('Y') . '-2/' . $invoiceDate->format('n'), $invoiceDate],
|
||||||
['{Y,5}/{cy,5}', '0' . date('Y') . '/00002', $timestamp],
|
['{Y,5}/{cy,5}', '0' . $invoiceDate->format('Y') . '/00002', $invoiceDate],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dataProvider getTestData
|
* @dataProvider getTestData
|
||||||
*/
|
*/
|
||||||
public function testGetInvoiceNumber(string $format, string $expectedInvoiceNumber, int $timestamp)
|
public function testGetInvoiceNumber(string $format, string $expectedInvoiceNumber, \DateTime $invoiceDate)
|
||||||
{
|
{
|
||||||
$sut = $this->getSut($format);
|
$sut = $this->getSut($format);
|
||||||
$model = new InvoiceModel(new DebugFormatter());
|
$model = new InvoiceModel(new DebugFormatter());
|
||||||
$model->setInvoiceDate((new \DateTime())->setTimestamp($timestamp));
|
$model->setInvoiceDate($invoiceDate);
|
||||||
$sut->setModel($model);
|
$sut->setModel($model);
|
||||||
|
|
||||||
$this->assertEquals($expectedInvoiceNumber, $sut->getInvoiceNumber());
|
$this->assertEquals($expectedInvoiceNumber, $sut->getInvoiceNumber());
|
||||||
|
|||||||
77
tests/Invoice/Renderer/JsonRendererTest.php
Normal file
77
tests/Invoice/Renderer/JsonRendererTest.php
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
<?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\Invoice\Renderer\JsonRenderer;
|
||||||
|
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||||
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
use Twig\Environment;
|
||||||
|
use Twig\Loader\FilesystemLoader;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \App\Invoice\Renderer\JsonRenderer
|
||||||
|
* @group integration
|
||||||
|
*/
|
||||||
|
class JsonRendererTest extends KernelTestCase
|
||||||
|
{
|
||||||
|
use RendererTestTrait;
|
||||||
|
|
||||||
|
public function testSupports()
|
||||||
|
{
|
||||||
|
$loader = new FilesystemLoader();
|
||||||
|
$env = new Environment($loader);
|
||||||
|
$sut = new JsonRenderer($env);
|
||||||
|
|
||||||
|
$this->assertFalse($sut->supports($this->getInvoiceDocument('default.html.twig')));
|
||||||
|
$this->assertFalse($sut->supports($this->getInvoiceDocument('freelancer.html.twig')));
|
||||||
|
$this->assertFalse($sut->supports($this->getInvoiceDocument('timesheet.html.twig')));
|
||||||
|
$this->assertFalse($sut->supports($this->getInvoiceDocument('foo.html.twig')));
|
||||||
|
$this->assertFalse($sut->supports($this->getInvoiceDocument('company.docx')));
|
||||||
|
$this->assertFalse($sut->supports($this->getInvoiceDocument('export.csv')));
|
||||||
|
$this->assertFalse($sut->supports($this->getInvoiceDocument('spreadsheet.xlsx')));
|
||||||
|
$this->assertFalse($sut->supports($this->getInvoiceDocument('open-spreadsheet.ods')));
|
||||||
|
$this->assertFalse($sut->supports($this->getInvoiceDocument('text.txt.twig')));
|
||||||
|
$this->assertTrue($sut->supports($this->getInvoiceDocument('javascript.json.twig')));
|
||||||
|
$this->assertFalse($sut->supports($this->getInvoiceDocument('xml.xml.twig')));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testRender()
|
||||||
|
{
|
||||||
|
$kernel = self::bootKernel();
|
||||||
|
/** @var Environment $twig */
|
||||||
|
$twig = $kernel->getContainer()->get('twig');
|
||||||
|
$stack = $kernel->getContainer()->get('request_stack');
|
||||||
|
$request = new Request();
|
||||||
|
$request->setLocale('en');
|
||||||
|
$stack->push($request);
|
||||||
|
|
||||||
|
/** @var FilesystemLoader $loader */
|
||||||
|
$loader = $twig->getLoader();
|
||||||
|
$loader->addPath($this->getInvoiceTemplatePath(), 'invoice');
|
||||||
|
|
||||||
|
$sut = new JsonRenderer($twig);
|
||||||
|
|
||||||
|
$model = $this->getInvoiceModel();
|
||||||
|
|
||||||
|
$document = $this->getInvoiceDocument('javascript.json.twig');
|
||||||
|
$response = $sut->render($document, $model);
|
||||||
|
|
||||||
|
self::assertEquals('application/json', $response->headers->get('Content-Type'));
|
||||||
|
$content = $response->getContent();
|
||||||
|
$json = json_decode($content, true);
|
||||||
|
|
||||||
|
$expected = $model->toArray();
|
||||||
|
$expected['items'] = [];
|
||||||
|
foreach ($model->getCalculator()->getEntries() as $entry) {
|
||||||
|
$expected['items'][] = $model->itemToArray($entry);
|
||||||
|
}
|
||||||
|
self::assertEquals($expected, $json);
|
||||||
|
}
|
||||||
|
}
|
||||||
79
tests/Invoice/Renderer/TextRendererTest.php
Normal file
79
tests/Invoice/Renderer/TextRendererTest.php
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
<?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\Invoice\Renderer\TextRenderer;
|
||||||
|
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||||
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
use Twig\Environment;
|
||||||
|
use Twig\Loader\FilesystemLoader;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \App\Invoice\Renderer\TextRenderer
|
||||||
|
* @group integration
|
||||||
|
*/
|
||||||
|
class TextRendererTest extends KernelTestCase
|
||||||
|
{
|
||||||
|
use RendererTestTrait;
|
||||||
|
|
||||||
|
public function testSupports()
|
||||||
|
{
|
||||||
|
$loader = new FilesystemLoader();
|
||||||
|
$env = new Environment($loader);
|
||||||
|
$sut = new TextRenderer($env);
|
||||||
|
|
||||||
|
$this->assertFalse($sut->supports($this->getInvoiceDocument('default.html.twig')));
|
||||||
|
$this->assertFalse($sut->supports($this->getInvoiceDocument('freelancer.html.twig')));
|
||||||
|
$this->assertFalse($sut->supports($this->getInvoiceDocument('timesheet.html.twig')));
|
||||||
|
$this->assertFalse($sut->supports($this->getInvoiceDocument('foo.html.twig')));
|
||||||
|
$this->assertFalse($sut->supports($this->getInvoiceDocument('company.docx')));
|
||||||
|
$this->assertFalse($sut->supports($this->getInvoiceDocument('export.csv')));
|
||||||
|
$this->assertFalse($sut->supports($this->getInvoiceDocument('spreadsheet.xlsx')));
|
||||||
|
$this->assertFalse($sut->supports($this->getInvoiceDocument('open-spreadsheet.ods')));
|
||||||
|
$this->assertTrue($sut->supports($this->getInvoiceDocument('text.txt.twig')));
|
||||||
|
$this->assertFalse($sut->supports($this->getInvoiceDocument('javascript.json.twig')));
|
||||||
|
$this->assertFalse($sut->supports($this->getInvoiceDocument('xml.xml.twig')));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testRender()
|
||||||
|
{
|
||||||
|
$kernel = self::bootKernel();
|
||||||
|
/** @var Environment $twig */
|
||||||
|
$twig = $kernel->getContainer()->get('twig');
|
||||||
|
$stack = $kernel->getContainer()->get('request_stack');
|
||||||
|
$request = new Request();
|
||||||
|
$request->setLocale('en');
|
||||||
|
$stack->push($request);
|
||||||
|
|
||||||
|
/** @var FilesystemLoader $loader */
|
||||||
|
$loader = $twig->getLoader();
|
||||||
|
$loader->addPath($this->getInvoiceTemplatePath(), 'invoice');
|
||||||
|
|
||||||
|
$sut = new TextRenderer($twig);
|
||||||
|
|
||||||
|
$model = $this->getInvoiceModel();
|
||||||
|
|
||||||
|
$document = $this->getInvoiceDocument('text.txt.twig');
|
||||||
|
$response = $sut->render($document, $model);
|
||||||
|
|
||||||
|
self::assertEquals('text/plain', $response->headers->get('Content-Type'));
|
||||||
|
|
||||||
|
$content = $response->getContent();
|
||||||
|
|
||||||
|
foreach ($model->toArray() as $key => $value) {
|
||||||
|
if (null === $value || '' === $value) {
|
||||||
|
self::assertStringContainsString(sprintf("%s:\n\n", $key), $content);
|
||||||
|
} else {
|
||||||
|
self::assertStringContainsString(sprintf("%s:\n %s", $key, explode("\n", $value)[0]), $content);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self::assertEquals(count($model->getCalculator()->getEntries()), substr_count($content, PHP_EOL . '---' . PHP_EOL));
|
||||||
|
}
|
||||||
|
}
|
||||||
83
tests/Invoice/Renderer/XmlRendererTest.php
Normal file
83
tests/Invoice/Renderer/XmlRendererTest.php
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
<?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\Invoice\Renderer\XmlRenderer;
|
||||||
|
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||||
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
use Twig\Environment;
|
||||||
|
use Twig\Loader\FilesystemLoader;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \App\Invoice\Renderer\XmlRenderer
|
||||||
|
* @group integration
|
||||||
|
*/
|
||||||
|
class XmlRendererTest extends KernelTestCase
|
||||||
|
{
|
||||||
|
use RendererTestTrait;
|
||||||
|
|
||||||
|
public function testSupports()
|
||||||
|
{
|
||||||
|
$loader = new FilesystemLoader();
|
||||||
|
$env = new Environment($loader);
|
||||||
|
$sut = new XmlRenderer($env);
|
||||||
|
|
||||||
|
$this->assertFalse($sut->supports($this->getInvoiceDocument('default.html.twig')));
|
||||||
|
$this->assertFalse($sut->supports($this->getInvoiceDocument('freelancer.html.twig')));
|
||||||
|
$this->assertFalse($sut->supports($this->getInvoiceDocument('timesheet.html.twig')));
|
||||||
|
$this->assertFalse($sut->supports($this->getInvoiceDocument('javascript.json.twig')));
|
||||||
|
$this->assertFalse($sut->supports($this->getInvoiceDocument('foo.html.twig')));
|
||||||
|
$this->assertFalse($sut->supports($this->getInvoiceDocument('company.docx')));
|
||||||
|
$this->assertFalse($sut->supports($this->getInvoiceDocument('export.csv')));
|
||||||
|
$this->assertFalse($sut->supports($this->getInvoiceDocument('spreadsheet.xlsx')));
|
||||||
|
$this->assertFalse($sut->supports($this->getInvoiceDocument('open-spreadsheet.ods')));
|
||||||
|
$this->assertFalse($sut->supports($this->getInvoiceDocument('text.txt.twig')));
|
||||||
|
$this->assertFalse($sut->supports($this->getInvoiceDocument('javascript.json.twig')));
|
||||||
|
$this->assertTrue($sut->supports($this->getInvoiceDocument('xml.xml.twig')));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testRender()
|
||||||
|
{
|
||||||
|
$kernel = self::bootKernel();
|
||||||
|
/** @var Environment $twig */
|
||||||
|
$twig = $kernel->getContainer()->get('twig');
|
||||||
|
$stack = $kernel->getContainer()->get('request_stack');
|
||||||
|
$request = new Request();
|
||||||
|
$request->setLocale('en');
|
||||||
|
$stack->push($request);
|
||||||
|
|
||||||
|
/** @var FilesystemLoader $loader */
|
||||||
|
$loader = $twig->getLoader();
|
||||||
|
$loader->addPath($this->getInvoiceTemplatePath(), 'invoice');
|
||||||
|
|
||||||
|
$sut = new XmlRenderer($twig);
|
||||||
|
|
||||||
|
$model = $this->getInvoiceModel();
|
||||||
|
|
||||||
|
$document = $this->getInvoiceDocument('xml.xml.twig');
|
||||||
|
$response = $sut->render($document, $model);
|
||||||
|
|
||||||
|
self::assertEquals('application/xml', $response->headers->get('Content-Type'));
|
||||||
|
|
||||||
|
$content = $response->getContent();
|
||||||
|
$xml = new \SimpleXMLElement($content);
|
||||||
|
|
||||||
|
$expected = $model->toArray();
|
||||||
|
|
||||||
|
foreach ($xml as $element) {
|
||||||
|
$name = $element->getName();
|
||||||
|
if ($name === 'items') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
self::assertEquals((string) $expected[$name], (string) $element);
|
||||||
|
}
|
||||||
|
self::assertEquals(count($model->getCalculator()->getEntries()), count($xml->items->item));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -24,7 +24,7 @@ use Symfony\Component\HttpKernel\HttpKernelBrowser;
|
|||||||
trait KernelTestTrait
|
trait KernelTestTrait
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @param $client HttpKernelBrowser|EntityManager|KernelTestCase
|
* @param HttpKernelBrowser|EntityManager|KernelTestCase $client
|
||||||
* @param Fixture $fixture
|
* @param Fixture $fixture
|
||||||
*/
|
*/
|
||||||
protected function importFixture($client, Fixture $fixture)
|
protected function importFixture($client, Fixture $fixture)
|
||||||
|
|||||||
@@ -27,6 +27,9 @@ class InvoiceDocumentRepositoryTest extends TestCase
|
|||||||
'default.html.twig',
|
'default.html.twig',
|
||||||
'freelancer.html.twig',
|
'freelancer.html.twig',
|
||||||
'timesheet.html.twig',
|
'timesheet.html.twig',
|
||||||
|
'text.txt.twig',
|
||||||
|
'javascript.json.twig',
|
||||||
|
'xml.xml.twig',
|
||||||
];
|
];
|
||||||
|
|
||||||
public function testWithEmptyDirectory()
|
public function testWithEmptyDirectory()
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ class ExtensionsTest extends TestCase
|
|||||||
|
|
||||||
public function testGetFilters()
|
public function testGetFilters()
|
||||||
{
|
{
|
||||||
$filters = ['duration', 'duration_decimal', 'money', 'currency', 'country', 'language', 'amount', 'docu_link'];
|
$filters = ['duration', 'duration_decimal', 'money', 'currency', 'country', 'language', 'amount', 'docu_link', 'multiline_indent'];
|
||||||
$sut = $this->getSut($this->localeDe);
|
$sut = $this->getSut($this->localeDe);
|
||||||
$twigFilters = $sut->getFilters();
|
$twigFilters = $sut->getFilters();
|
||||||
$this->assertCount(count($filters), $twigFilters);
|
$this->assertCount(count($filters), $twigFilters);
|
||||||
@@ -305,4 +305,43 @@ class ExtensionsTest extends TestCase
|
|||||||
$this->assertNull($sut->getClassName(null));
|
$this->assertNull($sut->getClassName(null));
|
||||||
$this->assertEquals('App\Entity\User', $sut->getClassName(new User()));
|
$this->assertEquals('App\Entity\User', $sut->getClassName(new User()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getMultilineTestData()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
[' ', null, ['']],
|
||||||
|
[' ', '', ['']],
|
||||||
|
[' ', 0, [' 0']],
|
||||||
|
[' ', 'sdfsdf
|
||||||
|
sdfsdf
|
||||||
|
|
||||||
|
aksljdfh laksjd hflka sjhdf lakjhsdflak jsdfh
|
||||||
|
dfsdfsdfsdfsdf',
|
||||||
|
[' sdfsdf', ' sdfsdf', ' ', ' aksljdfh laksjd hflka sjhdf lakjhsdflak jsdfh', ' dfsdfsdfsdfsdf']
|
||||||
|
],
|
||||||
|
['###', 'sdfsdf' . PHP_EOL .
|
||||||
|
'sdfsdf' . PHP_EOL .
|
||||||
|
'' . PHP_EOL .
|
||||||
|
' aksljdfh laksjd hflka sjhdf lakjhsdflak jsdfh' . PHP_EOL .
|
||||||
|
'dfsdfsdfsdfsdf',
|
||||||
|
['###sdfsdf', '###sdfsdf', '###', '### aksljdfh laksjd hflka sjhdf lakjhsdflak jsdfh', '###dfsdfsdfsdfsdf']
|
||||||
|
],
|
||||||
|
[' ', 'sdfsdf' . "\r\n" .
|
||||||
|
'sdfsdf' . "\r\n" .
|
||||||
|
'' . "\r\n" .
|
||||||
|
' aksljdfh laksjd hflka sjhdf lakjhsdflak jsdfh' . "\r\n" .
|
||||||
|
'dfsdfsdfsdfsdf',
|
||||||
|
[' sdfsdf', ' sdfsdf', ' ', ' aksljdfh laksjd hflka sjhdf lakjhsdflak jsdfh', ' dfsdfsdfsdfsdf']
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider getMultilineTestData
|
||||||
|
*/
|
||||||
|
public function testMultilineIndent($indent, $string, $expected)
|
||||||
|
{
|
||||||
|
$sut = $this->getSut($this->localeEn);
|
||||||
|
self::assertEquals(implode("\n", $expected), $sut->multilineIndent($string, $indent));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user