Skip to content

Commit

Permalink
Added validation for end date before start date (#167)
Browse files Browse the repository at this point in the history
* added validation for end date before start date
* added unit test for new validations
  • Loading branch information
kevinpapst authored Jun 20, 2018
1 parent 071abc8 commit 8c686b0
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/Entity/Timesheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use App\Entity\User;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;

/**
* Timesheet entity.
Expand Down Expand Up @@ -57,6 +58,7 @@ class Timesheet
* @var integer
*
* @ORM\Column(name="duration", type="integer", nullable=true)
* @Assert\GreaterThanOrEqual(0)
*/
private $duration = 0;

Expand Down Expand Up @@ -252,4 +254,20 @@ public function getRate()
{
return $this->rate;
}

/**
* @param ExecutionContextInterface $context
* @param mixed $payload
*
* @Assert\Callback
*/
public function validate(ExecutionContextInterface $context, $payload)
{
if ($this->getEnd() !== null && $this->getEnd()->getTimestamp() < $this->getBegin()->getTimestamp()) {
$context->buildViolation('End date must not be earlier then start date.')
->atPath('end')
->setTranslationDomain('validators')
->addViolation();
}
}
}
128 changes: 128 additions & 0 deletions tests/Entity/TimesheetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?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\Twig;

use App\Entity\Activity;
use App\Entity\Customer;
use App\Entity\Project;
use App\Entity\Timesheet;
use App\Entity\User;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\ConstraintViolationInterface;
use Symfony\Component\Validator\ConstraintViolationListInterface;
use Symfony\Component\Validator\Validation;

/**
* @covers \App\Entity\Timesheet
*/
class TimesheetTest extends TestCase
{

protected function getEntity()
{
$customer = new Customer();
$customer->setName('Test Customer');

$project = new Project();
$project->setName('Test Project');
$project->setCustomer($customer);

$activity = new Activity();
$activity->setName('Test');
$activity->setProject($project);

$entity = new Timesheet();
$entity->setUser(new User());
$entity->setActivity($activity);

return $entity;
}

public function testValidationEndNotEarlierThanBegin()
{
$entity = $this->getEntity();
$begin = new \DateTime();
$end = clone $begin;
$end = $end->modify('-1 second');
$entity->setBegin($begin);
$entity->setEnd($end);

$this->assertHasViolationForField($entity, 'end');

// allow same begin and end
$entity = $this->getEntity();
$begin = new \DateTime();
$end = clone $begin;
$entity->setBegin($begin);
$entity->setEnd($end);

$this->assertHasViolationForField($entity, []);
}

public function testDurationMustBeGreatorOrEqualThanZero()
{
$entity = $this->getEntity();
$begin = new \DateTime();
$end = clone $begin;
$entity->setBegin($begin);
$entity->setEnd($end);
$entity->setDuration(-1);

$this->assertHasViolationForField($entity, 'duration');

// allow zero duration
$entity = $this->getEntity();
$begin = new \DateTime();
$end = clone $begin;
$entity->setBegin($begin);
$entity->setEnd($end);
$entity->setDuration(0);

$this->assertHasViolationForField($entity, []);
}

/**
* @param $value
* @param array|string $fieldNames
*/
protected function assertHasViolationForField($value, $fieldNames)
{
$validator = Validation::createValidatorBuilder()->enableAnnotationMapping()->getValidator();
$validations = $validator->validate($value);

if (!is_array($fieldNames)) {
$fieldNames = [$fieldNames];
}

$violatedFields = [];
/** @var ConstraintViolationInterface $validation */
foreach($validations as $validation) {
$violatedFields[] = $validation->getPropertyPath();
}


foreach($fieldNames as $id => $propertyPath) {
$foundField = false;
if (in_array($propertyPath, $violatedFields)) {
$foundField = true;
unset($violatedFields[$id]);
}

$this->assertTrue($foundField, 'Failed finding violation for field: ' . $propertyPath);
}

$this->assertEmpty($violatedFields, sprintf('Unexpected violations found: %s', implode(', ', $violatedFields)));

$expected = count($fieldNames);
$actual = $validations->count();

$this->assertEquals($expected, $actual, sprintf('Expected %s violations, found %s.', $expected, $actual));
}
}
4 changes: 4 additions & 0 deletions translations/validators.de.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
<source>This value is not a valid role.</source>
<target>Dieser Wert ist keine gültige Rolle.</target>
</trans-unit>
<trans-unit id="End date must not be earlier then start date.">
<source>End date must not be earlier then start date.</source>
<target>Das Enddatum darf nicht vor dem Startdatum liegen.</target>
</trans-unit>
</body>
</file>
</xliff>
4 changes: 4 additions & 0 deletions translations/validators.en.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
<source>This value is not a valid role.</source>
<target>This value is not a valid role.</target>
</trans-unit>
<trans-unit id="End date must not be earlier then start date.">
<source>End date must not be earlier then start date.</source>
<target>End date must not be earlier then start date.</target>
</trans-unit>
</body>
</file>
</xliff>
4 changes: 4 additions & 0 deletions translations/validators.ru.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
<source>This value is not a valid role.</source>
<target>Это значение - недействительная задача.</target>
</trans-unit>
<trans-unit id="End date must not be earlier then start date.">
<source>End date must not be earlier then start date.</source>
<target>End date must not be earlier then start date.</target>
</trans-unit>
</body>
</file>
</xliff>

0 comments on commit 8c686b0

Please sign in to comment.