1<?php
2
3declare(strict_types=1);
4
5namespace JMS\Serializer\Tests\Metadata\Driver;
6
7use JMS\Serializer\Metadata\Driver\XmlDriver;
8use JMS\Serializer\Metadata\PropertyMetadata;
9use JMS\Serializer\Naming\IdenticalPropertyNamingStrategy;
10use Metadata\Driver\FileLocator;
11
12class XmlDriverTest extends BaseDriverTest
13{
14    /**
15     * @expectedException \JMS\Serializer\Exception\InvalidMetadataException
16     * @expectedExceptionMessage Invalid XML content for metadata
17     */
18    public function testInvalidXml()
19    {
20        $driver = $this->getDriver();
21
22        $ref = new \ReflectionMethod($driver, 'loadMetadataFromFile');
23        $ref->setAccessible(true);
24        $ref->invoke($driver, new \ReflectionClass('stdClass'), __DIR__ . '/xml/invalid.xml');
25    }
26
27    public function testBlogPostExcludeAllStrategy()
28    {
29        $m = $this->getDriver('exclude_all')->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\BlogPost'));
30
31        self::assertArrayHasKey('title', $m->propertyMetadata);
32
33        $excluded = ['createdAt', 'published', 'comments', 'author'];
34        foreach ($excluded as $key) {
35            self::assertArrayNotHasKey($key, $m->propertyMetadata);
36        }
37    }
38
39    public function testBlogPostExcludeNoneStrategy()
40    {
41        $m = $this->getDriver('exclude_none')->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\BlogPost'));
42
43        self::assertArrayNotHasKey('title', $m->propertyMetadata);
44
45        $excluded = ['createdAt', 'published', 'comments', 'author'];
46        foreach ($excluded as $key) {
47            self::assertArrayHasKey($key, $m->propertyMetadata);
48        }
49    }
50
51    public function testBlogPostCaseInsensitive()
52    {
53        $m = $this->getDriver('case')->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\BlogPost'));
54
55        $p = new PropertyMetadata($m->name, 'title');
56        $p->serializedName = 'title';
57        $p->type = ['name' => 'string', 'params' => []];
58        self::assertEquals($p, $m->propertyMetadata['title']);
59    }
60
61    public function testAccessorAttributes()
62    {
63        $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\GetSetObject'));
64
65        $p = new PropertyMetadata($m->name, 'name');
66        $p->type = ['name' => 'string', 'params' => []];
67        $p->getter = 'getTrimmedName';
68        $p->setter = 'setCapitalizedName';
69        $p->serializedName = 'name';
70
71        self::assertEquals($p, $m->propertyMetadata['name']);
72    }
73
74    public function testGroupsTrim()
75    {
76        $first = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\GroupsTrim'));
77
78        self::assertArrayHasKey('amount', $first->propertyMetadata);
79        self::assertArraySubset(['first.test.group', 'second.test.group'], $first->propertyMetadata['currency']->groups);
80    }
81
82    public function testMultilineGroups()
83    {
84        $first = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\MultilineGroupsFormat'));
85
86        self::assertArrayHasKey('amount', $first->propertyMetadata);
87        self::assertArraySubset(['first.test.group', 'second.test.group'], $first->propertyMetadata['currency']->groups);
88    }
89
90    protected function getDriver()
91    {
92        $append = '';
93        if (1 === func_num_args()) {
94            $append = '/' . func_get_arg(0);
95        }
96
97        return new XmlDriver(new FileLocator([
98            'JMS\Serializer\Tests\Fixtures' => __DIR__ . '/xml' . $append,
99            '' => __DIR__ . '/xml/_' . $append,
100        ]), new IdenticalPropertyNamingStrategy(), null, $this->getExpressionEvaluator());
101    }
102}
103