fix spreadsheet renderer for invoices with one entry (#328)

This commit is contained in:
Kevin Papst
2018-09-25 21:16:41 +02:00
committed by GitHub
parent 98dc38ed99
commit eee7cb9bf4
5 changed files with 103 additions and 11 deletions

View File

@@ -39,7 +39,9 @@ abstract class AbstractSpreadsheetRenderer extends AbstractRenderer
$entries = $model->getCalculator()->getEntries();
$replacer = $this->modelToReplacer($model);
$timesheetAmount = count($entries);
$this->addTemplateRows($worksheet, $timesheetAmount);
if ($timesheetAmount > 1) {
$this->addTemplateRows($worksheet, $timesheetAmount);
}
$worksheet->setTitle($model->getTemplate()->getTitle());

View File

@@ -173,4 +173,65 @@ abstract class AbstractRendererTest extends KernelTestCase
return $model;
}
/**
* @param Timesheet[] $timesheets
* @return InvoiceModel
*/
protected function getInvoiceModelOneEntry()
{
$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');
$timesheet = new Timesheet();
$timesheet
->setDuration(3600)
->setRate(293.27)
->setUser($user1)
->setActivity($activity)
->setBegin(new \DateTime())
->setEnd(new \DateTime())
;
$entries = [$timesheet];
$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;
}
}

View File

@@ -10,6 +10,7 @@
namespace App\Tests\Invoice\Renderer;
use App\Invoice\Renderer\CsvRenderer;
use App\Model\InvoiceModel;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
/**
@@ -33,11 +34,19 @@ class CsvRendererTest extends AbstractRendererTest
$this->assertFalse($sut->supports($this->getInvoiceDocument('open-spreadsheet.ods')));
}
public function testRender()
public function getTestModel()
{
yield [$this->getInvoiceModel(), '1,947.99', 6, 5, 1, 2, 2];
yield [$this->getInvoiceModelOneEntry(), '293.27', 2, 1, 0, 1, 0];
}
/**
* @dataProvider getTestModel
*/
public function testRender(InvoiceModel $model, $expectedRate, $expectedRows, $expectedDescriptions, $expectedUser1, $expectedUser2, $expectedUser3)
{
/** @var CsvRenderer $sut */
$sut = $this->getAbstractRenderer(CsvRenderer::class);
$model = $this->getInvoiceModel();
$document = $this->getInvoiceDocument('export.csv');
/** @var BinaryFileResponse $response */
$response = $sut->render($document, $model);
@@ -50,12 +59,12 @@ class CsvRendererTest extends AbstractRendererTest
$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",'));
$this->assertContains(',"' . $expectedRate . '"', $content);
$this->assertEquals($expectedRows, substr_count($content, PHP_EOL));
$this->assertEquals($expectedDescriptions, substr_count($content, 'activity description'));
$this->assertEquals($expectedUser1, substr_count($content, ',"kevin",'));
$this->assertEquals($expectedUser3, substr_count($content, ',"hello-world",'));
$this->assertEquals($expectedUser2, substr_count($content, ',"foo-bar",'));
ob_start();
$response->sendContent();

View File

@@ -10,6 +10,7 @@
namespace App\Tests\Invoice\Renderer;
use App\Invoice\Renderer\OdsRenderer;
use App\Model\InvoiceModel;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
/**
@@ -33,7 +34,16 @@ class OdsRendererTest extends AbstractRendererTest
$this->assertTrue($sut->supports($this->getInvoiceDocument('open-spreadsheet.ods')));
}
public function testRender()
public function getTestModel()
{
yield [$this->getInvoiceModel(), '1,947.99', 6, 5, 1, 2, 2];
yield [$this->getInvoiceModelOneEntry(), '293.27', 2, 1, 0, 1, 0];
}
/**
* @dataProvider getTestModel
*/
public function testRender(InvoiceModel $model, $expectedRate, $expectedRows, $expectedDescriptions, $expectedUser1, $expectedUser2, $expectedUser3)
{
/** @var OdsRenderer $sut */
$sut = $this->getAbstractRenderer(OdsRenderer::class);

View File

@@ -10,6 +10,7 @@
namespace App\Tests\Invoice\Renderer;
use App\Invoice\Renderer\XlsxRenderer;
use App\Model\InvoiceModel;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
/**
@@ -33,7 +34,16 @@ class XlsxRendererTest extends AbstractRendererTest
$this->assertFalse($sut->supports($this->getInvoiceDocument('open-spreadsheet.ods')));
}
public function testRender()
public function getTestModel()
{
yield [$this->getInvoiceModel(), '1,947.99', 6, 5, 1, 2, 2];
yield [$this->getInvoiceModelOneEntry(), '293.27', 2, 1, 0, 1, 0];
}
/**
* @dataProvider getTestModel
*/
public function testRender(InvoiceModel $model, $expectedRate, $expectedRows, $expectedDescriptions, $expectedUser1, $expectedUser2, $expectedUser3)
{
/** @var XlsxRenderer $sut */
$sut = $this->getAbstractRenderer(XlsxRenderer::class);