support custom fields for timesheets, customers, projects and activities (#871)

This commit is contained in:
Kevin Papst
2019-06-26 00:39:05 +02:00
committed by GitHub
parent 994c671fd8
commit d8621f0b7a
103 changed files with 2567 additions and 238 deletions

View File

@@ -220,10 +220,12 @@ class ActivityControllerTest extends APIControllerBaseTest
protected function assertStructure(array $result, $full = true)
{
$expectedKeys = ['id', 'name', 'visible', 'project', 'hourlyRate', 'fixedRate', 'color'];
$expectedKeys = ['id', 'name', 'visible', 'project', 'hourlyRate', 'fixedRate', 'color', 'metaFields'];
if ($full) {
$expectedKeys = array_merge($expectedKeys, ['comment', 'budget', 'timeBudget']);
$expectedKeys = array_merge($expectedKeys, [
'comment', 'budget', 'timeBudget'
]);
}
$actual = array_keys($result);

View File

@@ -163,7 +163,7 @@ class CustomerControllerTest extends APIControllerBaseTest
protected function assertStructure(array $result, $full = true)
{
$expectedKeys = ['id', 'name', 'visible', 'hourlyRate', 'fixedRate', 'color'];
$expectedKeys = ['id', 'name', 'visible', 'hourlyRate', 'fixedRate', 'color', 'metaFields'];
if ($full) {
$expectedKeys = array_merge($expectedKeys, [

View File

@@ -212,14 +212,13 @@ class ProjectControllerTest extends APIControllerBaseTest
protected function assertStructure(array $result, $full = true)
{
$expectedKeys = [
'id', 'name', 'visible', 'customer', 'hourlyRate', 'fixedRate', 'color'
'id', 'name', 'visible', 'customer', 'hourlyRate', 'fixedRate', 'color', 'metaFields'
];
if ($full) {
$expectedKeys = array_merge(
$expectedKeys,
['comment', 'budget', 'timeBudget', 'orderNumber']
);
$expectedKeys = array_merge($expectedKeys, [
'comment', 'budget', 'timeBudget', 'orderNumber'
]);
}
$actual = array_keys($result);

View File

@@ -819,7 +819,7 @@ class TimesheetControllerTest extends APIControllerBaseTest
protected function assertDefaultStructure(array $result, $full = true)
{
$expectedKeys = [
'id', 'begin', 'end', 'duration', 'description', 'rate', 'activity', 'project', 'tags', 'user'
'id', 'begin', 'end', 'duration', 'description', 'rate', 'activity', 'project', 'tags', 'user', 'metaFields'
];
if ($full) {

View File

@@ -15,6 +15,7 @@ use App\Entity\User;
use App\Tests\DataFixtures\ActivityFixtures;
use App\Tests\DataFixtures\ProjectFixtures;
use App\Tests\DataFixtures\TimesheetFixtures;
use App\Tests\Mocks\ActivityTestMetaFieldSubscriberMock;
use Doctrine\ORM\EntityManager;
/**
@@ -77,6 +78,18 @@ class ActivityControllerTest extends ControllerBaseTest
$this->assertEquals('1', $editForm->get('activity_edit_form[customer]')->getValue());
}
public function testCreateActionShowsMetaFields()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN);
$client->getContainer()->get('event_dispatcher')->addSubscriber(new ActivityTestMetaFieldSubscriberMock());
$this->assertAccessIsGranted($client, '/admin/activity/create');
$this->assertTrue($client->getResponse()->isSuccessful());
$form = $client->getCrawler()->filter('form[name=activity_edit_form]')->form();
$this->assertTrue($form->has('activity_edit_form[metaFields][0][value]'));
$this->assertFalse($form->has('activity_edit_form[metaFields][1][value]'));
}
public function testCreateActionWithCreateMore()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN);

View File

@@ -14,6 +14,7 @@ use App\Entity\Timesheet;
use App\Entity\User;
use App\Tests\DataFixtures\CustomerFixtures;
use App\Tests\DataFixtures\TimesheetFixtures;
use App\Tests\Mocks\CustomerTestMetaFieldSubscriberMock;
use Doctrine\ORM\EntityManager;
/**
@@ -77,6 +78,18 @@ class CustomerControllerTest extends ControllerBaseTest
$this->assertHasFlashSuccess($client);
}
public function testCreateActionShowsMetaFields()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN);
$client->getContainer()->get('event_dispatcher')->addSubscriber(new CustomerTestMetaFieldSubscriberMock());
$this->assertAccessIsGranted($client, '/admin/customer/create');
$this->assertTrue($client->getResponse()->isSuccessful());
$form = $client->getCrawler()->filter('form[name=customer_edit_form]')->form();
$this->assertTrue($form->has('customer_edit_form[metaFields][0][value]'));
$this->assertFalse($form->has('customer_edit_form[metaFields][1][value]'));
}
public function testEditAction()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN);

View File

@@ -15,6 +15,7 @@ use App\Entity\User;
use App\Tests\DataFixtures\CustomerFixtures;
use App\Tests\DataFixtures\ProjectFixtures;
use App\Tests\DataFixtures\TimesheetFixtures;
use App\Tests\Mocks\ProjectTestMetaFieldSubscriberMock;
use Doctrine\ORM\EntityManager;
/**
@@ -70,6 +71,18 @@ class ProjectControllerTest extends ControllerBaseTest
$this->assertHasFlashSuccess($client);
}
public function testCreateActionShowsMetaFields()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN);
$client->getContainer()->get('event_dispatcher')->addSubscriber(new ProjectTestMetaFieldSubscriberMock());
$this->assertAccessIsGranted($client, '/admin/project/create');
$this->assertTrue($client->getResponse()->isSuccessful());
$form = $client->getCrawler()->filter('form[name=project_edit_form]')->form();
$this->assertTrue($form->has('project_edit_form[metaFields][0][value]'));
$this->assertFalse($form->has('project_edit_form[metaFields][1][value]'));
}
public function testCreateActionWithCreateMore()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN);

View File

@@ -13,6 +13,7 @@ use App\Entity\Timesheet;
use App\Entity\User;
use App\Form\Type\DateRangeType;
use App\Tests\DataFixtures\TimesheetFixtures;
use App\Tests\Mocks\TimesheetTestMetaFieldSubscriberMock;
/**
* @group integration
@@ -144,6 +145,18 @@ class TimesheetControllerTest extends ControllerBaseTest
$this->assertNull($timesheet->getFixedRate());
}
public function testCreateActionShowsMetaFields()
{
$client = $this->getClientForAuthenticatedUser();
$client->getContainer()->get('event_dispatcher')->addSubscriber(new TimesheetTestMetaFieldSubscriberMock());
$this->request($client, '/timesheet/create');
$this->assertTrue($client->getResponse()->isSuccessful());
$form = $client->getCrawler()->filter('form[name=timesheet_edit_form]')->form();
$this->assertTrue($form->has('timesheet_edit_form[metaFields][0][value]'));
$this->assertFalse($form->has('timesheet_edit_form[metaFields][1][value]'));
}
public function testCreateActionDoesNotShowRateFieldsForUser()
{
$client = $this->getClientForAuthenticatedUser();

View File

@@ -0,0 +1,114 @@
<?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\Entity;
use App\Entity\EntityWithMetaFields;
use App\Entity\MetaTableTypeInterface;
use App\Form\Type\DateTimePickerType;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\NotNull;
abstract class AbstractMetaEntityTest extends TestCase
{
abstract protected function getEntity(): EntityWithMetaFields;
abstract protected function getMetaEntity(): MetaTableTypeInterface;
public function testDefaultValues()
{
$sut = $this->getMetaEntity();
self::assertNull($sut->getName());
self::assertNull($sut->getType());
self::assertNull($sut->getValue());
self::assertNull($sut->getEntity());
self::assertIsArray($sut->getConstraints());
self::assertEmpty($sut->getConstraints());
self::assertFalse($sut->isVisible());
self::assertFalse($sut->isRequired());
}
public function testSetterAndGetter()
{
$sut = $this->getMetaEntity();
self::assertInstanceOf(MetaTableTypeInterface::class, $sut->setName('foo-bar'));
self::assertEquals('foo-bar', $sut->getName());
self::assertInstanceOf(MetaTableTypeInterface::class, $sut->setisVisible(true));
self::assertTrue($sut->isVisible());
self::assertInstanceOf(MetaTableTypeInterface::class, $sut->setIsRequired(true));
self::assertTrue($sut->isRequired());
self::assertInstanceOf(MetaTableTypeInterface::class, $sut->setValue('hello world'));
self::assertEquals('hello world', $sut->getValue());
self::assertInstanceOf(MetaTableTypeInterface::class, $sut->setValue(956.32));
self::assertEquals(956.32, $sut->getValue());
self::assertInstanceOf(MetaTableTypeInterface::class, $sut->setType(DateTimePickerType::class));
self::assertEquals(DateTimePickerType::class, $sut->getType());
self::assertInstanceOf(MetaTableTypeInterface::class, $sut->addConstraint(new Length(['max' => 10])));
self::assertInstanceOf(MetaTableTypeInterface::class, $sut->addConstraint(new NotNull([])));
self::assertInstanceOf(MetaTableTypeInterface::class, $sut->addConstraint(new NotBlank([])));
self::assertCount(3, $sut->getConstraints());
self::assertInstanceOf(MetaTableTypeInterface::class, $sut->setConstraints([new Length(['min' => 2])]));
self::assertCount(1, $sut->getConstraints());
$entity = $this->getEntity();
self::assertInstanceOf(MetaTableTypeInterface::class, $sut->setEntity($entity));
self::assertSame($entity, $sut->getEntity());
}
public function testMerge()
{
$entity1 = $this->getEntity();
$entity2 = $this->getEntity();
$meta1 = $this->getMetaEntity();
$meta1
->setName('foo')
->setValue('bar')
->setType('blub')
->setEntity($entity1)
->setConstraints([new NotNull()])
;
self::assertEquals('foo', $meta1->getName());
self::assertEquals('bar', $meta1->getValue());
self::assertEquals('blub', $meta1->getType());
self::assertFalse($meta1->isRequired());
self::assertFalse($meta1->isVisible());
self::assertSame($entity1, $meta1->getEntity());
self::assertCount(1, $meta1->getConstraints());
$meta2 = $this->getMetaEntity();
$meta2
->setName('foo2')
->setValue('bar2')
->setType('blub2')
->setEntity($entity2)
->setIsRequired(true)
->setisVisible(true)
->setConstraints([new NotBlank(), new Length(['min' => 1])])
;
self::assertInstanceOf(MetaTableTypeInterface::class, $meta1->merge($meta2));
self::assertEquals('foo', $meta1->getName());
self::assertEquals('bar', $meta1->getValue());
self::assertEquals('blub2', $meta1->getType());
self::assertTrue($meta1->isRequired());
self::assertTrue($meta1->isVisible());
self::assertSame($entity1, $meta1->getEntity());
self::assertCount(2, $meta1->getConstraints());
}
}

View File

@@ -0,0 +1,42 @@
<?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\Entity;
use App\Entity\Activity;
use App\Entity\ActivityMeta;
use App\Entity\EntityWithMetaFields;
use App\Entity\MetaTableTypeInterface;
use App\Entity\Timesheet;
/**
* @covers \App\Entity\ActivityMeta
*/
class ActivityMetaTest extends AbstractMetaEntityTest
{
protected function getEntity(): EntityWithMetaFields
{
return new Activity();
}
protected function getMetaEntity(): MetaTableTypeInterface
{
return new ActivityMeta();
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Expected instanceof Activity, received "App\Entity\Timesheet"
*/
public function testSetEntityThrowsException()
{
$sut = new ActivityMeta();
$sut->setEntity(new Timesheet());
}
}

View File

@@ -10,6 +10,8 @@
namespace App\Tests\Entity;
use App\Entity\Activity;
use App\Entity\ActivityMeta;
use Doctrine\Common\Collections\Collection;
use PHPUnit\Framework\TestCase;
/**
@@ -31,6 +33,9 @@ class ActivityTest extends TestCase
$this->assertNull($sut->getColor());
$this->assertEquals(0.0, $sut->getBudget());
$this->assertEquals(0, $sut->getTimeBudget());
$this->assertInstanceOf(Collection::class, $sut->getMetaFields());
$this->assertEquals(0, $sut->getMetaFields()->count());
$this->assertNull($sut->getMetaField('foo'));
}
public function testSetterAndGetter()
@@ -61,4 +66,31 @@ class ActivityTest extends TestCase
$this->assertInstanceOf(Activity::class, $sut->setTimeBudget(937321));
$this->assertEquals(937321, $sut->getTimeBudget());
}
public function testMetaFields()
{
$sut = new Activity();
$meta = new ActivityMeta();
$meta->setName('foo')->setValue('bar')->setType('test');
$this->assertInstanceOf(Activity::class, $sut->setMetaField($meta));
self::assertEquals(1, $sut->getMetaFields()->count());
$result = $sut->getMetaField('foo');
self::assertSame($result, $meta);
self::assertEquals('test', $result->getType());
$meta2 = new ActivityMeta();
$meta2->setName('foo')->setValue('bar')->setType('test2');
$this->assertInstanceOf(Activity::class, $sut->setMetaField($meta2));
self::assertEquals(1, $sut->getMetaFields()->count());
self::assertCount(0, $sut->getVisibleMetaFields());
$result = $sut->getMetaField('foo');
self::assertSame($result, $meta);
self::assertEquals('test2', $result->getType());
$sut->setMetaField((new ActivityMeta())->setName('blub')->setIsVisible(true));
$sut->setMetaField((new ActivityMeta())->setName('blab')->setIsVisible(true));
self::assertEquals(3, $sut->getMetaFields()->count());
self::assertCount(2, $sut->getVisibleMetaFields());
}
}

View File

@@ -0,0 +1,42 @@
<?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\Entity;
use App\Entity\Activity;
use App\Entity\Customer;
use App\Entity\CustomerMeta;
use App\Entity\EntityWithMetaFields;
use App\Entity\MetaTableTypeInterface;
/**
* @covers \App\Entity\CustomerMeta
*/
class CustomerMetaTest extends AbstractMetaEntityTest
{
protected function getEntity(): EntityWithMetaFields
{
return new Customer();
}
protected function getMetaEntity(): MetaTableTypeInterface
{
return new CustomerMeta();
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Expected instanceof Customer, received "App\Entity\Activity"
*/
public function testSetEntityThrowsException()
{
$sut = new CustomerMeta();
$sut->setEntity(new Activity());
}
}

View File

@@ -10,6 +10,8 @@
namespace App\Tests\Entity;
use App\Entity\Customer;
use App\Entity\CustomerMeta;
use Doctrine\Common\Collections\Collection;
use PHPUnit\Framework\TestCase;
/**
@@ -46,6 +48,9 @@ class CustomerTest extends TestCase
$this->assertNull($sut->getColor());
$this->assertEquals(0.0, $sut->getBudget());
$this->assertEquals(0, $sut->getTimeBudget());
$this->assertInstanceOf(Collection::class, $sut->getMetaFields());
$this->assertEquals(0, $sut->getMetaFields()->count());
$this->assertNull($sut->getMetaField('foo'));
}
public function testSetterAndGetter()
@@ -97,4 +102,31 @@ class CustomerTest extends TestCase
$this->assertInstanceOf(Customer::class, $sut->setTimeBudget(937321));
$this->assertEquals(937321, $sut->getTimeBudget());
}
public function testMetaFields()
{
$sut = new Customer();
$meta = new CustomerMeta();
$meta->setName('foo')->setValue('bar')->setType('test');
$this->assertInstanceOf(Customer::class, $sut->setMetaField($meta));
self::assertEquals(1, $sut->getMetaFields()->count());
$result = $sut->getMetaField('foo');
self::assertSame($result, $meta);
self::assertEquals('test', $result->getType());
$meta2 = new CustomerMeta();
$meta2->setName('foo')->setValue('bar')->setType('test2');
$this->assertInstanceOf(Customer::class, $sut->setMetaField($meta2));
self::assertEquals(1, $sut->getMetaFields()->count());
self::assertCount(0, $sut->getVisibleMetaFields());
$result = $sut->getMetaField('foo');
self::assertSame($result, $meta);
self::assertEquals('test2', $result->getType());
$sut->setMetaField((new CustomerMeta())->setName('blub')->setIsVisible(true));
$sut->setMetaField((new CustomerMeta())->setName('blab')->setIsVisible(true));
self::assertEquals(3, $sut->getMetaFields()->count());
self::assertCount(2, $sut->getVisibleMetaFields());
}
}

View File

@@ -0,0 +1,42 @@
<?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\Entity;
use App\Entity\Customer;
use App\Entity\EntityWithMetaFields;
use App\Entity\MetaTableTypeInterface;
use App\Entity\Project;
use App\Entity\ProjectMeta;
/**
* @covers \App\Entity\ProjectMeta
*/
class ProjectMetaTest extends AbstractMetaEntityTest
{
protected function getEntity(): EntityWithMetaFields
{
return new Project();
}
protected function getMetaEntity(): MetaTableTypeInterface
{
return new ProjectMeta();
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Expected instanceof Project, received "App\Entity\Customer"
*/
public function testSetEntityThrowsException()
{
$sut = new ProjectMeta();
$sut->setEntity(new Customer());
}
}

View File

@@ -11,6 +11,8 @@ namespace App\Tests\Entity;
use App\Entity\Customer;
use App\Entity\Project;
use App\Entity\ProjectMeta;
use Doctrine\Common\Collections\Collection;
use PHPUnit\Framework\TestCase;
/**
@@ -36,6 +38,9 @@ class ProjectTest extends TestCase
$this->assertNull($sut->getColor());
$this->assertEquals(0.0, $sut->getBudget());
$this->assertEquals(0, $sut->getTimeBudget());
$this->assertInstanceOf(Collection::class, $sut->getMetaFields());
$this->assertEquals(0, $sut->getMetaFields()->count());
$this->assertNull($sut->getMetaField('foo'));
}
public function testSetterAndGetter()
@@ -73,4 +78,31 @@ class ProjectTest extends TestCase
$this->assertInstanceOf(Project::class, $sut->setTimeBudget(937321));
$this->assertEquals(937321, $sut->getTimeBudget());
}
public function testMetaFields()
{
$sut = new Project();
$meta = new ProjectMeta();
$meta->setName('foo')->setValue('bar')->setType('test');
$this->assertInstanceOf(Project::class, $sut->setMetaField($meta));
self::assertEquals(1, $sut->getMetaFields()->count());
$result = $sut->getMetaField('foo');
self::assertSame($result, $meta);
self::assertEquals('test', $result->getType());
$meta2 = new ProjectMeta();
$meta2->setName('foo')->setValue('bar')->setType('test2');
$this->assertInstanceOf(Project::class, $sut->setMetaField($meta2));
self::assertEquals(1, $sut->getMetaFields()->count());
self::assertCount(0, $sut->getVisibleMetaFields());
$result = $sut->getMetaField('foo');
self::assertSame($result, $meta);
self::assertEquals('test2', $result->getType());
$sut->setMetaField((new ProjectMeta())->setName('blub')->setIsVisible(true));
$sut->setMetaField((new ProjectMeta())->setName('blab')->setIsVisible(true));
self::assertEquals(3, $sut->getMetaFields()->count());
self::assertCount(2, $sut->getVisibleMetaFields());
}
}

View File

@@ -0,0 +1,42 @@
<?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\Entity;
use App\Entity\EntityWithMetaFields;
use App\Entity\MetaTableTypeInterface;
use App\Entity\Project;
use App\Entity\Timesheet;
use App\Entity\TimesheetMeta;
/**
* @covers \App\Entity\TimesheetMeta
*/
class TimesheetMetaTest extends AbstractMetaEntityTest
{
protected function getEntity(): EntityWithMetaFields
{
return new Timesheet();
}
protected function getMetaEntity(): MetaTableTypeInterface
{
return new TimesheetMeta();
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Expected instanceof Timesheet, received "App\Entity\Project"
*/
public function testSetEntityThrowsException()
{
$sut = new TimesheetMeta();
$sut->setEntity(new Project());
}
}

View File

@@ -14,8 +14,10 @@ use App\Entity\Customer;
use App\Entity\Project;
use App\Entity\Tag;
use App\Entity\Timesheet;
use App\Entity\TimesheetMeta;
use App\Entity\User;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use PHPUnit\Framework\TestCase;
/**
@@ -39,11 +41,13 @@ class TimesheetTest extends TestCase
$this->assertNull($sut->getHourlyRate());
$this->assertEquals(new ArrayCollection(), $sut->getTags());
$this->assertEquals([], $sut->getTagsAsArray());
$this->assertInstanceOf(Timesheet::class, $sut->setFixedRate(13.47));
$this->assertEquals(13.47, $sut->getFixedRate());
$this->assertInstanceOf(Timesheet::class, $sut->setHourlyRate(99));
$this->assertEquals(99, $sut->getHourlyRate());
$this->assertInstanceOf(Collection::class, $sut->getMetaFields());
$this->assertEquals(0, $sut->getMetaFields()->count());
$this->assertNull($sut->getMetaField('foo'));
}
protected function getEntity()
@@ -89,4 +93,31 @@ class TimesheetTest extends TestCase
$sut->removeTag($tag1);
$this->assertEmpty($sut->getTags());
}
public function testMetaFields()
{
$sut = new Timesheet();
$meta = new TimesheetMeta();
$meta->setName('foo')->setValue('bar')->setType('test');
$this->assertInstanceOf(Timesheet::class, $sut->setMetaField($meta));
self::assertEquals(1, $sut->getMetaFields()->count());
$result = $sut->getMetaField('foo');
self::assertSame($result, $meta);
self::assertEquals('test', $result->getType());
$meta2 = new TimesheetMeta();
$meta2->setName('foo')->setValue('bar')->setType('test2');
$this->assertInstanceOf(Timesheet::class, $sut->setMetaField($meta2));
self::assertEquals(1, $sut->getMetaFields()->count());
self::assertCount(0, $sut->getVisibleMetaFields());
$result = $sut->getMetaField('foo');
self::assertSame($result, $meta);
self::assertEquals('test2', $result->getType());
$sut->setMetaField((new TimesheetMeta())->setName('blub')->setIsVisible(true));
$sut->setMetaField((new TimesheetMeta())->setName('blab')->setIsVisible(true));
self::assertEquals(3, $sut->getMetaFields()->count());
self::assertCount(2, $sut->getVisibleMetaFields());
}
}

View File

@@ -0,0 +1,27 @@
<?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\Event;
use App\Entity\Activity;
use App\Event\ActivityMetaDefinitionEvent;
use PHPUnit\Framework\TestCase;
/**
* @covers \App\Event\ActivityMetaDefinitionEvent
*/
class ActivityMetaDefinitionEventTest extends TestCase
{
public function testGetterAndSetter()
{
$activity = new Activity();
$sut = new ActivityMetaDefinitionEvent($activity);
$this->assertSame($activity, $sut->getEntity());
}
}

View File

@@ -0,0 +1,27 @@
<?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\Event;
use App\Entity\Customer;
use App\Event\CustomerMetaDefinitionEvent;
use PHPUnit\Framework\TestCase;
/**
* @covers \App\Event\CustomerMetaDefinitionEvent
*/
class CustomerMetaDefinitionEventTest extends TestCase
{
public function testGetterAndSetter()
{
$customer = new Customer();
$sut = new CustomerMetaDefinitionEvent($customer);
$this->assertSame($customer, $sut->getEntity());
}
}

View File

@@ -0,0 +1,28 @@
<?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\Event;
use App\Entity\User;
use App\Event\PrepareUserEvent;
use PHPUnit\Framework\TestCase;
/**
* @covers \App\Event\PrepareUserEvent
*/
class PrepareUserEventTest extends TestCase
{
public function testGetterAndSetter()
{
$user = new User();
$sut = new PrepareUserEvent($user);
$this->assertEquals('app.prepare_user', PrepareUserEvent::PREPARE);
$this->assertSame($user, $sut->getUser());
}
}

View File

@@ -0,0 +1,27 @@
<?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\Event;
use App\Entity\Project;
use App\Event\ProjectMetaDefinitionEvent;
use PHPUnit\Framework\TestCase;
/**
* @covers \App\Event\ProjectMetaDefinitionEvent
*/
class ProjectMetaDefinitionEventTest extends TestCase
{
public function testGetterAndSetter()
{
$project = new Project();
$sut = new ProjectMetaDefinitionEvent($project);
$this->assertSame($project, $sut->getEntity());
}
}

View File

@@ -0,0 +1,27 @@
<?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\Event;
use App\Entity\Timesheet;
use App\Event\TimesheetMetaDefinitionEvent;
use PHPUnit\Framework\TestCase;
/**
* @covers \App\Event\TimesheetMetaDefinitionEvent
*/
class TimesheetMetaDefinitionEventTest extends TestCase
{
public function testGetterAndSetter()
{
$timesheet = new Timesheet();
$sut = new TimesheetMetaDefinitionEvent($timesheet);
$this->assertSame($timesheet, $sut->getEntity());
}
}

View File

@@ -15,6 +15,7 @@ use App\Entity\Customer;
use App\Entity\Project;
use App\Entity\Tag;
use App\Entity\Timesheet;
use App\Entity\TimesheetMeta;
use App\Entity\User;
use App\Export\RendererInterface;
use App\Repository\Query\TimesheetQuery;
@@ -139,6 +140,8 @@ abstract class AbstractRendererTest extends KernelTestCase
->setEnd(new \DateTime('2019-06-16 12:06:40'))
->addTag((new Tag())->setName('foo'))
->addTag((new Tag())->setName('bar'))
->setMetaField((new TimesheetMeta())->setName('foo')->setValue('meta-bar')->setIsVisible(true))
->setMetaField((new TimesheetMeta())->setName('foo2')->setValue('meta-bar2')->setIsVisible(true))
;
$entries = [$timesheet, $timesheet2, $timesheet3, $timesheet4, $timesheet5];

View File

@@ -74,9 +74,6 @@ class CsvRendererTest extends AbstractRendererTest
foreach ($rows as $row) {
$all[] = str_getcsv($row);
}
self::assertEquals(7, count($all));
self::assertEquals(13, count($all[0]));
self::assertEquals('foo', $all[4][8]);
$expected = [
0 => '2019.06.16 12:00',
@@ -88,12 +85,18 @@ class CsvRendererTest extends AbstractRendererTest
6 => '',
7 => '',
8 => 'foo,bar',
9 => '€0.00',
10 => '€84.00',
11 => '00:06 h',
12 => '€0.00',
9 => 'meta-bar',
10 => 'meta-bar2',
11 => '€0.00',
12 => '€84.00',
13 => '00:06 h',
14 => '€0.00',
];
self::assertEquals(7, count($all));
self::assertEquals(count($expected), count($all[0]));
self::assertEquals('foo', $all[4][8]);
self::assertEquals($expected, $all[5]);
}
}

View File

@@ -20,14 +20,14 @@ class DebugRendererTest extends TestCase
public function getTestModel()
{
yield [$this->getInvoiceModel(), '1,947.99', 5, 5, 1, 2, 2, true];
yield [$this->getInvoiceModelOneEntry(), '293.27', 1, 1, 0, 1, 0, false];
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->getInvoiceModelOneEntry(), '293.27', 1, 1, 0, 1, 0, false, []];
}
/**
* @dataProvider getTestModel
*/
public function testRender(InvoiceModel $model, $expectedRate, $expectedRows, $expectedDescriptions, $expectedUser1, $expectedUser2, $expectedUser3, $hasProject)
public function testRender(InvoiceModel $model, $expectedRate, $expectedRows, $expectedDescriptions, $expectedUser1, $expectedUser2, $expectedUser3, $hasProject, $metaFields = [])
{
$document = new InvoiceDocument(new \SplFileInfo(__DIR__ . '/DebugRenderer.php'));
$sut = new DebugRenderer();
@@ -39,14 +39,16 @@ class DebugRendererTest extends TestCase
$rows = $data['entries'];
$this->assertEquals($expectedRows, count($rows));
$i = 0;
foreach ($rows as $row) {
$this->assertEntryStructure($row);
$meta = isset($metaFields[$i]) ? $metaFields[$i++] : [];
$this->assertEntryStructure($row, $meta);
}
// TODO check values or formats?
}
protected function assertModelStructure(array $model, $hasProject = true)
protected function assertModelStructure(array $model, $hasProject = true, $hasActivity = false)
{
$keys = [
'invoice.due_date',
@@ -77,6 +79,11 @@ class DebugRendererTest extends TestCase
'customer.number',
'customer.homepage',
'customer.comment',
'customer.meta.foo-customer',
'activity.id',
'activity.name',
'activity.comment',
'activity.meta.foo-activity',
];
if ($hasProject) {
@@ -85,6 +92,15 @@ class DebugRendererTest extends TestCase
'project.name',
'project.comment',
'project.order_number',
'project.meta.foo-project',
]);
}
if ($hasActivity) {
$keys = array_merge($keys, [
'activity.id',
'activity.name',
'activity.comment',
]);
}
@@ -95,7 +111,7 @@ class DebugRendererTest extends TestCase
$this->assertEquals($keys, $givenKeys);
}
protected function assertEntryStructure(array $model)
protected function assertEntryStructure(array $model, array $metaFields)
{
$keys = [
'entry.row',
@@ -125,6 +141,8 @@ class DebugRendererTest extends TestCase
'entry.customer_id',
];
$keys = array_merge($keys, $metaFields);
foreach ($keys as $key) {
$this->assertArrayHasKey($key, $model);
}
@@ -134,7 +152,7 @@ class DebugRendererTest extends TestCase
$givenKeys = array_keys($model);
sort($givenKeys);
$this->assertEquals(count($keys), count($givenKeys));
$this->assertEquals($expectedKeys, $givenKeys);
$this->assertEquals(count($keys), count($givenKeys));
}
}

View File

@@ -11,11 +11,15 @@ namespace App\Tests\Invoice\Renderer;
use App\Configuration\LanguageFormattings;
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;
use App\Entity\Timesheet;
use App\Entity\TimesheetMeta;
use App\Entity\User;
use App\Invoice\Calculator\DefaultCalculator;
use App\Invoice\NumberGenerator\DateNumberGenerator;
@@ -85,6 +89,8 @@ trait RendererTestTrait
{
$customer = new Customer();
$customer->setCurrency('EUR');
$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);
@@ -92,10 +98,12 @@ trait RendererTestTrait
$project = new Project();
$project->setName('project name');
$project->setCustomer($customer);
$project->setMetaField((new ProjectMeta())->setName('foo-project')->setValue('bar-project')->setIsVisible(true));
$activity = new Activity();
$activity->setName('activity description');
$activity->setProject($project);
$activity->setMetaField((new ActivityMeta())->setName('foo-activity')->setValue('bar-activity')->setIsVisible(true));
$userMethods = ['getId', 'getPreferenceValue', 'getUsername'];
$user1 = $this->getMockBuilder(User::class)->setMethods($userMethods)->disableOriginalConstructor()->getMock();
@@ -116,7 +124,7 @@ trait RendererTestTrait
->setProject($project)
->setBegin(new \DateTime())
->setEnd(new \DateTime())
;
->setMetaField((new TimesheetMeta())->setName('foo-timesheet')->setValue('bar-timesheet')->setIsVisible(true));
$timesheet2 = new Timesheet();
$timesheet2
@@ -127,6 +135,8 @@ trait RendererTestTrait
->setProject($project)
->setBegin(new \DateTime())
->setEnd(new \DateTime())
->setMetaField((new TimesheetMeta())->setName('foo-timesheet')->setValue('bar-timesheet'))
->setMetaField((new TimesheetMeta())->setName('foo-timesheet2')->setValue('bar-timesheet2')->setIsVisible(true))
;
$timesheet3 = new Timesheet();
@@ -138,6 +148,7 @@ trait RendererTestTrait
->setProject($project)
->setBegin(new \DateTime())
->setEnd(new \DateTime())
->setMetaField((new TimesheetMeta())->setName('foo-timesheet')->setValue('bar-timesheet1')->setIsVisible(true))
;
$timesheet4 = new Timesheet();
@@ -149,6 +160,7 @@ trait RendererTestTrait
->setProject($project)
->setBegin(new \DateTime())
->setEnd(new \DateTime())
->setMetaField((new TimesheetMeta())->setName('foo-timesheet3')->setValue('bluuuub')->setIsVisible(true))
;
$timesheet5 = new Timesheet();
@@ -193,6 +205,8 @@ trait RendererTestTrait
{
$customer = new Customer();
$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);
@@ -200,10 +214,12 @@ trait RendererTestTrait
$project = new Project();
$project->setName('project name');
$project->setCustomer($customer);
$project->setMetaField((new ProjectMeta())->setName('foo-project')->setValue('bar-project')->setIsVisible(true));
$activity = new Activity();
$activity->setName('activity description');
$activity->setProject($project);
$activity->setMetaField((new ActivityMeta())->setName('foo-activity')->setValue('bar-activity')->setIsVisible(true));
$userMethods = ['getId', 'getPreferenceValue', 'getUsername'];
$user1 = $this->getMockBuilder(User::class)->setMethods($userMethods)->disableOriginalConstructor()->getMock();

View File

@@ -0,0 +1,37 @@
<?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\Mocks;
use App\Entity\ActivityMeta;
use App\Event\ActivityMetaDefinitionEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Validator\Constraints\Length;
class ActivityTestMetaFieldSubscriberMock implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
ActivityMetaDefinitionEvent::class => ['loadMeta', 200],
];
}
public function loadMeta(ActivityMetaDefinitionEvent $event)
{
$definition = (new ActivityMeta())
->setName('metatestmock')
->setType(TextType::class)
->addConstraint(new Length(['max' => 200]))
->setIsVisible(true);
$event->getEntity()->setMetaField($definition);
}
}

View File

@@ -0,0 +1,37 @@
<?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\Mocks;
use App\Entity\CustomerMeta;
use App\Event\CustomerMetaDefinitionEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Validator\Constraints\Length;
class CustomerTestMetaFieldSubscriberMock implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
CustomerMetaDefinitionEvent::class => ['loadMeta', 200],
];
}
public function loadMeta(CustomerMetaDefinitionEvent $event)
{
$definition = (new CustomerMeta())
->setName('metatestmock')
->setType(TextType::class)
->addConstraint(new Length(['max' => 200]))
->setIsVisible(true);
$event->getEntity()->setMetaField($definition);
}
}

View File

@@ -0,0 +1,37 @@
<?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\Mocks;
use App\Entity\ProjectMeta;
use App\Event\ProjectMetaDefinitionEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Validator\Constraints\Length;
class ProjectTestMetaFieldSubscriberMock implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
ProjectMetaDefinitionEvent::class => ['loadMeta', 200],
];
}
public function loadMeta(ProjectMetaDefinitionEvent $event)
{
$definition = (new ProjectMeta())
->setName('metatestmock')
->setType(TextType::class)
->addConstraint(new Length(['max' => 200]))
->setIsVisible(true);
$event->getEntity()->setMetaField($definition);
}
}

View File

@@ -0,0 +1,37 @@
<?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\Mocks;
use App\Entity\TimesheetMeta;
use App\Event\TimesheetMetaDefinitionEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Validator\Constraints\Length;
class TimesheetTestMetaFieldSubscriberMock implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
TimesheetMetaDefinitionEvent::class => ['loadMeta', 200],
];
}
public function loadMeta(TimesheetMetaDefinitionEvent $event)
{
$definition = (new TimesheetMeta())
->setName('metatestmock')
->setType(TextType::class)
->addConstraint(new Length(['max' => 200]))
->setIsVisible(true);
$event->getEntity()->setMetaField($definition);
}
}

View File

@@ -13,14 +13,14 @@ use App\Configuration\TimesheetConfiguration;
use App\Entity\Timesheet;
use App\Tests\Configuration\TestConfigLoader;
use App\Tests\Mocks\Security\UserDateTimeFactoryFactory;
use App\Timesheet\TrackingMode\DurationFixedStartMode;
use App\Timesheet\TrackingMode\DurationFixedBeginMode;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
/**
* @covers \App\Timesheet\TrackingMode\DurationFixedStartMode
* @covers \App\Timesheet\TrackingMode\DurationFixedBeginMode
*/
class DurationFixedStartModeTest extends TestCase
class DurationFixedBeginModeTest extends TestCase
{
protected function createSut()
{
@@ -28,7 +28,7 @@ class DurationFixedStartModeTest extends TestCase
$dateTime = (new UserDateTimeFactoryFactory($this))->create();
$configuration = new TimesheetConfiguration($loader, ['default_begin' => '13:47']);
return new DurationFixedStartMode($dateTime, $configuration);
return new DurationFixedBeginMode($dateTime, $configuration);
}
public function testDefaultValues()
@@ -40,7 +40,7 @@ class DurationFixedStartModeTest extends TestCase
self::assertTrue($sut->canEditDuration());
self::assertFalse($sut->canUpdateTimesWithAPI());
self::assertFalse($sut->canSeeBeginAndEndTimes());
self::assertEquals('duration_fixed_start', $sut->getId());
self::assertEquals('duration_fixed_begin', $sut->getId());
}
public function testCreate()
@@ -54,4 +54,14 @@ class DurationFixedStartModeTest extends TestCase
$sut->create($timesheet, $request);
self::assertEquals('13:47', $timesheet->getBegin()->format('H:i'));
}
public function testCreateWithoutBeginInjectsBegin()
{
$timesheet = new Timesheet();
$request = new Request();
$sut = $this->createSut();
$sut->create($timesheet, $request);
self::assertEquals('13:47', $timesheet->getBegin()->format('H:i'));
}
}

View File

@@ -40,7 +40,7 @@ class TrackingModeServiceTest extends TestCase
self::assertContains('default', $ids);
self::assertContains('punch', $ids);
self::assertContains('duration_only', $ids);
self::assertContains('duration_fixed_start', $ids);
self::assertContains('duration_fixed_begin', $ids);
}
public function testGetActiveMode()