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

please remove this PR #29507

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,103 @@
<?php
namespace Symfony\Component\Serializer\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;

class DeserializeNestedArrayOfObjectsTest extends TestCase
{
public function provider()
{
return [
//from property PhpDoc
[Zoo::class],
//from argument constructor PhpDoc
[ZooImmutable::class],
];
}
/**
* @dataProvider provider
*/
public function testPropertyPhpDoc($class)
{
//GIVEN
$json = <<<EOF
{
"animals": [
{"name": "Bug"}
]
}
EOF;
$serializer = new Serializer(array(
new ObjectNormalizer(null,null, null, new PhpDocExtractor()),
new ArrayDenormalizer(),
), array('json' => new JsonEncoder()));
//WHEN
/** @var Zoo $zoo */
$zoo = $serializer->deserialize($json, $class, 'json');
//THEN
self::assertCount(1, $zoo->getAnimals());
self::assertInstanceOf(Animal::class, $zoo->getAnimals()[0]);
}
}

class Zoo {
/** @var Animal[] */
private $animals = [];
/**
* @return Animal[]
*/
public function getAnimals(): array
{
return $this->animals;
}
/**
* @param Animal[] $animals
*/
public function setAnimals(array $animals): void
{
$this->animals = $animals;
}
}

class ZooImmutable {
/** @var Animal[] */
private $animals = [];
/**
* @param Animal[] $animals
*/
public function __construct(array $animals = [])
{
$this->animals = $animals;
}
/**
* @return Animal[]
*/
public function getAnimals(): array
{
return $this->animals;
}
}

class Animal {
/** @var string */
private $name;
/**
* @return string
*/
public function getName(): ?string
{
return $this->name;
}
/**
* @param string $name
*/
public function setName(?string $name): void
{
$this->name = $name;
}
}