improved invoices with new renderer (#306)
* added docx renderer and demo template * added csv renderer and demo template * added xlsx renderer and demo template * added ods renderer and demo template * added user calculator * added invoice documentation
This commit is contained in:
96
tests/Invoice/Calculator/AbstractInvoiceCalculatorTest.php
Normal file
96
tests/Invoice/Calculator/AbstractInvoiceCalculatorTest.php
Normal file
@@ -0,0 +1,96 @@
|
||||
<?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\Calculator;
|
||||
|
||||
use App\Entity\Activity;
|
||||
use App\Entity\Customer;
|
||||
use App\Entity\InvoiceTemplate;
|
||||
use App\Entity\Project;
|
||||
use App\Entity\Timesheet;
|
||||
use App\Entity\User;
|
||||
use App\Invoice\CalculatorInterface;
|
||||
use App\Model\InvoiceModel;
|
||||
use App\Repository\Query\InvoiceQuery;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
abstract class AbstractCalculatorTest extends TestCase
|
||||
{
|
||||
protected function assertEmptyModel(CalculatorInterface $sut)
|
||||
{
|
||||
$customer = new Customer();
|
||||
$template = new InvoiceTemplate();
|
||||
$query = new InvoiceQuery();
|
||||
|
||||
$model = new InvoiceModel();
|
||||
$model->setCustomer($customer);
|
||||
$model->setTemplate($template);
|
||||
$model->setQuery($query);
|
||||
|
||||
$sut->setModel($model);
|
||||
|
||||
$this->assertEquals(0, $sut->getTotal());
|
||||
$this->assertEquals(0, $sut->getVat());
|
||||
$this->assertEquals('EUR', $sut->getCurrency());
|
||||
$this->assertEquals(0, $sut->getSubtotal());
|
||||
$this->assertEquals(0, $sut->getTimeWorked());
|
||||
$this->assertEquals(0, count($sut->getEntries()));
|
||||
$this->assertEquals(0, $sut->getTax());
|
||||
}
|
||||
|
||||
protected function assertDescription(CalculatorInterface $sut, $addProject = false, $addActivity = false)
|
||||
{
|
||||
$customer = new Customer();
|
||||
$template = new InvoiceTemplate();
|
||||
$template->setVat(19);
|
||||
|
||||
$project = new Project();
|
||||
$project->setName('project description');
|
||||
$project->setCustomer($customer);
|
||||
|
||||
$activity = new Activity();
|
||||
$activity->setName('activity description');
|
||||
$activity->setProject($project);
|
||||
|
||||
$query = new InvoiceQuery();
|
||||
if ($addProject === true) {
|
||||
$query->setProject($project);
|
||||
} elseif ($addActivity === true) {
|
||||
$query->setActivity($activity);
|
||||
}
|
||||
|
||||
$timesheet = new Timesheet();
|
||||
$timesheet
|
||||
->setDescription('timesheet description')
|
||||
->setDuration(3600)
|
||||
->setRate(293.27)
|
||||
->setUser(new User())
|
||||
->setActivity((new Activity())->setName('foo'))
|
||||
;
|
||||
|
||||
$model = new InvoiceModel();
|
||||
$model->setCustomer($customer);
|
||||
$model->setTemplate($template);
|
||||
$model->setEntries([$timesheet]);
|
||||
$model->setQuery($query);
|
||||
|
||||
$sut->setModel($model);
|
||||
$this->assertEquals(1, count($sut->getEntries()));
|
||||
|
||||
/** @var Timesheet $result */
|
||||
$result = $sut->getEntries()[0];
|
||||
if ($addProject === true) {
|
||||
$this->assertEquals('project description', $result->getDescription());
|
||||
} elseif ($addActivity === true) {
|
||||
$this->assertEquals('activity description', $result->getDescription());
|
||||
} else {
|
||||
$this->assertEquals('foo', $result->getDescription());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,38 +7,23 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Tests\Invoice;
|
||||
namespace App\Tests\Invoice\Calculator;
|
||||
|
||||
use App\Entity\Customer;
|
||||
use App\Entity\InvoiceTemplate;
|
||||
use App\Entity\Timesheet;
|
||||
use App\Invoice\DefaultCalculator;
|
||||
use App\Invoice\Calculator\DefaultCalculator;
|
||||
use App\Model\InvoiceModel;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* @covers \App\Invoice\DefaultCalculator
|
||||
* @covers \App\Invoice\Calculator\DefaultCalculator
|
||||
* @covers \App\Invoice\Calculator\AbstractCalculator
|
||||
*/
|
||||
class DefaultCalculatorTest extends TestCase
|
||||
class DefaultCalculatorTest extends AbstractCalculatorTest
|
||||
{
|
||||
public function testEmptyModel()
|
||||
{
|
||||
$customer = new Customer();
|
||||
$template = new InvoiceTemplate();
|
||||
|
||||
$model = new InvoiceModel();
|
||||
$model->setCustomer($customer);
|
||||
$model->setTemplate($template);
|
||||
|
||||
$sut = new DefaultCalculator();
|
||||
$sut->setModel($model);
|
||||
|
||||
$this->assertEquals(0, $sut->getTotal());
|
||||
$this->assertEquals(0, $sut->getVat());
|
||||
$this->assertEquals('EUR', $sut->getCurrency());
|
||||
$this->assertEquals(0, $sut->getSubtotal());
|
||||
$this->assertEquals(0, $sut->getTimeWorked());
|
||||
$this->assertEquals([], $sut->getEntries());
|
||||
$this->assertEmptyModel(new DefaultCalculator());
|
||||
}
|
||||
|
||||
public function testWithMultipleEntries()
|
||||
@@ -69,6 +54,7 @@ class DefaultCalculatorTest extends TestCase
|
||||
$sut = new DefaultCalculator();
|
||||
$sut->setModel($model);
|
||||
|
||||
$this->assertEquals('default', $sut->getId());
|
||||
$this->assertEquals(581.17, $sut->getTotal());
|
||||
$this->assertEquals(19, $sut->getVat());
|
||||
$this->assertEquals('EUR', $sut->getCurrency());
|
||||
@@ -7,43 +7,26 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Tests\Invoice;
|
||||
namespace App\Tests\Invoice\Calculator;
|
||||
|
||||
use App\Entity\Activity;
|
||||
use App\Entity\Customer;
|
||||
use App\Entity\InvoiceTemplate;
|
||||
use App\Entity\Project;
|
||||
use App\Entity\Timesheet;
|
||||
use App\Invoice\ShortInvoiceCalculator;
|
||||
use App\Entity\User;
|
||||
use App\Invoice\Calculator\ShortInvoiceCalculator;
|
||||
use App\Model\InvoiceModel;
|
||||
use App\Repository\Query\InvoiceQuery;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* @covers \App\Invoice\ShortInvoiceCalculator
|
||||
* @covers \App\Invoice\Calculator\ShortInvoiceCalculator
|
||||
* @covers \App\Invoice\Calculator\AbstractCalculator
|
||||
*/
|
||||
class ShortInvoiceCalculatorTest extends TestCase
|
||||
class ShortInvoiceCalculatorTest extends AbstractCalculatorTest
|
||||
{
|
||||
public function testEmptyModel()
|
||||
{
|
||||
$customer = new Customer();
|
||||
$template = new InvoiceTemplate();
|
||||
$query = new InvoiceQuery();
|
||||
|
||||
$model = new InvoiceModel();
|
||||
$model->setCustomer($customer);
|
||||
$model->setTemplate($template);
|
||||
$model->setQuery($query);
|
||||
|
||||
$sut = new ShortInvoiceCalculator();
|
||||
$sut->setModel($model);
|
||||
|
||||
$this->assertEquals(0, $sut->getTotal());
|
||||
$this->assertEquals(0, $sut->getVat());
|
||||
$this->assertEquals('EUR', $sut->getCurrency());
|
||||
$this->assertEquals(0, $sut->getSubtotal());
|
||||
$this->assertEquals(0, $sut->getTimeWorked());
|
||||
$this->assertEquals(1, count($sut->getEntries()));
|
||||
$this->assertEmptyModel(new ShortInvoiceCalculator());
|
||||
}
|
||||
|
||||
public function testWithMultipleEntries()
|
||||
@@ -56,16 +39,34 @@ class ShortInvoiceCalculatorTest extends TestCase
|
||||
$activity->setName('activity description');
|
||||
|
||||
$timesheet = new Timesheet();
|
||||
$timesheet->setDuration(3600);
|
||||
$timesheet->setRate(293.27);
|
||||
$timesheet
|
||||
->setDuration(3600)
|
||||
->setRate(293.27)
|
||||
->setUser(new User())
|
||||
->setActivity($activity)
|
||||
->setBegin(new \DateTime())
|
||||
->setEnd(new \DateTime())
|
||||
;
|
||||
|
||||
$timesheet2 = new Timesheet();
|
||||
$timesheet2->setDuration(400);
|
||||
$timesheet2->setRate(84);
|
||||
$timesheet2
|
||||
->setDuration(400)
|
||||
->setRate(84)
|
||||
->setUser(new User())
|
||||
->setActivity($activity)
|
||||
->setBegin(new \DateTime())
|
||||
->setEnd(new \DateTime())
|
||||
;
|
||||
|
||||
$timesheet3 = new Timesheet();
|
||||
$timesheet3->setDuration(1800);
|
||||
$timesheet3->setRate(111.11);
|
||||
$timesheet3
|
||||
->setDuration(1800)
|
||||
->setRate(111.11)
|
||||
->setUser(new User())
|
||||
->setActivity($activity)
|
||||
->setBegin(new \DateTime())
|
||||
->setEnd(new \DateTime())
|
||||
;
|
||||
|
||||
$entries = [$timesheet, $timesheet2, $timesheet3];
|
||||
|
||||
@@ -81,6 +82,7 @@ class ShortInvoiceCalculatorTest extends TestCase
|
||||
$sut = new ShortInvoiceCalculator();
|
||||
$sut->setModel($model);
|
||||
|
||||
$this->assertEquals('short', $sut->getId());
|
||||
$this->assertEquals(581.17, $sut->getTotal());
|
||||
$this->assertEquals(19, $sut->getVat());
|
||||
$this->assertEquals('EUR', $sut->getCurrency());
|
||||
@@ -95,30 +97,18 @@ class ShortInvoiceCalculatorTest extends TestCase
|
||||
$this->assertEquals(5800, $result->getDuration());
|
||||
}
|
||||
|
||||
public function testDescriptionByTimesheet()
|
||||
{
|
||||
$this->assertDescription(new ShortInvoiceCalculator(), false, false);
|
||||
}
|
||||
|
||||
public function testDescriptionByActivity()
|
||||
{
|
||||
$this->assertDescription(new ShortInvoiceCalculator(), false, true);
|
||||
}
|
||||
|
||||
public function testDescriptionByProject()
|
||||
{
|
||||
$customer = new Customer();
|
||||
$template = new InvoiceTemplate();
|
||||
$template->setVat(19);
|
||||
|
||||
$project = new Project();
|
||||
$project->setName('project description');
|
||||
|
||||
$query = new InvoiceQuery();
|
||||
$query->setProject($project);
|
||||
|
||||
$model = new InvoiceModel();
|
||||
$model->setCustomer($customer);
|
||||
$model->setTemplate($template);
|
||||
$model->setEntries([]);
|
||||
$model->setQuery($query);
|
||||
|
||||
$sut = new ShortInvoiceCalculator();
|
||||
$sut->setModel($model);
|
||||
$this->assertEquals(1, count($sut->getEntries()));
|
||||
|
||||
/** @var Timesheet $result */
|
||||
$result = $sut->getEntries()[0];
|
||||
$this->assertEquals('project description', $result->getDescription());
|
||||
$this->assertDescription(new ShortInvoiceCalculator(), true, false);
|
||||
}
|
||||
}
|
||||
129
tests/Invoice/Calculator/UserInvoiceCalculatorTest.php
Normal file
129
tests/Invoice/Calculator/UserInvoiceCalculatorTest.php
Normal file
@@ -0,0 +1,129 @@
|
||||
<?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\Calculator;
|
||||
|
||||
use App\Entity\Activity;
|
||||
use App\Entity\Customer;
|
||||
use App\Entity\InvoiceTemplate;
|
||||
use App\Entity\Timesheet;
|
||||
use App\Entity\User;
|
||||
use App\Invoice\Calculator\UserInvoiceCalculator;
|
||||
use App\Model\InvoiceModel;
|
||||
use App\Repository\Query\InvoiceQuery;
|
||||
|
||||
/**
|
||||
* @covers \App\Invoice\Calculator\UserInvoiceCalculator
|
||||
* @covers \App\Invoice\Calculator\AbstractCalculator
|
||||
*/
|
||||
class UserInvoiceCalculatorTest extends AbstractCalculatorTest
|
||||
{
|
||||
public function testEmptyModel()
|
||||
{
|
||||
$this->assertEmptyModel(new UserInvoiceCalculator());
|
||||
}
|
||||
|
||||
public function testWithMultipleEntries()
|
||||
{
|
||||
$customer = new Customer();
|
||||
$template = new InvoiceTemplate();
|
||||
$template->setVat(19);
|
||||
|
||||
$activity = new Activity();
|
||||
$activity->setName('activity description');
|
||||
|
||||
$user1 = $this->getMockBuilder(User::class)->setMethods(['getId'])->disableOriginalConstructor()->getMock();
|
||||
$user1->method('getId')->willReturn(1);
|
||||
|
||||
$user2 = $this->getMockBuilder(User::class)->setMethods(['getId'])->disableOriginalConstructor()->getMock();
|
||||
$user2->method('getId')->willReturn(2);
|
||||
|
||||
$timesheet = new Timesheet();
|
||||
$timesheet
|
||||
->setDuration(3600)
|
||||
->setRate(293.27)
|
||||
->setUser($user1)
|
||||
->setActivity($activity)
|
||||
;
|
||||
|
||||
$timesheet2 = new Timesheet();
|
||||
$timesheet2
|
||||
->setDuration(400)
|
||||
->setRate(84.75)
|
||||
->setUser($user2)
|
||||
->setActivity($activity)
|
||||
;
|
||||
|
||||
$timesheet3 = new Timesheet();
|
||||
$timesheet3
|
||||
->setDuration(1800)
|
||||
->setRate(111.11)
|
||||
->setUser($user1)
|
||||
->setActivity($activity)
|
||||
;
|
||||
|
||||
$timesheet4 = new Timesheet();
|
||||
$timesheet4
|
||||
->setDuration(400)
|
||||
->setRate(1947.99)
|
||||
->setUser($user2)
|
||||
->setActivity($activity)
|
||||
;
|
||||
|
||||
$timesheet5 = new Timesheet();
|
||||
$timesheet5
|
||||
->setDuration(400)
|
||||
->setRate(84)
|
||||
->setUser(new User())
|
||||
->setActivity($activity)
|
||||
;
|
||||
|
||||
$entries = [$timesheet, $timesheet2, $timesheet3, $timesheet4, $timesheet5];
|
||||
|
||||
$query = new InvoiceQuery();
|
||||
$query->setActivity($activity);
|
||||
|
||||
$model = new InvoiceModel();
|
||||
$model->setCustomer($customer);
|
||||
$model->setTemplate($template);
|
||||
$model->setEntries($entries);
|
||||
$model->setQuery($query);
|
||||
|
||||
$sut = new UserInvoiceCalculator();
|
||||
$sut->setModel($model);
|
||||
|
||||
$this->assertEquals('user', $sut->getId());
|
||||
$this->assertEquals(3000.13, $sut->getTotal());
|
||||
$this->assertEquals(19, $sut->getVat());
|
||||
$this->assertEquals('EUR', $sut->getCurrency());
|
||||
$this->assertEquals(2521.12, $sut->getSubtotal());
|
||||
$this->assertEquals(6600, $sut->getTimeWorked());
|
||||
$this->assertEquals(3, count($sut->getEntries()));
|
||||
|
||||
$entries = $sut->getEntries();
|
||||
$this->assertEquals(404.38, $entries[0]->getRate());
|
||||
$this->assertEquals(2032.74, $entries[1]->getRate());
|
||||
$this->assertEquals(84, $entries[2]->getRate());
|
||||
}
|
||||
|
||||
public function testDescriptionByTimesheet()
|
||||
{
|
||||
$this->assertDescription(new UserInvoiceCalculator(), false, false);
|
||||
}
|
||||
|
||||
public function testDescriptionByActivity()
|
||||
{
|
||||
$this->assertDescription(new UserInvoiceCalculator(), false, true);
|
||||
}
|
||||
|
||||
public function testDescriptionByProject()
|
||||
{
|
||||
$this->assertDescription(new UserInvoiceCalculator(), true, false);
|
||||
}
|
||||
}
|
||||
29
tests/Invoice/NumberGenerator/DateNumberGeneratorTest.php
Normal file
29
tests/Invoice/NumberGenerator/DateNumberGeneratorTest.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?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\Calculator;
|
||||
|
||||
use App\Invoice\NumberGenerator\DateNumberGenerator;
|
||||
use App\Model\InvoiceModel;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* @covers \App\Invoice\NumberGenerator\DateNumberGenerator
|
||||
*/
|
||||
class DateNumberGeneratorTest extends TestCase
|
||||
{
|
||||
public function testGetInvoiceNumber()
|
||||
{
|
||||
$sut = new DateNumberGenerator();
|
||||
$sut->setModel(new InvoiceModel());
|
||||
|
||||
$this->assertEquals(date('ymd'), $sut->getInvoiceNumber());
|
||||
$this->assertEquals('default', $sut->getId());
|
||||
}
|
||||
}
|
||||
176
tests/Invoice/Renderer/AbstractRendererTest.php
Normal file
176
tests/Invoice/Renderer/AbstractRendererTest.php
Normal file
@@ -0,0 +1,176 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai time-tracking app.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Tests\Invoice\Renderer;
|
||||
|
||||
use App\Entity\Activity;
|
||||
use App\Entity\Customer;
|
||||
use App\Entity\InvoiceDocument;
|
||||
use App\Entity\InvoiceTemplate;
|
||||
use App\Entity\Project;
|
||||
use App\Entity\Timesheet;
|
||||
use App\Entity\User;
|
||||
use App\Invoice\Calculator\DefaultCalculator;
|
||||
use App\Invoice\NumberGenerator\DateNumberGenerator;
|
||||
use App\Invoice\Renderer\AbstractRenderer;
|
||||
use App\Model\InvoiceModel;
|
||||
use App\Repository\Query\InvoiceQuery;
|
||||
use App\Twig\DateExtensions;
|
||||
use App\Twig\Extensions;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
|
||||
abstract class AbstractRendererTest extends KernelTestCase
|
||||
{
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
protected function getInvoiceTemplatePath()
|
||||
{
|
||||
return __DIR__ . '/../../../templates/invoice/renderer/';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $filename
|
||||
* @return InvoiceDocument
|
||||
*/
|
||||
protected function getInvoiceDocument(string $filename)
|
||||
{
|
||||
return new InvoiceDocument(
|
||||
new \SplFileInfo($this->getInvoiceTemplatePath() . $filename)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $classname
|
||||
* @return AbstractRenderer
|
||||
*/
|
||||
protected function getAbstractRenderer(string $classname)
|
||||
{
|
||||
$requestStack = new RequestStack();
|
||||
$dateSettings = [];
|
||||
$languages = [];
|
||||
|
||||
$request = new Request();
|
||||
$request->setLocale('en');
|
||||
$requestStack->push($request);
|
||||
|
||||
$translator = $this->getMockBuilder(TranslatorInterface::class)->getMock();
|
||||
$dateExtension = new DateExtensions($requestStack, $dateSettings);
|
||||
$extensions = new Extensions($requestStack, $languages);
|
||||
|
||||
return new $classname($translator, $dateExtension, $extensions);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return InvoiceModel
|
||||
*/
|
||||
protected function getInvoiceModel()
|
||||
{
|
||||
$customer = new Customer();
|
||||
$template = new InvoiceTemplate();
|
||||
$template->setTitle('a test invoice template title');
|
||||
$template->setVat(19);
|
||||
|
||||
$project = new Project();
|
||||
$project->setName('project name');
|
||||
$project->setCustomer($customer);
|
||||
|
||||
$activity = new Activity();
|
||||
$activity->setName('activity description');
|
||||
$activity->setProject($project);
|
||||
|
||||
$userMethods = ['getId', 'getPreferenceValue', 'getUsername'];
|
||||
$user1 = $this->getMockBuilder(User::class)->setMethods($userMethods)->disableOriginalConstructor()->getMock();
|
||||
$user1->method('getId')->willReturn(1);
|
||||
$user1->method('getPreferenceValue')->willReturn('50');
|
||||
$user1->method('getUsername')->willReturn('foo-bar');
|
||||
|
||||
$user2 = $this->getMockBuilder(User::class)->setMethods($userMethods)->disableOriginalConstructor()->getMock();
|
||||
$user2->method('getId')->willReturn(2);
|
||||
$user2->method('getUsername')->willReturn('hello-world');
|
||||
|
||||
$timesheet = new Timesheet();
|
||||
$timesheet
|
||||
->setDuration(3600)
|
||||
->setRate(293.27)
|
||||
->setUser($user1)
|
||||
->setActivity($activity)
|
||||
->setBegin(new \DateTime())
|
||||
->setEnd(new \DateTime())
|
||||
;
|
||||
|
||||
$timesheet2 = new Timesheet();
|
||||
$timesheet2
|
||||
->setDuration(400)
|
||||
->setRate(84.75)
|
||||
->setUser($user2)
|
||||
->setActivity($activity)
|
||||
->setBegin(new \DateTime())
|
||||
->setEnd(new \DateTime())
|
||||
;
|
||||
|
||||
$timesheet3 = new Timesheet();
|
||||
$timesheet3
|
||||
->setDuration(1800)
|
||||
->setRate(111.11)
|
||||
->setUser($user1)
|
||||
->setActivity($activity)
|
||||
->setBegin(new \DateTime())
|
||||
->setEnd(new \DateTime())
|
||||
;
|
||||
|
||||
$timesheet4 = new Timesheet();
|
||||
$timesheet4
|
||||
->setDuration(400)
|
||||
->setRate(1947.99)
|
||||
->setUser($user2)
|
||||
->setActivity($activity)
|
||||
->setBegin(new \DateTime())
|
||||
->setEnd(new \DateTime())
|
||||
;
|
||||
|
||||
$timesheet5 = new Timesheet();
|
||||
$timesheet5
|
||||
->setDuration(400)
|
||||
->setFixedRate(84)
|
||||
->setUser((new User())->setUsername('kevin'))
|
||||
->setActivity($activity)
|
||||
->setBegin(new \DateTime())
|
||||
->setEnd(new \DateTime())
|
||||
;
|
||||
|
||||
$entries = [$timesheet, $timesheet2, $timesheet3, $timesheet4, $timesheet5];
|
||||
|
||||
$query = new InvoiceQuery();
|
||||
$query->setActivity($activity);
|
||||
$query->setBegin(new \DateTime());
|
||||
$query->setEnd(new \DateTime());
|
||||
|
||||
$model = new InvoiceModel();
|
||||
$model->setCustomer($customer);
|
||||
$model->setTemplate($template);
|
||||
$model->setEntries($entries);
|
||||
$model->setQuery($query);
|
||||
|
||||
$calculator = new DefaultCalculator();
|
||||
$calculator->setModel($model);
|
||||
|
||||
$model->setCalculator($calculator);
|
||||
|
||||
$numberGenerator = new DateNumberGenerator();
|
||||
$numberGenerator->setModel($model);
|
||||
|
||||
$model->setNumberGenerator($numberGenerator);
|
||||
|
||||
return $model;
|
||||
}
|
||||
}
|
||||
67
tests/Invoice/Renderer/CsvRendererTest.php
Normal file
67
tests/Invoice/Renderer/CsvRendererTest.php
Normal file
@@ -0,0 +1,67 @@
|
||||
<?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\CsvRenderer;
|
||||
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
||||
|
||||
/**
|
||||
* @covers \App\Invoice\Renderer\CsvRenderer
|
||||
* @covers \App\Invoice\Renderer\AbstractRenderer
|
||||
* @covers \App\Invoice\Renderer\AbstractSpreadsheetRenderer
|
||||
*/
|
||||
class CsvRendererTest extends AbstractRendererTest
|
||||
{
|
||||
public function testSupports()
|
||||
{
|
||||
$sut = $this->getAbstractRenderer(CsvRenderer::class);
|
||||
|
||||
$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->assertTrue($sut->supports($this->getInvoiceDocument('export.csv')));
|
||||
$this->assertFalse($sut->supports($this->getInvoiceDocument('spreadsheet.xlsx')));
|
||||
$this->assertFalse($sut->supports($this->getInvoiceDocument('open-spreadsheet.ods')));
|
||||
}
|
||||
|
||||
public function testRender()
|
||||
{
|
||||
/** @var CsvRenderer $sut */
|
||||
$sut = $this->getAbstractRenderer(CsvRenderer::class);
|
||||
$model = $this->getInvoiceModel();
|
||||
$document = $this->getInvoiceDocument('export.csv');
|
||||
/** @var BinaryFileResponse $response */
|
||||
$response = $sut->render($document, $model);
|
||||
|
||||
$file = $response->getFile();
|
||||
$this->assertEquals('text/csv', $response->headers->get('Content-Type'));
|
||||
$this->assertEquals('attachment; filename=export.csv', $response->headers->get('Content-Disposition'));
|
||||
|
||||
$this->assertTrue(file_exists($file->getRealPath()));
|
||||
$content = file_get_contents($file->getRealPath());
|
||||
|
||||
$this->assertNotContains('${', $content);
|
||||
$this->assertContains(',"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();
|
||||
|
||||
$this->assertEquals($content, $content2);
|
||||
$this->assertFalse(file_exists($file->getRealPath()));
|
||||
}
|
||||
}
|
||||
69
tests/Invoice/Renderer/DocxRendererTest.php
Normal file
69
tests/Invoice/Renderer/DocxRendererTest.php
Normal file
@@ -0,0 +1,69 @@
|
||||
<?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\DocxRenderer;
|
||||
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
||||
|
||||
/**
|
||||
* @covers \App\Invoice\Renderer\DocxRenderer
|
||||
* @covers \App\Invoice\Renderer\AbstractRenderer
|
||||
*/
|
||||
class DocxRendererTest extends AbstractRendererTest
|
||||
{
|
||||
public function testSupports()
|
||||
{
|
||||
$sut = $this->getAbstractRenderer(DocxRenderer::class);
|
||||
|
||||
$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->assertTrue($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')));
|
||||
}
|
||||
|
||||
public function testRender()
|
||||
{
|
||||
/** @var DocxRenderer $sut */
|
||||
$sut = $this->getAbstractRenderer(DocxRenderer::class);
|
||||
$model = $this->getInvoiceModel();
|
||||
$document = $this->getInvoiceDocument('company.docx');
|
||||
/** @var BinaryFileResponse $response */
|
||||
$response = $sut->render($document, $model);
|
||||
|
||||
$file = $response->getFile();
|
||||
$this->assertEquals('application/vnd.openxmlformats-officedocument.wordprocessingml.document', $response->headers->get('Content-Type'));
|
||||
$this->assertEquals('attachment; filename=company.docx', $response->headers->get('Content-Disposition'));
|
||||
|
||||
$this->assertTrue(file_exists($file->getRealPath()));
|
||||
|
||||
// TODO test document content?
|
||||
/*
|
||||
$content = file_get_contents($file->getRealPath());
|
||||
$this->assertNotContains('${', $content);
|
||||
$this->assertContains(',"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();
|
||||
$this->assertNotEmpty($content2);
|
||||
|
||||
$this->assertFalse(file_exists($file->getRealPath()));
|
||||
}
|
||||
}
|
||||
70
tests/Invoice/Renderer/OdsRendererTest.php
Normal file
70
tests/Invoice/Renderer/OdsRendererTest.php
Normal file
@@ -0,0 +1,70 @@
|
||||
<?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\OdsRenderer;
|
||||
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
||||
|
||||
/**
|
||||
* @covers \App\Invoice\Renderer\OdsRenderer
|
||||
* @covers \App\Invoice\Renderer\AbstractRenderer
|
||||
* @covers \App\Invoice\Renderer\AbstractSpreadsheetRenderer
|
||||
*/
|
||||
class OdsRendererTest extends AbstractRendererTest
|
||||
{
|
||||
public function testSupports()
|
||||
{
|
||||
$sut = $this->getAbstractRenderer(OdsRenderer::class);
|
||||
|
||||
$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->assertTrue($sut->supports($this->getInvoiceDocument('open-spreadsheet.ods')));
|
||||
}
|
||||
|
||||
public function testRender()
|
||||
{
|
||||
/** @var OdsRenderer $sut */
|
||||
$sut = $this->getAbstractRenderer(OdsRenderer::class);
|
||||
$model = $this->getInvoiceModel();
|
||||
$document = $this->getInvoiceDocument('open-spreadsheet.ods');
|
||||
/** @var BinaryFileResponse $response */
|
||||
$response = $sut->render($document, $model);
|
||||
|
||||
$file = $response->getFile();
|
||||
$this->assertEquals('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', $response->headers->get('Content-Type'));
|
||||
$this->assertEquals('attachment; filename=open-spreadsheet.ods', $response->headers->get('Content-Disposition'));
|
||||
|
||||
$this->assertTrue(file_exists($file->getRealPath()));
|
||||
|
||||
// TODO test spreadsheet content?
|
||||
/*
|
||||
$content = file_get_contents($file->getRealPath());
|
||||
$this->assertNotContains('${', $content);
|
||||
$this->assertContains(',"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();
|
||||
$this->assertNotEmpty($content2);
|
||||
|
||||
$this->assertFalse(file_exists($file->getRealPath()));
|
||||
}
|
||||
}
|
||||
65
tests/Invoice/Renderer/TwigRendererTest.php
Normal file
65
tests/Invoice/Renderer/TwigRendererTest.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?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\TwigRenderer;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Twig\Loader\FilesystemLoader;
|
||||
|
||||
/**
|
||||
* @covers \App\Invoice\Renderer\TwigRenderer
|
||||
*/
|
||||
class TwigRendererTest extends AbstractRendererTest
|
||||
{
|
||||
public function testSupports()
|
||||
{
|
||||
$loader = new FilesystemLoader();
|
||||
$env = new \Twig_Environment($loader);
|
||||
$sut = new TwigRenderer($env);
|
||||
|
||||
$this->assertTrue($sut->supports($this->getInvoiceDocument('default.html.twig')));
|
||||
$this->assertTrue($sut->supports($this->getInvoiceDocument('freelancer.html.twig')));
|
||||
$this->assertTrue($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')));
|
||||
}
|
||||
|
||||
public function testRender()
|
||||
{
|
||||
$kernel = self::bootKernel();
|
||||
/** @var \Twig_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 TwigRenderer($twig);
|
||||
|
||||
$model = $this->getInvoiceModel();
|
||||
|
||||
$document = $this->getInvoiceDocument('timesheet.html.twig');
|
||||
$response = $sut->render($document, $model);
|
||||
|
||||
$content = $response->getContent();
|
||||
|
||||
$this->assertContains('<h2 class="page-header">
|
||||
<span contenteditable="true">a test invoice template title</span>
|
||||
</h2>', $content);
|
||||
$this->assertEquals(5, substr_count($content, '<td>activity description / project name</td>'));
|
||||
}
|
||||
}
|
||||
70
tests/Invoice/Renderer/XlsxRendererTest.php
Normal file
70
tests/Invoice/Renderer/XlsxRendererTest.php
Normal file
@@ -0,0 +1,70 @@
|
||||
<?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\XlsxRenderer;
|
||||
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
||||
|
||||
/**
|
||||
* @covers \App\Invoice\Renderer\XlsxRenderer
|
||||
* @covers \App\Invoice\Renderer\AbstractRenderer
|
||||
* @covers \App\Invoice\Renderer\AbstractSpreadsheetRenderer
|
||||
*/
|
||||
class XlsxRendererTest extends AbstractRendererTest
|
||||
{
|
||||
public function testSupports()
|
||||
{
|
||||
$sut = $this->getAbstractRenderer(XlsxRenderer::class);
|
||||
|
||||
$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->assertTrue($sut->supports($this->getInvoiceDocument('spreadsheet.xlsx')));
|
||||
$this->assertFalse($sut->supports($this->getInvoiceDocument('open-spreadsheet.ods')));
|
||||
}
|
||||
|
||||
public function testRender()
|
||||
{
|
||||
/** @var XlsxRenderer $sut */
|
||||
$sut = $this->getAbstractRenderer(XlsxRenderer::class);
|
||||
$model = $this->getInvoiceModel();
|
||||
$document = $this->getInvoiceDocument('spreadsheet.xlsx');
|
||||
/** @var BinaryFileResponse $response */
|
||||
$response = $sut->render($document, $model);
|
||||
|
||||
$file = $response->getFile();
|
||||
$this->assertEquals('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', $response->headers->get('Content-Type'));
|
||||
$this->assertEquals('attachment; filename=spreadsheet.xlsx', $response->headers->get('Content-Disposition'));
|
||||
|
||||
$this->assertTrue(file_exists($file->getRealPath()));
|
||||
|
||||
// TODO test document content?
|
||||
/*
|
||||
$content = file_get_contents($file->getRealPath());
|
||||
$this->assertNotContains('${', $content);
|
||||
$this->assertContains(',"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();
|
||||
$this->assertNotEmpty($content2);
|
||||
|
||||
$this->assertFalse(file_exists($file->getRealPath()));
|
||||
}
|
||||
}
|
||||
77
tests/Invoice/ServiceInvoiceTest.php
Normal file
77
tests/Invoice/ServiceInvoiceTest.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;
|
||||
|
||||
use App\Entity\InvoiceDocument;
|
||||
use App\Invoice\Calculator\DefaultCalculator;
|
||||
use App\Invoice\NumberGenerator\DateNumberGenerator;
|
||||
use App\Invoice\Renderer\TwigRenderer;
|
||||
use App\Invoice\ServiceInvoice;
|
||||
use App\Repository\InvoiceDocumentRepository;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class ServiceInvoiceTest extends TestCase
|
||||
{
|
||||
public function testEmptyObject()
|
||||
{
|
||||
$repo = new InvoiceDocumentRepository([]);
|
||||
$sut = new ServiceInvoice($repo);
|
||||
|
||||
$this->assertEmpty($sut->getCalculator());
|
||||
$this->assertInternalType('array', $sut->getCalculator());
|
||||
$this->assertEmpty($sut->getRenderer());
|
||||
$this->assertInternalType('array', $sut->getRenderer());
|
||||
$this->assertEmpty($sut->getNumberGenerator());
|
||||
$this->assertInternalType('array', $sut->getNumberGenerator());
|
||||
$this->assertEmpty($sut->getDocuments());
|
||||
$this->assertInternalType('array', $sut->getDocuments());
|
||||
|
||||
$this->assertNull($sut->getCalculatorByName('default'));
|
||||
$this->assertNull($sut->getDocumentByName('default'));
|
||||
$this->assertNull($sut->getNumberGeneratorByName('default'));
|
||||
}
|
||||
|
||||
public function testWithDocumentDirectory()
|
||||
{
|
||||
$repo = new InvoiceDocumentRepository(['templates/invoice/renderer/']);
|
||||
$sut = new ServiceInvoice($repo);
|
||||
|
||||
$actual = $sut->getDocuments();
|
||||
$this->assertNotEmpty($actual);
|
||||
foreach ($actual as $document) {
|
||||
$this->assertInstanceOf(InvoiceDocument::class, $document);
|
||||
}
|
||||
|
||||
$actual = $sut->getDocumentByName('default');
|
||||
$this->assertInstanceOf(InvoiceDocument::class, $actual);
|
||||
}
|
||||
|
||||
public function testAdd()
|
||||
{
|
||||
$repo = new InvoiceDocumentRepository([]);
|
||||
$sut = new ServiceInvoice($repo);
|
||||
|
||||
$sut->addCalculator(new DefaultCalculator());
|
||||
$sut->addNumberGenerator(new DateNumberGenerator());
|
||||
$sut->addRenderer(
|
||||
new TwigRenderer(
|
||||
$this->getMockBuilder(\Twig_Environment::class)->disableOriginalConstructor()->getMock()
|
||||
)
|
||||
);
|
||||
|
||||
$this->assertEquals(1, count($sut->getCalculator()));
|
||||
$this->assertInstanceOf(DefaultCalculator::class, $sut->getCalculatorByName('default'));
|
||||
|
||||
$this->assertEquals(1, count($sut->getNumberGenerator()));
|
||||
$this->assertInstanceOf(DateNumberGenerator::class, $sut->getNumberGeneratorByName('default'));
|
||||
|
||||
$this->assertEquals(1, count($sut->getRenderer()));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user