Skip to content

Commit

Permalink
Merge pull request #1127 from schmittjoh/short-expose-syntax-xml
Browse files Browse the repository at this point in the history
Implement short expose syntax for XML as it is available for YAML
  • Loading branch information
goetas authored Sep 20, 2019
2 parents 7ede68b + fb5b49f commit 519263f
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 27 deletions.
5 changes: 3 additions & 2 deletions src/Metadata/Driver/XmlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,16 @@ protected function loadMetadataFromFile(\ReflectionClass $class, string $path):
}
$pName = $property->getName();
$propertiesMetadata[] = new PropertyMetadata($name, $pName);
$pElems = $elem->xpath("./property[@name = '" . $pName . "']");

$pElems = $elem->xpath("./property[@name = '" . $pName . "']");
$propertiesNodes[] = $pElems ? reset($pElems) : null;
}

foreach ($propertiesMetadata as $propertyKey => $pMetadata) {
$isExclude = false;
$isExpose = $pMetadata instanceof VirtualPropertyMetadata
|| $pMetadata instanceof ExpressionPropertyMetadata;
|| $pMetadata instanceof ExpressionPropertyMetadata
|| isset($propertiesNodes[$propertyKey]);

$pElem = $propertiesNodes[$propertyKey];
if (!empty($pElem)) {
Expand Down
8 changes: 7 additions & 1 deletion tests/Metadata/Driver/AnnotationDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use JMS\Serializer\Metadata\Driver\AnnotationDriver;
use JMS\Serializer\Naming\IdenticalPropertyNamingStrategy;
use JMS\Serializer\Tests\Fixtures\AllExcludedObject;
use Metadata\Driver\DriverInterface;

class AnnotationDriverTest extends BaseDriverTest
{
Expand All @@ -20,7 +21,7 @@ public function testAllExcluded()
self::assertArrayHasKey('bar', $m->propertyMetadata);
}

protected function getDriver()
protected function getDriver(?string $subDir = null, bool $addUnderscoreDir = true): DriverInterface
{
return new AnnotationDriver(new AnnotationReader(), new IdenticalPropertyNamingStrategy(), null, $this->getExpressionEvaluator());
}
Expand All @@ -29,4 +30,9 @@ public function testCanDefineMetadataForInternalClass()
{
$this->markTestSkipped('Can not define annotation metadata for internal classes');
}

public function testShortExposeSyntax(): void
{
$this->markTestSkipped('Short expose syntax not supported on annotations');
}
}
11 changes: 10 additions & 1 deletion tests/Metadata/Driver/BaseDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use JMS\Serializer\Tests\Fixtures\ObjectWithVirtualPropertiesAndDuplicatePropNameExcludeAll;
use JMS\Serializer\Tests\Fixtures\ObjectWithVirtualPropertiesAndExcludeAll;
use JMS\Serializer\Tests\Fixtures\ParentSkipWithEmptyChild;
use JMS\Serializer\Tests\Fixtures\Person;
use Metadata\Driver\DriverInterface;
use PHPUnit\Framework\TestCase;
use Symfony\Component\ExpressionLanguage\ExpressionFunction;
Expand Down Expand Up @@ -616,10 +617,18 @@ public function testExcludePropertyNoPublicAccessorException()
self::assertArrayNotHasKey('iShallNotBeAccessed', $first->propertyMetadata);
}

public function testShortExposeSyntax(): void
{
$m = $this->getDriver('short_expose')->loadMetadataForClass(new \ReflectionClass(Person::class));

self::assertArrayHasKey('name', $m->propertyMetadata);
self::assertArrayNotHasKey('age', $m->propertyMetadata);
}

/**
* @return DriverInterface
*/
abstract protected function getDriver();
abstract protected function getDriver(?string $subDir = null, bool $addUnderscoreDir = true): DriverInterface;

protected function getExpressionEvaluator()
{
Expand Down
17 changes: 9 additions & 8 deletions tests/Metadata/Driver/XmlDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use JMS\Serializer\Metadata\Driver\XmlDriver;
use JMS\Serializer\Metadata\PropertyMetadata;
use JMS\Serializer\Naming\IdenticalPropertyNamingStrategy;
use Metadata\Driver\DriverInterface;
use Metadata\Driver\FileLocator;

class XmlDriverTest extends BaseDriverTest
Expand Down Expand Up @@ -90,16 +91,16 @@ public function testMultilineGroups()
self::assertContains('second.test.group', $first->propertyMetadata['currency']->groups);
}

protected function getDriver()
protected function getDriver(?string $subDir = null, bool $addUnderscoreDir = true): DriverInterface
{
$append = '';
if (1 === func_num_args()) {
$append = '/' . func_get_arg(0);
$dirs = [
'JMS\Serializer\Tests\Fixtures' => __DIR__ . '/xml' . ($subDir ? '/' . $subDir : ''),
];

if ($addUnderscoreDir) {
$dirs[''] = __DIR__ . '/xml/_' . ($subDir ? '/' . $subDir : '');
}

return new XmlDriver(new FileLocator([
'JMS\Serializer\Tests\Fixtures' => __DIR__ . '/xml' . $append,
'' => __DIR__ . '/xml/_' . $append,
]), new IdenticalPropertyNamingStrategy(), null, $this->getExpressionEvaluator());
return new XmlDriver(new FileLocator($dirs), new IdenticalPropertyNamingStrategy(), null, $this->getExpressionEvaluator());
}
}
26 changes: 11 additions & 15 deletions tests/Metadata/Driver/YamlDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,28 @@
use JMS\Serializer\Naming\IdenticalPropertyNamingStrategy;
use JMS\Serializer\Tests\Fixtures\BlogPost;
use JMS\Serializer\Tests\Fixtures\Person;
use Metadata\Driver\DriverInterface;
use Metadata\Driver\FileLocator;

class YamlDriverTest extends BaseDriverTest
{
public function testAccessorOrderIsInferred(): void
{
$m = $this->getDriverForSubDir('accessor_inferred')->loadMetadataForClass(new \ReflectionClass(Person::class));
$m = $this->getDriver('accessor_inferred')->loadMetadataForClass(new \ReflectionClass(Person::class));
self::assertEquals(['age', 'name'], array_keys($m->propertyMetadata));
}

public function testShortExposeSyntax(): void
{
$m = $this->getDriverForSubDir('short_expose')->loadMetadataForClass(new \ReflectionClass(Person::class));
$m = $this->getDriver('short_expose')->loadMetadataForClass(new \ReflectionClass(Person::class));

self::assertArrayHasKey('name', $m->propertyMetadata);
self::assertArrayNotHasKey('age', $m->propertyMetadata);
}

public function testBlogPost(): void
{
$m = $this->getDriverForSubDir('exclude_all')->loadMetadataForClass(new \ReflectionClass(BlogPost::class));
$m = $this->getDriver('exclude_all')->loadMetadataForClass(new \ReflectionClass(BlogPost::class));

self::assertArrayHasKey('title', $m->propertyMetadata);

Expand All @@ -42,7 +43,7 @@ public function testBlogPost(): void

public function testBlogPostExcludeNoneStrategy(): void
{
$m = $this->getDriverForSubDir('exclude_none')->loadMetadataForClass(new \ReflectionClass(BlogPost::class));
$m = $this->getDriver('exclude_none')->loadMetadataForClass(new \ReflectionClass(BlogPost::class));

self::assertArrayNotHasKey('title', $m->propertyMetadata);

Expand All @@ -54,7 +55,7 @@ public function testBlogPostExcludeNoneStrategy(): void

public function testBlogPostCaseInsensitive(): void
{
$m = $this->getDriverForSubDir('case')->loadMetadataForClass(new \ReflectionClass(BlogPost::class));
$m = $this->getDriver('case')->loadMetadataForClass(new \ReflectionClass(BlogPost::class));

$p = new PropertyMetadata($m->name, 'title');
$p->serializedName = 'title';
Expand All @@ -64,7 +65,7 @@ public function testBlogPostCaseInsensitive(): void

public function testBlogPostAccessor(): void
{
$m = $this->getDriverForSubDir('accessor')->loadMetadataForClass(new \ReflectionClass(BlogPost::class));
$m = $this->getDriver('accessor')->loadMetadataForClass(new \ReflectionClass(BlogPost::class));

self::assertArrayHasKey('title', $m->propertyMetadata);

Expand All @@ -79,19 +80,19 @@ public function testInvalidMetadataFileCausesException(): void
{
$this->expectException(InvalidMetadataException::class);

$this->getDriverForSubDir('invalid_metadata')->loadMetadataForClass(new \ReflectionClass(BlogPost::class));
$this->getDriver('invalid_metadata')->loadMetadataForClass(new \ReflectionClass(BlogPost::class));
}

public function testLoadingYamlFileWithLongExtension(): void
{
$m = $this->getDriverForSubDir('multiple_types')->loadMetadataForClass(new \ReflectionClass(Person::class));
$m = $this->getDriver('multiple_types')->loadMetadataForClass(new \ReflectionClass(Person::class));

self::assertArrayHasKey('name', $m->propertyMetadata);
}

public function testLoadingMultipleMetadataExtensions(): void
{
$classNames = $this->getDriverForSubDir('multiple_types', false)->getAllClassNames();
$classNames = $this->getDriver('multiple_types', false)->getAllClassNames();

self::assertEquals(
[
Expand All @@ -102,7 +103,7 @@ public function testLoadingMultipleMetadataExtensions(): void
);
}

private function getDriverForSubDir($subDir = null, bool $addUnderscoreDir = true): YamlDriver
protected function getDriver(?string $subDir = null, bool $addUnderscoreDir = true): DriverInterface
{
$dirs = [
'JMS\Serializer\Tests\Fixtures' => __DIR__ . '/yml' . ($subDir ? '/' . $subDir : ''),
Expand All @@ -114,9 +115,4 @@ private function getDriverForSubDir($subDir = null, bool $addUnderscoreDir = tru

return new YamlDriver(new FileLocator($dirs), new IdenticalPropertyNamingStrategy(), null, $this->getExpressionEvaluator());
}

protected function getDriver()
{
return $this->getDriverForSubDir();
}
}
6 changes: 6 additions & 0 deletions tests/Metadata/Driver/xml/short_expose/Person.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<serializer>
<class name="JMS\Serializer\Tests\Fixtures\Person" exclusion-policy="ALL">
<property name="name"/>
</class>
</serializer>

0 comments on commit 519263f

Please sign in to comment.