Skip to content
This repository has been archived by the owner on May 4, 2022. It is now read-only.

Commit

Permalink
Merge pull request #21 from NicolasGuilloux/feature/doctrine-strip-null
Browse files Browse the repository at this point in the history
Prevent searching a doctrine entity with a null value
  • Loading branch information
NicolasGuilloux authored Feb 14, 2020
2 parents 99d6c1a + dadefa4 commit 7a9b04c
Show file tree
Hide file tree
Showing 7 changed files with 662 additions and 491 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 2.2.3

Bug fix:
* Don't treat null as a valid value when converting a value to an entity.

## 2.2.2

Bug fix:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Chaplean\Bundle\DtoHandlerBundle\Exception\DataTransferObjectValidationException;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\KernelEvents;

/**
Expand All @@ -27,11 +27,11 @@ public static function getSubscribedEvents(): array
}

/**
* @param GetResponseForExceptionEvent $event
* @param ExceptionEvent $event
*
* @return void
*/
public function onKernelException(GetResponseForExceptionEvent $event): void
public function onKernelException(ExceptionEvent $event): void
{
$exception = $event->getException();

Expand Down
9 changes: 5 additions & 4 deletions ParamConverter/DataTransferObjectParamConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessor;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
* Class DataTransferObjectParamConverter.
Expand Down Expand Up @@ -308,6 +308,7 @@ protected function autoConfigureOne(
if ($propertyConfigurationModel->getMapTo() !== null) {
$config->setOptions(
[
'strip_null' => true,
'mapping' => [
$name => $propertyConfigurationModel->getMapTo()
]
Expand Down Expand Up @@ -373,7 +374,7 @@ protected function getViolationFromException(Request $request, \Exception $excep
$propertyName,
$value,
null,
$exception->getCode()
(string) $exception->getCode()
);
}

Expand Down Expand Up @@ -468,7 +469,7 @@ protected function validate($object, Request $request, array $options): void
}

$violations = new ConstraintViolationList();
$violationsHandler = $options['violations'] ?? false;
$violationsHandler = $options['violations'] ?? '';
$groups = $options['groups'] ?? null;

if ($groups !== null) {
Expand All @@ -485,7 +486,7 @@ protected function validate($object, Request $request, array $options): void
$this->validator->validate($object, null, $group['validation_group'])
);

if (!$violationsHandler && $violations->count() > 0) {
if ($violationsHandler === '' && $violations->count() > 0) {
throw new DataTransferObjectValidationException($violations, $group['http_status_code']);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
use Chaplean\Bundle\DtoHandlerBundle\EventListener\DataTransferObjectValidationExceptionSubscriber;
use Chaplean\Bundle\DtoHandlerBundle\Exception\DataTransferObjectValidationException;
use Mockery\Adapter\Phpunit\MockeryTestCase;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Validator\ConstraintViolation;
Expand Down Expand Up @@ -64,7 +61,7 @@ public function testOnKernelExceptionWithAnotherException(): void
{
$exception = new BadRequestHttpException();

$event = \Mockery::mock(GetResponseForExceptionEvent::class);
$event = \Mockery::mock(ExceptionEvent::class);
$event->shouldNotReceive('setResponse');
$event->shouldReceive('getException')
->once()
Expand Down Expand Up @@ -93,7 +90,7 @@ public function testOnKernelException(): void

$exception = new DataTransferObjectValidationException($violations);

$event = \Mockery::mock(GetResponseForExceptionEvent::class);
$event = \Mockery::mock(ExceptionEvent::class);

$event->shouldReceive('getException')
->once()
Expand Down
12 changes: 6 additions & 6 deletions Tests/ParamConverter/DataTransferObjectParamConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@
use Doctrine\Common\Annotations\AnnotationException;
use Mockery\Adapter\Phpunit\MockeryTestCase;
use Mockery\MockInterface;
use phpmock\mockery\PHPMockery;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterManager;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Tests\Chaplean\Bundle\DtoHandlerBundle\Resources\DTO\DummyDataTransferObject;
use Tests\Chaplean\Bundle\DtoHandlerBundle\Resources\DTO\SubDataTransferObject;
use Tests\Chaplean\Bundle\DtoHandlerBundle\Resources\Entity\DummyEntity;
use Tests\Chaplean\Bundle\DtoHandlerBundle\Resources\DTO\DummyDataTransferObject;
use phpmock\mockery\PHPMockery;

/**
* Class DataTransferObjectParamConverterTest
Expand Down Expand Up @@ -328,7 +328,7 @@ public function testApplyWithValidationSuccess(): void
]
);

$request->attributes->set(0, 'UselessAttribute');
$request->attributes->set('0', 'UselessAttribute');
$request->attributes->set('parasite_', 'UselessAttribute');

$this->manager->shouldReceive('apply')->times(7);
Expand Down Expand Up @@ -422,7 +422,7 @@ public function testApplySubDto(): void
['keyname' => 'test2']
]
]);
$request->attributes->set(0, 'UselessAttribute');
$request->attributes->set('0', 'UselessAttribute');
$request->attributes->set('parasite_', 'UselessAttribute');

$this->manager->shouldReceive('apply')->times(7);
Expand Down Expand Up @@ -689,7 +689,7 @@ public function testApplyWithUnwrittableProperties(): void
$request->request->set('accessible', 'Accessible');

$request->attributes->set('dataTransferObject', null);
$request->attributes->set(0, 'UselessAttribute');
$request->attributes->set('0', 'UselessAttribute');
$request->attributes->set('parasite_', 'UselessAttribute');

$this->manager->shouldReceive('apply')->never();
Expand Down
Loading

0 comments on commit 7a9b04c

Please sign in to comment.