Skip to content

Commit

Permalink
migrate PHPTs to PHPUnit
Browse files Browse the repository at this point in the history
  • Loading branch information
ashnazg committed Jan 30, 2017
1 parent b00f400 commit 012d64d
Show file tree
Hide file tree
Showing 44 changed files with 1,531 additions and 1,203 deletions.
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,14 @@ composer.phar
vendor
README.html
dist/

# Eclipse
.buildpath
.project
.settings

# tarballs
*.tgz

# buildtools
build/
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ script:
- pear list
- pear install --force package.xml
- pear list
- pear run-tests --showdiff -r tests
- pear package
- pear package-validate
- pear install --force *.tgz
Expand Down
2 changes: 1 addition & 1 deletion XML/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class XML_Util
*/
public static function apiVersion()
{
return '1.1';
return '1.4';
}

/**
Expand Down
39 changes: 20 additions & 19 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,26 @@
<dir baseinstalldir="/" name="/">
<file baseinstalldir="/" name="examples/example.php" role="doc" />
<file baseinstalldir="/" name="examples/example2.php" role="doc" />
<file baseinstalldir="/" name="tests/testBasic_apiVersion.phpt" role="test" />
<file baseinstalldir="/" name="tests/testBasic_attributesToString.phpt" role="test" />
<file baseinstalldir="/" name="tests/testBasic_collapseEmptyTags.phpt" role="test" />
<file baseinstalldir="/" name="tests/testBasic_createCDataSection.phpt" role="test" />
<file baseinstalldir="/" name="tests/testBasic_createComment.phpt" role="test" />
<file baseinstalldir="/" name="tests/testBasic_createEndElement.phpt" role="test" />
<file baseinstalldir="/" name="tests/testBasic_createStartElement.phpt" role="test" />
<file baseinstalldir="/" name="tests/testBasic_createTag.phpt" role="test" />
<file baseinstalldir="/" name="tests/testBasic_createTagFromArray.phpt" role="test" />
<file baseinstalldir="/" name="tests/testBasic_getDocTypeDeclaration.phpt" role="test" />
<file baseinstalldir="/" name="tests/testBasic_getXmlDeclaration.phpt" role="test" />
<file baseinstalldir="/" name="tests/testBasic_isValidName.phpt" role="test" />
<file baseinstalldir="/" name="tests/testBasic_raiseError.phpt" role="test" />
<file baseinstalldir="/" name="tests/testBasic_replaceEntities.phpt" role="test" />
<file baseinstalldir="/" name="tests/testBasic_reverseEntities.phpt" role="test" />
<file baseinstalldir="/" name="tests/testBasic_splitQualifiedName.phpt" role="test" />
<file baseinstalldir="/" name="tests/testBug_4950.phpt" role="test" />
<file baseinstalldir="/" name="tests/testBug_5392.phpt" role="test" />
<file baseinstalldir="/" name="tests/testBug_18343.phpt" role="test" />
<file baseinstalldir="/" name="tests/AbstractUnitTests.php" role="test" />
<file baseinstalldir="/" name="tests/ApiVersionTests.php" role="test" />
<file baseinstalldir="/" name="tests/AttributesToStringTests.php" role="test" />
<file baseinstalldir="/" name="tests/CollapseEmptyTagsTests.php" role="test" />
<file baseinstalldir="/" name="tests/CreateCDataSectionTests.php" role="test" />
<file baseinstalldir="/" name="tests/CreateCommentTests.php" role="test" />
<file baseinstalldir="/" name="tests/CreateEndElementTests.php" role="test" />
<file baseinstalldir="/" name="tests/CreateStartElementTests.php" role="test" />
<file baseinstalldir="/" name="tests/CreateTagTests.php" role="test" />
<file baseinstalldir="/" name="tests/CreateTagFromArrayTests.php" role="test" />
<file baseinstalldir="/" name="tests/GetDocTypeDeclarationTests.php" role="test" />
<file baseinstalldir="/" name="tests/GetXmlDeclarationTests.php" role="test" />
<file baseinstalldir="/" name="tests/IsValidNameTests.php" role="test" />
<file baseinstalldir="/" name="tests/RaiseErrorTests.php" role="test" />
<file baseinstalldir="/" name="tests/ReplaceEntitiesTests.php" role="test" />
<file baseinstalldir="/" name="tests/ReverseEntitiesTests.php" role="test" />
<file baseinstalldir="/" name="tests/SplitQualifiedNameTests.php" role="test" />
<file baseinstalldir="/" name="tests/Bug4950Tests.php" role="test" />
<file baseinstalldir="/" name="tests/Bug5392Tests.php" role="test" />
<file baseinstalldir="/" name="tests/Bug18343Tests.php" role="test" />
<file baseinstalldir="/" name="XML/Util.php" role="php">
<tasks:replace from="@version@" to="version" type="package-info" />
</file>
Expand Down
3 changes: 1 addition & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<testsuites>
<testsuite name="Test Suite">
<directory suffix=".phpt">tests/</directory>
<directory suffix=".php">tests/</directory>
</testsuite>
</testsuites>

Expand All @@ -24,7 +24,6 @@
<logging>
<log type="coverage-clover" target="build/logs/clover.xml"/>
<log type="coverage-html" target="build/coverage/"/>
<log type="coverage-text" target="php://stdout"/>
</logging>

</phpunit>
10 changes: 10 additions & 0 deletions tests/AbstractUnitTests.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

abstract class AbstractUnitTests extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
// ensure the class is defined, and thus its constants are declared
new XML_Util();
}
}
12 changes: 12 additions & 0 deletions tests/ApiVersionTests.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

class ApiVersionTests extends AbstractUnitTests
{
/**
* @covers XML_Util::apiVersion()
*/
public function testApiVersion()
{
$this->assertEquals('1.4', XML_Util::apiVersion());
}
}
217 changes: 217 additions & 0 deletions tests/AttributesToStringTests.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
<?php

class AttributesToStringTests extends AbstractUnitTests
{
/**
* @covers XML_Util::attributesToString()
*/
public function testAttributesToStringBasicUsage()
{
$original = array('foo' => 'bar','boo' => 'baz',);
$expected = " boo=\"baz\" foo=\"bar\"";
$this->assertEquals($expected, XML_Util::attributesToString($original));
}

/**
* @covers XML_Util::attributesToString()
*/
public function testAttributesToStringWithExplicitSortTrue()
{
$original = array('foo' => 'bar','boo' => 'baz',);
$expected = " boo=\"baz\" foo=\"bar\"";
$sort = true;
$this->assertEquals($expected, XML_Util::attributesToString($original, $sort));
}

/**
* @covers XML_Util::attributesToString()
*/
public function testAttributesToStringWithExplicitSortFalse()
{
$original = array('foo' => 'bar','boo' => 'baz',);
$expected = " foo=\"bar\" boo=\"baz\"";
$sort = false;
$this->assertEquals($expected, XML_Util::attributesToString($original, $sort));
}

/**
* @covers XML_Util::attributesToString()
*/
public function testAttributesToStringWithMultilineFalse()
{
$original = array('foo' => 'bar','boo' => 'baz',);
$expected = " boo=\"baz\" foo=\"bar\"";
$sort = true;
$multiline = false;
$this->assertEquals($expected, XML_Util::attributesToString($original, $sort, $multiline));
}

/**
* @covers XML_Util::attributesToString()
*/
public function testAttributesToStringWithMultilineTrue()
{
$original = array('foo' => 'bar','boo' => 'baz',);
$expected =
<<< EOF
boo="baz"
foo="bar"
EOF;
$sort = true;
$multiline = true;
$this->assertEquals($expected, XML_Util::attributesToString($original, $sort, $multiline));
}

/**
* @covers XML_Util::attributesToString()
*/
public function testAttributesToStringWithExplicitIndent()
{
$original = array('foo' => 'bar','boo' => 'baz',);
$expected = " boo=\"baz\"\n foo=\"bar\"";
$sort = true;
$multiline = true;
$indent = ' '; // 8 spaces
$this->assertEquals($expected, XML_Util::attributesToString($original, $sort, $multiline, $indent));
}

/**
* @covers XML_Util::attributesToString()
*/
public function testAttributesToStringWithExplicitLinebreak()
{
$original = array('foo' => 'bar','boo' => 'baz',);
$expected = " boo=\"baz\"\n^foo=\"bar\"";
$sort = true;
$multiline = true;
$linebreak = '^'; // some dummy character
$this->assertEquals($expected, XML_Util::attributesToString($original, $sort, $multiline, $linebreak));
}

/**
* @covers XML_Util::attributesToString()
*/
public function testAttributesToStringWithOptionsThatIncludesSort()
{
$original = array('foo' => 'bar','boo' => 'baz',);
$options = array(
'multiline' => true,
'indent' => '----',
'linebreak' => "^",
'entities' => XML_UTIL_ENTITIES_XML,
'sort' => true,
);

$expected = " boo=\"baz\"\n----foo=\"bar\"";
$this->assertEquals($expected, XML_Util::attributesToString($original, $options));
}

/**
* @covers XML_Util::attributesToString()
*/
public function testAttributesToStringWithOptionsThatExcludesSort()
{
$original = array('foo' => 'bar','boo' => 'baz',);
$options = array(
'multiline' => true,
'indent' => '----',
'linebreak' => "^",
'entities' => XML_UTIL_ENTITIES_XML,
);

$expected = " boo=\"baz\"\n----foo=\"bar\"";
$this->assertEquals($expected, XML_Util::attributesToString($original, $options));
}

/**
* @covers XML_Util::attributesToString()
*/
public function testAttributesToStringWithEntitiesNone()
{
$original = array("foo" => "b@&r", "boo" => "b><z");
$expected = " boo=\"b><z\" foo=\"b@&r\"";
$sort = true;
$multiline = false;
$linebreak = ' ';
$this->assertEquals($expected, XML_Util::attributesToString($original, $sort, $multiline, $linebreak, PHP_EOL, XML_UTIL_ENTITIES_NONE));
}

/**
* @covers XML_Util::attributesToString()
*/
public function testAttributesToStringWithEntitiesXml()
{
$original = array("foo" => "b@&r", "boo" => "b><z");
$expected = " boo=\"b&gt;&lt;z\" foo=\"b@&amp;r\"";
$sort = true;
$multiline = false;
$linebreak = ' ';
$this->assertEquals($expected, XML_Util::attributesToString($original, $sort, $multiline, $linebreak, PHP_EOL, XML_UTIL_ENTITIES_XML));
}

/**
* @covers XML_Util::attributesToString()
*/
public function testAttributesToStringWithEntitiesXmlRequired()
{
$original = array("foo" => "b@&r", "boo" => "b><z");
$expected = " boo=\"b>&lt;z\" foo=\"b@&amp;r\"";
$sort = true;
$multiline = false;
$linebreak = ' ';
$this->assertEquals($expected, XML_Util::attributesToString($original, $sort, $multiline, $linebreak, PHP_EOL, XML_UTIL_ENTITIES_XML_REQUIRED));
}

/**
* @covers XML_Util::attributesToString()
*/
public function testAttributesToStringWithEntitiesHtml()
{
$original = array("foo" => "b@&r", "boo" => "b><z");
$expected = " boo=\"b&gt;&lt;z\" foo=\"b@&amp;r\"";
$sort = true;
$multiline = false;
$linebreak = ' ';
$this->assertEquals($expected, XML_Util::attributesToString($original, $sort, $multiline, $linebreak, PHP_EOL, XML_UTIL_ENTITIES_HTML));
}

/**
* Tag attributes should not be treated as CDATA,
* so the operation will instead quietly use XML_UTIL_ENTITIES_XML.
*
* @covers XML_Util::attributesToString()
*/
public function testAttributesToStringWithCDataSectionForSingleAttribute()
{
$original = array('foo' => 'bar'); // need exactly one attribute here
$options = array(
'sort' => true, // doesn't matter for this testcase
'multiline' => false, // doesn't matter for this testcase
'indent' => null, // doesn't matter for this testcase
'linebreak' => null, // doesn't matter for this testcase
'entities' => XML_UTIL_CDATA_SECTION, // DOES matter for this testcase
);
$expected = " foo=\"bar\"";
$this->assertEquals($expected, XML_Util::attributesToString($original, $options));
}

/**
* Tag attributes should not be treated as CDATA,
* so the operation will instead quietly use XML_UTIL_ENTITIES_XML.
*
* @covers XML_Util::attributesToString()
*/
public function testAttributesToStringWithCDataSectionForMultipleAttributesAndMultilineFalse()
{
$original = array('foo' => 'bar', 'boo' => 'baz'); // need more than one attribute here
$options = array(
'sort' => true, // doesn't matter for this testcase
'multiline' => false, // DOES matter for this testcase, must be false
'indent' => null, // doesn't matter for this testcase
'linebreak' => null, // doesn't matter for this testcase
'entities' => XML_UTIL_CDATA_SECTION, // DOES matter for this testcase
);
$expected = " boo=\"baz\" foo=\"bar\"";
$this->assertEquals($expected, XML_Util::attributesToString($original, $options));
}
}
48 changes: 48 additions & 0 deletions tests/Bug18343Tests.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/**
* Bug #18343 "Entities in file names decoded during packaging"
*
* No matter what flags are given to createTagFromArray(),
* an attribute must *always* be at least ENTITIES_XML encoded.
*
* @link https://pear.php.net/bugs/bug.php?id=18343
*/
class Bug18343Tests extends AbstractUnitTests
{
private $tagArray = array(
"qname" => "install",
"attributes" => array(
"as" => "Horde/Feed/fixtures/lexicon/http-p.moreover.com-cgi-local-page%2Fo=rss&s=Newsweek",
"name" => "test/Horde/Feed/fixtures/lexicon/http-p.moreover.com-cgi-local-page%2Fo=rss&s=Newsweek",
)
);

public function getFlagsToTest()
{
new XML_Util(); // for constants to be declared

return array(
array('no flag', null),
array('false', false),
array('ENTITIES_NONE', XML_UTIL_ENTITIES_NONE),
array('ENTITIES_XML', XML_UTIL_ENTITIES_XML),
array('ENTITIES_XML_REQUIRED', XML_UTIL_ENTITIES_XML_REQUIRED),
array('ENTITIES_HTML', XML_UTIL_ENTITIES_HTML),
array('REPLACE_ENTITIES', XML_UTIL_REPLACE_ENTITIES),
);
}

/**
* @dataProvider getFlagsToTest()
*/
public function testCreateTagFromArrayForBug18343($key, $flag)
{
// all flags for the candidate input should return the same result
$expected =
<<< EOF
<install as="Horde/Feed/fixtures/lexicon/http-p.moreover.com-cgi-local-page%2Fo=rss&amp;s=Newsweek" name="test/Horde/Feed/fixtures/lexicon/http-p.moreover.com-cgi-local-page%2Fo=rss&amp;s=Newsweek" />
EOF;
$this->assertEquals($expected, XML_Util::createTagFromArray($this->tagArray, $flag), "Failed bugcheck for $key.");
}
}
Loading

0 comments on commit 012d64d

Please sign in to comment.