Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit 8dd77ec

Browse files
committed
Ensures objects implementing __toString() are serialized correctly by XmlRenderer
1 parent 5ec9f3c commit 8dd77ec

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

test/Renderer/XmlRendererTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99

1010
use DateTime;
1111
use PHPUnit\Framework\TestCase;
12+
use stdClass;
1213
use Zend\Expressive\Hal\HalResource;
1314
use Zend\Expressive\Hal\Link;
1415
use Zend\Expressive\Hal\Renderer\XmlRenderer;
16+
use ZendTest\Expressive\Hal\TestAsset\StringSerializable;
1517

1618
class XmlRendererTest extends TestCase
1719
{
@@ -80,4 +82,18 @@ public function testCanRenderPhpDateTimeInstances()
8082
$xml = $renderer->render($resource);
8183
$this->assertContains($dateTime->format('c'), $xml);
8284
}
85+
86+
public function testCanRenderObjectsThatImplementToString()
87+
{
88+
$instance = new StringSerializable();
89+
90+
$resource = new HalResource([
91+
'key' => $instance,
92+
]);
93+
$resource = $resource->withLink(new Link('self', '/example'));
94+
95+
$renderer = new XmlRenderer();
96+
$xml = $renderer->render($resource);
97+
$this->assertContains((string) $instance, $xml);
98+
}
8399
}

test/TestAsset/StringSerializable.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
/**
3+
* @see https://github.com/zendframework/zend-expressive-hal for the canonical source repository
4+
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (https://www.zend.com)
5+
* @license https://github.com/zendframework/zend-expressive-hal/blob/master/LICENSE.md New BSD License
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace ZendTest\Expressive\Hal\TestAsset;
11+
12+
class StringSerializable
13+
{
14+
public function __toString()
15+
{
16+
return __METHOD__;
17+
}
18+
}

0 commit comments

Comments
 (0)