Drop special handling of MACOSX_DEPLOYMENT_TARGET
[xapian.git] / xapian-bindings / php / smoketest.php
bloba950845c16ba37791c1b8e045416f35039abed2c
1 <?php
2 // Run this PHP script using 'make check' in the build tree.
4 /* Simple test to ensure that we can load the xapian module and exercise basic
5 * functionality successfully.
7 * Copyright (C) 2004,2005,2006,2007,2009,2011,2012,2013,2014,2015,2016,2017 Olly Betts
8 * Copyright (C) 2010 Richard Boulton
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of the
13 * License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
23 * USA
26 # Die on any error, warning, notice, etc.
27 function die_on_error($errno, $errstr, $file, $line) {
28 if ($file !== Null) {
29 print $file;
30 if ($line !== Null) print ":$line";
31 print ": ";
33 print "$errstr\n";
34 exit(1);
36 set_error_handler("die_on_error", -1);
38 include "xapian.php";
40 # Test the version number reporting functions give plausible results.
41 $v = Xapian::major_version().'.'.Xapian::minor_version().'.'.Xapian::revision();
42 $v2 = Xapian::version_string();
43 if ($v != $v2) {
44 print "Unexpected version output ($v != $v2)\n";
45 exit(1);
48 $db = new XapianWritableDatabase('', Xapian::DB_BACKEND_INMEMORY);
49 $db2 = new XapianWritableDatabase('', Xapian::DB_BACKEND_INMEMORY);
51 # Check PHP5 handling of Xapian::DocNotFoundError
52 try {
53 $doc2 = $db->get_document(2);
54 print "Retrieved non-existent document\n";
55 exit(1);
56 } catch (Exception $e) {
57 if ($e->getMessage() !== "DocNotFoundError: Docid 2 not found") {
58 print "DocNotFoundError Exception string not as expected, got: '{$e->getMessage()}'\n";
59 exit(1);
63 # Check QueryParser parsing error.
64 try {
65 $qp = new XapianQueryParser;
66 $qp->set_stemmer(new XapianStem("en"));
67 $qp->parse_query("test AND");
68 print "Successfully parsed bad query\n";
69 exit(1);
70 } catch (Exception $e) {
71 if ($e->getMessage() !== "QueryParserError: Syntax: <expression> AND <expression>") {
72 print "QueryParserError Exception string not as expected, got: '$e->getMessage()'\n";
73 exit(1);
77 # Check that open_stub() is wrapped as expected.
78 try {
79 $db = Xapian::auto_open_stub("nosuchdir/nosuchdb");
80 print "Opened non-existent stub database\n";
81 exit(1);
82 } catch (Exception $e) {
83 if ($e->getMessage() !== "DatabaseNotFoundError: Couldn't open stub database file: nosuchdir/nosuchdb (No such file or directory)") {
84 print "DatabaseOpeningError Exception string not as expected, got: '{$e->getMessage()}'\n";
85 exit(1);
89 # Check that DB_BACKEND_STUB works as expected.
90 try {
91 $db = new XapianDatabase("nosuchdir/nosuchdb", Xapian::DB_BACKEND_STUB);
92 print "Opened non-existent stub database\n";
93 exit(1);
94 } catch (Exception $e) {
95 if ($e->getMessage() !== "DatabaseNotFoundError: Couldn't open stub database file: nosuchdir/nosuchdb (No such file or directory)") {
96 print "DatabaseNotFoundError Exception string not as expected, got: '{$e->getMessage()}'\n";
97 exit(1);
101 # Check that open_stub() writable form is wrapped as expected.
102 try {
103 $db = Xapian::auto_open_stub("nosuchdir/nosuchdb", Xapian::DB_OPEN);
104 print "Opened non-existent stub database\n";
105 exit(1);
106 } catch (Exception $e) {
107 if ($e->getMessage() !== "DatabaseNotFoundError: Couldn't open stub database file: nosuchdir/nosuchdb (No such file or directory)") {
108 print "DatabaseOpeningError Exception string not as expected, got: '{$e->getMessage()}'\n";
109 exit(1);
113 # Check that DB_BACKEND_STUB works as expected.
114 try {
115 $db = new XapianWritableDatabase("nosuchdir/nosuchdb",
116 Xapian::DB_OPEN|Xapian::DB_BACKEND_STUB);
117 print "Opened non-existent stub database\n";
118 exit(1);
119 } catch (Exception $e) {
120 if ($e->getMessage() !== "DatabaseNotFoundError: Couldn't open stub database file: nosuchdir/nosuchdb (No such file or directory)") {
121 print "DatabaseNotFoundError Exception string not as expected, got: '{$e->getMessage()}'\n";
122 exit(1);
126 # Regression test for bug#193, fixed in 1.0.3.
127 $vrp = new XapianNumberValueRangeProcessor(0, '$', true);
128 $a = '$10';
129 $b = '20';
130 $vrp->apply($a, $b);
131 if (Xapian::sortable_unserialise($a) != 10) {
132 print Xapian::sortable_unserialise($a)." != 10\n";
133 exit(1);
135 if (Xapian::sortable_unserialise($b) != 20) {
136 print Xapian::sortable_unserialise($b)." != 20\n";
137 exit(1);
140 $stem = new XapianStem("english");
141 if ($stem->get_description() != "Xapian::Stem(english)") {
142 print "Unexpected \$stem->get_description()\n";
143 exit(1);
146 $doc = new XapianDocument();
147 $doc->set_data("a\x00b");
148 if ($doc->get_data() === "a") {
149 print "get_data+set_data truncates at a zero byte\n";
150 exit(1);
152 if ($doc->get_data() !== "a\x00b") {
153 print "get_data+set_data doesn't transparently handle a zero byte\n";
154 exit(1);
156 $doc->set_data("is there anybody out there?");
157 $doc->add_term("XYzzy");
158 $doc->add_posting($stem->apply("is"), 1);
159 $doc->add_posting($stem->apply("there"), 2);
160 $doc->add_posting($stem->apply("anybody"), 3);
161 $doc->add_posting($stem->apply("out"), 4);
162 $doc->add_posting($stem->apply("there"), 5);
164 // Check virtual function dispatch.
165 if (substr($db->get_description(), 0, 17) !== "WritableDatabase(") {
166 print "Unexpected \$db->get_description()\n";
167 exit(1);
169 $db->add_document($doc);
170 if ($db->get_doccount() != 1) {
171 print "Unexpected \$db->get_doccount()\n";
172 exit(1);
175 $terms = array("smoke", "test", "terms");
176 $query = new XapianQuery(XapianQuery::OP_OR, $terms);
177 if ($query->get_description() != "Query((smoke OR test OR terms))") {
178 print "Unexpected \$query->get_description()\n";
179 exit(1);
181 $query1 = new XapianQuery(XapianQuery::OP_PHRASE, array("smoke", "test", "tuple"));
182 if ($query1->get_description() != "Query((smoke PHRASE 3 test PHRASE 3 tuple))") {
183 print "Unexpected \$query1->get_description()\n";
184 exit(1);
186 $query1b = new XapianQuery(XapianQuery::OP_NEAR, array("smoke", "test", "tuple"), 4);
187 if ($query1b->get_description() != "Query((smoke NEAR 4 test NEAR 4 tuple))") {
188 print "Unexpected \$query1b->get_description()\n";
189 exit(1);
191 $query2 = new XapianQuery(XapianQuery::OP_XOR, array(new XapianQuery("smoke"), $query1, "string"));
192 if ($query2->get_description() != "Query((smoke XOR (smoke PHRASE 3 test PHRASE 3 tuple) XOR string))") {
193 print "Unexpected \$query2->get_description()\n";
194 exit(1);
196 $subqs = array("a", "b");
197 $query3 = new XapianQuery(XapianQuery::OP_OR, $subqs);
198 if ($query3->get_description() != "Query((a OR b))") {
199 print "Unexpected \$query3->get_description()\n";
200 exit(1);
202 $enq = new XapianEnquire($db);
204 // This ought to be wrapped as a constant, but this tests how it has been
205 // wrapped for some time in PHP bindings, which we need to maintain for
206 // compatibility with existing user code.
207 $enq->set_collapse_key(Xapian::BAD_VALUENO_get());
209 $enq->set_query(new XapianQuery(XapianQuery::OP_OR, "there", "is"));
210 $mset = $enq->get_mset(0, 10);
211 if ($mset->size() != 1) {
212 print "Unexpected \$mset->size()\n";
213 exit(1);
215 $terms = join(" ", $enq->get_matching_terms($mset->get_hit(0)));
216 if ($terms != "is there") {
217 print "Unexpected matching terms: $terms\n";
218 exit(1);
221 # Feature test for MatchDecider
222 $doc = new XapianDocument();
223 $doc->set_data("Two");
224 $doc->add_posting($stem->apply("out"), 1);
225 $doc->add_posting($stem->apply("outside"), 1);
226 $doc->add_posting($stem->apply("source"), 2);
227 $doc->add_value(0, "yes");
228 $db->add_document($doc);
230 class testmatchdecider extends XapianMatchDecider {
231 function apply($doc) {
232 return ($doc->get_value(0) == "yes");
236 $query = new XapianQuery($stem->apply("out"));
237 $enquire = new XapianEnquire($db);
238 $enquire->set_query($query);
239 $mdecider = new testmatchdecider();
240 $mset = $enquire->get_mset(0, 10, null, $mdecider);
241 if ($mset->size() != 1) {
242 print "Unexpected number of documents returned by match decider (".$mset->size().")\n";
243 exit(1);
245 if ($mset->get_docid(0) != 2) {
246 print "MatchDecider mset has wrong docid in\n";
247 exit(1);
250 class testexpanddecider extends XapianExpandDecider {
251 function apply($term) {
252 return ($term[0] !== 'a');
256 $enquire = new XapianEnquire($db);
257 $rset = new XapianRSet();
258 $rset->add_document(1);
259 $eset = $enquire->get_eset(10, $rset, XapianEnquire::USE_EXACT_TERMFREQ, 1.0, new testexpanddecider());
260 foreach ($eset->begin() as $t) {
261 if ($t[0] === 'a') {
262 print "XapianExpandDecider was not used\n";
263 exit(1);
267 # Check min_wt argument to get_eset() works (new in 1.2.5).
268 $eset = $enquire->get_eset(100, $rset, XapianEnquire::USE_EXACT_TERMFREQ);
269 $min_wt = 0;
270 foreach ($eset->begin() as $i => $dummy) {
271 $min_wt = $i->get_weight();
273 if ($min_wt >= 1.9) {
274 print "ESet min weight too high for testcase\n";
275 exit(1);
277 $eset = $enquire->get_eset(100, $rset, XapianEnquire::USE_EXACT_TERMFREQ, 1.0, NULL, 1.9);
278 $min_wt = 0;
279 foreach ($eset->begin() as $i => $dummy) {
280 $min_wt = $i->get_weight();
282 if ($min_wt < 1.9) {
283 print "ESet min_wt threshold not applied\n";
284 exit(1);
287 if (XapianQuery::OP_ELITE_SET != 10) {
288 print "OP_ELITE_SET is XapianQuery::OP_ELITE_SET not 10\n";
289 exit(1);
292 # Regression test - overload resolution involving boolean types failed.
293 $enq->set_sort_by_value(1, TRUE);
295 # Regression test - fixed in 0.9.10.1.
296 $oqparser = new XapianQueryParser();
297 $oquery = $oqparser->parse_query("I like tea");
299 # Regression test for bug#192 - fixed in 1.0.3.
300 $enq->set_cutoff(100);
302 # Check DateRangeProcessor works.
303 function add_rp_date(&$qp) {
304 $rpdate = new XapianDateRangeProcessor(1, Xapian::RP_DATE_PREFER_MDY, 1960);
305 $qp->add_rangeprocessor($rpdate);
307 $qp = new XapianQueryParser();
308 add_rp_date($qp);
309 $query = $qp->parse_query('12/03/99..12/04/01');
310 if ($query->get_description() !== 'Query(VALUE_RANGE 1 19991203 20011204)') {
311 print "XapianDateRangeProcessor didn't work - result was ".$query->get_description()."\n";
312 exit(1);
315 # Check DateValueRangeProcessor works.
316 function add_vrp_date(&$qp) {
317 $vrpdate = new XapianDateValueRangeProcessor(1, 1, 1960);
318 $qp->add_valuerangeprocessor($vrpdate);
320 $qp = new XapianQueryParser();
321 add_vrp_date($qp);
322 $query = $qp->parse_query('12/03/99..12/04/01');
323 if ($query->get_description() !== 'Query(VALUE_RANGE 1 19991203 20011204)') {
324 print "XapianDateValueRangeProcessor didn't work - result was ".$query->get_description()."\n";
325 exit(1);
328 # Feature test for XapianFieldProcessor
329 class testfieldprocessor extends XapianFieldProcessor {
330 function apply($str) {
331 if ($str === 'spam') throw new Exception('already spam');
332 return new XapianQuery("spam");
336 $tfp = new testfieldprocessor();
337 $qp->add_prefix('spam', $tfp);
338 $query = $qp->parse_query('spam:ignored');
339 if ($query->get_description() !== 'Query(spam)') {
340 print "testfieldprocessor didn't work - result was ".$query->get_description()."\n";
341 exit(1);
344 try {
345 $query = $qp->parse_query('spam:spam');
346 print "testfieldprocessor exception not rethrown\n";
347 exit(1);
348 } catch (Exception $e) {
349 if ($e->getMessage() !== 'already spam') {
350 print "Exception has wrong message\n";
351 exit(1);
355 # Test setting and getting metadata
356 if ($db->get_metadata('Foo') !== '') {
357 print "Unexpected value for metadata associated with 'Foo' (expected ''): '".$db->get_metadata('Foo')."'\n";
358 exit(1);
360 $db->set_metadata('Foo', 'Foo');
361 if ($db->get_metadata('Foo') !== 'Foo') {
362 print "Unexpected value for metadata associated with 'Foo' (expected 'Foo'): '".$db->get_metadata('Foo')."'\n";
363 exit(1);
366 # Test OP_SCALE_WEIGHT and corresponding constructor
367 $query4 = new XapianQuery(XapianQuery::OP_SCALE_WEIGHT, new XapianQuery('foo'), 5.0);
368 if ($query4->get_description() != "Query(5 * foo)") {
369 print "Unexpected \$query4->get_description()\n";
370 exit(1);
373 # Test MultiValueKeyMaker.
375 $doc = new XapianDocument();
376 $doc->add_term("foo");
377 $doc->add_value(0, "ABB");
378 $db2->add_document($doc);
379 $doc->add_value(0, "ABC");
380 $db2->add_document($doc);
381 $doc->add_value(0, "ABC\0");
382 $db2->add_document($doc);
383 $doc->add_value(0, "ABCD");
384 $db2->add_document($doc);
385 $doc->add_value(0, "ABC\xff");
386 $db2->add_document($doc);
388 $enquire = new XapianEnquire($db2);
389 $enquire->set_query(new XapianQuery("foo"));
392 $sorter = new XapianMultiValueKeyMaker();
393 $sorter->add_value(0);
394 $enquire->set_sort_by_key($sorter, true);
395 $mset = $enquire->get_mset(0, 10);
396 mset_expect_order($mset, array(5, 4, 3, 2, 1));
400 $sorter = new XapianMultiValueKeyMaker();
401 $sorter->add_value(0, true);
402 $enquire->set_sort_by_key($sorter, true);
403 $mset = $enquire->get_mset(0, 10);
404 mset_expect_order($mset, array(1, 2, 3, 4, 5));
408 $sorter = new XapianMultiValueKeyMaker();
409 $sorter->add_value(0);
410 $sorter->add_value(1);
411 $enquire->set_sort_by_key($sorter, true);
412 $mset = $enquire->get_mset(0, 10);
413 mset_expect_order($mset, array(5, 4, 3, 2, 1));
417 $sorter = new XapianMultiValueKeyMaker();
418 $sorter->add_value(0, true);
419 $sorter->add_value(1);
420 $enquire->set_sort_by_key($sorter, true);
421 $mset = $enquire->get_mset(0, 10);
422 mset_expect_order($mset, array(1, 2, 3, 4, 5));
426 $sorter = new XapianMultiValueKeyMaker();
427 $sorter->add_value(0);
428 $sorter->add_value(1, true);
429 $enquire->set_sort_by_key($sorter, true);
430 $mset = $enquire->get_mset(0, 10);
431 mset_expect_order($mset, array(5, 4, 3, 2, 1));
435 $sorter = new XapianMultiValueKeyMaker();
436 $sorter->add_value(0, true);
437 $sorter->add_value(1, true);
438 $enquire->set_sort_by_key($sorter, true);
439 $mset = $enquire->get_mset(0, 10);
440 mset_expect_order($mset, array(1, 2, 3, 4, 5));
443 # Feature test for ValueSetMatchDecider:
445 $md = new XapianValueSetMatchDecider(0, true);
446 $md->add_value("ABC");
447 $doc = new XapianDocument();
448 $doc->add_value(0, "ABCD");
449 if ($md->apply($doc)) {
450 print "Unexpected result from ValueSetMatchDecider->apply(); expected false\n";
451 exit(1);
454 $doc = new XapianDocument();
455 $doc->add_value(0, "ABC");
456 if (!$md->apply($doc)) {
457 print "Unexpected result from ValueSetMatchDecider->apply(); expected true\n";
458 exit(1);
461 $mset = $enquire->get_mset(0, 10, 0, null, $md, null);
462 mset_expect_order($mset, array(2));
464 $md = new XapianValueSetMatchDecider(0, false);
465 $md->add_value("ABC");
466 $mset = $enquire->get_mset(0, 10, 0, null, $md, null);
467 mset_expect_order($mset, array(1, 3, 4, 5));
470 function mset_expect_order($mset, $a) {
471 if ($mset->size() != sizeof($a)) {
472 print "MSet has ".$mset->size()." entries, expected ".sizeof($a)."\n";
473 exit(1);
475 for ($j = 0; $j < sizeof($a); ++$j) {
476 $docid = $mset->get_hit($j)->get_docid();
477 if ($docid != $a[$j]) {
478 print "Expected MSet[$j] to be $a[$j], got ".$docid()."\n";
479 exit(1);
484 # Feature tests for Query "term" constructor optional arguments:
485 $query_wqf = new XapianQuery('wqf', 3);
486 if ($query_wqf->get_description() != 'Query(wqf#3)') {
487 print "Unexpected \$query_wqf->get_description():\n";
488 print $query_wqf->get_description() . "\n";
489 exit(1);
492 $query = new XapianQuery(XapianQuery::OP_VALUE_GE, 0, "100");
493 if ($query->get_description() != 'Query(VALUE_GE 0 100)') {
494 print "Unexpected \$query->get_description():\n";
495 print $query->get_description() . "\n";
496 exit(1);
499 $query = XapianQuery::MatchAll();
500 if ($query->get_description() != 'Query(<alldocuments>)') {
501 print "Unexpected \$query->get_description():\n";
502 print $query->get_description() . "\n";
503 exit(1);
506 $query = XapianQuery::MatchNothing();
507 if ($query->get_description() != 'Query()') {
508 print "Unexpected \$query->get_description():\n";
509 print $query->get_description() . "\n";
510 exit(1);
513 # Test access to matchspy values:
515 $matchspy = new XapianValueCountMatchSpy(0);
516 $enquire->add_matchspy($matchspy);
517 $enquire->get_mset(0, 10);
518 $values = array();
519 foreach ($matchspy->values_begin() as $k => $term) {
520 $values[$term] = $k->get_termfreq();
522 $expected = array(
523 "ABB" => 1,
524 "ABC" => 1,
525 "ABC\0" => 1,
526 "ABCD" => 1,
527 "ABC\xff" => 1,
529 if ($values != $expected) {
530 print "Unexpected matchspy values():\n";
531 var_dump($values);
532 var_dump($expected);
533 print "\n";
534 exit(1);
539 class testspy extends XapianMatchSpy {
540 public $matchspy_count = 0;
542 function apply($doc, $wt) {
543 if (substr($doc->get_value(0), 0, 3) == "ABC") ++$this->matchspy_count;
547 $matchspy = new testspy();
548 $enquire->clear_matchspies();
549 $enquire->add_matchspy($matchspy);
550 $enquire->get_mset(0, 10);
551 if ($matchspy->matchspy_count != 4) {
552 print "Unexpected matchspy count of {$matchspy->matchspy_count}\n";
553 exit(1);
557 # Regression test for SWIG bug - it was generating "return $r;" in wrapper
558 # functions which didn't set $r.
559 $indexer = new XapianTermGenerator();
560 $doc = new XapianDocument();
562 $indexer->set_document($doc);
563 $indexer->index_text("I ask nothing in return");
564 $indexer->index_text_without_positions("Tea time");
565 $indexer->index_text("Return in time");
567 $s = '';
568 foreach ($doc->termlist_begin() as $term) {
569 $s .= $term . ' ';
571 if ($s !== 'ask i in nothing return tea time ') {
572 print "PHP Iterator wrapping of TermIterator doesn't work ($s)\n";
573 exit(1);
576 $s = '';
577 foreach ($doc->termlist_begin() as $k => $term) {
578 $s .= $term . ':' . $k->get_wdf() . ' ';
580 if ($s !== 'ask:1 i:1 in:2 nothing:1 return:2 tea:1 time:2 ') {
581 print "PHP Iterator wrapping of TermIterator keys doesn't work ($s)\n";
582 exit(1);
585 # Test GeoSpatial API
586 $coord = new XapianLatLongCoord();
587 $coord = new XapianLatLongCoord(-41.288889, 174.777222);
589 define('COORD_SLOT', 2);
590 $metric = new XapianGreatCircleMetric();
591 $range = 42.0;
593 $centre = new XapianLatLongCoords($coord);
594 $query = new XapianQuery(new XapianLatLongDistancePostingSource(COORD_SLOT, $centre, $metric, $range));
596 $db = new XapianWritableDatabase('', Xapian::DB_BACKEND_INMEMORY);
597 $coords = new XapianLatLongCoords();
598 $coords->append(new XapianLatLongCoord(40.6048, -74.4427));
599 $doc = new XapianDocument();
600 $doc->add_term("coffee");
601 $doc->add_value(COORD_SLOT, $coords->serialise());
602 $db->add_document($doc);
604 $centre = new XapianLatLongCoords();
605 $centre->append(new XapianLatLongCoord(40.6048, -74.4427));
607 $ps = new XapianLatLongDistancePostingSource(COORD_SLOT, $centre, $metric, $range);
608 $q = new XapianQuery("coffee");
609 $q = new XapianQuery(XapianQuery::OP_AND, $q, new XapianQuery($ps));
611 $enq = new XapianEnquire($db);
612 $enq->set_query($q);
613 $mset = $enq->get_mset(0, 10);
614 if ($mset->size() != 1) {
615 print "Expected one result with XapianLatLongDistancePostingSource, got ";
616 print $mset->size() . "\n";
617 exit(1);
620 $s = '';
621 foreach ($db->allterms_begin() as $k => $term) {
622 $s .= "($term:{$k->get_termfreq()})";
624 if ($s !== '(coffee:1)') {
625 print "PHP Iterator iteration of allterms doesn't work ($s)\n";
626 exit(1);
629 # Test reference tracking and regression test for #659.
630 $qp = new XapianQueryParser();
632 $stop = new XapianSimpleStopper();
633 $stop->add('a');
634 $qp->set_stopper($stop);
636 $query = $qp->parse_query('a b');
637 if ($query->get_description() !== 'Query(b@2)') {
638 print "XapianQueryParser::set_stopper() didn't work as expected - result was ".$query->get_description()."\n";
639 exit(1);