/**
  * @see xfParser
  *
  * @param string $query
  * @param string $encoding (optional)
  */
 public function parse($query, $encoding = 'utf8')
 {
     $c = new xfCriteria();
     foreach (preg_split('/\\s+/', $query) as $token) {
         $token = trim($token);
         if (!empty($token)) {
             $c->add(new xfCriterionTerm($token));
         }
     }
     return $c->optimize();
 }
$trans = new xfCriterionTranslatorString();
$c->translate($trans);
$t->is($trans->getString(), '{{ foobar foo }}', '->translate() translates the query');
$c = new xfCriteria();
try {
    $msg = '->getLast() fails if there is no last criterion.';
    $c->getLast();
    $t->fail($msg);
} catch (Exception $e) {
    $t->pass($msg);
}
try {
    $msg = '->replaceLast() fails if there is no last criterion';
    $c->replaceLast(new xfCriteria());
    $t->fail($msg);
} catch (Exception $e) {
    $t->pass($msg);
}
$c = new xfCriteria();
$t->is($c->optimize()->toString(), 'EMPTY', '->optimize() eliminates empty subqueries');
$c->add(new xfCriterionTerm('foo'));
$t->is($c->optimize()->toString(), 'foo', '->optimize() reduces itself to just the subquery if only one subquery');
$c->add(new xfCriterionTerm('bar'));
$t->is($c->optimize()->toString(), 'BOOLEAN {[foo] AND [bar]}', '->optimize() does nothing if it cannot be optimized');
$c1 = new xfCriteria();
$c1->add(new xfCriterionEmpty());
$c1->add(new xfCriterionTerm('foo'));
$c2 = new xfCriteria();
$c2->add(new xfCriterionTerm('bar'));
$c1->add($c2);
$t->is($c1->optimize()->toString(), 'BOOLEAN {[foo] AND [bar]}', '->optimize() recursively optimizes');