Skip to content

Commit

Permalink
Add test for #144
Browse files Browse the repository at this point in the history
  • Loading branch information
Korbeil committed Nov 30, 2019
1 parent 77501ab commit c128185
Show file tree
Hide file tree
Showing 7 changed files with 236 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/JsonSchema/Tests/fixtures/array-object-nullable/.jane
@@ -0,0 +1,10 @@
<?php

return [
'json-schema-file' => __DIR__ . '/schema.json',
'root-class' => 'Test',
'namespace' => 'Jane\JsonSchema\Tests\Expected',
'directory' => __DIR__ . '/generated',
'use-fixer' => false,
'strict' => true,
];
@@ -0,0 +1,34 @@
<?php

namespace Jane\JsonSchema\Tests\Expected\Model;

class Attributes
{
/**
*
*
* @var string
*/
protected $foo;
/**
*
*
* @return string
*/
public function getFoo() : string
{
return $this->foo;
}
/**
*
*
* @param string $foo
*
* @return self
*/
public function setFoo(string $foo) : self
{
$this->foo = $foo;
return $this;
}
}
@@ -0,0 +1,34 @@
<?php

namespace Jane\JsonSchema\Tests\Expected\Model;

class Document
{
/**
*
*
* @var Attributes[]|null
*/
protected $attributes;
/**
*
*
* @return Attributes[]|null
*/
public function getAttributes() : ?array
{
return $this->attributes;
}
/**
*
*
* @param Attributes[]|null $attributes
*
* @return self
*/
public function setAttributes(?array $attributes) : self
{
$this->attributes = $attributes;
return $this;
}
}
@@ -0,0 +1,50 @@
<?php

namespace Jane\JsonSchema\Tests\Expected\Normalizer;

use Jane\JsonSchemaRuntime\Reference;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
class AttributesNormalizer implements DenormalizerInterface, NormalizerInterface, DenormalizerAwareInterface, NormalizerAwareInterface
{
use DenormalizerAwareTrait;
use NormalizerAwareTrait;
public function supportsDenormalization($data, $type, $format = null)
{
return $type === 'Jane\\JsonSchema\\Tests\\Expected\\Model\\Attributes';
}
public function supportsNormalization($data, $format = null)
{
return $data instanceof \Jane\JsonSchema\Tests\Expected\Model\Attributes;
}
public function denormalize($data, $class, $format = null, array $context = array())
{
if (!is_object($data)) {
throw new InvalidArgumentException();
}
if (isset($data->{'$ref'})) {
return new Reference($data->{'$ref'}, $context['document-origin']);
}
if (isset($data->{'$recursiveRef'})) {
return new Reference($data->{'$recursiveRef'}, $context['document-origin']);
}
$object = new \Jane\JsonSchema\Tests\Expected\Model\Attributes();
if (property_exists($data, 'foo')) {
$object->setFoo($data->{'foo'});
}
return $object;
}
public function normalize($object, $format = null, array $context = array())
{
$data = new \stdClass();
if (null !== $object->getFoo()) {
$data->{'foo'} = $object->getFoo();
}
return $data;
}
}
@@ -0,0 +1,68 @@
<?php

namespace Jane\JsonSchema\Tests\Expected\Normalizer;

use Jane\JsonSchemaRuntime\Reference;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
class DocumentNormalizer implements DenormalizerInterface, NormalizerInterface, DenormalizerAwareInterface, NormalizerAwareInterface
{
use DenormalizerAwareTrait;
use NormalizerAwareTrait;
public function supportsDenormalization($data, $type, $format = null)
{
return $type === 'Jane\\JsonSchema\\Tests\\Expected\\Model\\Document';
}
public function supportsNormalization($data, $format = null)
{
return $data instanceof \Jane\JsonSchema\Tests\Expected\Model\Document;
}
public function denormalize($data, $class, $format = null, array $context = array())
{
if (!is_object($data)) {
throw new InvalidArgumentException();
}
if (isset($data->{'$ref'})) {
return new Reference($data->{'$ref'}, $context['document-origin']);
}
if (isset($data->{'$recursiveRef'})) {
return new Reference($data->{'$recursiveRef'}, $context['document-origin']);
}
$object = new \Jane\JsonSchema\Tests\Expected\Model\Document();
if (property_exists($data, 'attributes')) {
$value = $data->{'attributes'};
if (is_array($data->{'attributes'})) {
$values = array();
foreach ($data->{'attributes'} as $value_1) {
$values[] = $this->denormalizer->denormalize($value_1, 'Jane\\JsonSchema\\Tests\\Expected\\Model\\Attributes', 'json', $context);
}
$value = $values;
} elseif (is_null($data->{'attributes'})) {
$value = $data->{'attributes'};
}
$object->setAttributes($value);
}
return $object;
}
public function normalize($object, $format = null, array $context = array())
{
$data = new \stdClass();
$value = $object->getAttributes();
if (is_array($object->getAttributes())) {
$values = array();
foreach ($object->getAttributes() as $value_1) {
$values[] = $this->normalizer->normalize($value_1, 'json', $context);
}
$value = $values;
} elseif (is_null($object->getAttributes())) {
$value = $object->getAttributes();
}
$data->{'attributes'} = $value;
return $data;
}
}
@@ -0,0 +1,16 @@
<?php

namespace Jane\JsonSchema\Tests\Expected\Normalizer;

class NormalizerFactory
{
public static function create()
{
$normalizers = array();
$normalizers[] = new \Symfony\Component\Serializer\Normalizer\ArrayDenormalizer();
$normalizers[] = new \Jane\JsonSchemaRuntime\Normalizer\ReferenceNormalizer();
$normalizers[] = new DocumentNormalizer();
$normalizers[] = new AttributesNormalizer();
return $normalizers;
}
}
24 changes: 24 additions & 0 deletions src/JsonSchema/Tests/fixtures/array-object-nullable/schema.json
@@ -0,0 +1,24 @@
{
"$schema": "http://json-schema.org/2019-09/schema#",
"definitions": {
"Document": {
"type": "object",
"properties": {
"attributes": {
"type": ["array", "null"],
"items": {
"$ref": "#/definitions/Attributes"
}
}
}
},
"Attributes": {
"type": "object",
"properties": {
"foo": {
"type": "string"
}
}
}
}
}

0 comments on commit c128185

Please sign in to comment.