$t = new lime_test(12, new lime_output_color());
$identifier = new xfMockIdentifier();
$service = new xfService($identifier);
$t->diag('->getIdentifier()');
$t->is($service->getIdentifier(), $identifier, '->getIdentifier() returns the identifier');
$t->diag('->addBuilder(), ->buildDocument()');
$doc = $service->buildDocument(42);
$t->is($doc->getGuid(), $identifier->getGuid(42), '->buildDocument() builds a document with the correct GUID');
$t->is(count($doc->getFields()), 1, '->buildDocument() builds a document with one field only with no builders');
$t->is($doc->getField('_service')->getValue(), 'foobar', '->buildDocument() builds a document with a field "_service" as the name of the service');
$service->addBuilder(new xfMockBuilder());
$doc = $service->buildDocument(42);
$t->is($doc->getField('foobar')->getValue(), 'bar', '->buildDocument() acknowledges registered builders');
$t->diag('->addRetort(), ->getRetorts()');
$retort = new xfMockRetort();
$service->addRetort($retort);
$t->is($service->getRetorts(), array($retort), '->getRetorts() returns the retorts registered');
$t->diag('->setOption(), ->getOption(), ->hasOption()');
$t->ok(!$service->hasOption('foobar'), '->hasOption() returns false for unset options');
$t->is($service->getOption('foobar'), null, '->getOption() returns null for unset options');
$t->is($service->getOption('foobar', 42), 42, '->getOption() returns the default response for unset options');
$service->setOption('foobar', 82);
$t->ok($service->hasOption('foobar'), '->hasOption() returns true for set options');
$t->is($service->getOption('foobar', 42), 82, '->getOption() returns the option value');
$t->diag('->configure()');
class FooService extends xfService
{
    public $configured = false;
    public function configure()
    {
        $this->configured = true;
        return $method == 'getTitle';
    }
    public function respond(xfDocumentHit $hit, $method, array $args = array())
    {
        return 'Title' . $hit->getDocument()->getField('input')->getValue();
    }
}
class DescriptionRetort implements xfRetort
{
    public function can(xfDocumentHit $hit, $method, array $args = array())
    {
        return $method == 'getDescription';
    }
    public function respond(xfDocumentHit $hit, $method, array $args = array())
    {
        return 'Description' . $hit->getDocument()->getField('input')->getValue();
    }
}
$index = xfIndexManager::get('TestSearch');
$index->setEngine(new xfMockEngine());
$service = new xfService(new xfMockIdentifier());
$service->addBuilder(new xfMockBuilder());
$service->addRetort(new TitleRetort());
$service->addRetort(new DescriptionRetort());
$service->addRetort(new xfRetortRoute('module/action?input=$input$'));
$index->getServiceRegistry()->register($service);
for ($x = 0; $x < 245; $x++) {
    $index->insert($x);
}
$b = new xfTestBrowser();
$b->get('search/search', array('do' => 'search', 'search' => array('query' => 'foobar')))->isStatusCode(200)->isRequestParameter('module', 'search')->isRequestParameter('action', 'search')->checkResponseElement('title', 'Results 1 to 10')->checkResponseElement('h2', 'Search Results', array('position' => 0))->checkResponseElement('ol[start="1"]', true)->checkResponseElement('ol li', 10)->checkResponseElement('ol li a', 10)->checkResponseElement('ol li a[href="/index.php/module/action/input/0"]', 'Title0', array('count' => 1))->checkResponseElement('ol li', '/Description0/', array('position' => 0))->checkResponseElement('form[method="get"]', true)->checkResponseElement('form label[for="search_query"]', 'Query')->checkResponseElement('form input[id="search_query"]', true)->checkResponseElement('form input[id="search_query"][value="foobar"]', true)->checkResponseElement('form input[type="submit"]', true)->checkResponseElement('div.pager', true)->checkResponseElement('div.pager strong', '1', array('position' => 0))->checkResponseElement('div.pager a', '2', array('position' => 0))->checkResponseElement('div.pager a', '3', array('position' => 1))->checkResponseElement('div.pager a', 'Next', array('position' => 3))->click('3')->isStatusCode(200)->isRequestParameter('module', 'search')->isRequestParameter('action', 'search')->isRequestParameter('search', array('query' => 'foobar', 'page' => 3))->checkResponseElement('title', 'Results 21 to 30')->checkResponseElement('h2', 'Search Results', array('position' => 0))->checkResponseElement('ol[start="21"]', true)->checkResponseElement('ol li', 10)->checkResponseElement('ol li', '/Description20/', array('position' => 0))->checkResponseElement('form[method="get"]', true)->checkResponseElement('form label[for="search_query"]', 'Query')->checkResponseElement('form input[id="search_query"]', true)->checkResponseElement('form input[id="search_query"][value="foobar"]', true)->checkResponseElement('form input[type="submit"]', true)->checkResponseElement('div.pager a', 'Prev', array('position' => 0))->checkResponseElement('div.pager a', '1', array('position' => 1))->checkResponseElement('div.pager a', '2', array('position' => 2))->checkResponseElement('div.pager a + a + a + strong', '3', array('position' => 0))->checkResponseElement('div.pager a', '4', array('position' => 3))->checkResponseElement('div.pager a', '5', array('position' => 4))->checkResponseElement('div.pager a', '6', array('position' => 5))->checkResponseElement('div.pager a', 'Next', array('position' => 6))->click('Next')->isStatusCode(200)->isRequestParameter('module', 'search')->isRequestParameter('action', 'search')->isRequestParameter('search', array('query' => 'foobar', 'page' => 4))->checkResponseElement('title', 'Results 31 to 40')->checkResponseElement('ol[start="31"]', true)->click('Prev')->isStatusCode(200)->isRequestParameter('module', 'search')->isRequestParameter('action', 'search')->isRequestParameter('search', array('query' => 'foobar', 'page' => 3))->get('search/search', array('do' => 'search', 'search' => array('query' => 'foobar', 'page' => 25)))->isStatusCode(200)->isRequestParameter('module', 'search')->isRequestParameter('action', 'search')->checkResponseElement('ol li', 5)->checkResponseElement('div.pager a', 'Prev', array('position' => 0))->checkResponseElement('div.pager a', '23', array('position' => 2))->checkResponseElement('div.pager a', '24', array('position' => 3))->checkResponseElement('div.pager a + a + a + strong', '25', array('position' => 0))->checkResponseElement('div.pager strong + a', 0);