Skip to content

Commit

Permalink
Change cURL to WebRequest component
Browse files Browse the repository at this point in the history
  • Loading branch information
byjg committed Jan 18, 2016
1 parent 1cd668a commit 6e2496d
Show file tree
Hide file tree
Showing 8 changed files with 426 additions and 486 deletions.
45 changes: 14 additions & 31 deletions SparQL/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace SparQL;

use ByJG\Util\CurlException;
use ByJG\Util\WebRequest;

class Connection
{

Expand Down Expand Up @@ -38,7 +41,7 @@ public function cgiParams($params = null)
*
* @param type $query
* @param type $timeout
* @return \SparQL\Result
* @return Result
*/
public function query($query, $timeout = null)
{
Expand Down Expand Up @@ -73,38 +76,18 @@ public function dispatchQuery($sparql, $timeout = null)
print "<div class='debug'><a href='" . htmlspecialchars($url) . "'>" . htmlspecialchars($prefixes . $query) . "</a></div>\n";
}

$ch = curl_init($url);
if ($timeout !== null) {
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
}
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Accept: application/sparql-results+xml"
));

$errno = null;
$error = null;

$output = curl_exec($ch);
$info = curl_getinfo($ch);
if (curl_errno($ch)) {
$errno = curl_errno($ch);
$error = 'Curl error: ' . curl_error($ch);
}
$webRequest = new WebRequest($url);

$output = $webRequest->get();

if ($output === '') {
$errno = "-1";
$error = 'URL returned no data';
}
if ($info['http_code'] != 200) {
$errno = $info['http_code'];
$error = 'Bad response, ' . $info['http_code'] . ': ' . $output;
throw new Exception('URL returned no data', -1);
}
curl_close($ch);

if (!is_null($errno)) {
throw new Exception($error, $errno);
if ($webRequest->getLastStatus() != 200) {
throw new Exception(
'Bad response, ' . $webRequest->getLastStatus() . ': ' . $output,
$webRequest->getLastStatus()
);
}

return $output;
Expand Down
16 changes: 9 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
"email": "cjg@ecs.soton.ac.uk"
}
],
"require": {},
"autoload": {
"psr-4": {
"SparQL\\": "SparQL/"
}
},
"license": "LGPL-3.0"
"require": {
"byjg/webrequest": "1.0.*"
},
"autoload": {
"psr-4": {
"SparQL\\": "SparQL/"
}
},
"license": "LGPL-3.0"
}
4 changes: 2 additions & 2 deletions nbproject/project.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
auxiliary.org-netbeans-modules-php-phpunit.bootstrap_2e_create_2e_tests=false
auxiliary.org-netbeans-modules-php-phpunit.bootstrap_2e_create_2e_tests=true
auxiliary.org-netbeans-modules-php-phpunit.bootstrap_2e_enabled=true
auxiliary.org-netbeans-modules-php-phpunit.bootstrap_2e_path=test/bootstrap.php
auxiliary.org-netbeans-modules-php-phpunit.bootstrap_2e_path=tests/bootstrap.php
auxiliary.org-netbeans-modules-php-phpunit.configuration_2e_enabled=false
auxiliary.org-netbeans-modules-php-phpunit.configuration_2e_path=
auxiliary.org-netbeans-modules-php-phpunit.customSuite_2e_enabled=false
Expand Down
165 changes: 82 additions & 83 deletions tests/SparQL/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,87 +8,86 @@
class ConnectionTest extends \PHPUnit_Framework_TestCase
{

const SPARQL_URL = 'http://rdf.ecs.soton.ac.uk/sparql/';
protected static $SPARQL_NS = array("foaf" => "http://xmlns.com/foaf/0.1/");


/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{

}

/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown()
{

}


/**
* @covers SparQL\Connection::query
* @covers SparQL\Connection::ns
*/
public function testQueryOk()
{
$connection = new Connection(self::SPARQL_URL);
foreach (self::$SPARQL_NS as $key => $value)
{
$connection->ns($key, $value);
}

$result = $connection->query("SELECT * WHERE { ?person a foaf:Person . ?person foaf:name ?name } LIMIT 5");

$this->assertEquals(5, $result->numRows());
}

/**
* @covers SparQL\Connection::get
* @covers SparQL\Connection::query
* @covers SparQL\Connection::ns
*/
public function testGetOk()
{
$result = Connection::get(self::SPARQL_URL, "SELECT * WHERE { ?person a foaf:Person . ?person foaf:name ?name } LIMIT 5", self::$SPARQL_NS);
$this->assertEquals(5, count($result));
}

/**
* @expectedException \SparQL\Exception
*/
function test_wrongSparQLDataset()
{
Connection::get("http://localhost/", "SELECT * WHERE { ?person a foaf:Person . ?person foaf:name ?name } LIMIT 5", self::$SPARQL_NS);
}

/**
* @expectedException \SparQL\Exception
*/
function test_wrongSparQLDataset2()
{
Connection::get(self::SPARQL_URL, "SELECT * WHERE { ?person a foaf:Person . ?person foaf:name ?name } LIMIT 5");
}

function test_navigateSparQLDataset()
{
$result = Connection::get(self::SPARQL_URL, "SELECT * WHERE { ?person a foaf:Person . ?person foaf:name ?name } LIMIT 2", self::$SPARQL_NS);

$this->assertEquals(2, count($result));

$this->assertEquals($result[0]["person.type"], "bnode");
$this->assertEquals($result[0]["name.type"], "literal");
$this->assertEquals($result[0]["name.datatype"], "http://www.w3.org/2001/XMLSchema#string");

$this->assertEquals($result[1]["person.type"], "bnode");
$this->assertEquals($result[1]["name.type"], "literal");
$this->assertEquals($result[1]["name.datatype"], "http://www.w3.org/2001/XMLSchema#string");

}

const SPARQL_URL = 'http://rdf.ecs.soton.ac.uk/sparql/';

protected static $SPARQL_NS = array("foaf" => "http://xmlns.com/foaf/0.1/");

/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{

}

/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown()
{

}

/**
* @covers SparQL\Connection::query
* @covers SparQL\Connection::ns
*/
public function testQueryOk()
{
$connection = new Connection(self::SPARQL_URL);
foreach (self::$SPARQL_NS as $key => $value) {
$connection->ns($key, $value);
}

$result = $connection->query("SELECT * WHERE { ?person a foaf:Person . ?person foaf:name ?name } LIMIT 5");

$this->assertEquals(5, $result->numRows());
}

/**
* @covers SparQL\Connection::get
* @covers SparQL\Connection::query
* @covers SparQL\Connection::ns
*/
public function testGetOk()
{
$result = Connection::get(self::SPARQL_URL,
"SELECT * WHERE { ?person a foaf:Person . ?person foaf:name ?name } LIMIT 5", self::$SPARQL_NS);
$this->assertEquals(5, count($result));
}

/**
* @expectedException \ByJG\Util\CurlException
*/
function test_wrongSparQLDataset()
{
Connection::get("http://localhost:9812/",
"SELECT * WHERE { ?person a foaf:Person . ?person foaf:name ?name } LIMIT 5", self::$SPARQL_NS);
}

/**
* @expectedException \SparQL\Exception
*/
function test_wrongSparQLDataset2()
{
Connection::get(self::SPARQL_URL, "SELECT * WHERE { ?person a foaf:Person . ?person foaf:name ?name } LIMIT 5");
}

function test_navigateSparQLDataset()
{
$result = Connection::get(self::SPARQL_URL,
"SELECT * WHERE { ?person a foaf:Person . ?person foaf:name ?name } LIMIT 2", self::$SPARQL_NS);

$this->assertEquals(2, count($result));

$this->assertEquals($result[0]["person.type"], "bnode");
$this->assertEquals($result[0]["name.type"], "literal");
$this->assertEquals($result[0]["name.datatype"], "http://www.w3.org/2001/XMLSchema#string");

$this->assertEquals($result[1]["person.type"], "bnode");
$this->assertEquals($result[1]["name.type"], "literal");
$this->assertEquals($result[1]["name.datatype"], "http://www.w3.org/2001/XMLSchema#string");
}
}
Loading

0 comments on commit 6e2496d

Please sign in to comment.