10000 [Serializer] Add support for auto generated custom normalizers by Nyholm · Pull Request #52905 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Serializer] Add support for auto generated custom normalizers #52905

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

Open
wants to merge 13 commits into
base: 7.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix some tests
  • Loading branch information
Nyholm committed Jan 14, 2024
commit fea6eeb852c0a60cebe051afd55b390b49fe2c85
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,7 @@ protected static function getBundleDefaultConfig()
'enabled' => true,
'enable_attributes' => !class_exists(FullStack::class),
'mapping' => ['paths' => []],
'auto_normalizer' => ['paths'=>[]],
],
'property_access' => [
'enabled' => true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
8000 'serializer' => [
'enabled' => true,
'enable_attributes' => true,
'auto_normalizer' => ['paths'=>[]],
'name_converter' => 'serializer.name_converter.camel_case_to_snake_case',
'circular_reference_handler' => 'my.circular.reference.handler',
'max_depth_handler' => 'my.max.depth.handler',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,28 @@

namespace Symfony\Component\Serializer\Tests\Builder\CodeGenerator;

use App\CodeGenerator\_Attribute;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Serializer\Builder\CodeGenerator\Attribute;

class AttributeTest extends TestCase
{
public function testNoParameters()
{
$output = _Attribute::create('Foobar')->toString();
$output = Attribute::create('Foobar')->toString();
$this->assertEquals('#[Foobar]', $output);

$output = _Attribute::create('Foo\\Bar')->toString();
$output = Attribute::create('Foo\\Bar')->toString();
$this->assertEquals('#[Foo\\Bar]', $output);
}

public function testParameters()
{
$output = _Attribute::create('Foobar')
$output = Attribute::create('Foobar')
->addParameter(null, 7)
->toString();
$this->assertEquals('#[Foobar(7)]', $output);

$output = _Attribute::create('Foobar')
$output = Attribute::create('Foobar')
->addParameter(null, 7)
->addParameter(null, true)
->addParameter(null, false)
Expand All @@ -46,12 +46,12 @@ public function testParameters()

public function testNamedParameters()
{
$output = _Attribute::create('Foobar')
$output = Attribute::create('Foobar')
->addParameter('seven', 7)
->toString();
$this->assertEquals('#[Foobar(seven: 7)]', $output);

$output = _Attribute::create('Foobar')
$output = Attribute::create('Foobar')
->addParameter('seven', 7)
->addParameter('true', true)
->addParameter('false', false)
Expand All @@ -65,10 +65,10 @@ public function testNamedParameters()

public function testNested()
{
$nested = _Attribute::create('Baz')
$nested = Attribute::create('Baz')
->addParameter('name', 'tobias');

$output = _Attribute::create('Foobar')
$output = Attribute::create('Foobar')
->addParameter('seven', 7)
->addParameter('nested', $nested)
->toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,29 @@

namespace Symfony\Component\Serializer\Tests\Builder\CodeGenerator;

use App\CodeGenerator\_Attribute;
use App\CodeGenerator\_Method;
use App\CodeGenerator\_Property;
use App\CodeGenerator\ClassGenerator;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Serializer\Builder\CodeGenerator\Attribute;
use Symfony\Component\Serializer\Builder\CodeGenerator\ClassGenerator;
use Symfony\Component\Serializer\Builder\CodeGenerator\Method;
use Symfony\Component\Serializer\Builder\CodeGenerator\Property;

class ClassGeneratorTest extends TestCase
{
public function testPerson()
{
$generator = new ClassGenerator('Person', 'Test\\CodeGenerator\\Fixtures');

$generator->addProperty(_Property::create('name')
$generator->addProperty(Property::create('name')
->setVisibility('private')
->setType('string')
);
6D40 $generator->addProperty(_Property::create('age')
$generator->addProperty(Property::create('age')
->setVisibility('private')
->setType('int')
);

$generator->addMethod(_Method::create('__construct')
$generator->addMethod(Method::create('__construct')
->addArgument('name', 'string')
->addArgument('age', 'int')
->setBody(<<<PHP
Expand All @@ -41,12 +42,12 @@ public function testPerson()
PHP
));

$generator->addMethod(_Method::create('getName')
$generator->addMethod(Method::create('getName')
->setReturnType('string')
->setBody('return $this->name;')
);

$generator->addMethod(_Method::create('getAge')
$generator->addMethod(Method::create('getAge')
->setReturnType('int')
->setBody('return $this->age;')
);
Expand All @@ -62,17 +63,17 @@ public function testCat()
{
$generator = new ClassGenerator('Cat', 'Test\\CodeGenerator\\Fixtures');

$generator->addMethod(_Method::create('__construct')
$generator->addMethod(Method::create('__construct')
->addArgument('name', 'private string')
->addArgument('age', 'private int')
);

$generator->addMethod(_Method::create('getName')
$generator->addMethod(Method::create('getName')
->setReturnType('string')
->setBody('return $this->name;')
);

$generator->addMethod(_Method::create('getAge')
$generator->addMethod(Method::create('getAge')
->setReturnType('int')
->setBody('return $this->age;')
);
Expand Down Expand Up @@ -104,19 +105,19 @@ public function testFull()
TEXT
);

$generator->addProperty(_Property::create('name')
$generator->addProperty(Property::create('name')
->setVisibility('private')
->setType('string')
);

$generator->addMethod(_Method::create('__construct')
$generator->addMethod(Method::create('__construct')
->addArgument('name', 'string', 'foobar')
->setBody(<<<PHP
\$this->name = \$name;
PHP
));

$generator->addMethod(_Method::create('getName')
$generator->addMethod(Method::create('getName')
->setReturnType('string')
->setBody('return $this->name;')
->setComment(<<<TEXT
Expand All @@ -125,7 +126,7 @@ public function testFull()
@return string
TEXT));

$generator->addAttribute(_Attribute::create('MyAttribute')
$generator->addAttribute(Attribute::create('MyAttribute')
->addParameter('name', 'test')
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Serializer\Tests\Builder\CodeGenerator\Fixtures;
namespace Test\CodeGenerator\Fixtures;

class Cat
{
Expand All @@ -26,4 +17,5 @@ public function getAge(): int
{
return $this->age;
}

}
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
* This is a fixture class.
* We use it for verifying the code generation.
*/

namespace Symfony\Component\Serializer\Tests\Builder\CodeGenerator\Fixtures;
namespace Test\CodeGenerator\Fixtures;

use Test\CodeGenerator\Fixtures\Bar;
use Test\CodeGenerator\Fixtures\Cat;
use Test\CodeGenerator\Fixtures\Foo;
use Test\CodeGenerator\Fixtures\Bar;
use Test\CodeGenerator\Fixtures\MyAttribute;

/**
* Perfect class comment.
*
*
* It has some lines
*/
#[MyAttribute(name: 'test')]
#[MyAttribute(name: "test")]
class Full extends Cat implements Foo, Bar
{
private string $name;
Expand All @@ -32,10 +28,13 @@ public function __construct(string $name = 'foobar')
}

/**
* Returns the name of the cat.
* Returns the name of the cat
*
* @return string
*/
public function getName(): string
{
return $this->name;
}

}
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Serializer\Tests\Builder\CodeGenerator\Fixtures;
namespace Test\CodeGenerator\Fixtures;

class Person
{
Expand All @@ -31,4 +22,5 @@ public function getAge(): int
{
return $this->age;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,65 +11,66 @@

namespace Symfony\Component\Serializer\Tests\Builder\CodeGenerator;

use App\CodeGenerator\_Method;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Serializer\Builder\CodeGenerator\Method;

class MethodTest extends TestCase
{
public function testEmpty()
{
$output = _Method::create('foobar')->toString();
$output = Method::create('foobar')->toString();
$this->assertEquals('public function foobar()
{
}', $output);
}

public function testAbstractMethod()
{
$output = _Method::create('foobar')->setVisibility('protected')->setBody(null)->toString();
$output = Method::create('foobar')->setVisibility('protected')->setBody(null)->toString();
$this->assertEquals('abstract protected function foobar();', $output);
}

public function testVisibility()
{
$output = _Method::create('foobar')->setVisibility('private')->toString();
$output = Method::create('foobar')->setVisibility('private')->toString();
$this->assertEquals('private function foobar()
{
}', $output);

$output = _Method::create('foobar')->setVisibility('protected')->toString();
$output = Method::create('foobar')->setVisibility('protected')->toString();
$this->assertEquals('protected function foobar()
{
}', $output);

// We dont care about logic
$output = _Method::create('foobar')->setVisibility('crazy')->toString();
$output = Method::create('foobar')->setVisibility('crazy')->toString();
$this->assertEquals('crazy function foobar()
{
}', $output);
}

public function testReturnType()
{
$output = _Method::create('foobar')->setReturnType('int')->toString();
$output = Method::create('foobar')->setReturnType('int')->toString();
$this->assertEquals('public function foobar(): int
{
}', $output);

$output = _Method::create('foobar')->setReturnType('mixed')->toString();
$output = Method::create('foobar')->setReturnType('mixed')->toString();
$this->assertEquals('public function foobar(): mixed
{
}', $output);

$output = _Method::create('foobar')->setReturnType('?string')->toString();
$output = Method::create('foobar')->setReturnType('?string')->toString();
$this->assertEquals('public function foobar(): ?string
{
}', $output);
}

public function testArguments()
{
$output = _Method::create('foobar')
$output = Method::create('foobar')
->addArgument('foo')
->addArgument('bar', null, 'test')
->addArgument('baz', null, null)
Expand All @@ -78,7 +79,7 @@ public function testArguments()
{
}', $output);

$output = _Method::create('foobar')
$output = Method::create('foobar')
->addArgument('foo', 'int')
->addArgument('bar', 'string')
->addArgument('baz', '?string', null)
Expand All @@ -90,7 +91,7 @@ public function testArguments()

public function testBody()
{
$output = _Method::create('foobar')
$output = Method::create('foobar')
->setBody('return 2;')
->toString(' ');
$this->assertEquals('public function foobar()
Expand Down
Loading
0