1<?php
2require_once('stratatest.inc.php');
3require_once(DOKU_INC.'lib/plugins/strata/helper/util.php');
4
5/**
6 * Tests query.
7 *
8 * @group plugin_strata
9 * @group plugins
10 */
11class Strata_Query_UnitTestCase extends Strata_UnitTestCase {
12
13    function setup() {
14        parent::setup();
15        // Load types
16        $types = new helper_plugin_strata_util();
17        $string = $types->loadType('string');
18        $ref = $types->loadType('ref');
19        $image = $types->loadType('image');
20
21        // Create objects
22        $bob = $ref->normalize('Bob', 'person');
23        $alice = $ref->normalize('Alice', 'person');
24        $carol = $ref->normalize('Carol', 'person');
25
26        $img_bob = $ref->normalize('Bob.png', 50);
27        $img_alice = $ref->normalize('Alice.svg', 50);
28        $img_carol = $ref->normalize('Carol.jpg', 50);
29
30        // Fill database
31        $this->_triples->addTriple($bob, 'class', 'person', 'wiki');
32        $this->_triples->addTriple($alice, 'class', 'person', 'wiki');
33        $this->_triples->addTriple($carol, 'class', 'person', 'wiki');
34
35        $this->_triples->addTriple($bob, 'name', 'Bob', 'wiki');
36        $this->_triples->addTriple($alice, 'name', 'Alice', 'wiki');
37        $this->_triples->addTriple($carol, 'name', 'Carol', 'wiki');
38
39        $this->_triples->addTriple($bob, 'identifier', 'Β', 'wiki');
40        $this->_triples->addTriple($alice, 'identifier', 'α', 'wiki');
41        $this->_triples->addTriple($carol, 'identifier', 'γ', 'wiki');
42
43        $this->_triples->addTriple($bob, 'knows', $alice, 'wiki');
44        $this->_triples->addTriple($alice, 'knows', $carol, 'wiki');
45        $this->_triples->addTriple($carol, 'knows', $bob, 'wiki');
46        $this->_triples->addTriple($carol, 'knows', $alice, 'wiki');
47
48        $this->_triples->addTriple($bob, 'likes', $alice, 'wiki');
49
50        $this->_triples->addTriple($bob, 'looks like', $img_bob, 'wiki');
51        $this->_triples->addTriple($alice, 'looks like', $img_alice, 'wiki');
52        $this->_triples->addTriple($carol, 'looks like', $img_carol, 'wiki');
53
54        $this->_triples->addTriple($bob, 'is rated', 8, 'wiki');
55        $this->_triples->addTriple($alice, 'is rated', 10, 'wiki');
56        $this->_triples->addTriple($carol, 'is rated', 1, 'wiki');
57
58        $this->_triples->addTriple($bob, 'has length', '5 ft 10 in', 'wiki');
59        $this->_triples->addTriple($alice, 'has length', '5 ft 5 in', 'wiki');
60        $this->_triples->addTriple($carol, 'has length', '4 ft 11 in', 'wiki');
61
62        $this->_triples->addTriple($bob, 'tax rate', '25%', 'wiki');
63        $this->_triples->addTriple($alice, 'tax rate', '10%', 'wiki');
64        $this->_triples->addTriple($carol, 'tax rate', '2%', 'wiki');
65    }
66
67    function assertQueryResult($query, $expectedResult, $message='') {
68        $relations = $this->_triples->queryRelations($query);
69        if ($relations === false) {
70            $this->fail($message.' Query failed.');
71        } else {
72            $this->assertIteratorsEqual($relations, new ArrayIterator($expectedResult), $message);
73            $relations->closeCursor();
74        }
75    }
76
77    function assertIteratorsEqual($x, $y, $message='') {
78        $message = $message?$message.': ':'';
79        do {
80            $this->assertEquals($x->valid(), $y->valid(), $message.'Number of result and expected rows differ: %s');
81            $this->assertEquals($x->current(), $y->current(), $message.'Result row differs from expected one: %s');
82            $x->next();
83            $y->next();
84        } while ($x->valid() || $y->valid());
85    }
86}
87