Skip to content

Commit

Permalink
Merge pull request #5 from dokudeps/master
Browse files Browse the repository at this point in the history
Adding tests and porting over a few fixes from DokuWiki
  • Loading branch information
kissifrot authored Jul 17, 2022
2 parents bbff73d + 823fa36 commit 4a17452
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 25 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
.project
.idea
.vscode
.phpunit.result.cache
composer.lock
vendor
7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@
}
],
"require": {
"php": ">=5.4.0"
"php": ">=5.4.0",
"ext-xml": "*"
},
"require-dev": {
"phpunit/phpunit": "^8.0"
},
"autoload": {
"psr-4": {
"IXR\\tests\\": "tests/",
"IXR\\": "src/"
}
}
Expand Down
23 changes: 23 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">

<testsuites>
<testsuite name="all">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src/</directory>
</whitelist>
</filter>
</phpunit>
31 changes: 8 additions & 23 deletions src/DataType/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,14 @@ private function parseTimestamp($timestamp)
$this->dateTime = $date->setTimestamp($timestamp);
}

private function parseIso($iso)
{
$formats = [
\DateTime::ATOM,
/* The older version of IXR_Date (which is still used in e.g. WordPress) does not parse
* iso8601 dates using DateTime::createFromFormat(DateTime::ATOM,..),
* but using substr() with fixed offsets.
* The parser does not expect dashes between year, month and day.
* To be compatible with wordpress clients, lets mimic the behaviour here. */
// with timezone
'Ymd\TH:i:sP',
'Ymd\THisP',
// without timezone
'Ymd\TH:i:s',
'Ymd\THis',
];

foreach ($formats as $format) {
$this->dateTime = \DateTime::createFromFormat($format, $iso);
if ($this->dateTime !== false) {
break;
}
}
/**
* Parses more or less complete iso dates and much more, if no timezone given assumes UTC
*
* @param string $iso
* @throws \Exception when no valid date is given
*/
protected function parseIso($iso) {
$this->dateTime = new \DateTime($iso, new \DateTimeZone('UTC'));
}

public function getIso()
Expand Down
2 changes: 1 addition & 1 deletion src/Message/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public function tagClose($parser, $tag)
$valueFlag = true;
break;
case 'string':
$value = (string)trim($this->_currentTagContents);
$value = (string)($this->_currentTagContents);
$valueFlag = true;
break;
case 'dateTime.iso8601':
Expand Down
53 changes: 53 additions & 0 deletions tests/DataType/DateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace IXR\tests\DataType;

use IXR\DataType\Date;
use PHPUnit\Framework\TestCase;

class DateTest extends TestCase
{

/**
* @return array
* @see testParseIso
*/
function provideDates()
{
return [
// full datetime, different formats
['2010-08-17T09:23:14', 1282036994],
['20100817T09:23:14', 1282036994],
['2010-08-17 09:23:14', 1282036994],
['20100817 09:23:14', 1282036994],
['2010-08-17T09:23:14Z', 1282036994],
['20100817T09:23:14Z', 1282036994],

// with timezone
['2010-08-17 09:23:14+0000', 1282036994],
['2010-08-17 09:23:14+00:00', 1282036994],
['2010-08-17 12:23:14+03:00', 1282036994],

// no seconds
['2010-08-17T09:23', 1282036980],
['20100817T09:23', 1282036980],

// no time
['2010-08-17', 1282003200],
[1282036980, 1282036980],
// array('20100817', 1282003200), #this will NOT be parsed, but is assumed to be timestamp
];
}

/**
* @dataProvider provideDates
* @param mixed $input
* @param int $expect
*/
function testParseIso($input, $expect)
{
$dt = new Date($input);
$this->assertEquals($expect, $dt->getTimeStamp());
}

}
110 changes: 110 additions & 0 deletions tests/Message/MessageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

namespace IXR\tests\Message;

use IXR\Message\Message;
use PHPUnit\Framework\TestCase;

class ixr_library_ixr_message_test extends TestCase
{

function testUntypedValue()
{
$xml = '<?xml version="1.0" encoding="UTF-8"?>
<methodCall>
<methodName>wiki.getBackLinks</methodName>
<params>
<param>
<value> change </value>
</param>
</params>
</methodCall>';

$ixrmsg = new Message($xml);
$ixrmsg->parse();

$this->assertEquals($ixrmsg->messageType, 'methodCall');
$this->assertEquals($ixrmsg->methodName, 'wiki.getBackLinks');
$this->assertEquals($ixrmsg->params, [' change ']);
}

function testStringValue()
{
$xml = '<?xml version="1.0" encoding="UTF-8"?>
<methodCall>
<methodName>wiki.getBackLinks</methodName>
<params>
<param>
<value>
<string> change </string>
</value>
</param>
</params>
</methodCall>';

$ixrmsg = new Message($xml);
$ixrmsg->parse();

$this->assertEquals($ixrmsg->messageType, 'methodCall');
$this->assertEquals($ixrmsg->methodName, 'wiki.getBackLinks');
$this->assertEquals($ixrmsg->params, [' change ']);
}

function testEmptyValue()
{
$xml = '<?xml version="1.0" encoding="UTF-8"?>
<methodCall>
<methodName>wiki.getBackLinks</methodName>
<params>
<param>
<value>
<string></string>
</value>
</param>
</params>
</methodCall>';

$ixrmsg = new Message($xml);
$ixrmsg->parse();

$this->assertEquals($ixrmsg->messageType, 'methodCall');
$this->assertEquals($ixrmsg->methodName, 'wiki.getBackLinks');
$this->assertEquals($ixrmsg->params, ['']);
}

function testStruct()
{
$xml = '<?xml version=\'1.0\'?>
<methodCall>
<methodName>wiki.putPage</methodName>
<params>
<param>
<value><string>start</string></value>
</param>
<param>
<value><string>test text </string></value>
</param>
<param>
<value><struct>
<member>
<name>sum</name>
<value><string>xmlrpc edit</string></value>
</member>
<member>
<name>minor</name>
<value><string>1</string></value>
</member>
</struct></value>
</param>
</params>
</methodCall>';

$ixrmsg = new Message($xml);
$ixrmsg->parse();

$this->assertEquals($ixrmsg->messageType, 'methodCall');
$this->assertEquals($ixrmsg->methodName, 'wiki.putPage');
$this->assertEquals($ixrmsg->params, ['start', 'test text ', ['sum' => 'xmlrpc edit', 'minor' => '1']]);
}

}

0 comments on commit 4a17452

Please sign in to comment.