summaryrefslogtreecommitdiff
path: root/lib/querypath/test/Tests/QueryPath/ExtensionTest.php
blob: 5f98612b554e2aa4c98bef1308575efe2d5c47f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
/**
 * Tests for the QueryPath library.
 * @author M Butcher <matt@aleph-null.tv>
 * @license The GNU Lesser GPL (LGPL) or an MIT-like license.
 */
namespace QueryPath\Tests;
//require_once 'PHPUnit/Autoload.php';
require_once __DIR__ . '/TestCase.php';
require_once __DIR__ . '/../../../src/QueryPath/Extension.php';
//require_once __DIR__ . '/../../../src/QueryPath.php';
//require_once 'QueryPathTest.php';

use \QueryPath\Extension;
use \QueryPath\ExtensionRegistry;

/**
 * 
 */
//define('self::DATA_FILE', 'test/data.xml');

/**
 * Run all of the usual tests, plus some extras, with some extensions loaded.
 * @ingroup querypath_tests
 * @group extension
 */
class QueryPathExtensionTest extends TestCase {

  public static function setUpBeforeClass() {
    ExtensionRegistry::extend('\QueryPath\Tests\StubExtensionOne');
    ExtensionRegistry::extend('\QueryPath\Tests\StubExtensionTwo');
  }

  public function testExtensions() {
   $this->assertNotNull(qp());
  }

  public function testHasExtension() {
   $this->assertTrue(ExtensionRegistry::hasExtension('\QueryPath\Tests\StubExtensionOne'));
  }

  public function testStubToe() {
   $this->assertEquals(1, qp(self::DATA_FILE, 'unary')->stubToe()->top(':root > toe')->size());
  }

  public function testStuble() {
   $this->assertEquals('arg1arg2', qp(self::DATA_FILE)->stuble('arg1', 'arg2'));
  }

 /**
  * @expectedException \QueryPath\Exception
  */
 public function testNoRegistry() {
   ExtensionRegistry::$useRegistry = FALSE;
   try {
    qp(self::DATA_FILE)->stuble('arg1', 'arg2'); 
   }
   catch (\QueryPath\Exception $e) {
     ExtensionRegistry::$useRegistry = TRUE;
     throw $e;
   }

 }

 public function testExtend() {
   $this->assertFalse(ExtensionRegistry::hasExtension('\QueryPath\Tests\StubExtensionThree'));
   ExtensionRegistry::extend('\QueryPath\Tests\StubExtensionThree');
   $this->assertTrue(ExtensionRegistry::hasExtension('\QueryPath\Tests\StubExtensionThree'));
 }

 public function tearDown() {
   ExtensionRegistry::$useRegistry = TRUE;
 }

 /**
  * @expectedException \QueryPath\Exception
  */
 public function testAutoloadExtensions() {
   // FIXME: This isn't really much of a test.
   ExtensionRegistry::autoloadExtensions(FALSE);
   try {
    qp()->stubToe();
   }
   catch (Exception $e) {
     ExtensionRegistry::autoloadExtensions(TRUE);
     throw $e;
   }
 }
 
 /**
  * @expectedException \QueryPath\Exception
  */
 public function testCallFailure() {
   qp()->foo();
 }
 
 // This does not (and will not) throw an exception.
 // /**
 //   * @expectedException QueryPathException
 //   */
 //  public function testExtendNoSuchClass() {
 //    ExtensionRegistry::extend('StubExtensionFour');
 //  }
 
}
// Create a stub extension:
/**
 * Create a stub extension
 *
 * @ingroup querypath_tests
 */
class StubExtensionOne implements Extension {
  private $qp = NULL;
  public function __construct(\QueryPath\Query $qp) {
    $this->qp = $qp;
  }

  public function stubToe() {
    $this->qp->top()->append('<toe/>')->end();
    return $this->qp;
  }
}
/**
 * Create a stub extension
 *
 * @ingroup querypath_tests
 */
class StubExtensionTwo implements Extension {
  private $qp = NULL;
  public function __construct(\QueryPath\Query $qp) {
    $this->qp = $qp;
  }
  public function stuble($arg1, $arg2) {
    return $arg1 . $arg2;
  }
}
/**
 * Create a stub extension
 *
 * @ingroup querypath_tests
 */
class StubExtensionThree implements Extension {
  private $qp = NULL;
  public function __construct(\QueryPath\Query $qp) {
    $this->qp = $qp;
  }
  public function stuble($arg1, $arg2) {
    return $arg1 . $arg2;
  }
}

//ExtensionRegistry::extend('StubExtensionOne');
//ExtensionRegistry::extend('StubExtensionTwo');