GraphBuilderTest::testWhere_invalidSubject()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace Asparagus\Tests;
4
5
use Asparagus\GraphBuilder;
6
use Asparagus\QueryBuilder;
7
use Asparagus\UsageValidator;
8
9
/**
10
 * @covers Asparagus\GraphBuilder
11
 *
12
 * @license GNU GPL v2+
13
 * @author Bene* < [email protected] >
14
 */
15
class GraphBuilderTest extends \PHPUnit_Framework_TestCase {
16
17
	public function testWhere() {
18
		$graphBuilder = new GraphBuilder( new UsageValidator() );
19
		$this->assertSame(
20
			$graphBuilder,
21
			$graphBuilder->where( '?a', '?b', '?c' )
22
		);
23
24
		$this->assertEquals( ' ?a ?b ?c .', $graphBuilder->getSPARQL() );
25
	}
26
27
	public function testWhere_invalidSubject() {
28
		$graphBuilder = new GraphBuilder( new UsageValidator() );
29
		$this->setExpectedException( 'InvalidArgumentException' );
30
31
		$graphBuilder->where( null, '?b', '?c' );
32
	}
33
34
	public function testWhere_invalidPredicate() {
35
		$graphBuilder = new GraphBuilder( new UsageValidator() );
36
		$this->setExpectedException( 'InvalidArgumentException' );
37
38
		$graphBuilder->where( '?a', null, '?c' );
39
	}
40
41
	public function testWhere_invalidObject() {
42
		$graphBuilder = new GraphBuilder( new UsageValidator() );
43
		$this->setExpectedException( 'InvalidArgumentException' );
44
45
		$graphBuilder->where( '?a', '?b', null );
46
	}
47
48
	public function testAlso_knownSubject() {
49
		$graphBuilder = new GraphBuilder( new UsageValidator() );
50
		$graphBuilder->where( '?a', '?b', '?c' );
51
		$this->assertSame(
52
			$graphBuilder,
53
			$graphBuilder->also( '?x', '?y' )
54
		);
55
56
		$this->assertEquals( ' ?a ?b ?c ; ?x ?y .', $graphBuilder->getSPARQL() );
57
	}
58
59
	public function testAlso_knownPredicate() {
60
		$graphBuilder = new GraphBuilder( new UsageValidator() );
61
		$graphBuilder->where( '?a', '?b', '?c' );
62
		$this->assertSame(
63
			$graphBuilder,
64
			$graphBuilder->also( '?x' )
65
		);
66
67
		$this->assertEquals( ' ?a ?b ?c , ?x .', $graphBuilder->getSPARQL() );
68
	}
69
70
	public function testAlso_unknownSubject() {
71
		$graphBuilder = new GraphBuilder( new UsageValidator() );
72
		$this->setExpectedException( 'InvalidArgumentException' );
73
74
		$graphBuilder->also( '?x', '?y' );
75
	}
76
77
	public function testAlso_unknownPredicate() {
78
		$graphBuilder = new GraphBuilder( new UsageValidator() );
79
		$this->setExpectedException( 'InvalidArgumentException' );
80
81
		$graphBuilder->also( '?y' );
82
	}
83
84
	public function testFilter() {
85
		$graphBuilder = new GraphBuilder( new UsageValidator() );
86
		$this->assertSame(
87
			$graphBuilder,
88
			$graphBuilder->filter( 'AVG (?x) > 5' )
89
		);
90
91
		$this->assertEquals( ' FILTER (AVG (?x) > 5)', $graphBuilder->getSPARQL() );
92
	}
93
94
	public function testFilter_invalidExpression() {
95
		$graphBuilder = new GraphBuilder( new UsageValidator() );
96
		$this->setExpectedException( 'InvalidArgumentException' );
97
98
		$graphBuilder->filter( 'FooBar' );
99
	}
100
101
	public function testFilterExists() {
102
		$graphBuilder = new GraphBuilder( new UsageValidator() );
103
		$graphBuilder->where( '?a', '?b', '?c' );
104
		$this->assertSame(
105
			$graphBuilder,
106
			$graphBuilder->filterExists( $graphBuilder )
107
		);
108
109
		$this->assertEquals( ' ?a ?b ?c . FILTER EXISTS { ?a ?b ?c . }', $graphBuilder->getSPARQL() );
110
	}
111
112
	public function testFilterExists_triple() {
113
		$graphBuilder = new GraphBuilder( new UsageValidator() );
114
		$this->assertSame(
115
			$graphBuilder,
116
			$graphBuilder->filterExists( '?a', '?b', '?c' )
117
		);
118
119
		$this->assertEquals( ' FILTER EXISTS { ?a ?b ?c . }', $graphBuilder->getSPARQL() );
120
	}
121
122
	public function testFilterNotExists() {
123
		$graphBuilder = new GraphBuilder( new UsageValidator() );
124
		$graphBuilder->where( '?a', '?b', '?c' );
125
		$this->assertSame(
126
			$graphBuilder,
127
			$graphBuilder->filterNotExists( $graphBuilder )
128
		);
129
130
		$this->assertEquals( ' ?a ?b ?c . FILTER NOT EXISTS { ?a ?b ?c . }', $graphBuilder->getSPARQL() );
131
	}
132
133
	public function testFilterNotExists_triple() {
134
		$graphBuilder = new GraphBuilder( new UsageValidator() );
135
		$this->assertSame(
136
			$graphBuilder,
137
			$graphBuilder->filterNotExists( '?a', '?b', '?c' )
138
		);
139
140
		$this->assertEquals( ' FILTER NOT EXISTS { ?a ?b ?c . }', $graphBuilder->getSPARQL() );
141
	}
142
143
	public function testOptional() {
144
		$graphBuilder = new GraphBuilder( new UsageValidator() );
145
		$graphBuilder->where( '?a', '?b', '?c' );
146
		$this->assertSame(
147
			$graphBuilder,
148
			$graphBuilder->optional( $graphBuilder )
149
		);
150
151
		$this->assertEquals( ' ?a ?b ?c . OPTIONAL { ?a ?b ?c . }', $graphBuilder->getSPARQL() );
152
	}
153
154
	public function testOptional_triple() {
155
		$graphBuilder = new GraphBuilder( new UsageValidator() );
156
		$this->assertSame(
157
			$graphBuilder,
158
			$graphBuilder->optional( '?a', '?b', '?c' )
159
		);
160
161
		$this->assertEquals( ' OPTIONAL { ?a ?b ?c . }', $graphBuilder->getSPARQL() );
162
	}
163
164
	public function testUnion() {
165
		$graphBuilder = new GraphBuilder( new UsageValidator() );
166
		$graphBuilder->where( '?a', '?b', '?c' );
167
		$this->assertSame(
168
			$graphBuilder,
169
			$graphBuilder->union( $graphBuilder, $graphBuilder )
170
		);
171
172
		$this->assertEquals( ' ?a ?b ?c . { ?a ?b ?c . } UNION { ?a ?b ?c . }', $graphBuilder->getSPARQL() );
173
	}
174
175
	public function testSubquery() {
176
		$graphBuilder = new GraphBuilder( new UsageValidator() );
177
		$queryBuilder = new QueryBuilder();
178
		$queryBuilder->where( '?a', '?b', '?c' );
179
		$this->assertSame(
180
			$graphBuilder,
181
			$graphBuilder->subquery( $queryBuilder )
182
		);
183
184
		$this->assertEquals( ' { SELECT * WHERE { ?a ?b ?c . } }', $graphBuilder->getSPARQL() );
185
	}
186
187
	public function testGetSPARQL() {
188
		$graphBuilder = new GraphBuilder( new UsageValidator() );
189
		$graphBuilder->where( '?a', '?b', '?c' );
190
		$graphBuilder->also( '?x', '?y' );
191
		$graphBuilder->also( '?z' );
192
		$graphBuilder->where( '?a', '?b', '?z' );
193
194
		$this->assertEquals( ' ?a ?b ?c , ?z ; ?x ?y , ?z .', $graphBuilder->getSPARQL() );
195
	}
196
197
}
198