|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Asparagus\Tests\Integration; |
|
4
|
|
|
|
|
5
|
|
|
use Asparagus\QueryBuilder; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* @license GNU GPL v2+ |
|
9
|
|
|
* @author Bene* < [email protected] > |
|
10
|
|
|
*/ |
|
11
|
|
|
class QueryBuilderTest extends \PHPUnit_Framework_TestCase { |
|
12
|
|
|
|
|
13
|
|
|
private static $prefixes = array( |
|
14
|
|
|
'test' => 'http://www.example.com/test#' |
|
15
|
|
|
); |
|
16
|
|
|
|
|
17
|
|
|
public function testBasicFunctionality() { |
|
18
|
|
|
$queryBuilder = new QueryBuilder( self::$prefixes ); |
|
19
|
|
|
|
|
20
|
|
|
$queryBuilder->select( '?name', '?email' ) |
|
21
|
|
|
->where( '?person', 'test:name', '?name' ) |
|
22
|
|
|
->also( 'test:email', '?email' ) |
|
23
|
|
|
->limit( 10 ); |
|
24
|
|
|
|
|
25
|
|
|
$this->assertIsExpected( 'basic_functionality', $queryBuilder->format() ); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function testOptionalFilter() { |
|
29
|
|
|
$queryBuilder = new QueryBuilder( self::$prefixes ); |
|
30
|
|
|
$queryBuilder->select( '?name' ) |
|
31
|
|
|
->where( '?person', 'test:name', '?name' ) |
|
32
|
|
|
->optional( '?person', 'test:email', '?email' ) |
|
33
|
|
|
->filter( '!BOUND (?email)' ); |
|
34
|
|
|
|
|
35
|
|
|
$this->assertIsExpected( 'optional_filter', $queryBuilder->format() ); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function testUnion() { |
|
39
|
|
|
$queryBuilder = new QueryBuilder( array( |
|
40
|
|
|
'dc10' => 'http://purl.org/dc/elements/1.0/', |
|
41
|
|
|
'dc11' => 'http://purl.org/dc/elements/1.1/' |
|
42
|
|
|
) ); |
|
43
|
|
|
|
|
44
|
|
|
$queryBuilder->select( '?title', '?author' ) |
|
45
|
|
|
->union( |
|
46
|
|
|
$queryBuilder->newSubgraph() |
|
47
|
|
|
->where( '?book', 'dc10:title', '?title' ) |
|
48
|
|
|
->also( 'dc10:creator', '?author' ), |
|
49
|
|
|
$queryBuilder->newSubgraph() |
|
50
|
|
|
->where( '?book', 'dc11:title', '?title' ) |
|
51
|
|
|
->also( 'dc11:creator', '?author' ) |
|
52
|
|
|
); |
|
53
|
|
|
|
|
54
|
|
|
$this->assertIsExpected( 'union', $queryBuilder->format() ); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function testUndefinedPrefixDetected() { |
|
58
|
|
|
$queryBuilder = new QueryBuilder( self::$prefixes ); |
|
59
|
|
|
|
|
60
|
|
|
$queryBuilder->select( '?age' ) |
|
61
|
|
|
->where( '?person', 'test:name', '?name' ) |
|
62
|
|
|
->also( 'nyan:age', '?age' ); |
|
63
|
|
|
|
|
64
|
|
|
$this->setExpectedException( 'RangeException', 'nyan' ); |
|
65
|
|
|
$queryBuilder->getSPARQL(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function testUndefinedVariableDetected() { |
|
69
|
|
|
$queryBuilder = new QueryBuilder( self::$prefixes ); |
|
70
|
|
|
|
|
71
|
|
|
$queryBuilder->select( '?email' ) |
|
72
|
|
|
->where( '?person', 'test:name', '?name' ) |
|
73
|
|
|
->also( 'test:age', '?age' ); |
|
74
|
|
|
|
|
75
|
|
|
$this->setExpectedException( 'RangeException', '?email' ); |
|
76
|
|
|
$queryBuilder->getSPARQL(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
private function assertIsExpected( $name, $sparql ) { |
|
80
|
|
|
$expected = file_get_contents( __DIR__ . '/../data/builder_' . $name . '.rq' ); |
|
81
|
|
|
|
|
82
|
|
|
$this->assertEquals( $expected, $sparql, 'Query didn\'t match the expected content of integration_' . $name . '.rq' ); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
|