added json, xml and txt invoice renderer (#1576)
This commit is contained in:
@@ -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));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user