Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
goetas committed Nov 1, 2018
1 parent 110bebf commit 4008e9f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 21 deletions.
19 changes: 0 additions & 19 deletions src/SerializationContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,23 +153,4 @@ public function getInitialType(): ?string
? $this->initialType
: $this->hasAttribute('initial_type') ? $this->getAttribute('initial_type') : null;
}

/**
* Set if NULLs should be serialized (TRUE) ot not (FALSE)
*/
public function setSerializeNull(bool $bool): self
{
$this->serializeNull = $bool;

return $this;
}

/**
* Returns TRUE when NULLs should be serialized
* Returns FALSE when NULLs should not be serialized
*/
public function shouldSerializeNull(): bool
{
return $this->serializeNull;
}
}
34 changes: 32 additions & 2 deletions tests/Serializer/BaseSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -746,8 +746,7 @@ public function testDeserializingNull()
self::assertEquals($this->getContent('blog_post_unauthored'), $this->serialize($post, SerializationContext::create()->setSerializeNull(true)));

if ($this->hasDeserializer()) {
$ctx = DeserializationContext::create()
$deserialized = $this->deserialize($this->getContent('blog_post_unauthored'), get_class($post), $ctx);
$deserialized = $this->deserialize($this->getContent('blog_post_unauthored'), get_class($post));

self::assertEquals('2011-07-30T00:00:00+00:00', $this->getField($deserialized, 'createdAt')->format(\DateTime::ATOM));
self::assertAttributeEquals('This is a nice title.', 'title', $deserialized);
Expand All @@ -758,6 +757,37 @@ public function testDeserializingNull()
}
}

public function testDeserializingNullAllowed()
{
$objectConstructor = new InitializedBlogPostConstructor();

$builder = SerializerBuilder::create();
$builder->setObjectConstructor($objectConstructor);
$this->serializer = $builder->build();

$post = new BlogPost('This is a nice title.', $author = new Author('Foo Bar'), new \DateTime('2011-07-30 00:00', new \DateTimeZone('UTC')), new Publisher('Bar Foo'));

$this->setField($post, 'author', null);
$this->setField($post, 'publisher', null);

$ctx = SerializationContext::create()->setSerializeNull(true);
self::assertEquals($this->getContent('blog_post_unauthored'), $this->serialize($post, $ctx));

if ($this->hasDeserializer()) {
$ctx = DeserializationContext::create();
$ctx->setDeserializeNull(true);

$deserialized = $this->deserialize($this->getContent('blog_post_unauthored'), get_class($post), $ctx);

self::assertEquals('2011-07-30T00:00:00+00:00', $this->getField($deserialized, 'createdAt')->format(\DateTime::ATOM));
self::assertAttributeEquals('This is a nice title.', 'title', $deserialized);
self::assertAttributeSame(false, 'published', $deserialized);
self::assertAttributeSame(false, 'reviewed', $deserialized);
self::assertAttributeEquals(new ArrayCollection(), 'comments', $deserialized);
self::assertEquals(null, $this->getField($deserialized, 'author'));
}
}

public function testExpressionAuthor()
{
$evaluator = new ExpressionEvaluator(new ExpressionLanguage());
Expand Down
5 changes: 5 additions & 0 deletions tests/Serializer/XmlSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,11 @@ public function testDeserializingNull()
$this->markTestSkipped('Not supported in XML.');
}

public function testDeserializingNullAllowed()
{
$this->markTestSkipped('Not supported in XML.');
}

public function testObjectWithXmlNamespaces()
{
$object = new ObjectWithXmlNamespaces('This is a nice title.', 'Foo Bar', new \DateTime('2011-07-30 00:00', new \DateTimeZone('UTC')), 'en');
Expand Down

0 comments on commit 4008e9f

Please sign in to comment.