Source

SolrQueryBuilder / tests / FieldTest.php

Full commit
<?php
require('setup.php');

use Solr\QueryBuilder\Field;

/**
 * Test class for Param.
 * Generated by PHPUnit on 2011-04-16 at 01:00:29.
 */
class FieldTest extends PHPUnit_Framework_TestCase
{
    /**
     * @var Solr\QueryBuilder\Field
     */
    protected $param;

    protected function setUp()
    {
        $this->param = new Field('name');
    }

    public function testSimpleFieldValueTermsWorks()
    {
        $this->param->addTerm('toys');
        $this->assertEquals('name:toys', $this->param->__toString());
    }

    public function testCanBeInitializedWithASingleValue()
    {
        $Param = new Field('name', 'toys');
        $this->assertEquals('name:toys', $Param->__toString());
    }

    public function testMultipleTermsAreGroupedTogether()
    {
        $this->param->addTerm('toys')
                ->addTerm('electronics')
                ->addTerm('food');

        $this->assertEquals('name:(toys electronics food)', $this->param->__toString());
    }

    public function testSpecialCharactersAreQuoted()
    {
        $this->param->addTerm('t[o:y.s')
                ->addTerm('electr]on-ics')
                ->addTerm('food');

        $this->assertEquals('name:("t[o:y.s" "electr]on-ics" food)', $this->param->__toString());
    }


    public function testPhrasesAreQuoted()
    {
        $this->param->addTerm('baby toys')
                ->addTerm('electronics')
                ->addTerm('dry food');

        $this->assertEquals('name:("baby toys" electronics "dry food")', $this->param->__toString());
    }

    public function testOperatorCanBeSetWithTerm()
    {
        $this->param->addTerm('baby toys', Field::OPERATOR_REQUIRED)
                ->addTerm('electronics', Field::OPERATOR_RESTRICTED)
                ->addTerm('dry food');

        $this->assertEquals('name:(+"baby toys" -electronics "dry food")', $this->param->__toString());
    }

    public function testBoostedTermWorks()
    {
        $this->param->addBoostedTerm('masjeans', 0.6)
            ->addBoostedTerm('RBS', 2.4)
            ->addBoostedTerm('KDS Group', 1);

        $this->assertEquals('name:(masjeans^0.6 RBS^2.4 "KDS Group")', $this->param->__toString());
    }

    /**
     * @expectedException InvalidArgumentException
     * @dataProvider invalidBoostFactorProvider
     */
    public function testBoostedTermThrowsExceptionOnInvalidBoostFactor($boostFactor)
    {
        $this->param->addBoostedTerm('WNeeds', $boostFactor);
        $this->assertEquals("name:WNeeds^$boostFactor", $this->param->__toString());
    }

    public function testFuzzyTermWorks()
    {
        $this->param->addFuzzyTerm('masjeans')
            ->addFuzzyTerm('RBS', 0.6);

        $this->assertEquals('name:(masjeans~ RBS~0.6)', $this->param->__toString());
    }

    /**
     * @expectedException InvalidArgumentException
     * @dataProvider invalidSimilarityProvider
     */
    public function testFuzzyTermThrowsExceptionOnInvalidSimilarity($similarityValue)
    {
        $this->param->addFuzzyTerm('WNeeds', $similarityValue);
        $this->assertEquals("name:WNeeds~$similarityValue", $this->param->__toString());
    }

    public function testProximityPhraseWorks()
    {
        $this->param->addProximityPhrase('Baby Toy', 3);
        $this->assertEquals('name:"Baby Toy"~3', $this->param->__toString());
    }

    /**
     * @expectedException InvalidArgumentException
     * @dataProvider invalidProximityProvider
     */
    public function testProximityTermThrowsExceptionOnInvalidProximity($proximityValue)
    {
        $this->param->addProximityPhrase('WNeeds Ltd', $proximityValue);
        $this->assertEquals("name:\"WNeeds Ltd\"~$proximityValue", $this->param->__toString());
    }

    /**
     * @expectedException InvalidArgumentException
     */
    public function testProximityTermThrowsExceptionOnInvalidPhrase()
    {
        $this->param->addProximityPhrase('WNeeds', 2);
        $this->assertEquals('"name:WNeeds"~2', $this->param->__toString());
    }

    public function testRangeQueryWorksForDate()
    {
        $dateField = new Field('create_date');

        $dateField->setRange('1976-03-06T23:59:59.999Z', 'NOW');
        $this->assertEquals('create_date:[1976-03-06T23:59:59.999Z TO NOW]', $dateField->__toString());
    }

    public function testRangeQueryWorksForNumber()
    {
        $ageField = new Field('age');

        $ageField->setRange(18, 70);
        $this->assertEquals('age:[18 TO 70]', $ageField->__toString());
    }

    public function testRangeQuerySetsStarForOpenEnd()
    {
        $ageField = new Field('age');
        $ageField->setRange(18);
        $this->assertEquals('age:[18 TO *]', $ageField->__toString());

        $dateField = new Field('create_date');
        $dateField->setRange(null, 'NOW');
        $this->assertEquals('create_date:[* TO NOW]', $dateField->__toString());
    }


    //----------- Data provider functions -------------

    public static function invalidSimilarityProvider()
    {
        return array(
            array(-1),
            array(0),
            array(1,),
            array(2,),
            array('NOT-A-NUMBER')
        );
    }

    public static function invalidBoostFactorProvider()
    {
        return array(
            array(-1),
            array(0),
            array('NOT-A-NUMBER')
        );
    }

    public static function invalidProximityProvider()
    {
        return array(
            array(-1),
            array(0),
            array(1.2),
            array('NOT-A-NUMBER')
        );
    }
}