Next major version 2 with PHP 8.1, Symfony 6, Tabler UI, 2FA ... (#2902)

This commit is contained in:
Kevin Papst
2022-12-31 21:19:55 +01:00
committed by GitHub
parent 95e06746bd
commit 90a0fd8a22
2164 changed files with 83700 additions and 86426 deletions

View File

@@ -9,10 +9,10 @@
namespace App\Tests\Invoice\Renderer;
use App\Entity\InvoiceDocument;
use App\Invoice\InvoiceModel;
use App\Invoice\Renderer\AbstractRenderer;
use App\Invoice\RendererInterface;
use App\Model\InvoiceDocument;
use Symfony\Component\HttpFoundation\Response;
class DebugRenderer extends AbstractRenderer implements RendererInterface

View File

@@ -9,11 +9,11 @@
namespace App\Tests\Invoice\Renderer;
use App\Entity\InvoiceDocument;
use App\Invoice\InvoiceItem;
use App\Invoice\InvoiceItemHydrator;
use App\Invoice\InvoiceModel;
use App\Invoice\InvoiceModelHydrator;
use App\Model\InvoiceDocument;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Response;
@@ -23,7 +23,7 @@ class DebugRendererTest extends TestCase
public function getTestModel()
{
yield [$this->getInvoiceModel(), '1,947.99', 5, 5, 1, 2, 2, true, [['entry.meta.foo-timesheet'], ['entry.meta.foo-timesheet2'], ['entry.meta.foo-timesheet'], ['entry.meta.foo-timesheet3']]];
yield [$this->getInvoiceModel(), '1,947.99', 5, 5, 1, 2, 2, true, [['entry.meta.foo-timesheet'], ['entry.meta.foo-timesheet', 'entry.meta.foo-timesheet2'], ['entry.meta.foo-timesheet'], ['entry.meta.foo-timesheet3']]];
yield [$this->getInvoiceModelOneEntry(), '293.27', 1, 1, 0, 1, 0, false, []];
}
@@ -69,11 +69,10 @@ class DebugRendererTest extends TestCase
}
$begin = $model->getQuery()->getBegin();
self::assertEquals($begin->format('m'), $data['model']['query.month_number']);
self::assertEquals($begin->format('d'), $data['model']['query.day']);
// TODO check values or formats?
self::assertEquals('2020.08.12', $data['model']['invoice.first']);
self::assertEquals('2021.03.12', $data['model']['invoice.last']);
self::assertEquals($begin->format('m'), $data['model']['query.begin_month_number']);
self::assertEquals($begin->format('d'), $data['model']['query.begin_day']);
self::assertEquals('20.08.12', $data['model']['invoice.first']);
self::assertEquals('21.03.12', $data['model']['invoice.last']);
}
protected function assertModelStructure(array $model, int $projectCounter = 0, int $activityCounter = 0)
@@ -108,16 +107,16 @@ class DebugRendererTest extends TestCase
'template.vat_id',
'template.contact',
'template.payment_details',
'query.begin',
'query.day',
'query.end',
'query.month',
'query.month_number',
'query.year',
'query.begin',
'query.begin_day',
'query.begin_month',
'query.begin_month_number',
'query.begin_year',
'query.end',
'query.end_day',
'query.end_month',
'query.end_month_number',
@@ -141,6 +140,7 @@ class DebugRendererTest extends TestCase
'customer.time_budget_open_plain',
'customer.mobile',
'customer.meta.foo-customer',
'customer.invoice_text',
'activity.id',
'activity.name',
'activity.comment',
@@ -158,12 +158,6 @@ class DebugRendererTest extends TestCase
'testFromModelHydrator',
];
if ($activityCounter === 1) {
$keys = array_merge($keys, [
'activity',
]);
}
if ($activityCounter > 1) {
$keys = array_merge($keys, [
'activity.1.budget_open',
@@ -198,11 +192,7 @@ class DebugRendererTest extends TestCase
'project.time_budget_open',
'project.time_budget_open_plain',
]);
if ($projectCounter === 1) {
$keys = array_merge($keys, [
'project',
]);
}
if ($projectCounter > 1) {
$keys = array_merge($keys, [
'project.1.id',

View File

@@ -26,10 +26,10 @@ class DocxRendererTest extends TestCase
{
$sut = $this->getAbstractRenderer(DocxRenderer::class);
$this->assertFalse($sut->supports($this->getInvoiceDocument('default.html.twig')));
$this->assertFalse($sut->supports($this->getInvoiceDocument('freelancer.pdf.twig')));
$this->assertFalse($sut->supports($this->getInvoiceDocument('invoice.html.twig')));
$this->assertFalse($sut->supports($this->getInvoiceDocument('service-date.pdf.twig')));
$this->assertFalse($sut->supports($this->getInvoiceDocument('timesheet.html.twig')));
$this->assertTrue($sut->supports($this->getInvoiceDocument('company.docx')));
$this->assertTrue($sut->supports($this->getInvoiceDocument('company.docx', true)));
$this->assertFalse($sut->supports($this->getInvoiceDocument('spreadsheet.xlsx', true)));
$this->assertFalse($sut->supports($this->getInvoiceDocument('open-spreadsheet.ods', true)));
}
@@ -39,7 +39,7 @@ class DocxRendererTest extends TestCase
/** @var DocxRenderer $sut */
$sut = $this->getAbstractRenderer(DocxRenderer::class);
$model = $this->getInvoiceModel();
$document = $this->getInvoiceDocument('company.docx');
$document = $this->getInvoiceDocument('company.docx', true);
/** @var BinaryFileResponse $response */
$response = $sut->render($document, $model);
@@ -50,18 +50,6 @@ class DocxRendererTest extends TestCase
$this->assertTrue(file_exists($file->getRealPath()));
// TODO test document content?
/*
$content = file_get_contents($file->getRealPath());
$this->assertNotContains('${', $content);
$this->assertStringContainsString(',"1,947.99" ', $content);
$this->assertEquals(6, substr_count($content, PHP_EOL));
$this->assertEquals(5, substr_count($content, 'activity description'));
$this->assertEquals(1, substr_count($content, ',"kevin",'));
$this->assertEquals(2, substr_count($content, ',"hello-world",'));
$this->assertEquals(2, substr_count($content, ',"foo-bar",'));
*/
ob_start();
$response->sendContent();
$content2 = ob_get_clean();

View File

@@ -1,75 +0,0 @@
<?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\AbstractTwigRenderer
* @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('timesheet.html.twig')));
$this->assertFalse($sut->supports($this->getInvoiceDocument('company.docx')));
$this->assertFalse($sut->supports($this->getInvoiceDocument('spreadsheet.xlsx', true)));
$this->assertFalse($sut->supports($this->getInvoiceDocument('open-spreadsheet.ods', true)));
$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);
}
}

View File

@@ -28,10 +28,10 @@ class OdsRendererTest extends TestCase
{
$sut = $this->getAbstractRenderer(OdsRenderer::class);
$this->assertFalse($sut->supports($this->getInvoiceDocument('default.html.twig')));
$this->assertFalse($sut->supports($this->getInvoiceDocument('freelancer.pdf.twig')));
$this->assertFalse($sut->supports($this->getInvoiceDocument('invoice.html.twig')));
$this->assertFalse($sut->supports($this->getInvoiceDocument('service-date.pdf.twig')));
$this->assertFalse($sut->supports($this->getInvoiceDocument('timesheet.html.twig')));
$this->assertFalse($sut->supports($this->getInvoiceDocument('company.docx')));
$this->assertFalse($sut->supports($this->getInvoiceDocument('company.docx', true)));
$this->assertFalse($sut->supports($this->getInvoiceDocument('spreadsheet.xlsx', true)));
$this->assertTrue($sut->supports($this->getInvoiceDocument('open-spreadsheet.ods', true)));
}

View File

@@ -10,11 +10,11 @@
namespace App\Tests\Invoice\Renderer;
use App\Invoice\Renderer\PdfRenderer;
use App\Pdf\HtmlToPdfConverter;
use App\Pdf\MPdfConverter;
use App\Tests\Mocks\FileHelperFactory;
use App\Utils\MPdfConverter;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Twig\Environment;
use Twig\Loader\ArrayLoader;
use Twig\Loader\FilesystemLoader;
@@ -28,27 +28,14 @@ class PdfRendererTest extends KernelTestCase
{
use RendererTestTrait;
public function testDisposition()
{
$env = new Environment(new ArrayLoader([]));
$sut = new PdfRenderer($env, $this->createMock(MPdfConverter::class));
$this->assertEquals(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $sut->getDisposition());
$sut->setDispositionInline(false);
$this->assertEquals(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $sut->getDisposition());
$sut->setDispositionInline(true);
$this->assertEquals(ResponseHeaderBag::DISPOSITION_INLINE, $sut->getDisposition());
$sut->setDispositionInline(false);
$this->assertEquals(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $sut->getDisposition());
}
public function testSupports()
{
$env = new Environment(new ArrayLoader([]));
$sut = new PdfRenderer($env, $this->createMock(MPdfConverter::class));
$sut = new PdfRenderer($env, $this->createMock(HtmlToPdfConverter::class));
$this->assertTrue($sut->supports($this->getInvoiceDocument('default.pdf.twig', true)));
$this->assertTrue($sut->supports($this->getInvoiceDocument('freelancer.pdf.twig')));
$this->assertTrue($sut->supports($this->getInvoiceDocument('service-date.pdf.twig')));
$this->assertFalse($sut->supports($this->getInvoiceDocument('timesheet.html.twig')));
$this->assertFalse($sut->supports($this->getInvoiceDocument('company.docx')));
$this->assertFalse($sut->supports($this->getInvoiceDocument('company.docx', true)));
$this->assertFalse($sut->supports($this->getInvoiceDocument('spreadsheet.xlsx', true)));
$this->assertFalse($sut->supports($this->getInvoiceDocument('open-spreadsheet.ods', true)));
}
@@ -57,8 +44,8 @@ class PdfRendererTest extends KernelTestCase
{
$kernel = self::bootKernel();
/** @var Environment $twig */
$twig = $kernel->getContainer()->get('twig');
$stack = $kernel->getContainer()->get('request_stack');
$twig = self::getContainer()->get('twig');
$stack = self::getContainer()->get('request_stack');
$cacheDir = $kernel->getContainer()->getParameter('kernel.cache_dir');
$request = new Request();
@@ -75,5 +62,11 @@ class PdfRendererTest extends KernelTestCase
$response = $sut->render($document, $model);
$this->assertEquals('application/pdf', $response->headers->get('Content-Type'));
$this->assertStringContainsString('attachment; filename', $response->headers->get('Content-Disposition'));
$this->assertNotEmpty($response->getContent());
$sut->setDispositionInline(true);
$response = $sut->render($document, $model);
$this->assertStringContainsString('inline; filename', $response->headers->get('Content-Disposition'));
}
}

View File

@@ -9,12 +9,11 @@
namespace App\Tests\Invoice\Renderer;
use App\Configuration\LanguageFormattings;
use App\Configuration\LocaleService;
use App\Entity\Activity;
use App\Entity\ActivityMeta;
use App\Entity\Customer;
use App\Entity\CustomerMeta;
use App\Entity\InvoiceDocument;
use App\Entity\InvoiceTemplate;
use App\Entity\Project;
use App\Entity\ProjectMeta;
@@ -28,25 +27,20 @@ use App\Invoice\InvoiceFormatter;
use App\Invoice\InvoiceModel;
use App\Invoice\NumberGenerator\DateNumberGenerator;
use App\Invoice\Renderer\AbstractRenderer;
use App\Model\InvoiceDocument;
use App\Repository\InvoiceRepository;
use App\Repository\Query\InvoiceQuery;
use App\Tests\Mocks\InvoiceModelFactoryFactory;
use Doctrine\Common\Collections\ArrayCollection;
trait RendererTestTrait
{
/**
* @return string
*/
protected function getInvoiceTemplatePath()
protected function getInvoiceTemplatePath(): string
{
return __DIR__ . '/../../../templates/invoice/renderer/';
}
/**
* @param string $filename
* @return InvoiceDocument
*/
protected function getInvoiceDocument(string $filename, bool $testOnly = false)
protected function getInvoiceDocument(string $filename, bool $testOnly = false): InvoiceDocument
{
if (!$testOnly) {
return new InvoiceDocument(
@@ -59,11 +53,7 @@ trait RendererTestTrait
);
}
/**
* @param string $classname
* @return AbstractRenderer
*/
protected function getAbstractRenderer(string $classname)
protected function getAbstractRenderer(string $classname): AbstractRenderer
{
return new $classname();
}
@@ -72,13 +62,13 @@ trait RendererTestTrait
{
$languages = [
'en' => [
'date' => 'Y.m.d',
'duration' => '%h:%m h',
'date' => 'yy.MM.dd',
'time' => 'H:i',
'rtl' => false,
]
];
$formattings = new LanguageFormattings($languages);
$formattings = new LocaleService($languages);
return new DefaultInvoiceFormatter($formattings, 'en');
}
@@ -86,15 +76,14 @@ trait RendererTestTrait
protected function getInvoiceModel(): InvoiceModel
{
$user = new User();
$user->setUsername('one-user');
$user->setUserIdentifier('one-user');
$user->setTitle('user title');
$user->setAlias('genious alias');
$user->setEmail('fantastic@four');
$user->addPreference((new UserPreference())->setName('kitty')->setValue('kat'));
$user->addPreference((new UserPreference())->setName('hello')->setValue('world'));
$user->addPreference(new UserPreference('kitty', 'kat'));
$user->addPreference(new UserPreference('hello', 'world'));
$customer = new Customer();
$customer->setName('customer,with/special#name');
$customer = new Customer('customer,with/special#name');
$customer->setAddress('Foo' . PHP_EOL . 'Street' . PHP_EOL . '1111 City');
$customer->setCurrency('EUR');
$customer->setMetaField((new CustomerMeta())->setName('foo-customer')->setValue('bar-customer')->setIsVisible(true));
@@ -104,35 +93,57 @@ trait RendererTestTrait
$template->setVat(19);
$template->setLanguage('en');
$project = new Project();
$project->setName('project name');
$project->setCustomer($customer);
$project->setMetaField((new ProjectMeta())->setName('foo-project')->setValue('bar-project')->setIsVisible(true));
$pMeta = new ProjectMeta();
$pMeta->setName('foo-project')->setValue('bar-project')->setIsVisible(true);
$project = $this->createMock(Project::class);
$project->method('getId')->willReturn(0);
$project->method('getName')->willReturn('project name');
$project->method('getCustomer')->willReturn($customer);
$project->method('getMetaFields')->willReturn(new ArrayCollection([$pMeta]));
$project->method('getVisibleMetaFields')->willReturn([$pMeta]);
$activity = new Activity();
$activity->setName('activity description');
$activity->setProject($project);
$activity->setMetaField((new ActivityMeta())->setName('foo-activity')->setValue('bar-activity')->setIsVisible(true));
$aMeta = new ActivityMeta();
$aMeta->setName('foo-activity');
$aMeta->setValue('bar-activity');
$aMeta->setIsVisible(true);
$activity = $this->createMock(Activity::class);
$activity->method('getId')->willReturn(0);
$activity->method('getName')->willReturn('activity description');
$activity->method('getProject')->willReturn($project);
$activity->method('getMetaFields')->willReturn(new ArrayCollection([$aMeta]));
$activity->method('getVisibleMetaFields')->willReturn([$aMeta]);
$project2 = new Project();
$project2->setName('project 2 name');
$project2->setCustomer($customer);
$project2->setMetaField((new ProjectMeta())->setName('foo-project')->setValue('bar-project2')->setIsVisible(true));
$pMeta2 = new ProjectMeta();
$pMeta2->setName('foo-project')->setValue('bar-project2')->setIsVisible(true);
$project2 = $this->createMock(Project::class);
$project2->method('getId')->willReturn(1);
$project2->method('getName')->willReturn('project 2 name');
$project2->method('getCustomer')->willReturn($customer);
$project2->method('getMetaFields')->willReturn(new ArrayCollection([$pMeta2]));
$project2->method('getVisibleMetaFields')->willReturn([$pMeta2]);
$activity2 = new Activity();
$activity2->setName('activity 1 description');
$activity2->setProject($project2);
$activity2->setMetaField((new ActivityMeta())->setName('foo-activity')->setValue('bar-activity2')->setIsVisible(true));
$aMeta2 = new ActivityMeta();
$aMeta2->setName('foo-activity');
$aMeta2->setValue('bar-activity2');
$aMeta2->setIsVisible(true);
$activity2 = $this->createMock(Activity::class);
$activity2->method('getId')->willReturn(1);
$activity2->method('getName')->willReturn('activity 1 description');
$activity2->method('getProject')->willReturn($project2);
$activity2->method('getMetaFields')->willReturn(new ArrayCollection([$aMeta2]));
$activity2->method('getVisibleMetaFields')->willReturn([$aMeta2]);
$userMethods = ['getId', 'getPreferenceValue', 'getUsername'];
$userMethods = ['getId', 'getPreferenceValue', 'getUsername', 'getUserIdentifier'];
$user1 = $this->getMockBuilder(User::class)->onlyMethods($userMethods)->disableOriginalConstructor()->getMock();
$user1->method('getId')->willReturn(1);
$user1->method('getPreferenceValue')->willReturn('50');
$user1->method('getUsername')->willReturn('foo-bar');
$user1->method('getUserIdentifier')->willReturn('foo-bar');
$user2 = $this->getMockBuilder(User::class)->onlyMethods($userMethods)->disableOriginalConstructor()->getMock();
$user2->method('getId')->willReturn(2);
$user2->method('getUsername')->willReturn('hello-world');
$user2->method('getUserIdentifier')->willReturn('hello-world');
$timesheet = new Timesheet();
$timesheet
@@ -191,15 +202,18 @@ trait RendererTestTrait
->setMetaField((new TimesheetMeta())->setName('foo-timesheet3')->setValue('bluuuub')->setIsVisible(true))
;
$userKevin = new User();
$userKevin->setUserIdentifier('kevin');
$timesheet5 = new Timesheet();
$timesheet5
->setDuration(400)
->setFixedRate(84)
->setUser((new User())->setUsername('kevin'))
->setUser($userKevin)
->setActivity($activity)
->setProject($project)
->setBegin(new \DateTime('2021-03-12 07:13:00'))
->setEnd(new \DateTime('2021-03-12 07:17:40'))
->setBegin(new \DateTime('2021-03-12 12:13:00'))
->setEnd(new \DateTime('2021-03-12 12:17:40'))
->setDescription(
"foo\n" .
"foo\r\n" .
@@ -252,21 +266,21 @@ trait RendererTestTrait
protected function getInvoiceModelOneEntry(): InvoiceModel
{
$user = new User();
$user->setUsername('one-user');
$user->setUserIdentifier('one-user');
$user->setTitle('user title');
$user->setAlias('genious alias');
$user->setEmail('fantastic@four');
$user->addPreference((new UserPreference())->setName('kitty')->setValue('kat'));
$user->addPreference((new UserPreference())->setName('hello')->setValue('world'));
$user->addPreference(new UserPreference('kitty', 'kat'));
$user->addPreference(new UserPreference('hello', 'world'));
$customer = new Customer();
$customer->setName('customer,with/special#name');
$customer = new Customer('customer,with/special#name');
$customer->setCurrency('USD');
$customer->setMetaField((new CustomerMeta())->setName('foo-customer')->setValue('bar-customer')->setIsVisible(true));
$template = new InvoiceTemplate();
$template->setTitle('a test invoice template title');
$template->setVat(19);
$template->setLanguage('it');
$project = new Project();
$project->setName('project name');
@@ -278,11 +292,11 @@ trait RendererTestTrait
$activity->setProject($project);
$activity->setMetaField((new ActivityMeta())->setName('foo-activity')->setValue('bar-activity')->setIsVisible(true));
$userMethods = ['getId', 'getPreferenceValue', 'getUsername'];
$user1 = $this->getMockBuilder(User::class)->onlyMethods($userMethods)->disableOriginalConstructor()->getMock();
$user1 = $this->createMock(User::class);
$user1->method('getId')->willReturn(1);
$user1->method('getPreferenceValue')->willReturn('50');
$user1->method('getUsername')->willReturn('foo-bar');
$user1->method('getUserIdentifier')->willReturn('foo-bar');
$timesheet = new Timesheet();
$timesheet
@@ -299,6 +313,7 @@ trait RendererTestTrait
$query = new InvoiceQuery();
$query->addActivity($activity);
$query->addProject($project);
$query->setBegin(new \DateTime());
$query->setEnd(new \DateTime());

View File

@@ -1,98 +0,0 @@
<?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\AbstractTwigRenderer
* @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.pdf.twig')));
$this->assertFalse($sut->supports($this->getInvoiceDocument('timesheet.html.twig')));
$this->assertFalse($sut->supports($this->getInvoiceDocument('company.docx')));
$this->assertFalse($sut->supports($this->getInvoiceDocument('spreadsheet.xlsx', true)));
$this->assertFalse($sut->supports($this->getInvoiceDocument('open-spreadsheet.ods', true)));
$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", $key), $content);
} else {
self::assertStringContainsString(sprintf("%s\n %s", $key, explode("\n", $value)[0]), $content);
}
}
foreach ($model->getCalculator()->getEntries() as $entry) {
foreach ($model->itemToArray($entry) as $key => $value) {
if (null === $value || '' === $value) {
self::assertStringContainsString(sprintf("%s\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));
$this->assertStringContainsString(
'entry.description' . PHP_EOL .
' foo' . PHP_EOL .
' foo' . PHP_EOL .
' foo' . PHP_EOL .
' bar' . PHP_EOL .
' bar' . PHP_EOL .
' Hello',
$content
);
}
}

View File

@@ -30,10 +30,10 @@ class TwigRendererTest extends KernelTestCase
$env = new Environment($loader);
$sut = new TwigRenderer($env);
$this->assertTrue($sut->supports($this->getInvoiceDocument('default.html.twig')));
$this->assertTrue($sut->supports($this->getInvoiceDocument('invoice.html.twig')));
$this->assertTrue($sut->supports($this->getInvoiceDocument('timesheet.html.twig')));
$this->assertFalse($sut->supports($this->getInvoiceDocument('freelancer.pdf.twig')));
$this->assertFalse($sut->supports($this->getInvoiceDocument('company.docx')));
$this->assertFalse($sut->supports($this->getInvoiceDocument('service-date.pdf.twig')));
$this->assertFalse($sut->supports($this->getInvoiceDocument('company.docx', true)));
$this->assertFalse($sut->supports($this->getInvoiceDocument('spreadsheet.xlsx', true)));
$this->assertFalse($sut->supports($this->getInvoiceDocument('open-spreadsheet.ods', true)));
}
@@ -42,8 +42,8 @@ class TwigRendererTest extends KernelTestCase
{
$kernel = self::bootKernel();
/** @var Environment $twig */
$twig = $kernel->getContainer()->get('twig');
$stack = $kernel->getContainer()->get('request_stack');
$twig = self::getContainer()->get('twig');
$stack = self::getContainer()->get('request_stack');
$request = new Request();
$request->setLocale('en');
$stack->push($request);
@@ -65,7 +65,9 @@ class TwigRendererTest extends KernelTestCase
$filename = $model->getInvoiceNumber() . '-customer_with_special_name';
$this->assertStringContainsString('<title>' . $filename . '</title>', $content);
$this->assertStringContainsString('<span contenteditable="true">a very *long* test invoice / template title with [ßpecial] chäracter</span>', $content);
$this->assertEquals(3, substr_count($content, 'activity description'));
// 3 timesheets have a description and therefor do not render the activity
// 2 timesheets have no description and the correct activity assigned
$this->assertEquals(2, substr_count($content, 'activity description'));
$this->assertStringContainsString(nl2br("foo\n" .
"foo\r\n" .
'foo' . PHP_EOL .

View File

@@ -29,10 +29,10 @@ class XlsxRendererTest extends TestCase
{
$sut = $this->getAbstractRenderer(XlsxRenderer::class);
$this->assertFalse($sut->supports($this->getInvoiceDocument('default.html.twig')));
$this->assertFalse($sut->supports($this->getInvoiceDocument('freelancer.pdf.twig')));
$this->assertFalse($sut->supports($this->getInvoiceDocument('invoice.html.twig')));
$this->assertFalse($sut->supports($this->getInvoiceDocument('service-date.pdf.twig')));
$this->assertFalse($sut->supports($this->getInvoiceDocument('timesheet.html.twig')));
$this->assertFalse($sut->supports($this->getInvoiceDocument('company.docx')));
$this->assertFalse($sut->supports($this->getInvoiceDocument('company.docx', true)));
$this->assertTrue($sut->supports($this->getInvoiceDocument('spreadsheet.xlsx', true)));
$this->assertFalse($sut->supports($this->getInvoiceDocument('open-spreadsheet.ods', true)));
}

View File

@@ -1,82 +0,0 @@
<?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\AbstractTwigRenderer
* @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.pdf.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('company.docx')));
$this->assertFalse($sut->supports($this->getInvoiceDocument('spreadsheet.xlsx', true)));
$this->assertFalse($sut->supports($this->getInvoiceDocument('open-spreadsheet.ods', true)));
$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));
}
}