Skip to content

Commit

Permalink
Issue 84 (#86)
Browse files Browse the repository at this point in the history
* write failing test

* Split text into 255-byte chunks.

* Updated CHANGELOG
  • Loading branch information
samuelwilliams authored Nov 29, 2020
1 parent ff806b8 commit dfe0cd0
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-4.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,4 @@ must instantiate the object first, and then call `fromText` method. All paramete
* [Issue #80](https://github.com/Badcow/DNS/issues/80) - Parser now supports the $INCLUDE directive to import and parse
child or subdomain zone files.
* [PR #82](https://github.com/Badcow/DNS/pull/82) - Fix character escaping in TXT records. (Thank you, [@fbett](https://github.com/fbett))
* [Issue #84](https://github.com/Badcow/DNS/issues/84) - `TXT::toText()` now splits string into 255-byte chunks. (Thank you, [@fbett](https://github.com/fbett))
1 change: 0 additions & 1 deletion lib/AlignedRdataFormatters.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ public static function TXT(TXT $txt, int $padding): string

$rdata = Tokens::OPEN_BRACKET.Tokens::SPACE;
foreach ($lines as $line) {

$txtSplit = new TXT();
$txtSplit->setText($line);

Expand Down
9 changes: 5 additions & 4 deletions lib/Rdata/TXT.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@ public function getText(): ?string
*/
public function toText(): string
{
return sprintf(
Tokens::DOUBLE_QUOTES . '%s' . Tokens::DOUBLE_QUOTES,
addcslashes($this->text ?? '', Tokens::DOUBLE_QUOTES . '\\')
);
$chunks = array_map(function (string $chunk) {
return sprintf('"%s"', addcslashes($chunk, Tokens::DOUBLE_QUOTES.Tokens::BACKSLASH));
}, str_split($this->text ?? '', 255));

return implode(' ', $chunks);
}

/**
Expand Down
24 changes: 20 additions & 4 deletions tests/Rdata/TxtTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,30 @@ public function testSetText(): void
$this->assertEquals($text, $txt->getText());
}

public function testOutput(): void
public function dp_testToText(): array
{
return [
//[$text, $expectation]
['"This is some quoted text". It\'s a nice piece of text.', '"\"This is some quoted text\". It\'s a nice piece of text."'],
[
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis vel lorem in massa elementum blandit nec sed massa. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec eu purus id arcu venenatis elementum in quis enim. Aenean at urna varius sapien dapibus.',
'"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis vel lorem in massa elementum blandit nec sed massa. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec eu purus id arcu venenatis elementum in quis enim. Aenean at urna varius sapie" "n dapibus."',
],
];
}

/**
* @dataProvider dp_testToText
*
* @param string $text the input text value
* @param string $expectation The expected output of TXT::toText()
*/
public function testToText(string $text, string $expectation): void
{
$text = '"This is some quoted text". It\'s a nice piece of text.';
$expected = '"\"This is some quoted text\". It\'s a nice piece of text."';
$txt = new TXT();
$txt->setText($text);

$this->assertEquals($expected, $txt->toText());
$this->assertEquals($expectation, $txt->toText());
}

public function testFromTxt(): void
Expand Down

1 comment on commit dfe0cd0

@fbett
Copy link
Contributor

@fbett fbett commented on dfe0cd0 Nov 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks good, thanks for this commit!

Please sign in to comment.