[7576] | 1 | <?php |
---|
| 2 | /** |
---|
| 3 | * Copyright 2011 Bas de Nooijer. All rights reserved. |
---|
| 4 | * |
---|
| 5 | * Redistribution and use in source and binary forms, with or without |
---|
| 6 | * modification, are permitted provided that the following conditions are met: |
---|
| 7 | * |
---|
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
---|
| 9 | * this list of conditions and the following disclaimer. |
---|
| 10 | * |
---|
| 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
---|
| 12 | * this listof conditions and the following disclaimer in the documentation |
---|
| 13 | * and/or other materials provided with the distribution. |
---|
| 14 | * |
---|
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS" |
---|
| 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
| 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
| 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
---|
| 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
| 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
| 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
| 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
| 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
| 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
| 25 | * POSSIBILITY OF SUCH DAMAGE. |
---|
| 26 | * |
---|
| 27 | * The views and conclusions contained in the software and documentation are |
---|
| 28 | * those of the authors and should not be interpreted as representing official |
---|
| 29 | * policies, either expressed or implied, of the copyright holder. |
---|
| 30 | */ |
---|
| 31 | |
---|
| 32 | class Solarium_Client_RequestBuilder_Select_Component_FacetSetTest extends PHPUnit_Framework_TestCase |
---|
| 33 | { |
---|
| 34 | |
---|
| 35 | /** |
---|
| 36 | * @var Solarium_Client_RequestBuilder_Select_Component_FacetSet |
---|
| 37 | */ |
---|
| 38 | protected $_builder; |
---|
| 39 | |
---|
| 40 | /** |
---|
| 41 | * @var Solarium_Client_Request |
---|
| 42 | */ |
---|
| 43 | protected $_request; |
---|
| 44 | |
---|
| 45 | /** |
---|
| 46 | * @var Solarium_Query_Select_Component_FacetSet(); |
---|
| 47 | */ |
---|
| 48 | protected $_component; |
---|
| 49 | |
---|
| 50 | |
---|
| 51 | public function setUp() |
---|
| 52 | { |
---|
| 53 | $this->_builder = new Solarium_Client_RequestBuilder_Select_Component_FacetSet; |
---|
| 54 | $this->_request = new Solarium_Client_Request(); |
---|
| 55 | $this->_component = new Solarium_Query_Select_Component_FacetSet(); |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | public function testBuildEmptyFacetSet() |
---|
| 59 | { |
---|
| 60 | $request = $this->_builder->buildComponent($this->_component, $this->_request); |
---|
| 61 | |
---|
| 62 | $this->assertEquals( |
---|
| 63 | array(), |
---|
| 64 | $request->getParams() |
---|
| 65 | ); |
---|
| 66 | |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | public function testBuildWithFacets() |
---|
| 70 | { |
---|
| 71 | $this->_component->addFacet(new Solarium_Query_Select_Component_Facet_Field(array('key' => 'f1', 'field' => 'owner'))); |
---|
| 72 | $this->_component->addFacet(new Solarium_Query_Select_Component_Facet_Query(array('key' => 'f2', 'query' => 'category:23'))); |
---|
| 73 | $this->_component->addFacet(new Solarium_Query_Select_Component_Facet_MultiQuery(array('key' => 'f3', 'query' => array('f4' => array('query' => 'category:40'))))); |
---|
| 74 | $request = $this->_builder->buildComponent($this->_component, $this->_request); |
---|
| 75 | |
---|
| 76 | $this->assertEquals( |
---|
| 77 | null, |
---|
| 78 | $request->getRawData() |
---|
| 79 | ); |
---|
| 80 | |
---|
| 81 | $this->assertEquals( |
---|
| 82 | '?facet=true&facet.field={!key=f1}owner&facet.query={!key=f2}category:23&facet.query={!key=f4}category:40', |
---|
| 83 | urldecode($request->getUri()) |
---|
| 84 | ); |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | public function testBuildWithRangeFacet() |
---|
| 88 | { |
---|
| 89 | $this->_component->addFacet(new Solarium_Query_Select_Component_Facet_Range( |
---|
| 90 | array( |
---|
| 91 | 'key' => 'f1', |
---|
| 92 | 'field' => 'price', |
---|
| 93 | 'start' => '1', |
---|
| 94 | 'end' => 100, |
---|
| 95 | 'gap' => 10, |
---|
| 96 | 'other' => 'all', |
---|
| 97 | 'include' => 'outer' |
---|
| 98 | ) |
---|
| 99 | )); |
---|
| 100 | |
---|
| 101 | $request = $this->_builder->buildComponent($this->_component, $this->_request); |
---|
| 102 | |
---|
| 103 | $this->assertEquals( |
---|
| 104 | null, |
---|
| 105 | $request->getRawData() |
---|
| 106 | ); |
---|
| 107 | |
---|
| 108 | $this->assertEquals( |
---|
| 109 | '?facet=true&facet.range={!key=f1}price&f.price.facet.range.start=1&f.price.facet.range.end=100&f.price.facet.range.gap=10&f.price.facet.range.other=all&f.price.facet.range.include=outer', |
---|
| 110 | urldecode($request->getUri()) |
---|
| 111 | ); |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | public function testBuildWithRangeFacetExcludingOptionalParams() |
---|
| 115 | { |
---|
| 116 | $this->_component->addFacet(new Solarium_Query_Select_Component_Facet_Range( |
---|
| 117 | array( |
---|
| 118 | 'key' => 'f1', |
---|
| 119 | 'field' => 'price', |
---|
| 120 | 'start' => '1', |
---|
| 121 | 'end' => 100, |
---|
| 122 | 'gap' => 10, |
---|
| 123 | ) |
---|
| 124 | )); |
---|
| 125 | |
---|
| 126 | $request = $this->_builder->buildComponent($this->_component, $this->_request); |
---|
| 127 | |
---|
| 128 | $this->assertEquals( |
---|
| 129 | null, |
---|
| 130 | $request->getRawData() |
---|
| 131 | ); |
---|
| 132 | |
---|
| 133 | $this->assertEquals( |
---|
| 134 | '?facet=true&facet.range={!key=f1}price&f.price.facet.range.start=1&f.price.facet.range.end=100&f.price.facet.range.gap=10', |
---|
| 135 | urldecode($request->getUri()) |
---|
| 136 | ); |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | public function testBuildWithFacetsAndGlobalFacetSettings() |
---|
| 140 | { |
---|
| 141 | $this->_component->setMissing(true); |
---|
| 142 | $this->_component->setLimit(10); |
---|
| 143 | $this->_component->addFacet(new Solarium_Query_Select_Component_Facet_Field(array('key' => 'f1', 'field' => 'owner'))); |
---|
| 144 | $this->_component->addFacet(new Solarium_Query_Select_Component_Facet_Query(array('key' => 'f2', 'query' => 'category:23'))); |
---|
| 145 | $this->_component->addFacet(new Solarium_Query_Select_Component_Facet_MultiQuery(array('key' => 'f3', 'query' => array('f4' =>array('query' => 'category:40'))))); |
---|
| 146 | $request = $this->_builder->buildComponent($this->_component, $this->_request); |
---|
| 147 | |
---|
| 148 | $this->assertEquals( |
---|
| 149 | null, |
---|
| 150 | $request->getRawData() |
---|
| 151 | ); |
---|
| 152 | |
---|
| 153 | $this->assertEquals( |
---|
| 154 | '?facet=true&facet.missing=true&facet.limit=10&facet.field={!key=f1}owner&facet.query={!key=f2}category:23&facet.query={!key=f4}category:40', |
---|
| 155 | urldecode($request->getUri()) |
---|
| 156 | ); |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | public function testBuildUnknownFacetType() |
---|
| 160 | { |
---|
| 161 | $this->_component->addFacet(new UnknownFacet(array('key' => 'f1', 'field' => 'owner'))); |
---|
| 162 | $this->setExpectedException('Solarium_Exception'); |
---|
| 163 | $request = $this->_builder->buildComponent($this->_component, $this->_request); |
---|
| 164 | $request->getUri(); |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | class UnknownFacet extends Solarium_Query_Select_Component_Facet_Field{ |
---|
| 170 | |
---|
| 171 | public function getType() |
---|
| 172 | { |
---|
| 173 | return 'unknown'; |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | } |
---|