Skip to content

Commit

Permalink
Fix caching error in Text.
Browse files Browse the repository at this point in the history
  • Loading branch information
mekras committed Jul 7, 2016
1 parent 814a940 commit 4aeffc0
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 81 deletions.
35 changes: 23 additions & 12 deletions src/Element/Construct/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,7 @@ public function __toString()
$string = $this->getCachedProperty(
'value',
function () {
if ($this->getType() === 'xhtml') {
try {
/** @var \DOMElement $xhtml */
$xhtml = $this->query('xhtml:div', Node::SINGLE | Node::REQUIRED);
} catch (MalformedNodeException $e) {
return '';
}

return Xhtml::extract($xhtml);
}

return $this->getDomElement()->textContent;
return $this->getStringValue();
}
);

Expand Down Expand Up @@ -83,5 +72,27 @@ public function setValue($text, $type = 'text')
{
$this->getDomElement()->setAttribute('type', $type);
$this->getDomElement()->nodeValue = $text;
$this->setCachedProperty('value', $this->getStringValue());
}

/**
* Return string value.
*
* @return string
*/
private function getStringValue()
{
if ($this->getType() === 'xhtml') {
try {
/** @var \DOMElement $xhtml */
$xhtml = $this->query('xhtml:div', Node::SINGLE | Node::REQUIRED);
} catch (MalformedNodeException $e) {
return '';
}

return Xhtml::extract($xhtml);
}

return $this->getDomElement()->textContent;
}
}
69 changes: 0 additions & 69 deletions tests/Element/TextTest.php

This file was deleted.

81 changes: 81 additions & 0 deletions tests/Element/TitleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
/**
* Atom Protocol support
*
* @author Михаил Красильников <m.krasilnikov@yandex.ru>
* @license MIT
*/
namespace Mekras\Atom\Tests\Element;

use Mekras\Atom\Element\Title;
use Mekras\Atom\Tests\TestCase;

/**
* Tests for Mekras\Atom\Element\Content
*/
class TitleTest extends TestCase
{
/**
* Test "text" type
*/
public function testText()
{
$doc = $this->createDocument('Less: &lt;');
$title = new Title($this->createFakeNode(), $doc->documentElement);
static::assertEquals('text', $title->getType());
static::assertEquals('Less: <', (string) $title);
}

/**
* Test "html" type
*/
public function testHtml()
{
$doc = $this->createDocument('<title type="html">&lt;em> &amp;lt; &lt;/em></title>');

$title = new Title($this->createFakeNode(), $doc->documentElement->firstChild);
static::assertEquals('html', $title->getType());
static::assertEquals('<em> &lt; </em>', (string) $title);
}

/**
* Test "xhtml" type
*/
public function testXhtml()
{
$doc = $this->createDocument(
'<title type="xhtml" xmlns:xhtml="http://www.w3.org/1999/xhtml">' .
'<xhtml:div><xhtml:em> &lt; </xhtml:em></xhtml:div>' .
'</title>'
);

$title = new Title($this->createFakeNode(), $doc->documentElement->firstChild);
static::assertEquals('xhtml', $title->getType());
static::assertEquals('<em> &lt; </em>', (string) $title);
}

/**
* __toString should return an empty string in case of error.
*/
public function testRenderError()
{
$doc = $this->createDocument(
'<title type="xhtml" xmlns:xhtml="http://www.w3.org/1999/xhtml"></title>'
);

$title = new Title($this->createFakeNode(), $doc->documentElement->firstChild);
static::assertEquals('', (string) $title);
}

/**
* Test set/get
*/
public function testSetGet()
{
$doc = $this->createDocument('foo');
$title = new Title($this->createFakeNode(), $doc->documentElement);
static::assertEquals('foo', (string) $title);
$title->setValue('bar');
static::assertEquals('bar', (string) $title);
}
}

0 comments on commit 4aeffc0

Please sign in to comment.