added json, xml and txt invoice renderer (#1576)
This commit is contained in:
@@ -77,20 +77,20 @@ class InvoiceCreateCommandTest extends KernelTestCase
|
||||
}
|
||||
|
||||
/**
|
||||
'user'
|
||||
'start'
|
||||
'end'
|
||||
'timezone'
|
||||
'customer'
|
||||
'template'
|
||||
'search'
|
||||
'exported'
|
||||
'by-customer'
|
||||
'by-project'
|
||||
'set-exported'
|
||||
'template-meta'
|
||||
* @param $user
|
||||
* @param array $params
|
||||
* Allowed option: user
|
||||
* Allowed option: start
|
||||
* Allowed option: end
|
||||
* Allowed option: timezone
|
||||
* Allowed option: customer
|
||||
* Allowed option: template
|
||||
* Allowed option: search
|
||||
* Allowed option: exported
|
||||
* Allowed option: by-customer
|
||||
* Allowed option: by-project
|
||||
* Allowed option: set-exported
|
||||
* Allowed option: template-meta
|
||||
*
|
||||
* @param array $options
|
||||
* @return CommandTester
|
||||
*/
|
||||
protected function createInvoice(array $options = [])
|
||||
|
||||
@@ -51,47 +51,47 @@ class ConfigurableNumberGeneratorTest extends TestCase
|
||||
|
||||
public function getTestData()
|
||||
{
|
||||
$timestamp = time();
|
||||
$invoiceDate = new \DateTime();
|
||||
|
||||
return [
|
||||
// simple tests for single calls
|
||||
['{date}', date('ymd'), $timestamp],
|
||||
['{Y}', date('Y'), $timestamp],
|
||||
['{y}', date('y'), $timestamp],
|
||||
['{M}', date('m'), $timestamp],
|
||||
['{m}', date('n'), $timestamp],
|
||||
['{D}', date('d'), $timestamp],
|
||||
['{d}', date('j'), $timestamp],
|
||||
['{c}', '2', $timestamp],
|
||||
['{cy}', '2', $timestamp],
|
||||
['{cm}', '2', $timestamp],
|
||||
['{cd}', '2', $timestamp],
|
||||
['{date}', $invoiceDate->format('ymd'), $invoiceDate],
|
||||
['{Y}', $invoiceDate->format('Y'), $invoiceDate],
|
||||
['{y}', $invoiceDate->format('y'), $invoiceDate],
|
||||
['{M}', $invoiceDate->format('m'), $invoiceDate],
|
||||
['{m}', $invoiceDate->format('n'), $invoiceDate],
|
||||
['{D}', $invoiceDate->format('d'), $invoiceDate],
|
||||
['{d}', $invoiceDate->format('j'), $invoiceDate],
|
||||
['{c}', '2', $invoiceDate],
|
||||
['{cy}', '2', $invoiceDate],
|
||||
['{cm}', '2', $invoiceDate],
|
||||
['{cd}', '2', $invoiceDate],
|
||||
// number formatting (not testing the lower case versions, as the tests might break depending on the date)
|
||||
['{date,10}', '0000' . date('ymd'), $timestamp],
|
||||
['{Y,6}', '00' . date('Y'), $timestamp],
|
||||
['{M,3}', '0' . date('m'), $timestamp],
|
||||
['{D,3}', '0' . date('d'), $timestamp],
|
||||
['{c,2}', '02', $timestamp],
|
||||
['{cy,2}', '02', $timestamp],
|
||||
['{cm,2}', '02', $timestamp],
|
||||
['{cd,2}', '02', $timestamp],
|
||||
['{date,10}', '0000' . $invoiceDate->format('ymd'), $invoiceDate],
|
||||
['{Y,6}', '00' . $invoiceDate->format('Y'), $invoiceDate],
|
||||
['{M,3}', '0' . $invoiceDate->format('m'), $invoiceDate],
|
||||
['{D,3}', '0' . $invoiceDate->format('d'), $invoiceDate],
|
||||
['{c,2}', '02', $invoiceDate],
|
||||
['{cy,2}', '02', $invoiceDate],
|
||||
['{cm,2}', '02', $invoiceDate],
|
||||
['{cd,2}', '02', $invoiceDate],
|
||||
// mixing identifiers
|
||||
['{Y}{cy}', date('Y') . '2', $timestamp],
|
||||
['{Y}{cy}{m}', date('Y') . '2' . date('n'), $timestamp],
|
||||
['{Y}-{cy}/{m}', date('Y') . '-2/' . date('n'), $timestamp],
|
||||
['{Y}-{cy}/{m}', date('Y') . '-2/' . date('n'), $timestamp],
|
||||
['{Y,5}/{cy,5}', '0' . date('Y') . '/00002', $timestamp],
|
||||
['{Y}{cy}', $invoiceDate->format('Y') . '2', $invoiceDate],
|
||||
['{Y}{cy}{m}', $invoiceDate->format('Y') . '2' . $invoiceDate->format('n'), $invoiceDate],
|
||||
['{Y}-{cy}/{m}', $invoiceDate->format('Y') . '-2/' . $invoiceDate->format('n'), $invoiceDate],
|
||||
['{Y}-{cy}/{m}', $invoiceDate->format('Y') . '-2/' . $invoiceDate->format('n'), $invoiceDate],
|
||||
['{Y,5}/{cy,5}', '0' . $invoiceDate->format('Y') . '/00002', $invoiceDate],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getTestData
|
||||
*/
|
||||
public function testGetInvoiceNumber(string $format, string $expectedInvoiceNumber, int $timestamp)
|
||||
public function testGetInvoiceNumber(string $format, string $expectedInvoiceNumber, \DateTime $invoiceDate)
|
||||
{
|
||||
$sut = $this->getSut($format);
|
||||
$model = new InvoiceModel(new DebugFormatter());
|
||||
$model->setInvoiceDate((new \DateTime())->setTimestamp($timestamp));
|
||||
$model->setInvoiceDate($invoiceDate);
|
||||
$sut->setModel($model);
|
||||
|
||||
$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
|
||||
{
|
||||
/**
|
||||
* @param $client HttpKernelBrowser|EntityManager|KernelTestCase
|
||||
* @param HttpKernelBrowser|EntityManager|KernelTestCase $client
|
||||
* @param Fixture $fixture
|
||||
*/
|
||||
protected function importFixture($client, Fixture $fixture)
|
||||
|
||||
@@ -27,6 +27,9 @@ class InvoiceDocumentRepositoryTest extends TestCase
|
||||
'default.html.twig',
|
||||
'freelancer.html.twig',
|
||||
'timesheet.html.twig',
|
||||
'text.txt.twig',
|
||||
'javascript.json.twig',
|
||||
'xml.xml.twig',
|
||||
];
|
||||
|
||||
public function testWithEmptyDirectory()
|
||||
|
||||
@@ -50,7 +50,7 @@ class ExtensionsTest extends TestCase
|
||||
|
||||
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);
|
||||
$twigFilters = $sut->getFilters();
|
||||
$this->assertCount(count($filters), $twigFilters);
|
||||
@@ -305,4 +305,43 @@ class ExtensionsTest extends TestCase
|
||||
$this->assertNull($sut->getClassName(null));
|
||||
$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