Skip to content

Commit

Permalink
Replace assertTag() with XPath based assertions
Browse files Browse the repository at this point in the history
`assertTag()` has been deprecated as of PHPUnit 4.2 and removed as of
PHPUnit 5.0, so its usage prevents us from updating to a maintained
PHPUnit version. Therefore, we replace `assertTag()` with a couple of
custom XPath based assertions.
  • Loading branch information
cmb69 committed May 31, 2017
1 parent ebf7568 commit 60b83a0
Show file tree
Hide file tree
Showing 28 changed files with 568 additions and 1,150 deletions.
4 changes: 2 additions & 2 deletions build.xml
Expand Up @@ -48,7 +48,7 @@
</target>

<target name="unit-tests" description="runs all unit tests">
<phpunit bootstrap="tests\unit\bootstrap.php" haltonerror="true" haltonfailure="true">
<phpunit bootstrap="tests\bootstrap.php" haltonerror="true" haltonfailure="true">
<formatter type="plain" usefile="false"/>
<batchtest>
<fileset dir="tests/unit">
Expand All @@ -60,7 +60,7 @@

<target name="validation-tests" description="runs all validation tests">
<fail unless="env.CMSIMPLEDIR" message="CMSIMPLEDIR undefined!"/>
<phpunit haltonerror="true" haltonfailure="true">
<phpunit bootstrap="tests\bootstrap.php" haltonerror="true" haltonfailure="true">
<formatter type="plain" usefile="false"/>
<batchtest>
<fileset dir="tests/validation">
Expand Down
2 changes: 1 addition & 1 deletion coverage.xml
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<phpunit bootstrap="tests/unit/bootstrap.php">
<phpunit bootstrap="tests/bootstrap.php">
<testsuites>
<testsuite name="coverage">
<directory>tests/unit</directory>
Expand Down
134 changes: 134 additions & 0 deletions tests/TestCase.php
@@ -0,0 +1,134 @@
<?php

/**
* Our test case base class.
*
* @category Testing
* @package XH
* @author The CMSimple_XH developers <devs@cmsimple-xh.org>
* @copyright 2013-2017 The CMSimple_XH developers <http://cmsimple-xh.org/?The_Team>
* @license http://www.gnu.org/licenses/gpl-3.0.en.html GNU GPLv3
* @link http://cmsimple-xh.org/
*/

namespace XH;

use PHPUnit_Framework_TestCase;

class TestCase extends PHPUnit_Framework_TestCase
{
/**
* @param string $name
* @param object $scopeObject
* @return PHPUnit_Extension_MockFunction
*/
protected function getFunctionMock($name, $scopeObject)
{
return new FunctionMock($name, $scopeObject);
}

/**
* @param string $name
* @param mixed $value
*/
protected function setConstant($name, $value)
{
if (!defined($name)) {
define($name, $value);
} else {
runkit_constant_redefine($name, $value);
}
}

/**
* Asserts that the XPath $query matches at least one node in $html
*
* @param string $query
* @param string $html
*/
protected function assertXPath($query, $html)
{
$this->assertTrue($this->queryXPath($query, $html)->length > 0);
}

/**
* Assert that the XPath $query doesn't match a node in $html
*
* @param string $query
* @param string $html
*/
protected function assertNotXPath($query, $html)
{
$this->assertTrue($this->queryXPath($query, $html)->length === 0);
}

/**
* Assert that the XPath $query matches $count nodes in $html
*
* @param string $query
* @param int $count
* @param string $html
*/
protected function assertXPathCount($query, $count, $html)
{
$this->assertEquals($count, $this->queryXPath($query, $html)->length);
}

/**
* Asserts that the nodeValue of one of the nodes that matches the XPath
* $query on $html contains the string $text
*
* @param string $query
* @param string $text
* @param string $html
*/
protected function assertXPathContains($query, $text, $html)
{
$nodes = $this->queryXPath($query, $html);
if ($nodes && $nodes->length > 0) {
foreach ($nodes as $node) {
if (!$text || strpos($node->nodeValue, $text) !== false) {
$this->assertTrue(true);
return;
}
}
}
$this->assertTrue(false);
}

/**
* Asserts that the nodeValue of none of the nodes that matches the XPath
* $query on $html contains the string $text
*
* @param string $query
* @param string $text
* @param string $html
*/
protected function assertNotXPathContains($query, $text, $html)
{
$nodes = $this->queryXPath($query, $html);
if ($nodes && $nodes->length > 0) {
foreach ($nodes as $node) {
if (!$text || strpos($node->nodeValue, $text) !== false) {
$this->assertTrue(false);
return;
}
}
}
$this->assertTrue(true);
}

/**
* @param string $query
* @param string $html
* @return DOMNodeList
*/
private function queryXPath($query, $html)
{
libxml_use_internal_errors(true);
$doc = new \DOMDocument;
$doc->loadHTML($html);
$xpath = new \DOMXPath($doc);
return $xpath->query($query);
}
}
2 changes: 1 addition & 1 deletion tests/unit/bootstrap.php → tests/bootstrap.php
Expand Up @@ -9,7 +9,7 @@
require_once './cmsimple/compat.php';

require_once './tests/unit/FunctionMock.php';
require_once './tests/unit/TestCase.php';
require_once './tests/TestCase.php';
require_once './tests/unit/ControllerLogInOutTestCase.php';

spl_autoload_register(function ($className) {
Expand Down
75 changes: 25 additions & 50 deletions tests/unit/AdminMenuTest.php
Expand Up @@ -48,6 +48,7 @@ private function setUpLocalization()
$tx = array(
'editmenu' => array(
'backups' => 'Backups',
'change_password' => 'Password',
'configuration' => 'Configuration',
'downloads' => 'Downloads',
'edit' => 'Edit mode',
Expand Down Expand Up @@ -82,16 +83,11 @@ public function tearDown()
public function testShowsItem($item, $url, $edit = false)
{
$GLOBALS['edit'] = $edit;
$matcher = array(
'tag' => 'a',
'attributes' => array('href' => $url),
'content' => $item,
'ancestor' => array(
'tag' => 'div',
'id' => 'xh_adminmenu'
)
$this->assertXPathContains(
sprintf('//div[@id="xh_adminmenu"]//a[@href="%s"]', $url),
$item,
XH_adminMenu($this->plugins)
);
$this->assertMatches($matcher);
}

public function itemData()
Expand Down Expand Up @@ -127,15 +123,11 @@ public function itemData()
public function testShowsPluginsInColumns($count, $style)
{
$this->plugins = range(1, $count);
$matcher = array(
'tag' => 'a',
'content' => '1',
'ancestor' => array(
'tag' => 'ul',
'attributes' => array('style' => $style)
)
$this->assertXPathContains(
sprintf('//ul[@style="%s"]//a', $style),
'1',
XH_adminMenu($this->plugins)
);
$this->assertMatches($matcher);
}

public function pluginData()
Expand All @@ -151,15 +143,11 @@ public function pluginData()
public function testShowsAllPluginItems()
{
$this->plugins = range(1, 10);
$matcher = array(
'tag' => 'ul',
'attributes' => array('style' => 'width:125px; margin-left: 0px'),
'children' => array(
'count' => 10,
'only' => array('tag' => 'li')
)
$this->assertXPathCount(
'//ul[@style="width:125px; margin-left: 0px"]/li',
10,
XH_adminMenu($this->plugins)
);
$this->assertMatches($matcher);
}

public function testShowsAllVisiblePluginItems()
Expand All @@ -168,15 +156,11 @@ public function testShowsAllVisiblePluginItems()

$cf = array('plugins' => array('hidden' => '1, 5, 10'));
$this->plugins = range(1, 10);
$matcher = array(
'tag' => 'ul',
'attributes' => array('style' => 'width:125px; margin-left: 0px'),
'children' => array(
'count' => 7,
'only' => array('tag' => 'li')
)
$this->assertXPathCount(
'//ul[@style="width:125px; margin-left: 0px"]/li',
7,
XH_adminMenu($this->plugins)
);
$this->assertMatches($matcher);
}

public function testRegisterPluginMenuItemReturnsRegisteredItems()
Expand Down Expand Up @@ -215,31 +199,22 @@ public function testRegisterPluginMenuItemReturnsRegisteredItems()
public function testShowsRegisteredPluginMenuItem()
{
$this->plugins = array('foo');
$matcher = array(
'tag' => 'a',
'content' => 'Config',
'attributes' => array(
'href' => '?&foo&admin=plugin_config&action=plugin_edit'
)
$this->assertXPathContains(
'//a[@href="?&foo&admin=plugin_config&action=plugin_edit"]',
'Config',
XH_adminMenu($this->plugins)
);
$this->assertMatches($matcher);
}

public function testEditModeLinksToStartPage()
{
global $s, $tx;

$s = -1;
$matcher = array(
'tag' => 'a',
'content' => $tx['editmenu']['edit'],
'attributes' => array('href' => '/?Welcome&edit')
$this->assertXPathContains(
'//a[@href="/?Welcome&edit"]',
$tx['editmenu']['edit'],
XH_adminMenu($this->plugins)
);
$this->assertMatches($matcher);
}

private function assertMatches($matcher)
{
@$this->assertTag($matcher, XH_adminMenu($this->plugins));
}
}
17 changes: 7 additions & 10 deletions tests/unit/BackupTest.php
Expand Up @@ -114,12 +114,11 @@ public function testDoesntCreateBackupWhenLatestBackupIsUpToDate()
public function testReportsCreationOfBackup()
{
touch("{$this->contentFolder}content.htm");
$matcher = array(
'tag' => 'p',
'attributes' => array('class' => 'xh_info'),
'content' => 'created'
$this->assertXPathContains(
'//p[@class="xh_info"]',
'created',
$this->subject->execute()
);
@$this->assertTag($matcher, $this->subject->execute());
}

public function testDeletesTooOldBackups()
Expand Down Expand Up @@ -158,12 +157,10 @@ public function testReportsDeletionOfBackup()
touch("{$this->contentFolder}19700101_000102_content.htm");
touch("{$this->contentFolder}19700102_000102_content.htm");
touch("{$this->contentFolder}19700103_000102_content.htm");
$matcher = array(
'tag' => 'p',
'attributes' => array('class' => 'xh_info'),
'content' => 'deleted'
$this->assertXPath(
'//p[@class="xh_info"]',
$this->subject->execute()
);
@$this->assertTag($matcher, $this->subject->execute());
}

public function testReportsDeletionFailure()
Expand Down

0 comments on commit 60b83a0

Please sign in to comment.