Skip to content

Commit

Permalink
Add tests and fix some cs
Browse files Browse the repository at this point in the history
  • Loading branch information
joelwurtz committed Oct 17, 2018
1 parent f8cf8cf commit 6f77ce9
Show file tree
Hide file tree
Showing 7 changed files with 235 additions and 11 deletions.
4 changes: 2 additions & 2 deletions Generator/Normalizer/NormalizerGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ protected function createSupportsNormalizationMethod($modelFqdn)
/**
* Create the normalization method.
*
* @param string $modelFqdn
* @param Context $context
* @param string $modelFqdn
* @param Context $context
* @param ClassGuess $classGuess
*
* @return Stmt\ClassMethod
Expand Down
18 changes: 9 additions & 9 deletions Guesser/Guess/MultipleType.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,18 @@ public function getTypes()

/**
* We have to place mixed normalization path at the last.
*
* @return Type[]
*/
protected function getTypesSorted()
{
$types = $this->getTypes();
usort($types, function($first, $second) {
/** @var Type $first */
/** @var Type $second */
usort($types, function ($first, $second) {
/* @var Type $first */
/* @var Type $second */
return $first->name == 'mixed' ? 1 : 0;
});

return $types;
}

Expand Down Expand Up @@ -120,10 +122,9 @@ public function createDenormalizationStatement(Context $context, Expr $input): a
$condition = $type->createConditionStatement($input);
$statement = array_merge($typeStatements, [new Expr\Assign($output, $typeOutput)]);

if($ifStmt === null) {
if ($ifStmt === null) {
$ifStmt = new Stmt\If_($condition, ['stmts' => $statement]);
}
else {
} else {
$ifStmt->elseifs[] = new Stmt\ElseIf_($condition, $statement);
}
}
Expand All @@ -150,10 +151,9 @@ public function createNormalizationStatement(Context $context, Expr $input): arr
$condition = $type->createNormalizationConditionStatement($input);
$statement = array_merge($typeStatements, [new Expr\Assign($output, $typeOutput)]);

if($ifStmt === null) {
if ($ifStmt === null) {
$ifStmt = new Stmt\If_($condition, ['stmts' => $statement]);
}
else {
} else {
$ifStmt->elseifs[] = new Stmt\ElseIf_($condition, $statement);
}
}
Expand Down
8 changes: 8 additions & 0 deletions Tests/fixtures/one-of/.jane
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

return [
'json-schema-file' => __DIR__ . '/schema.json',
'root-class' => 'Test',
'namespace' => 'Jane\JsonSchema\Tests\Expected',
'directory' => __DIR__ . '/generated',
];
39 changes: 39 additions & 0 deletions Tests/fixtures/one-of/expected/Model/Foo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

/*
* This file has been auto generated by Jane,
*
* Do no edit it directly.
*/

namespace Jane\JsonSchema\Tests\Expected\Model;

class Foo
{
/**
* @var string|object|null[]
*/
protected $foo;

/**
* @return string|object|null[]
*/
public function getFoo()
{
return $this->foo;
}

/**
* @param string|object|null[] $foo
*
* @return self
*/
public function setFoo($foo): self
{
$this->foo = $foo;

return $this;
}
}
100 changes: 100 additions & 0 deletions Tests/fixtures/one-of/expected/Normalizer/FooNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

declare(strict_types=1);

/*
* This file has been auto generated by Jane,
*
* Do no edit it directly.
*/

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 FooNormalizer implements DenormalizerInterface, NormalizerInterface, DenormalizerAwareInterface, NormalizerAwareInterface
{
use DenormalizerAwareTrait;
use NormalizerAwareTrait;

public function supportsDenormalization($data, $type, $format = null)
{
return $type === 'Jane\\JsonSchema\\Tests\\Expected\\Model\\Foo';
}

public function supportsNormalization($data, $format = null)
{
return $data instanceof \Jane\JsonSchema\Tests\Expected\Model\Foo;
}

public function denormalize($data, $class, $format = null, array $context = [])
{
if (!is_object($data)) {
throw new InvalidArgumentException();
}
if (isset($data->{'$ref'})) {
return new Reference($data->{'$ref'}, $context['document-origin']);
}
$object = new \Jane\JsonSchema\Tests\Expected\Model\Foo();
if (property_exists($data, 'foo')) {
$value = $data->{'foo'};
if (is_string($data->{'foo'})) {
$value = $data->{'foo'};
} elseif (isset($data->{'foo'})) {
$values = new \ArrayObject([], \ArrayObject::ARRAY_AS_PROPS);
foreach ($data->{'foo'} as $key => $value_1) {
if (preg_match('/^[a-zA-Z0-9._-]+$/', $key) && isset($value_1)) {
$value_2 = $value_1;
if (is_object($value_1)) {
$value_2 = $value_1;
} elseif (is_null($value_1)) {
$value_2 = $value_1;
}
$values[$key] = $value_2;
continue;
}
}
$value = $values;
}
$object->setFoo($value);
}

return $object;
}

public function normalize($object, $format = null, array $context = [])
{
$data = new \stdClass();
if (null !== $object->getFoo()) {
$value = $object->getFoo();
if (is_string($object->getFoo())) {
$value = $object->getFoo();
} elseif (!is_null($object->getFoo())) {
$values = new \stdClass();
foreach ($object->getFoo() as $key => $value_1) {
if (preg_match('/^[a-zA-Z0-9._-]+$/', $key) && !is_null($value_1)) {
$value_2 = $value_1;
if (is_object($value_1)) {
$value_2 = $value_1;
} elseif (is_null($value_1)) {
$value_2 = $value_1;
}
$values->{$key} = $value_2;
continue;
}
}
$value = $values;
}
$data->{'foo'} = $value;
}

return $data;
}
}
24 changes: 24 additions & 0 deletions Tests/fixtures/one-of/expected/Normalizer/NormalizerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

/*
* This file has been auto generated by Jane,
*
* Do no edit it directly.
*/

namespace Jane\JsonSchema\Tests\Expected\Normalizer;

class NormalizerFactory
{
public static function create()
{
$normalizers = [];
$normalizers[] = new \Symfony\Component\Serializer\Normalizer\ArrayDenormalizer();
$normalizers[] = new \Jane\JsonSchemaRuntime\Normalizer\ReferenceNormalizer();
$normalizers[] = new FooNormalizer();

return $normalizers;
}
}
53 changes: 53 additions & 0 deletions Tests/fixtures/one-of/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"id": "http://json-schema.org/draft-04/schema#",
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "Schema with definitions",
"definitions": {
"Foo": {
"type": "object",
"properties": {
"foo": {
"oneOf": [
{
"$ref":"#/definitions/list_of_strings"
},
{
"type":"object",
"patternProperties":{
"^[a-zA-Z0-9._-]+$":{
"oneOf":[
{
"type":"object",
"properties":{
"aliases":{
"$ref":"#/definitions/list_of_strings"
},
"ipv4_address":{
"type":"string"
},
"ipv6_address":{
"type":"string"
}
},
"additionalProperties":false
},
{
"type":"null"
}
]
}
},
"additionalProperties":false
}
]
}
}
},
"list_of_strings": {
"type": "string",
"enum": [
"a", "b", "c"
]
}
}
}

0 comments on commit 6f77ce9

Please sign in to comment.