Skip to content

Instantly share code, notes, and snippets.

@OskarStark
Last active November 29, 2018 08:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OskarStark/95231861f6acd45ed5577930acd3c3dd to your computer and use it in GitHub Desktop.
Save OskarStark/95231861f6acd45ed5577930acd3c3dd to your computer and use it in GitHub Desktop.
<?php
namespace App\Validator\Constraints\Luna;
use Symfony\Component\Validator\Constraint;
/**
* @Annotation
*/
class MaxOrderQuantityCancelled extends Constraint
{
public $maxMessage = 'Qty Cancelled has to be {{ max }} or lower.';
}
<?php
namespace App\Validator\Constraints\Luna;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
class MaxOrderQuantityCancelledValidator extends ConstraintValidator
{
public function validate($value, Constraint $constraint)
{
if (null === $value || 0 === $value) {
return;
}
$object = $this->context->getObject();
$max = $object->getOrderQuantity() - $object->getQuantityGood() - $object->getQuantityBad();
if ($value > $max) {
$this->context->buildViolation($constraint->maxMessage)
->setParameter('{{ max }}', $max)
->addViolation();
return;
}
}
}
<?php
namespace App\Tests\Entity;
use App\Entity\GoodsReceipt;
use App\Entity\Order;
use App\Entity\OrderItem;
use App\Entity\Product;
use App\Tests\BaseEntityTest;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\Validation;
/**
* @group entity
*/
class OrderItemTest extends BaseEntityTest
{
/**
* @test
* @dataProvider orderQuantityCancelledProvider
*
* @group validator
*/
public function validateOrderQuantityCancelled($valid, ?string $message, ?int $cancelled, int $ordered, int $good, int $bad)
{
$entity = (new OrderItem())
->setOrderQuantityCancelled($cancelled)
->setOrderQuantity($ordered)
->addGoodsReceipt((new GoodsReceipt())->setQuantityGood($good))
->addGoodsReceipt((new GoodsReceipt())->setQuantityBad($bad));
$validator = Validation::createValidatorBuilder()->enableAnnotationMapping()->getValidator();
$errors = $validator->validateProperty($entity, 'orderQuantityCancelled');
if (!$valid) {
$this->assertGreaterThan(0, \count($errors));
}
if ($valid) {
$this->assertSame(0, \count($errors));
}
}
/**
* @return array
*/
public function orderQuantityCancelledProvider()
{
return [
[true, null, null, 2, 1, 1],
[true, null, 0, 2, 1, 1],
[true, null, 1, 1, 0, 0],
[false, 'Qty Cancelled has to be 1 or lower.', 2, 1, 0, 0],
];
}
}
<?php
namespace App\Traits\Property;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use App\Validator\Constraints\Luna as Luna;
trait OrderQuantityCancelledPropertyTrait
{
/**
* @Assert\Type("integer")
* @Luna\MaxOrderQuantityCancelled()
*
* @ORM\Column(type="integer", nullable=true)
*/
private $orderQuantityCancelled;
public function getOrderQuantityCancelled(): ?int
{
return $this->orderQuantityCancelled;
}
public function setOrderQuantityCancelled(?int $orderQuantityCancelled): self
{
$this->orderQuantityCancelled = $orderQuantityCancelled;
return $this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment