Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup #1278

Merged
merged 1 commit into from
Jan 23, 2021
Merged

Cleanup #1278

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Accessor/DefaultAccessorStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function __construct(?ExpressionEvaluatorInterface $evaluator = null)
public function getValue(object $object, PropertyMetadata $metadata, SerializationContext $context)
{
if ($metadata instanceof StaticPropertyMetadata) {
return $metadata->getValue(null);
return $metadata->getValue();
}

if ($metadata instanceof ExpressionPropertyMetadata) {
Expand Down
3 changes: 0 additions & 3 deletions src/ContextFactory/CallableContextFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ public function __construct(callable $callable)
$this->callable = $callable;
}

/**
* @return mixed
*/
protected function createContext(): Context
{
$callable = $this->callable;
Expand Down
6 changes: 5 additions & 1 deletion src/GraphNavigator/SerializationGraphNavigator.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
use JMS\Serializer\VisitorInterface;
use Metadata\MetadataFactoryInterface;

use function assert;

/**
* Handles traversal along the object graph.
*
Expand All @@ -37,7 +39,7 @@
*
* @author Johannes M. Schmitt <[email protected]>
*/
final class SerializationGraphNavigator extends GraphNavigator implements GraphNavigatorInterface
final class SerializationGraphNavigator extends GraphNavigator
{
/**
* @var SerializationVisitorInterface
Expand Down Expand Up @@ -97,6 +99,8 @@ public function __construct(

public function initialize(VisitorInterface $visitor, Context $context): void
{
assert($context instanceof SerializationContext);

parent::initialize($visitor, $context);
$this->shouldSerializeNull = $context->shouldSerializeNull();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Handler/ArrayCollectionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function serializeCollection(SerializationVisitorInterface $visitor, Coll
if (null !== $exclusionStrategy && $exclusionStrategy->shouldSkipClass($context->getMetadataFactory()->getMetadataForClass(\get_class($collection)), $context)) {
$context->startVisiting($collection);

return $visitor->visitArray([], $type, $context);
return $visitor->visitArray([], $type);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Handler/StdClassHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ public static function getSubscribingMethods()
public function serializeStdClass(SerializationVisitorInterface $visitor, \stdClass $stdClass, array $type, SerializationContext $context)
{
$classMetadata = $context->getMetadataFactory()->getMetadataForClass('stdClass');
$visitor->startVisitingObject($classMetadata, $stdClass, ['name' => 'stdClass'], $context);
$visitor->startVisitingObject($classMetadata, $stdClass, ['name' => 'stdClass']);

foreach ((array) $stdClass as $name => $value) {
$metadata = new StaticPropertyMetadata('stdClass', $name, $value);
$visitor->visitProperty($metadata, $value);
}

return $visitor->endVisitingObject($classMetadata, $stdClass, ['name' => 'stdClass'], $context);
return $visitor->endVisitingObject($classMetadata, $stdClass, ['name' => 'stdClass']);
}
}
3 changes: 2 additions & 1 deletion src/JsonDeserializationVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ public function __construct(
/**
* {@inheritdoc}
*/
public function visitNull($data, array $type): void
public function visitNull($data, array $type)
{
return null;
}

/**
Expand Down
14 changes: 3 additions & 11 deletions src/Visitor/DeserializationVisitorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,39 +23,31 @@ interface DeserializationVisitorInterface extends VisitorInterface
* @param mixed $data
* @param array $type
*
* @return mixed
* @return null
*/
public function visitNull($data, array $type): void;
public function visitNull($data, array $type);

/**
* @param mixed $data
* @param array $type
*
* @return mixed
*/
public function visitString($data, array $type): string;

/**
* @param mixed $data
* @param array $type
*
* @return mixed
*/
public function visitBoolean($data, array $type): bool;

/**
* @param mixed $data
* @param array $type
*
* @return mixed
*/
public function visitDouble($data, array $type): float;

/**
* @param mixed $data
* @param array $type
*
* @return mixed
*/
public function visitInteger($data, array $type): int;

Expand All @@ -70,7 +62,7 @@ public function visitDiscriminatorMapProperty($data, ClassMetadata $metadata): s
* @param mixed $data
* @param array $type
*
* @return mixed
* @return array<mixed>
*/
public function visitArray($data, array $type): array;

Expand Down
3 changes: 2 additions & 1 deletion src/XmlDeserializationVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,9 @@ private function emptyStringToSpaceCharacter($data): string
/**
* {@inheritdoc}
*/
public function visitNull($data, array $type): void
public function visitNull($data, array $type)
{
return null;
}

/**
Expand Down
8 changes: 3 additions & 5 deletions tests/Handler/DateHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,15 @@ public function testSerializeDate(array $params)
*/
public function testDeserializeDateInterval($dateInterval, $expected)
{
$context = $this->getMockBuilder(SerializationContext::class)->getMock();

$visitor = $this->getMockBuilder(DeserializationVisitorInterface::class)->getMock();
$visitor->method('visitString')->with('2017-06-18');

$deserialized = $this->handler->deserializeDateIntervalFromJson($visitor, $dateInterval, [], $context);
$deserialized = $this->handler->deserializeDateIntervalFromJson($visitor, $dateInterval, []);
if (isset($deserialized->f)) {
$this->assertEquals($expected['f'], $deserialized->f);
self::assertEquals($expected['f'], $deserialized->f);
}

$this->assertEquals($expected['s'], $deserialized->s);
self::assertEquals($expected['s'], $deserialized->s);
}

public function getDeserializeDateInterval()
Expand Down
8 changes: 2 additions & 6 deletions tests/Handler/FormErrorHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@

namespace JMS\Serializer\Tests\Handler;

use JMS\Serializer\GraphNavigatorInterface;
use JMS\Serializer\Handler\FormErrorHandler;
use JMS\Serializer\JsonSerializationVisitor;
use JMS\Serializer\SerializationContext;
use JMS\Serializer\Visitor\Factory\JsonSerializationVisitorFactory;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -53,11 +51,9 @@ class FormErrorHandlerTest extends TestCase
protected function setUp(): void
{
$this->handler = new FormErrorHandler(new Translator('en'));
$navigator = $this->getMockBuilder(GraphNavigatorInterface::class)->getMock();
$context = SerializationContext::create();
$this->visitor = (new JsonSerializationVisitorFactory())->getVisitor($navigator, $context);
$this->visitor = (new JsonSerializationVisitorFactory())->getVisitor();
$this->dispatcher = new EventDispatcher();
$this->factory = $this->getMockBuilder('Symfony\Component\Form\FormFactoryInterface')->getMock();
$this->factory = $this->getMockBuilder(FormFactoryInterface::class)->getMock();
}

protected function tearDown(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,45 @@
use JMS\Serializer\DeserializationContext;
use JMS\Serializer\EventDispatcher\EventDispatcher;
use JMS\Serializer\EventDispatcher\ObjectEvent;
use JMS\Serializer\EventDispatcher\Subscriber\SymfonyValidatorSubscriber;
use JMS\Serializer\EventDispatcher\Subscriber\SymfonyValidatorValidatorSubscriber;
use JMS\Serializer\Exception\ValidationFailedException;
use JMS\Serializer\SerializerBuilder;
use JMS\Serializer\Tests\Fixtures\AuthorList;
use PHPUnit\Framework\TestCase;
use stdClass;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Validator\Validator\ValidatorInterface;

class SymfonyValidatorValidatorSubscriberTest extends TestCase
{
private $validator;

/** @var SymfonyValidatorSubscriber */
/** @var SymfonyValidatorValidatorSubscriber */
private $subscriber;

public function testValidate()
public function testValidate(): void
{
$obj = new \stdClass();
$obj = new stdClass();

$this->validator->expects($this->once())
$this->validator->expects(self::once())
->method('validate')
->with($obj, null, ['foo'])
->will($this->returnValue(new ConstraintViolationList()));
->willReturn(new ConstraintViolationList());

$context = DeserializationContext::create()->setAttribute('validation_groups', ['foo']);

$this->subscriber->onPostDeserialize(new ObjectEvent($context, $obj, []));
}

public function testValidateThrowsExceptionWhenListIsNotEmpty()
public function testValidateThrowsExceptionWhenListIsNotEmpty(): void
{
$obj = new \stdClass();
$obj = new stdClass();

$this->validator->expects($this->once())
$this->validator->expects(self::once())
->method('validate')
->with($obj, null, ['foo'])
->will($this->returnValue(new ConstraintViolationList([new ConstraintViolation('foo', 'foo', [], 'a', 'b', 'c')])));
->willReturn(new ConstraintViolationList([new ConstraintViolation('foo', 'foo', [], 'a', 'b', 'c')]));

$context = DeserializationContext::create()->setAttribute('validation_groups', ['foo']);

Expand All @@ -53,20 +55,20 @@ public function testValidateThrowsExceptionWhenListIsNotEmpty()
$this->subscriber->onPostDeserialize(new ObjectEvent($context, $obj, []));
}

public function testValidatorIsNotCalledWhenNoGroupsAreSet()
public function testValidatorIsNotCalledWhenNoGroupsAreSet(): void
{
$this->validator->expects($this->never())
$this->validator->expects(self::never())
->method('validate');

$this->subscriber->onPostDeserialize(new ObjectEvent(DeserializationContext::create(), new \stdClass(), []));
$this->subscriber->onPostDeserialize(new ObjectEvent(DeserializationContext::create(), new stdClass(), []));
}

public function testValidationIsOnlyPerformedOnRootObject()
public function testValidationIsOnlyPerformedOnRootObject(): void
{
$this->validator->expects($this->once())
$this->validator->expects(self::once())
->method('validate')
->with($this->isInstanceOf('JMS\Serializer\Tests\Fixtures\AuthorList'), null, ['Foo'])
->will($this->returnValue(new ConstraintViolationList()));
->with(self::isInstanceOf(AuthorList::class), null, ['Foo'])
->willReturn(new ConstraintViolationList());

$subscriber = $this->subscriber;
$list = SerializerBuilder::create()
Expand All @@ -76,7 +78,7 @@ public function testValidationIsOnlyPerformedOnRootObject()
->build()
->deserialize(
'{"authors":[{"full_name":"foo"},{"full_name":"bar"}]}',
'JMS\Serializer\Tests\Fixtures\AuthorList',
AuthorList::class,
'json',
DeserializationContext::create()->setAttribute('validation_groups', ['Foo'])
);
Expand All @@ -86,7 +88,7 @@ public function testValidationIsOnlyPerformedOnRootObject()

protected function setUp(): void
{
$this->validator = $this->getMockBuilder('Symfony\Component\Validator\Validator\ValidatorInterface')->getMock();
$this->validator = $this->getMockBuilder(ValidatorInterface::class)->getMock();
$this->subscriber = new SymfonyValidatorValidatorSubscriber($this->validator);
}
}