added export module (#538)

This commit is contained in:
Kevin Papst
2019-02-05 21:37:33 +01:00
committed by GitHub
parent 062735c9e3
commit 816866549c
104 changed files with 3770 additions and 608 deletions

View File

@@ -54,6 +54,19 @@ class TimesheetControllerTest extends APIControllerBaseTest
$this->assertDefaultStructure($result[0], false);
}
public function testGetCollectionWithQuery()
{
$query = ['customer' => 1, 'project' => 1, 'page' => 2, 'size' => 5, 'order' => 'DESC', 'orderBy' => 'rate'];
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
$this->assertAccessIsGranted($client, '/api/timesheets', 'GET', $query);
$result = json_decode($client->getResponse()->getContent(), true);
$this->assertInternalType('array', $result);
$this->assertNotEmpty($result);
$this->assertEquals(5, count($result));
$this->assertDefaultStructure($result[0], false);
}
public function testGetEntity()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
@@ -166,6 +179,45 @@ class TimesheetControllerTest extends APIControllerBaseTest
$this->assertEntityNotFound(User::ROLE_USER, '/api/timesheets/20');
}
public function testPatchAction()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_TEAMLEAD);
$data = [
'activity' => 1,
'project' => 1,
'begin' => (new \DateTime('- 7 hours'))->format('Y-m-d H:m'),
'end' => (new \DateTime())->format('Y-m-d H:m'),
'description' => 'foo',
'exported' => true,
];
$this->request($client, '/api/timesheets/1', 'PATCH', [], json_encode($data));
$this->assertTrue($client->getResponse()->isSuccessful());
$result = json_decode($client->getResponse()->getContent(), true);
$this->assertInternalType('array', $result);
$this->assertDefaultStructure($result);
$this->assertNotEmpty($result['id']);
$this->assertEquals(25200, $result['duration']);
$this->assertEquals(1, $result['exported']);
}
public function testInvalidPatchAction()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
$data = [
'activity' => 10,
'project' => 1,
'begin' => (new \DateTime())->format('Y-m-d H:m'),
'end' => (new \DateTime('- 7 hours'))->format('Y-m-d H:m'),
'description' => 'foo',
];
$this->request($client, '/api/timesheets/1', 'PATCH', [], json_encode($data));
$response = $client->getResponse();
$this->assertEquals(400, $response->getStatusCode());
$this->assertApiCallValidationError($response, ['end', 'activity']);
}
protected function assertDefaultStructure(array $result, $full = true)
{
$expectedKeys = [
@@ -174,7 +226,7 @@ class TimesheetControllerTest extends APIControllerBaseTest
if ($full) {
$expectedKeys = array_merge($expectedKeys, [
'description', 'fixed_rate', 'hourly_rate'
'exported', 'description', 'fixed_rate', 'hourly_rate'
]);
}

View File

@@ -123,8 +123,8 @@ class ActivityControllerTest extends ControllerBaseTest
$this->request($client, '/admin/activity/1/delete');
$this->assertIsRedirect($client, $this->createUrl('/admin/activity/'));
$client->followRedirect();
$this->assertHasDataTable($client);
$this->assertHasFlashSuccess($client);
$this->assertHasFlashDeleteSuccess($client);
$this->assertHasNoEntriesWithFilter($client);
$this->request($client, '/admin/activity/1/edit');
$this->assertFalse($client->getResponse()->isSuccessful());
@@ -157,8 +157,8 @@ class ActivityControllerTest extends ControllerBaseTest
$this->assertIsRedirect($client, $this->createUrl('/admin/activity/'));
$client->followRedirect();
$this->assertHasDataTable($client);
$this->assertHasFlashSuccess($client);
$this->assertHasFlashDeleteSuccess($client);
$this->assertHasNoEntriesWithFilter($client);
// SQLIte does not necessarly support onCascade delete, so these timesheet will stay after deletion
// $em->clear();

View File

@@ -130,8 +130,8 @@ class CustomerControllerTest extends ControllerBaseTest
$this->assertIsRedirect($client, $this->createUrl('/admin/customer/'));
$client->followRedirect();
$this->assertHasDataTable($client);
$this->assertHasFlashSuccess($client);
$this->assertHasFlashDeleteSuccess($client);
$this->assertHasNoEntriesWithFilter($client);
// SQLIte does not necessarly support onCascade delete, so these timesheet will stay after deletion
// $em->clear();

View File

@@ -153,8 +153,8 @@ class ProjectControllerTest extends ControllerBaseTest
$this->assertIsRedirect($client, $this->createUrl('/admin/project/'));
$client->followRedirect();
$this->assertHasDataTable($client);
$this->assertHasFlashSuccess($client);
$this->assertHasFlashDeleteSuccess($client);
$this->assertHasNoEntriesWithFilter($client);
// SQLIte does not necessarly support onCascade delete, so these timesheet will stay after deletion
// $em->clear();

View File

@@ -31,7 +31,10 @@ class TimesheetControllerTest extends ControllerBaseTest
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_TEAMLEAD);
$this->assertAccessIsGranted($client, '/team/timesheet/');
$this->assertHasDataTable($client);
$this->assertTrue($client->getResponse()->isSuccessful());
// there are no records by default in the test database
$this->assertHasNoEntriesWithFilter($client);
$result = $client->getCrawler()->filter('div.breadcrumb div.box-tools div.btn-group a.btn');
$this->assertEquals(4, count($result));
@@ -47,9 +50,10 @@ class TimesheetControllerTest extends ControllerBaseTest
$client = $this->getClientForAuthenticatedUser(User::ROLE_TEAMLEAD);
$em = $client->getContainer()->get('doctrine.orm.entity_manager');
$user = $this->getUserByRole($em, User::ROLE_USER);
$fixture = new TimesheetFixtures();
$fixture->setAmount(10);
$fixture->setUser($this->getUserByRole($em, User::ROLE_USER));
$fixture->setUser($user);
$fixture->setStartDate(new \DateTime('-10 days'));
$this->importFixture($em, $fixture);
@@ -61,7 +65,7 @@ class TimesheetControllerTest extends ControllerBaseTest
$form = $client->getCrawler()->filter('form.navbar-form')->form();
$client->submit($form, [
'state' => 1,
'user' => 1,
'user' => $user->getId(),
'pageSize' => 25,
'daterange' => $dateRange,
'customer' => null,
@@ -69,8 +73,7 @@ class TimesheetControllerTest extends ControllerBaseTest
$this->assertTrue($client->getResponse()->isSuccessful());
$this->assertHasDataTable($client);
// TODO more assertions
$this->assertDataTableRowCount($client, 'datatable_timesheet_admin', 10);
}
public function testExportAction()

View File

@@ -178,6 +178,17 @@ abstract class ControllerBaseTest extends WebTestCase
$this->assertContains('<table class="table table-striped table-hover dataTable" role="grid">', $client->getResponse()->getContent());
}
/**
* @param Client $client
* @param string $id
* @param int $count
*/
protected function assertDataTableRowCount(Client $client, string $id, int $count)
{
$node = $client->getCrawler()->filter('section.content div#' . $id . ' table.table-striped tbody tr');
$this->assertEquals($count, $node->count());
}
/**
* @param string $role the USER role to use for the request
* @param string $url the URL of the page displaying the initial form to submit
@@ -221,13 +232,32 @@ abstract class ControllerBaseTest extends WebTestCase
}
}
protected function assertHasNoEntriesWithFilter(Client $client)
{
$node = $client->getCrawler()->filter('div.callout.callout-warning.lead');
$this->assertContains('No entries were found based on your selected filters.', $node->text());
}
/**
* @param Client $client
* @param string|null $message
*/
protected function assertHasFlashSuccess(Client $client)
protected function assertHasFlashDeleteSuccess(Client $client)
{
$this->assertHasFlashSuccess($client, 'Entry was deleted successful');
}
/**
* @param Client $client
* @param string|null $message
*/
protected function assertHasFlashSuccess(Client $client, string $message = null)
{
$node = $client->getCrawler()->filter('div.alert.alert-success.alert-dismissible');
$this->assertNotEmpty($node->text());
if (null !== $message) {
$this->assertContains($message, $node->text());
}
}
/**

View File

@@ -0,0 +1,134 @@
<?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\Controller;
use App\Entity\User;
use App\Tests\DataFixtures\TimesheetFixtures;
/**
* @coversDefaultClass \App\Controller\ExportController
* @group integration
*/
class ExportControllerTest extends ControllerBaseTest
{
public function testIsSecure()
{
$this->assertUrlIsSecured('/export/');
$this->assertUrlIsSecuredForRole(User::ROLE_USER, '/export/');
}
public function testIndexActionHasErrorMessageOnEmptyQuery()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_TEAMLEAD);
$this->request($client, '/export/');
$this->assertTrue($client->getResponse()->isSuccessful());
$this->assertHasNoEntriesWithFilter($client);
}
public function testIndexActionWithEntries()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_TEAMLEAD);
$em = $client->getContainer()->get('doctrine.orm.entity_manager');
$begin = new \DateTime('first day of this month');
$end = new \DateTime('last day of this month');
$fixture = new TimesheetFixtures();
$fixture
->setUser($this->getUserByRole($em, User::ROLE_USER))
->setAmount(20)
->setStartDate($begin)
;
$this->importFixture($em, $fixture);
$this->request($client, '/export/');
$this->assertTrue($client->getResponse()->isSuccessful());
// make sure all existing records are displayed
$this->assertHasDataTable($client);
$this->assertDataTableRowCount($client, 'datatable_export', 20);
// assert export type buttons are available
$expected = ['csv', 'html', 'pdf', 'ods', 'xlsx'];
$node = $client->getCrawler()->filter('#export-buttons button');
$this->assertEquals(count($expected), $node->count());
foreach ($node->getIterator() as $button) {
$type = $button->getAttribute('data-type');
$this->assertContains($type, $expected);
}
}
public function testExportActionWithMissingRenderer()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_TEAMLEAD);
$this->request($client, '/export/data');
$response = $client->getResponse();
$this->assertFalse($response->isSuccessful());
$this->assertEquals(404, $response->getStatusCode());
}
public function testExportActionWithInvalidRenderer()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_TEAMLEAD);
$this->request($client, '/export/');
$this->assertTrue($client->getResponse()->isSuccessful());
$form = $client->getCrawler()->filter('#export-form')->form();
$form->getFormNode()->setAttribute('action', $this->createUrl('/export/data'));
$client->submit($form, [
'type' => 'default'
]);
$response = $client->getResponse();
$this->assertFalse($response->isSuccessful());
$this->assertEquals(404, $response->getStatusCode());
}
public function testExportAction()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_TEAMLEAD);
$em = $client->getContainer()->get('doctrine.orm.entity_manager');
$begin = new \DateTime('first day of this month');
$fixture = new TimesheetFixtures();
$fixture
->setUser($this->getUserByRole($em, User::ROLE_USER))
->setAmount(20)
->setStartDate($begin)
;
$this->importFixture($em, $fixture);
$this->request($client, '/export/');
$this->assertTrue($client->getResponse()->isSuccessful());
$form = $client->getCrawler()->filter('#export-form')->form();
$form->getFormNode()->setAttribute('action', $this->createUrl('/export/data'));
// don't add daterange to make sure the current month is the default range
$client->submit($form, [
'type' => 'html'
]);
$response = $client->getResponse();
$this->assertTrue($response->isSuccessful());
$node = $client->getCrawler()->filter('body');
$this->assertEquals(1, $node->count());
// poor mans assertions ;-)
$this->assertContains('export_print', $node->getIterator()[0]->getAttribute('class'));
$this->assertContains('<h2>List of expenses</h2>', $response->getContent());
$this->assertContains('<h3>Summary</h3>', $response->getContent());
$node = $client->getCrawler()->filter('section.export div#export-records table.dataTable tbody tr');
$this->assertEquals(20, $node->count());
}
}

View File

@@ -46,9 +46,7 @@ class InvoiceControllerTest extends ControllerBaseTest
$this->request($client, '/invoice/');
$this->assertTrue($client->getResponse()->isSuccessful());
$node = $client->getCrawler()->filter('div.callout.callout-warning.lead');
$this->assertNotEmpty($node->text());
$this->assertContains('No invoice entries were found based on your selected filters.', $node->text());
$this->assertHasNoEntriesWithFilter($client);
}
public function testListTemplateAction()
@@ -150,6 +148,7 @@ class InvoiceControllerTest extends ControllerBaseTest
$this->assertTrue($client->getResponse()->isSuccessful());
// no datatable should be displayed
$node = $client->getCrawler()->filter('div.callout.callout-warning.lead');
$this->assertEquals(0, $node->count());
@@ -157,8 +156,7 @@ class InvoiceControllerTest extends ControllerBaseTest
$this->assertNotEmpty($node->text());
$this->assertContains('This is a preview of the data that will show up in your invoice document.', $node->text());
$node = $client->getCrawler()->filter('section.invoice div.table-responsive table.table-striped tbody tr');
$this->assertEquals(20, $node->count());
$this->assertDataTableRowCount($client, 'datatable_invoice', 20);
$form = $client->getCrawler()->filter('#invoice-print-form')->form();
$form->getFormNode()->setAttribute('action', $this->createUrl('/invoice/print'));

View File

@@ -30,7 +30,9 @@ class TimesheetControllerTest extends ControllerBaseTest
$client = $this->getClientForAuthenticatedUser();
$this->request($client, '/timesheet/');
$this->assertTrue($client->getResponse()->isSuccessful());
$this->assertHasDataTable($client);
// there are no records by default in the test database
$this->assertHasNoEntriesWithFilter($client);
$result = $client->getCrawler()->filter('div.breadcrumb div.box-tools div.btn-group a.btn');
$this->assertEquals(5, count($result));
@@ -43,7 +45,7 @@ class TimesheetControllerTest extends ControllerBaseTest
public function testIndexActionWithQuery()
{
$client = $this->getClientForAuthenticatedUser();
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
$em = $client->getContainer()->get('doctrine.orm.entity_manager');
$fixture = new TimesheetFixtures();
@@ -67,8 +69,7 @@ class TimesheetControllerTest extends ControllerBaseTest
$this->assertTrue($client->getResponse()->isSuccessful());
$this->assertHasDataTable($client);
// TODO more assertions
$this->assertDataTableRowCount($client, 'datatable_timesheet', 5);
}
public function testExportAction()

View File

@@ -0,0 +1,148 @@
<?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\Export\Renderer;
use App\Entity\Activity;
use App\Entity\Customer;
use App\Entity\Project;
use App\Entity\Timesheet;
use App\Entity\User;
use App\Export\RendererInterface;
use App\Repository\Query\TimesheetQuery;
use App\Twig\DateExtensions;
use App\Twig\Extensions;
use App\Utils\LocaleSettings;
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
{
/**
* @param string $classname
* @return RendererInterface
*/
protected function getAbstractRenderer(string $classname)
{
$requestStack = new RequestStack();
$languages = [
'en' => [
'date' => 'Y.m.d',
'duration' => '%h:%m h'
]
];
$request = new Request();
$request->setLocale('en');
$requestStack->push($request);
$localeSettings = new LocaleSettings($requestStack, $languages);
$translator = $this->getMockBuilder(TranslatorInterface::class)->getMock();
$dateExtension = new DateExtensions($localeSettings);
$extensions = new Extensions($requestStack, $localeSettings);
return new $classname($translator, $dateExtension, $extensions);
}
/**
* @param RendererInterface $renderer
* @return \Symfony\Component\HttpFoundation\Response
*/
protected function render(RendererInterface $renderer)
{
$customer = new Customer();
$customer->setName('Customer Name');
$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)
->setProject($project)
->setBegin(new \DateTime())
->setEnd(new \DateTime())
;
$timesheet2 = new Timesheet();
$timesheet2
->setDuration(400)
->setRate(84.75)
->setUser($user2)
->setActivity($activity)
->setProject($project)
->setBegin(new \DateTime())
->setEnd(new \DateTime())
;
$timesheet3 = new Timesheet();
$timesheet3
->setDuration(1800)
->setRate(111.11)
->setUser($user1)
->setActivity($activity)
->setProject($project)
->setBegin(new \DateTime())
->setEnd(new \DateTime())
;
$timesheet4 = new Timesheet();
$timesheet4
->setDuration(400)
->setRate(1947.99)
->setUser($user2)
->setActivity($activity)
->setProject($project)
->setBegin(new \DateTime())
->setEnd(new \DateTime())
;
$timesheet5 = new Timesheet();
$timesheet5
->setDuration(400)
->setFixedRate(84)
->setUser((new User())->setUsername('kevin'))
->setActivity($activity)
->setProject($project)
->setBegin(new \DateTime())
->setEnd(new \DateTime())
;
$entries = [$timesheet, $timesheet2, $timesheet3, $timesheet4, $timesheet5];
$query = new TimesheetQuery();
$query->setActivity($activity);
$query->setBegin(new \DateTime());
$query->setEnd(new \DateTime());
$query->setProject($project);
return $renderer->render($entries, $query);
}
}

View File

@@ -0,0 +1,71 @@
<?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\Export\Renderer;
use App\Export\Renderer\CsvRenderer;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
/**
* @covers \App\Export\Renderer\CsvRenderer
* @covers \App\Export\Renderer\AbstractSpreadsheetRenderer
* @covers \App\Export\Renderer\RendererTrait
*/
class CsvRendererTest extends AbstractRendererTest
{
public function testConfiguration()
{
$sut = $this->getAbstractRenderer(CsvRenderer::class);
$this->assertEquals('csv', $sut->getId());
$this->assertEquals('csv', $sut->getTitle());
$this->assertEquals('csv', $sut->getIcon());
}
public function getTestModel()
{
return [
['01:50 h', '2,437.12 €', '1,947.99 €', 7, 5, 1, 2, 2]
];
}
/**
* @dataProvider getTestModel
*/
public function testRender($totalDuration, $totalRate, $expectedRate, $expectedRows, $expectedDescriptions, $expectedUser1, $expectedUser2, $expectedUser3)
{
$sut = $this->getAbstractRenderer(CsvRenderer::class);
/** @var BinaryFileResponse $response */
$response = $this->render($sut);
$file = $response->getFile();
$this->assertEquals('text/csv', $response->headers->get('Content-Type'));
$this->assertEquals('attachment; filename=kimai-export.csv', $response->headers->get('Content-Disposition'));
$this->assertTrue(file_exists($file->getRealPath()));
$content = file_get_contents($file->getRealPath());
$this->assertContains('"' . $totalDuration . '"', $content);
$this->assertContains('"' . $totalRate . '"', $content);
$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();
$content2 = ob_get_clean();
$this->assertEquals($content, $content2);
$this->assertFalse(file_exists($file->getRealPath()));
}
}

View File

@@ -0,0 +1,62 @@
<?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\Export\Renderer;
use App\Export\Renderer\HtmlRenderer;
use Symfony\Component\HttpFoundation\Request;
use Twig\Loader\FilesystemLoader;
/**
* @covers \App\Export\Renderer\HtmlRenderer
* @covers \App\Export\Renderer\RendererTrait
*/
class HtmlRendererTest extends AbstractRendererTest
{
public function testConfiguration()
{
$sut = new HtmlRenderer(
$this->getMockBuilder(\Twig_Environment::class)->disableOriginalConstructor()->getMock()
);
$this->assertEquals('html', $sut->getId());
$this->assertEquals('print', $sut->getTitle());
$this->assertEquals('print', $sut->getIcon());
}
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();
$sut = new HtmlRenderer($twig);
$response = $this->render($sut);
$content = $response->getContent();
$this->assertContains('<h2>List of expenses</h2>', $content);
$this->assertContains('<h3>Summary</h3>', $content);
$this->assertContains('<td>Customer Name</td>', $content);
$this->assertContains('<td>project name</td>', $content);
$this->assertContains('<td class="duration">01:50 h</td>', $content);
$this->assertContains('<td class="cost">2,437.12 €</td>', $content);
$this->assertEquals(5, substr_count($content, '<td>activity description</td>'));
}
}

View File

@@ -0,0 +1,51 @@
<?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\Export\Renderer;
use App\Export\Renderer\OdsRenderer;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
/**
* @covers \App\Export\Renderer\OdsRenderer
* @covers \App\Export\Renderer\AbstractSpreadsheetRenderer
* @covers \App\Export\Renderer\RendererTrait
*/
class OdsRendererTest extends AbstractRendererTest
{
public function testConfiguration()
{
$sut = $this->getAbstractRenderer(OdsRenderer::class);
$this->assertEquals('ods', $sut->getId());
$this->assertEquals('ods', $sut->getTitle());
$this->assertEquals('ods', $sut->getIcon());
}
public function testRender()
{
$sut = $this->getAbstractRenderer(OdsRenderer::class);
/** @var BinaryFileResponse $response */
$response = $this->render($sut);
$file = $response->getFile();
$this->assertEquals('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', $response->headers->get('Content-Type'));
$this->assertEquals('attachment; filename=kimai-export.ods', $response->headers->get('Content-Disposition'));
$this->assertTrue(file_exists($file->getRealPath()));
ob_start();
$response->sendContent();
$content2 = ob_get_clean();
$this->assertNotEmpty($content2);
$this->assertFalse(file_exists($file->getRealPath()));
}
}

View File

@@ -0,0 +1,55 @@
<?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\Export\Renderer;
use App\Export\Renderer\PDFRenderer;
use Symfony\Component\HttpFoundation\Request;
use Twig\Loader\FilesystemLoader;
/**
* @covers \App\Export\Renderer\PDFRenderer
* @covers \App\Export\Renderer\RendererTrait
*/
class PdfRendererTest extends AbstractRendererTest
{
public function testConfiguration()
{
$sut = new PDFRenderer(
$this->getMockBuilder(\Twig_Environment::class)->disableOriginalConstructor()->getMock()
);
$this->assertEquals('pdf', $sut->getId());
$this->assertEquals('pdf', $sut->getTitle());
$this->assertEquals('pdf', $sut->getIcon());
}
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();
$sut = new PDFRenderer($twig);
$response = $this->render($sut);
$this->assertEquals('application/pdf', $response->headers->get('Content-Type'));
$this->assertEquals('attachment; filename=kimai-export.pdf', $response->headers->get('Content-Disposition'));
$this->assertNotEmpty($response->getContent());
}
}

View File

@@ -0,0 +1,51 @@
<?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\Export\Renderer;
use App\Export\Renderer\XlsxRenderer;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
/**
* @covers \App\Export\Renderer\XlsxRenderer
* @covers \App\Export\Renderer\AbstractSpreadsheetRenderer
* @covers \App\Export\Renderer\RendererTrait
*/
class XlsxRendererTest extends AbstractRendererTest
{
public function testConfiguration()
{
$sut = $this->getAbstractRenderer(XlsxRenderer::class);
$this->assertEquals('xlsx', $sut->getId());
$this->assertEquals('xlsx', $sut->getTitle());
$this->assertEquals('xlsx', $sut->getIcon());
}
public function testRender()
{
$sut = $this->getAbstractRenderer(XlsxRenderer::class);
/** @var BinaryFileResponse $response */
$response = $this->render($sut);
$file = $response->getFile();
$this->assertEquals('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', $response->headers->get('Content-Type'));
$this->assertEquals('attachment; filename=kimai-export.xlsx', $response->headers->get('Content-Disposition'));
$this->assertTrue(file_exists($file->getRealPath()));
ob_start();
$response->sendContent();
$content2 = ob_get_clean();
$this->assertNotEmpty($content2);
$this->assertFalse(file_exists($file->getRealPath()));
}
}

View File

@@ -0,0 +1,43 @@
<?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\Export;
use App\Export\Renderer\HtmlRenderer;
use App\Export\ServiceExport;
use PHPUnit\Framework\TestCase;
/**
* @covers \App\Export\ServiceExport
*/
class ServiceExportTest extends TestCase
{
public function testEmptyObject()
{
$sut = new ServiceExport();
$this->assertEmpty($sut->getRenderer());
}
public function testUnknownRendererReturnsNull()
{
$sut = new ServiceExport();
$this->assertNull($sut->getRendererById('default'));
}
public function testAdd()
{
$sut = new ServiceExport();
$sut->addRenderer(new HtmlRenderer(
$this->getMockBuilder(\Twig_Environment::class)->disableOriginalConstructor()->getMock()
));
$this->assertEquals(1, count($sut->getRenderer()));
}
}

View File

@@ -17,6 +17,9 @@ use App\Invoice\ServiceInvoice;
use App\Repository\InvoiceDocumentRepository;
use PHPUnit\Framework\TestCase;
/**
* @covers \App\Invoice\ServiceInvoice
*/
class ServiceInvoiceTest extends TestCase
{
public function testEmptyObject()

View File

@@ -0,0 +1,131 @@
<?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\Repository\Query;
use App\Entity\Activity;
use App\Entity\Customer;
use App\Entity\Project;
use App\Entity\User;
use App\Repository\Query\ExportQuery;
/**
* @covers \App\Repository\Query\ExportQuery
*/
class ExportQueryTest extends BaseQueryTest
{
public function testQuery()
{
$sut = new ExportQuery();
$this->assertResultType($sut);
$this->assertHiddenEntity($sut);
$this->assertPage($sut);
$this->assertPageSize($sut);
$this->assertOrderBy($sut, 'begin');
$this->assertOrder($sut, ExportQuery::ORDER_DESC);
$this->assertUser($sut);
$this->assertCustomer($sut);
$this->assertProject($sut);
$this->assertActivity($sut);
$this->assertState($sut);
$this->assertExported($sut);
$this->assertType($sut);
}
protected function assertUser(ExportQuery $sut)
{
$this->assertNull($sut->getUser());
$expected = new User();
$expected->setUsername('foo-bar');
$sut->setUser($expected);
$this->assertEquals($expected, $sut->getUser());
}
protected function assertCustomer(ExportQuery $sut)
{
$this->assertNull($sut->getCustomer());
$expected = new Customer();
$expected->setName('foo-bar');
$sut->setCustomer($expected);
$this->assertEquals($expected, $sut->getCustomer());
}
protected function assertProject(ExportQuery $sut)
{
$this->assertNull($sut->getProject());
$expected = new Project();
$expected->setName('foo-bar');
$sut->setProject($expected);
$this->assertEquals($expected, $sut->getProject());
}
protected function assertActivity(ExportQuery $sut)
{
$this->assertNull($sut->getActivity());
$expected = new Activity();
$expected->setName('foo-bar');
$sut->setActivity($expected);
$this->assertEquals($expected, $sut->getActivity());
}
protected function assertState(ExportQuery $sut)
{
$this->assertEquals(ExportQuery::STATE_ALL, $sut->getState());
$sut->setState(PHP_INT_MAX);
$this->assertEquals(ExportQuery::STATE_ALL, $sut->getState());
$sut->setState(ExportQuery::STATE_STOPPED);
$this->assertEquals(ExportQuery::STATE_STOPPED, $sut->getState());
$sut->setState(ExportQuery::STATE_RUNNING);
$this->assertEquals(ExportQuery::STATE_RUNNING, $sut->getState());
$sut->setState(ExportQuery::STATE_ALL);
$this->assertEquals(ExportQuery::STATE_ALL, $sut->getState());
}
protected function assertExported(ExportQuery $sut)
{
$this->assertEquals(ExportQuery::STATE_ALL, $sut->getExported());
$sut->setExported(PHP_INT_MAX);
$this->assertEquals(ExportQuery::STATE_ALL, $sut->getExported());
$sut->setExported(ExportQuery::STATE_EXPORTED);
$this->assertEquals(ExportQuery::STATE_EXPORTED, $sut->getExported());
$sut->setExported(ExportQuery::STATE_NOT_EXPORTED);
$this->assertEquals(ExportQuery::STATE_NOT_EXPORTED, $sut->getExported());
$sut->setExported(ExportQuery::STATE_ALL);
$this->assertEquals(ExportQuery::STATE_ALL, $sut->getExported());
}
protected function assertType(ExportQuery $sut)
{
$this->assertNull($sut->getType());
$allowed = ['html', 'csv', 'pdf', 'xlsx', 'ods'];
foreach ($allowed as $type) {
$sut->setType($type);
$this->assertEquals($type, $sut->getType());
}
$sut->setType('foo');
$this->assertEquals('ods', $sut->getType());
}
}

View File

@@ -36,6 +36,7 @@ class TimesheetQueryTest extends BaseQueryTest
$this->assertProject($sut);
$this->assertActivity($sut);
$this->assertState($sut);
$this->assertExported($sut);
}
protected function assertUser(TimesheetQuery $sut)
@@ -94,4 +95,24 @@ class TimesheetQueryTest extends BaseQueryTest
$sut->setState(TimesheetQuery::STATE_ALL);
$this->assertEquals(TimesheetQuery::STATE_ALL, $sut->getState());
}
protected function assertExported(TimesheetQuery $sut)
{
$this->assertEquals(TimesheetQuery::STATE_ALL, $sut->getExported());
$sut->setExported(PHP_INT_MAX);
$this->assertEquals(TimesheetQuery::STATE_ALL, $sut->getExported());
$sut->setExported(TimesheetQuery::STATE_EXPORTED);
$this->assertEquals(TimesheetQuery::STATE_EXPORTED, $sut->getExported());
$sut->setExported(TimesheetQuery::STATE_NOT_EXPORTED);
$this->assertEquals(TimesheetQuery::STATE_NOT_EXPORTED, $sut->getExported());
$sut->setExported(TimesheetQuery::STATE_ALL);
$this->assertEquals(TimesheetQuery::STATE_ALL, $sut->getExported());
$sut->setExported('02');
$this->assertEquals(TimesheetQuery::STATE_ALL, $sut->getExported());
}
}

View File

@@ -40,7 +40,7 @@ class DateExtensionsTest extends TestCase
public function testGetFilters()
{
$filters = ['month_name', 'date_short'];
$filters = ['month_name', 'date_short', 'date_time'];
$sut = $this->getSut('de', []);
$twigFilters = $sut->getFilters();
$this->assertCount(count($filters), $twigFilters);
@@ -77,6 +77,29 @@ class DateExtensionsTest extends TestCase
];
}
/**
* @param string $locale
* @param \DateTime $date
* @param string $result
* @dataProvider getDateTimeData
*/
public function testDateTime($locale, \DateTime $date, $result)
{
$sut = $this->getSut($locale, [
'de' => ['date_time' => 'd.m.Y H:i:s'],
'en' => ['date_time' => 'Y-m-d h:m A'],
]);
$this->assertEquals($result, $sut->dateTime($date));
}
public function getDateTimeData()
{
return [
['en', new \DateTime('7 January 2010'), '2010-01-07 12:01 AM'],
['de', (new \DateTime('1980-12-14'))->setTime(13, 27, 55), '14.12.1980 13:27:55'],
];
}
/**
* @param \DateTime $date
* @param string $result

View File

@@ -19,7 +19,6 @@ use Symfony\Component\HttpFoundation\RequestStack;
*/
class LocaleSettingsTest extends TestCase
{
protected function getRequestStack(string $locale)
{
$request = new Request();
@@ -44,6 +43,7 @@ class LocaleSettingsTest extends TestCase
'date_type' => 'dd.MM.yyyy',
'date_picker' => 'DD.MM.YYYY',
'date' => 'd.m.Y',
'date_time' => 'd.m. H:i',
'duration' => '%h:%m h',
],
'en' => [
@@ -52,6 +52,7 @@ class LocaleSettingsTest extends TestCase
'date_type' => 'yyyy-MM-dd',
'date_picker' => 'YYYY-MM-DD',
'date' => 'Y-m-d',
'date_time' => 'm-d H:i',
'duration' => '%h:%m h',
],
'pt_BR' => [
@@ -162,6 +163,13 @@ class LocaleSettingsTest extends TestCase
$this->assertEquals('d.m.Y', $sut->getDateFormat('de'));
}
public function testGetDateTimeFormat()
{
$sut = $this->getSut('en', $this->getDefaultSettings());
$this->assertEquals('m-d H:i', $sut->getDateTimeFormat());
$this->assertEquals('d.m. H:i', $sut->getDateTimeFormat('de'));
}
public function testGetDateTypeFormat()
{
$sut = $this->getSut('en', $this->getDefaultSettings());