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

Commit

Permalink
Ensures objects implementing __toString() are serialized correctly …
Browse files Browse the repository at this point in the history
…by XmlRenderer
  • Loading branch information
weierophinney committed Jan 3, 2018
1 parent 5ec9f3c commit 8dd77ec
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
16 changes: 16 additions & 0 deletions test/Renderer/XmlRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@

use DateTime;
use PHPUnit\Framework\TestCase;
use stdClass;
use Zend\Expressive\Hal\HalResource;
use Zend\Expressive\Hal\Link;
use Zend\Expressive\Hal\Renderer\XmlRenderer;
use ZendTest\Expressive\Hal\TestAsset\StringSerializable;

class XmlRendererTest extends TestCase
{
Expand Down Expand Up @@ -80,4 +82,18 @@ public function testCanRenderPhpDateTimeInstances()
$xml = $renderer->render($resource);
$this->assertContains($dateTime->format('c'), $xml);
}

public function testCanRenderObjectsThatImplementToString()
{
$instance = new StringSerializable();

$resource = new HalResource([
'key' => $instance,
]);
$resource = $resource->withLink(new Link('self', '/example'));

$renderer = new XmlRenderer();
$xml = $renderer->render($resource);
$this->assertContains((string) $instance, $xml);
}
}
18 changes: 18 additions & 0 deletions test/TestAsset/StringSerializable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
/**
* @see https://github.com/zendframework/zend-expressive-hal for the canonical source repository
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (https://www.zend.com)
* @license https://github.com/zendframework/zend-expressive-hal/blob/master/LICENSE.md New BSD License
*/

declare(strict_types=1);

namespace ZendTest\Expressive\Hal\TestAsset;

class StringSerializable
{
public function __toString()
{
return __METHOD__;
}
}

0 comments on commit 8dd77ec

Please sign in to comment.