Skip to content

Commit

Permalink
Merge pull request #1013 from liip-forks/fix-fileresource-for-interna…
Browse files Browse the repository at this point in the history
…l-classes

internal classes have false in reflection::getFilename()
  • Loading branch information
goetas authored Dec 9, 2018
2 parents 26cec7e + 82d8dd2 commit ecc7307
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/Metadata/Driver/AnnotationDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ public function __construct(Reader $reader, PropertyNamingStrategyInterface $nam
public function loadMetadataForClass(\ReflectionClass $class): ?BaseClassMetadata
{
$classMetadata = new ClassMetadata($name = $class->name);
$classMetadata->fileResources[] = $class->getFilename();
$fileResource = $class->getFilename();
if (false !== $fileResource) {
$classMetadata->fileResources[] = $fileResource;
}

$propertiesMetadata = [];
$propertiesAnnotations = [];
Expand Down
5 changes: 4 additions & 1 deletion src/Metadata/Driver/NullDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ class NullDriver implements DriverInterface
public function loadMetadataForClass(\ReflectionClass $class): ?BaseClassMetadata
{
$classMetadata = new ClassMetadata($name = $class->name);
$classMetadata->fileResources[] = $class->getFilename();
$fileResource = $class->getFilename();
if (false !== $fileResource) {
$classMetadata->fileResources[] = $fileResource;
}

return $classMetadata;
}
Expand Down
5 changes: 4 additions & 1 deletion src/Metadata/Driver/XmlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ protected function loadMetadataFromFile(\ReflectionClass $class, string $path):
$elem = reset($elems);

$metadata->fileResources[] = $path;
$metadata->fileResources[] = $class->getFileName();
$fileResource = $class->getFilename();
if (false !== $fileResource) {
$metadata->fileResources[] = $fileResource;
}
$exclusionPolicy = strtoupper((string) $elem->attributes()->{'exclusion-policy'}) ?: 'NONE';
$exclude = $elem->attributes()->exclude;
$excludeAll = null !== $exclude ? 'true' === strtolower((string) $exclude) : false;
Expand Down
6 changes: 5 additions & 1 deletion src/Metadata/Driver/YamlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ protected function loadMetadataFromFile(\ReflectionClass $class, string $file):
$config = $config[$name];
$metadata = new ClassMetadata($name);
$metadata->fileResources[] = $file;
$metadata->fileResources[] = $class->getFileName();
$fileResource = $class->getFilename();
if (false !== $fileResource) {
$metadata->fileResources[] = $fileResource;
}

$exclusionPolicy = isset($config['exclusion_policy']) ? strtoupper($config['exclusion_policy']) : 'NONE';
$excludeAll = isset($config['exclude']) ? (bool) $config['exclude'] : false;
$classAccessType = $config['access_type'] ?? PropertyMetadata::ACCESS_TYPE_PROPERTY;
Expand Down
5 changes: 5 additions & 0 deletions tests/Metadata/Driver/AnnotationDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,9 @@ protected function getDriver()
{
return new AnnotationDriver(new AnnotationReader(), new IdenticalPropertyNamingStrategy());
}

public function testCanDefineMetadataForInternalClass()
{
$this->markTestSkipped('Can not define annotation metadata for internal classes');
}
}
11 changes: 11 additions & 0 deletions tests/Metadata/Driver/BaseDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,17 @@ public function testLoadXmlDiscriminatorWithNamespaces()
self::assertFalse($m->xmlDiscriminatorAttribute);
}

public function testCanDefineMetadataForInternalClass()
{
/** @var ClassMetadata $m */
$m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass(\PDOStatement::class));

self::assertNotNull($m);
self::assertSame('int', $m->propertyMetadata['queryString']->type['name']);

self::assertCount(1, $m->fileResources);
}

public function testLoadXmlDiscriminatorWithAttributeNamespaces()
{
/** @var ClassMetadata $m */
Expand Down
1 change: 1 addition & 0 deletions tests/Metadata/Driver/XmlDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ protected function getDriver()

return new XmlDriver(new FileLocator([
'JMS\Serializer\Tests\Fixtures' => __DIR__ . '/xml' . $append,
'' => __DIR__ . '/xml/_' . $append,
]), new IdenticalPropertyNamingStrategy());
}
}
1 change: 1 addition & 0 deletions tests/Metadata/Driver/YamlDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ private function getDriverForSubDir($subDir = null)
{
return new YamlDriver(new FileLocator([
'JMS\Serializer\Tests\Fixtures' => __DIR__ . '/yml' . ($subDir ? '/' . $subDir : ''),
'' => __DIR__ . '/yml/_' . ($subDir ? '/' . $subDir : ''),
]), new IdenticalPropertyNamingStrategy());
}

Expand Down
6 changes: 6 additions & 0 deletions tests/Metadata/Driver/xml/_/PDOStatement.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="PDOStatement">
<property name="queryString" type="int"/>
</class>
</serializer>
5 changes: 5 additions & 0 deletions tests/Metadata/Driver/yml/_/PDOStatement.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
PDOStatement:
properties:
queryString:
type: int

0 comments on commit ecc7307

Please sign in to comment.