From 96cc74dea6fecba1b741af14285cfb3dafe3fda2 Mon Sep 17 00:00:00 2001 From: Daniel Neis Araujo Date: Tue, 14 May 2013 21:56:52 -0300 Subject: [PATCH] Some PHPUnit to test the "happy way" --- OAI2ServerTest.php | 305 +++++++++++++++++++++++++++++++++++++++++++++ oai2.php | 35 ++++-- oai2server.php | 22 ++-- oai2xml.php | 7 -- tests.php | 174 -------------------------- 5 files changed, 345 insertions(+), 198 deletions(-) create mode 100644 OAI2ServerTest.php delete mode 100644 tests.php diff --git a/OAI2ServerTest.php b/OAI2ServerTest.php new file mode 100644 index 0000000..99da377 --- /dev/null +++ b/OAI2ServerTest.php @@ -0,0 +1,305 @@ +xml = new DomDocument("1.0", "UTF-8"); + $xml = ' + + '.date('Y-m-d\TH:i:s\Z').' + '; + $this->xml->loadXML($xml); + $this->uri = 'test.oai_pmh'; + } + + function testIdentify() { + $xml = 'test.oai_pmh + + OAI2 PMH Test + http://198.199.108.242/~neis/oai_pmh/oai2.php + 2.0 + danielneis@gmail.com + 2013-01-01T12:00:00Z + no + YYYY-MM-DDThh:mm:ssZ + '; + + $f = $this->xml->createDocumentFragment(); + $f->appendXML($xml); + $this->xml->documentElement->appendChild($f); + + $return = true; + $uri = $this->uri; + $args = array('verb' => 'Identify'); + + $response = require('oai2.php'); + + $this->assertEqualXMLStructure($this->xml->firstChild, $response->firstChild); + } + + function testIdentifyIllegalParameter() { + $verb = 'Identify'; + $args = array('test' => 'test'); + } + + function testListMetadataFormats() { + $xml = 'test.oai_pmh + + + rif + http://services.ands.org.au/sandbox/orca/schemata/registryObjects.xsd + http://ands.org.au/standards/rif-cs/registryObjects/ + + + oai_dc + http://www.openarchives.org/OAI/2.0/oai_dc.xsd + http://www.openarchives.org/OAI/2.0/oai_dc/ + + '; + + $f = $this->xml->createDocumentFragment(); + $f->appendXML($xml); + $this->xml->documentElement->appendChild($f); + + $return = true; + $uri = $this->uri; + $args = array('verb' => 'ListMetadataFormats'); + + $response = require('oai2.php'); + + $this->assertEqualXMLStructure($this->xml->firstChild, $response->firstChild); + } + + function testListMetadataFormatsIdentifier() { + $verb = 'ListMetadataFormats'; + $args = array('identifier' => 'a.b.c'); + } + + function testListMetadataFormatsIllegalIdentifier() { + $verb = 'ListMetadataFormats'; + $args = array('identifier' => 'illegalIdentifier'); + } + + function testListSets() { + $xml = 'test.oai_pmh + + + class:collection + Collections + + + math + Mathematics + + + phys + Physics + + + phdthesis + PHD Thesis + + This set contains metadata describing electronic music recordings made during the 1950ies + + + '; + + $f = $this->xml->createDocumentFragment(); + $f->appendXML($xml); + $this->xml->documentElement->appendChild($f); + + $return = true; + $uri = $this->uri; + $args = array('verb' => 'ListSets'); + + $response = require('oai2.php'); + + $this->assertEqualXMLStructure($this->xml->firstChild, $response->firstChild); + } + + function testListSetsResumptionToken() { + $verb = 'ListSets'; + $args = array('resumptionToken' => '????'); + } + + function testListIdentifiersMetadataPrefix() { + $xml = 'test.oai_pmh + +
+ dev.testing.pmh + 2013-05-14T18:41:00Z + class:activity +
+
'; + + $f = $this->xml->createDocumentFragment(); + $f->appendXML($xml); + $this->xml->documentElement->appendChild($f); + + $return = true; + $uri = $this->uri; + $args = array('verb' => 'ListIdentifiers', 'metadataPrefix' => 'oai_dc'); + + $response = require('oai2.php'); + + $this->assertEqualXMLStructure($this->xml->firstChild, $response->firstChild); + } + + function testListIdentifiers() { + $args = array('verb' => 'ListIdentifiers'); + } + function testListIdentifiersResumptionToken() { + $args = array('verb' => 'ListIdentifiers', 'resumptionToken' => '????'); + } + function testListIdentifiersResumptionTokenMetadataPrefix() { + $args = array('verb' => 'ListIdentifiers', 'resumptionToken' => '????', 'metadataPrefix' => 'oai_dc'); + } + function testListIdentifiersMetadataPrefixSet() { + $args = array('verb' => 'ListIdentifiers', 'metadataPrefix' => 'oai_dc', 'set' => 'someSet'); + } + function testListIdentifiersMetadataPrefixFromUntil() { + $args = array('verb' => 'ListIdentifiers', 'metadataPrefix' => 'oai_dc', 'from' => '2000-01-01', 'until' => '2000-01-01'); + } + function testListIdentifiersMetadataPrefixSetFromUntil() { + $args = array('verb' => 'ListIdentifiers', 'metadataPrefix' => 'oai_dc', + 'set' => '????', 'from' => '2000-01-01', 'until' => '2000-01-01'); + } + function testListIdentifiersMetadataPrefixIllegalSetIllegalFromUntil() { + $args = array('verb' => 'ListIdentifiers', 'metadataPrefix' => 'oai_dc', + 'set' => 'really_wrong_set', + 'from' => 'some_random_from', 'until' => 'some_random_until'); + } + function testListIdentifiersDifferentGranularity() { + $args = array('verb' => 'ListIdentifiers', 'resumptionToken' => '????', 'metadataPrefix' => 'oai_dc', + 'from' => '2000-01-01', 'until' => '2000-01-01T00:00:00Z'); + } + function testListIdentifiersFromGreaterThanUntil() { + $args = array('verb' => 'ListIdentifiers', 'resumptionToken' => '????', 'metadataPrefix' => 'oai_dc', + 'from' => '2013-01-01', 'until' => '2000-01-01T00:00:00Z'); + } + function testListIdentifiersIllegalMetadataPrefix() { + $args = array('verb' => 'ListIdentifiers', 'metadataPrefix' => 'illegalPrefix'); + } + function testListIdentifiersMetadataPrefixMetadataPrefix() { + $args = array('verb' => 'ListIdentifiers', 'metadataPrefix' => 'oai_dc', 'metadataPrefix' => 'oai_dc'); + } + function testListIdentifiersIllegalResumptionToken() { + $args = array('verb' => 'ListIdentifiers', 'resumptionToken' => 'illegalToken'); + } + function testListIdentifiersMetadataPrefixFrom() { + $args = array('verb' => 'ListIdentifiers', 'metadataPrefix' => 'oai_dc', 'from' => '2001-01-01T00:00:00Z'); + } + function testListIdentifiersMetadataPrefixFromYear() { + $args = array('verb' => 'ListIdentifiers', 'metadataPrefix' => 'oai_dc', 'from' => '2001'); + } + + function testListRecords() { + $verb = 'ListRecords'; + $args = array('verb' => 'ListRecords'); + } + function testListRecordsMetadataPrefix() { + + $xml = 'test.oai_pmh + + +
+ dev.testing.pmh + 2013-05-14T18:11:00Z + class:activity +
+ + + Testing records + Neis + + +
+
'; + + $f = $this->xml->createDocumentFragment(); + $f->appendXML($xml); + $this->xml->documentElement->appendChild($f); + + $return = true; + $uri = $this->uri; + $args = array('verb' => 'ListRecords', 'metadataPrefix' => 'oai_dc'); + + $response = require('oai2.php'); + + $this->assertEqualXMLStructure($this->xml->firstChild, $response->firstChild); + } + function testListRecordsMetadataPrefixFromUntil() { + $args = array('verb' => 'ListRecords', 'metadataPrefix' => 'oai_dc', 'from' => '2000-01-01', 'until' => '2000-01-01'); + } + function testListRecordsResumptionToken() { + $args = array('verb' => 'ListRecords', 'resumptionToken' => '????'); + } + function testListRecordsMetadataPrefixIllegalSetIllegalFromUntil() { + $args = array('verb' => 'ListRecords', 'metadataPrefix' => 'oai_dc', + 'set' => 'illegalSet', + 'from' => 'some_random_from', 'until' => 'some_random_until'); + } + function testListRecordsDifferentGranularity() { + $args = array('verb' => 'ListRecords', 'resumptionToken' => '????', 'metadataPrefix' => 'oai_dc', + 'from' => '2000-01-01', 'until' => '2000-01-01T00:00:00Z'); + } + function testListRecordsUntilBeforeEarliestDatestamp() { + $args = array('verb' => 'ListRecords', 'metadataPrefix' => 'oai_dc', 'until' => '1969-01-01T00:00:00Z'); + } + function testListRecordsIllegalResumptionToken() { + $args = array('verb' => 'ListRecords', 'resumptionToken' => 'illegalToken'); + } + + function testGetRecord() { + $args = array('verb' => 'GetRecord'); + } + function testGetRecordIdentifier() { + $args = array('verb' => 'GetRecord', 'identifier' => 'a.b.c'); + } + function testGetRecordIdentifierMetadataPrefix() { + + $xml = 'test.oai_pmh + + +
+ a.b.c + 2013-05-14T18:08:00Z + class:activity +
+ + + Testing records + Neis + + +
+
'; + $f = $this->xml->createDocumentFragment(); + $f->appendXML($xml); + $this->xml->documentElement->appendChild($f); + + $return = true; + $uri = $this->uri; + $args = array('verb' => 'GetRecord', 'identifier' => 'a.b.c', 'metadataPrefix' => 'oai_dc'); + + $response = require('oai2.php'); + + $this->assertEqualXMLStructure($this->xml->firstChild, $response->firstChild); + } + function testGetRecordIdentifierIllegalMetadataPrefix() { + $args = array('verb' => 'GetRecord', 'identifier' => 'a.b.c', 'metadataPrefix' => 'illegalPrefix'); + } + function testGetRecordMetadataPrefix() { + $args = array('verb' => 'GetRecord', 'metadataPrefix' => 'oai_dc'); + } + function testGetRecordIllegalIdentifierMetadataPrefix() { + $args = array('verb' => 'GetRecord', 'identifier' => 'illegalID', 'metadataPrefix' => 'oai_dc'); + } + function testGetRecordInvalidIdentifierMetadataPrefix() { + $args = array('verb' => 'GetRecord', 'identifier' => 'invalidID', 'metadataPrefix' => 'oai_dc'); + } + + function testIllegalVerb() { + $args = array('verb' => 'IllegalVerb'); + } +} diff --git a/oai2.php b/oai2.php index a00015d..479ae5a 100644 --- a/oai2.php +++ b/oai2.php @@ -15,8 +15,8 @@ require_once('oai2server.php'); * */ $identifyResponse = array(); -$identifyResponse["repositoryName"] = 'Moodle Neis'; -$identifyResponse["baseURL"] = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['SCRIPT_NAME']; +$identifyResponse["repositoryName"] = 'OAI2 PMH Test'; +$identifyResponse["baseURL"] = 'http://198.199.108.242/~neis/oai_pmh/oai2.php'; $identifyResponse["protocolVersion"] = '2.0'; $identifyResponse['adminEmail'] = 'danielneis@gmail.com'; $identifyResponse["earliestDatestamp"] = '2013-01-01T12:00:00Z'; @@ -30,7 +30,7 @@ $identifyResponse["deletedRecord"] = 'no'; // How your repository handles deleti // maintained. It MAY reveal a deleted status for records. $identifyResponse["granularity"] = 'YYYY-MM-DDThh:mm:ssZ'; -$example_record = array('identifier' => 'dev.testing.pmh', +$example_record = array('identifier' => 'a.b.c', 'datestamp' => date('Y-m-d-H:s'), 'set' => 'class:activity', 'metadata' => array( @@ -48,11 +48,18 @@ $example_record = array('identifier' => 'dev.testing.pmh', ) )); -$oai2 = new OAI2Server('neis.moodle.ufsc.br', $_GET, $identifyResponse, +/* unit tests ;) */ +if (!isset($args)) { + $args = $_GET; +} +if (!isset($uri)) { + $uri = 'test.oai_pmh'; +} +$oai2 = new OAI2Server($uri, $args, $identifyResponse, array( 'ListMetadataFormats' => function($identifier = '') { - if ($identifier == 'a.b.c') { + if (!empty($identifier) && $identifier != 'a.b.c') { throw new OAI2Exception('idDoesNotExist', '', $identifier); } return @@ -87,8 +94,7 @@ $oai2 = new OAI2Server('neis.moodle.ufsc.br', $_GET, $identifyResponse, }, 'ListRecords' => - function($metadataPrefix, $from = '', $until = '', $set = '', $count = false, $deliveredRecords = 0, $maxItems = 0) { - global $example_record; + function($metadataPrefix, $from = '', $until = '', $set = '', $count = false, $deliveredRecords = 0, $maxItems = 0) use ($example_record) { // throws new OAI2Exception('noRecordsMatch') // throws new OAI2Exception('noSetHierarchy') if ($count) { @@ -98,12 +104,21 @@ $oai2 = new OAI2Server('neis.moodle.ufsc.br', $_GET, $identifyResponse, }, 'GetRecord' => - function($identifier, $metadataPrefix) { - global $example_record; - if ($identifier == 'a.b.c') { + function($identifier, $metadataPrefix) use ($example_record) { + if ($identifier != 'a.b.c') { throw new OAI2Exception('idDoesNotExist', '', $identifier); } return $example_record; }, ) ); + +$response = $oai2->response(); +if (isset($return)) { + return $response; +} else { + $response->formatOutput = true; + $response->preserveWhiteSpace = false; + header('Content-Type: text/xml'); + echo $response->saveXML(); +} diff --git a/oai2server.php b/oai2server.php index ee918ba..68bafdf 100644 --- a/oai2server.php +++ b/oai2server.php @@ -46,8 +46,11 @@ class OAI2Server { } } + } + + public function response() { if (empty($this->errors)) { - $this->response->display(); + return $this->response->doc; } else { $errorResponse = new OAI2XMLResponse($this->uri, $this->verb, $this->args); $oai_node = $errorResponse->doc->documentElement; @@ -55,7 +58,7 @@ class OAI2Server { $node = $errorResponse->addChild($oai_node,"error",$e->getMessage()); $node->setAttribute("code",$e->getOAI2Code()); } - $errorResponse->display(); + return $errorResponse->doc; } } @@ -79,9 +82,14 @@ class OAI2Server { $this->errors[] = new OAI2Exception('badArgument', $argument, $value); } } + if (isset($this->args['identifier'])) { + $identifier = $this->args['identifier']; + } else { + $identifier = ''; + } if (empty($this->errors)) { try { - if ($formats = call_user_func($this->listMetadataFormatsCallback, $this->args['identifier'])) { + if ($formats = call_user_func($this->listMetadataFormatsCallback, $identifier)) { foreach($formats as $key => $val) { $cmf = $this->response->addToVerbNode("metadataFormat"); $this->response->addChild($cmf,'metadataPrefix',$key); @@ -111,7 +119,7 @@ class OAI2Server { } else { $resumptionToken = null; } - if (!empty($this->errors)) { + if (empty($this->errors)) { if ($sets = call_user_func($this->listSetsCallback, $resumptionToken)) { foreach($sets as $set) { @@ -231,7 +239,7 @@ class OAI2Server { } } - if (!empty($this->errors)) { + if (empty($this->errors)) { try { $records_count = call_user_func($this->listRecordsCallback, $metadataPrefix, $from, $until, $set, true); @@ -248,8 +256,8 @@ class OAI2Server { (($this->identifyResponse['deletedRecord'] == 'transient') || ($this->identifyResponse['deletedRecord'] == 'persistent'))); - if($this->args['verb'] == 'ListRecords') { - $cur_record = $this->response->createToVerNode('record'); + if($this->verb == 'ListRecords') { + $cur_record = $this->response->addToVerbNode('record'); $cur_header = $this->response->createHeader($identifier, $datestamp,$setspec,$cur_record); if (!$status_deleted) { $this->add_metadata($cur_record, $record); diff --git a/oai2xml.php b/oai2xml.php index 68da71d..33d7de8 100644 --- a/oai2xml.php +++ b/oai2xml.php @@ -24,13 +24,6 @@ class OAI2XMLResponse { $this->verbNode = $this->addChild($this->doc->documentElement,$this->verb); } - function display() { - $this->doc->formatOutput = true; - $this->doc->preserveWhiteSpace = false; - header('Content-Type: text/xml'); - echo $this->doc->saveXML(); - } - /** * Add a child node to a parent node on a XML Doc: a worker function. * diff --git a/tests.php b/tests.php deleted file mode 100644 index 7f7e3ab..0000000 --- a/tests.php +++ /dev/null @@ -1,174 +0,0 @@ - 'test'); - } - - function testListMetadataFormats() { - $verb = 'ListMetadataFormats'; - } - - function testListMetadataFormatsIdentifier() { - $verb = 'ListMetadataFormats'; - $args = array('identifier' => 'a.b.c'); - } - - function testListMetadataFormatsIllegalIdentifier() { - $verb = 'ListMetadataFormats'; - $args = array('identifier' => 'illegalIdentifier'); - } - - function testListSets() { - $verb = 'ListSets'; - } - - function testListSetsResumptionToken() { - $verb = 'ListSets'; - $args = array('resumptionToken' => '????'); - } - - function testListIdentifiersMetadataPrefix() { - $verb = 'ListIdentifiers'; - $args = array('metadataPrefix' => 'oai_dc'); - } - - function testListIdentifiersResumptionToken() { - $verb = 'ListIdentifiers'; - $args = array('resumptionToken' => '????'); - } - - function testListIdentifiersResumptionTokenMetadataPrefix() { - $verb = 'ListIdentifiers'; - $args = array('resumptionToken' => '????', 'metadataPrefix' => 'oai_dc'); - } - - function testListIdentifiersMetadataPrefixSet() { - $verb = 'ListIdentifiers'; - $args = array('metadataPrefix' => 'oai_dc', 'set' => 'someSet'); - } - - function testListIdentifiersMetadataPrefixFromUntil() { - $verb = 'ListIdentifiers'; - $args = array('metadataPrefix' => 'oai_dc', 'from' => '2000-01-01', 'until' => '2000-01-01'); - } - - function testListIdentifiersMetadataPrefixSetFromUntil() { - $verb = 'ListIdentifiers'; - $args = array('metadataPrefix' => 'oai_dc', - 'set' => '????', 'from' => '2000-01-01', 'until' => '2000-01-01'); - } - - function testListIdentifiersMetadataPrefixIllegalSetIllegalFromUntil() { - $verb = 'ListIdentifiers'; - $args = array('metadataPrefix' => 'oai_dc', - 'set' => 'really_wrong_set', - 'from' => 'some_random_from', 'until' => 'some_random_until'); - } - - function testListIdentifiersDifferentGranularity() { - $verb = 'ListIdentifiers'; - $args = array('resumptionToken' => '????', 'metadataPrefix' => 'oai_dc', - 'from' => '2000-01-01', 'until' => '2000-01-01T00:00:00Z'); - } - - function testListIdentifiersFromGreaterThanUntil() { - $verb = 'ListIdentifiers'; - $args = array('resumptionToken' => '????', 'metadataPrefix' => 'oai_dc', - 'from' => '2013-01-01', 'until' => '2000-01-01T00:00:00Z'); - } - function testListIdentifiers() { - $verb = 'ListIdentifiers'; - $args = array(); - } - function testListIdentifiersIllegalMetadataPrefix() { - $verb = 'ListIdentifiers'; - $args = array('metadataPrefix' => 'illegalPrefix'); - } - function testListIdentifiersMetadataPrefixMetadataPrefix() { - $verb = 'ListIdentifiers'; - $args = array('metadataPrefix' => 'oai_dc', 'metadataPrefix' => 'oai_dc'); - } - function testListIdentifiersIllegalResumptionToken() { - $verb = 'ListIdentifiers'; - $args = array('resumptionToken' => 'illegalToken'); - } - function testListIdentifiersMetadataPrefixFrom() { - $verb = 'ListIdentifiers'; - $args = array('metadataPrefix' => 'oai_dc', 'from' => '2001-01-01T00:00:00Z'); - } - function testListIdentifiersMetadataPrefixFromYear() { - $verb = 'ListIdentifiers'; - $args = array('metadataPrefix' => 'oai_dc', 'from' => '2001'); - } - - function testListRecords() { - $verb = 'ListRecords'; - $args = array(); - } - function testListRecordsMetadataPrefixFromUntil() { - $verb = 'ListRecords'; - $args = array('metadataPrefix' => 'oai_dc', 'from' => '2000-01-01', 'until' => '2000-01-01'); - } - - function testListRecordsResumptionToken() { - $verb = 'ListRecords'; - $args = array('resumptionToken' => '????'); - } - - function testListRecordsMetadataPrefixIllegalSetIllegalFromUntil() { - $verb = 'ListRecords'; - $args = array('metadataPrefix' => 'oai_dc', - 'set' => 'illegalSet', - 'from' => 'some_random_from', 'until' => 'some_random_until'); - } - function testListRecordsDifferentGranularity() { - $verb = 'ListRecords'; - $args = array('resumptionToken' => '????', 'metadataPrefix' => 'oai_dc', - 'from' => '2000-01-01', 'until' => '2000-01-01T00:00:00Z'); - } - function testListRecordsUntilBeforeEarliestDatestamp() { - $verb = 'ListRecords'; - $args = array('metadataPrefix' => 'oai_dc', 'until' => '1969-01-01T00:00:00Z'); - } - function testListRecordsIllegalResumptionToken() { - $verb = 'ListRecords'; - $args = array('resumptionToken' => 'illegalToken'); - } - - function testGetRecordIdentifier() { - $verb = 'GetRecord'; - $args = array('identifier' => 'a.b.c'); - } - function testGetRecordIdentifierMetadataPrefix() { - $verb = 'GetRecord'; - $args = array('identifier' => 'a.b.c', 'metadataPrefix' => 'oai_dc'); - } - function testGetRecordIdentifierIllegalMetadataPrefix() { - $verb = 'GetRecord'; - $args = array('identifier' => 'a.b.c', 'metadataPrefix' => 'illegalPrefix'); - } - function testGetRecordMetadataPrefix() { - $verb = 'GetRecord'; - $args = array('metadataPrefix' => 'oai_dc'); - } - function testGetRecordIllegalIdentifierMetadataPrefix() { - $verb = 'GetRecord'; - $args = array('identifier' => 'illegalID', 'metadataPrefix' => 'oai_dc'); - } - function testGetRecordInvalidIdentifierMetadataPrefix() { - $verb = 'GetRecord'; - $args = array('identifier' => 'invalidID', 'metadataPrefix' => 'oai_dc'); - } - - function testIllegalVerb() { - $verb = 'IllegalVerb'; - $args = array(); - } -}