From a1789ddde42033f1b05cc4929491214ee6e79383 Mon Sep 17 00:00:00 2001 From: Pierre Schmitz Date: Thu, 17 Dec 2015 09:15:42 +0100 Subject: Update to MediaWiki 1.26.0 --- .../test/lib/Elastica/Test/Aggregation/AvgTest.php | 39 + .../Test/Aggregation/BaseAggregationTest.php | 8 + .../Elastica/Test/Aggregation/CardinalityTest.php | 132 +++ .../Test/Aggregation/DateHistogramTest.php | 103 ++ .../Elastica/Test/Aggregation/DateRangeTest.php | 52 + .../Test/Aggregation/ExtendedStatsTest.php | 45 + .../lib/Elastica/Test/Aggregation/FilterTest.php | 113 ++ .../lib/Elastica/Test/Aggregation/FiltersTest.php | 120 ++ .../Elastica/Test/Aggregation/GeoDistanceTest.php | 46 + .../Elastica/Test/Aggregation/GeohashGridTest.php | 46 + .../Test/Aggregation/GlobalAggregationTest.php | 27 + .../Elastica/Test/Aggregation/HistogramTest.php | 47 + .../lib/Elastica/Test/Aggregation/IpRangeTest.php | 57 + .../test/lib/Elastica/Test/Aggregation/MaxTest.php | 79 ++ .../test/lib/Elastica/Test/Aggregation/MinTest.php | 40 + .../lib/Elastica/Test/Aggregation/MissingTest.php | 39 + .../lib/Elastica/Test/Aggregation/NestedTest.php | 63 ++ .../Elastica/Test/Aggregation/PercentilesTest.php | 125 +++ .../lib/Elastica/Test/Aggregation/RangeTest.php | 78 ++ .../Test/Aggregation/ReverseNestedTest.php | 134 +++ .../lib/Elastica/Test/Aggregation/ScriptTest.php | 87 ++ .../Test/Aggregation/ScriptedMetricTest.php | 50 + .../Test/Aggregation/SignificantTermsTest.php | 72 ++ .../lib/Elastica/Test/Aggregation/StatsTest.php | 44 + .../test/lib/Elastica/Test/Aggregation/SumTest.php | 40 + .../lib/Elastica/Test/Aggregation/TermsTest.php | 41 + .../lib/Elastica/Test/Aggregation/TopHitsTest.php | 385 +++++++ .../Elastica/Test/Aggregation/ValueCountTest.php | 40 + .../elastica/test/lib/Elastica/Test/Base.php | 142 +++ .../test/lib/Elastica/Test/Bulk/ActionTest.php | 66 ++ .../lib/Elastica/Test/Bulk/ResponseSetTest.php | 200 ++++ .../elastica/test/lib/Elastica/Test/BulkTest.php | 792 +++++++++++++ .../elastica/test/lib/Elastica/Test/ClientTest.php | 1160 ++++++++++++++++++++ .../lib/Elastica/Test/Cluster/Health/IndexTest.php | 144 +++ .../lib/Elastica/Test/Cluster/Health/ShardTest.php | 85 ++ .../test/lib/Elastica/Test/Cluster/HealthTest.php | 147 +++ .../lib/Elastica/Test/Cluster/SettingsTest.php | 117 ++ .../test/lib/Elastica/Test/ClusterTest.php | 83 ++ .../Test/Connection/ConnectionPoolTest.php | 112 ++ .../Connection/Strategy/CallbackStrategyTest.php | 97 ++ .../Strategy/CallbackStrategyTestHelper.php | 20 + .../Test/Connection/Strategy/EmptyStrategy.php | 17 + .../Test/Connection/Strategy/RoundRobinTest.php | 128 +++ .../Test/Connection/Strategy/SimpleTest.php | 113 ++ .../Connection/Strategy/StrategyFactoryTest.php | 84 ++ .../test/lib/Elastica/Test/ConnectionTest.php | 121 ++ .../test/lib/Elastica/Test/DocumentTest.php | 349 ++++++ .../test/lib/Elastica/Test/ExampleTest.php | 61 + .../Test/Exception/AbstractExceptionTest.php | 31 + .../Bulk/Response/ActionExceptionTest.php | 8 + .../Test/Exception/Bulk/ResponseExceptionTest.php | 8 + .../Test/Exception/Bulk/UdpExceptionTest.php | 8 + .../Elastica/Test/Exception/BulkExceptionTest.php | 6 + .../Test/Exception/ClientExceptionTest.php | 6 + .../Exception/Connection/GuzzleExceptionTest.php | 14 + .../Exception/Connection/HttpExceptionTest.php | 8 + .../Exception/Connection/MemcacheExceptionTest.php | 8 + .../Exception/Connection/ThriftExceptionTest.php | 14 + .../Test/Exception/ConnectionExceptionTest.php | 6 + .../Test/Exception/ElasticsearchExceptionTest.php | 6 + .../Test/Exception/InvalidExceptionTest.php | 6 + .../Test/Exception/JSONParseExceptionTest.php | 6 + .../Test/Exception/NotFoundExceptionTest.php | 6 + .../Test/Exception/NotImplementedExceptionTest.php | 19 + .../Exception/PartialShardFailureExceptionTest.php | 54 + .../Test/Exception/QueryBuilderExceptionTest.php | 6 + .../Test/Exception/ResponseExceptionTest.php | 65 ++ .../Test/Exception/RuntimeExceptionTest.php | 6 + .../lib/Elastica/Test/Facet/DateHistogramTest.php | 106 ++ .../test/lib/Elastica/Test/Facet/FilterTest.php | 42 + .../lib/Elastica/Test/Facet/GeoClusterTest.php | 57 + .../test/lib/Elastica/Test/Facet/QueryTest.php | 42 + .../lib/Elastica/Test/Facet/StatisticalTest.php | 82 ++ .../lib/Elastica/Test/Facet/TermsStatsTest.php | 113 ++ .../test/lib/Elastica/Test/Facet/TermsTest.php | 74 ++ .../test/lib/Elastica/Test/Filter/AbstractTest.php | 81 ++ .../test/lib/Elastica/Test/Filter/BoolAndTest.php | 86 ++ .../lib/Elastica/Test/Filter/BoolFilterTest.php | 200 ++++ .../test/lib/Elastica/Test/Filter/BoolNotTest.php | 27 + .../test/lib/Elastica/Test/Filter/BoolOrTest.php | 90 ++ .../test/lib/Elastica/Test/Filter/ExistsTest.php | 36 + .../Elastica/Test/Filter/GeoBoundingBoxTest.php | 55 + .../Elastica/Test/Filter/GeoDistanceRangeTest.php | 220 ++++ .../lib/Elastica/Test/Filter/GeoDistanceTest.php | 141 +++ .../lib/Elastica/Test/Filter/GeoPolygonTest.php | 65 ++ .../Test/Filter/GeoShapePreIndexedTest.php | 102 ++ .../Elastica/Test/Filter/GeoShapeProvidedTest.php | 103 ++ .../lib/Elastica/Test/Filter/GeohashCellTest.php | 68 ++ .../test/lib/Elastica/Test/Filter/HasChildTest.php | 213 ++++ .../lib/Elastica/Test/Filter/HasParentTest.php | 153 +++ .../test/lib/Elastica/Test/Filter/IdsTest.php | 244 ++++ .../test/lib/Elastica/Test/Filter/IndicesTest.php | 125 +++ .../test/lib/Elastica/Test/Filter/LimitTest.php | 34 + .../test/lib/Elastica/Test/Filter/MatchAllTest.php | 20 + .../test/lib/Elastica/Test/Filter/MissingTest.php | 78 ++ .../test/lib/Elastica/Test/Filter/MultiTest.php | 109 ++ .../Test/Filter/NestedFilterWithSetFilterTest.php | 110 ++ .../test/lib/Elastica/Test/Filter/NestedTest.php | 127 +++ .../lib/Elastica/Test/Filter/NumericRangeTest.php | 37 + .../test/lib/Elastica/Test/Filter/PrefixTest.php | 138 +++ .../test/lib/Elastica/Test/Filter/QueryTest.php | 51 + .../test/lib/Elastica/Test/Filter/RangeTest.php | 61 + .../test/lib/Elastica/Test/Filter/RegexpTest.php | 162 +++ .../test/lib/Elastica/Test/Filter/ScriptTest.php | 57 + .../test/lib/Elastica/Test/Filter/TermTest.php | 24 + .../test/lib/Elastica/Test/Filter/TermsTest.php | 129 +++ .../test/lib/Elastica/Test/Filter/TypeTest.php | 32 + .../test/lib/Elastica/Test/Index/SettingsTest.php | 338 ++++++ .../test/lib/Elastica/Test/Index/StatsTest.php | 24 + .../test/lib/Elastica/Test/Index/StatusTest.php | 74 ++ .../elastica/test/lib/Elastica/Test/IndexTest.php | 901 +++++++++++++++ .../elastica/test/lib/Elastica/Test/LogTest.php | 196 ++++ .../test/lib/Elastica/Test/Multi/SearchTest.php | 575 ++++++++++ .../test/lib/Elastica/Test/Node/InfoTest.php | 79 ++ .../elastica/test/lib/Elastica/Test/NodeTest.php | 75 ++ .../elastica/test/lib/Elastica/Test/ParamTest.php | 115 ++ .../test/lib/Elastica/Test/PercolatorTest.php | 328 ++++++ .../test/lib/Elastica/Test/Query/BoolQueryTest.php | 171 +++ .../test/lib/Elastica/Test/Query/BoostingTest.php | 89 ++ .../test/lib/Elastica/Test/Query/BuilderTest.php | 273 +++++ .../test/lib/Elastica/Test/Query/CommonTest.php | 63 ++ .../lib/Elastica/Test/Query/ConstantScoreTest.php | 162 +++ .../test/lib/Elastica/Test/Query/DisMaxTest.php | 84 ++ .../lib/Elastica/Test/Query/EscapeStringTest.php | 36 + .../test/lib/Elastica/Test/Query/FilteredTest.php | 124 +++ .../lib/Elastica/Test/Query/FunctionScoreTest.php | 324 ++++++ .../lib/Elastica/Test/Query/FuzzyLikeThisTest.php | 300 +++++ .../test/lib/Elastica/Test/Query/FuzzyTest.php | 136 +++ .../test/lib/Elastica/Test/Query/HasChildTest.php | 117 ++ .../test/lib/Elastica/Test/Query/HasParentTest.php | 108 ++ .../test/lib/Elastica/Test/Query/HighlightTest.php | 48 + .../test/lib/Elastica/Test/Query/IdsTest.php | 187 ++++ .../test/lib/Elastica/Test/Query/ImageTest.php | 159 +++ .../test/lib/Elastica/Test/Query/MatchAllTest.php | 49 + .../test/lib/Elastica/Test/Query/MatchTest.php | 339 ++++++ .../lib/Elastica/Test/Query/MoreLikeThisTest.php | 240 ++++ .../lib/Elastica/Test/Query/MultiMatchTest.php | 214 ++++ .../test/lib/Elastica/Test/Query/NestedTest.php | 30 + .../lib/Elastica/Test/Query/PostFilterTest.php | 64 ++ .../test/lib/Elastica/Test/Query/PrefixTest.php | 27 + .../lib/Elastica/Test/Query/QueryStringTest.php | 189 ++++ .../test/lib/Elastica/Test/Query/RangeTest.php | 79 ++ .../test/lib/Elastica/Test/Query/RegexpTest.php | 31 + .../test/lib/Elastica/Test/Query/RescoreTest.php | 236 ++++ .../Elastica/Test/Query/SimpleQueryStringTest.php | 103 ++ .../test/lib/Elastica/Test/Query/SimpleTest.php | 19 + .../test/lib/Elastica/Test/Query/TermTest.php | 27 + .../test/lib/Elastica/Test/Query/TermsTest.php | 65 ++ .../test/lib/Elastica/Test/Query/WildcardTest.php | 106 ++ .../Test/QueryBuilder/DSL/AbstractDSLTest.php | 97 ++ .../Test/QueryBuilder/DSL/AggregationTest.php | 58 + .../Elastica/Test/QueryBuilder/DSL/FilterTest.php | 58 + .../Elastica/Test/QueryBuilder/DSL/QueryTest.php | 85 ++ .../Elastica/Test/QueryBuilder/DSL/SuggestTest.php | 32 + .../lib/Elastica/Test/QueryBuilder/VersionTest.php | 67 ++ .../test/lib/Elastica/Test/QueryBuilderTest.php | 88 ++ .../elastica/test/lib/Elastica/Test/QueryTest.php | 458 ++++++++ .../test/lib/Elastica/Test/RequestTest.php | 95 ++ .../test/lib/Elastica/Test/ResponseTest.php | 205 ++++ .../test/lib/Elastica/Test/ResultSetTest.php | 95 ++ .../elastica/test/lib/Elastica/Test/ResultTest.php | 131 +++ .../test/lib/Elastica/Test/ScanAndScrollTest.php | 78 ++ .../test/lib/Elastica/Test/ScriptFieldsTest.php | 93 ++ .../elastica/test/lib/Elastica/Test/ScriptTest.php | 169 +++ .../elastica/test/lib/Elastica/Test/ScrollTest.php | 105 ++ .../elastica/test/lib/Elastica/Test/SearchTest.php | 647 +++++++++++ .../test/lib/Elastica/Test/ShutdownTest.php | 74 ++ .../test/lib/Elastica/Test/SnapshotTest.php | 109 ++ .../elastica/test/lib/Elastica/Test/StatusTest.php | 133 +++ .../lib/Elastica/Test/Suggest/CompletionTest.php | 140 +++ .../test/lib/Elastica/Test/Suggest/PhraseTest.php | 82 ++ .../test/lib/Elastica/Test/Suggest/TermTest.php | 105 ++ .../test/lib/Elastica/Test/Tool/CrossIndexTest.php | 135 +++ .../Test/Transport/AbstractTransportTest.php | 80 ++ .../lib/Elastica/Test/Transport/GuzzleTest.php | 180 +++ .../test/lib/Elastica/Test/Transport/HttpTest.php | 246 +++++ .../lib/Elastica/Test/Transport/MemcacheTest.php | 176 +++ .../Elastica/Test/Transport/NullTransportTest.php | 96 ++ .../lib/Elastica/Test/Transport/ThriftTest.php | 135 +++ .../Test/Transport/TransportBenchmarkTest.php | 261 +++++ .../test/lib/Elastica/Test/Type/MappingTest.php | 331 ++++++ .../elastica/test/lib/Elastica/Test/TypeTest.php | 968 ++++++++++++++++ .../elastica/test/lib/Elastica/Test/UtilTest.php | 139 +++ 183 files changed, 23030 insertions(+) create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/AvgTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/BaseAggregationTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/CardinalityTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/DateHistogramTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/DateRangeTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/ExtendedStatsTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/FilterTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/FiltersTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/GeoDistanceTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/GeohashGridTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/GlobalAggregationTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/HistogramTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/IpRangeTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/MaxTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/MinTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/MissingTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/NestedTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/PercentilesTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/RangeTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/ReverseNestedTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/ScriptTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/ScriptedMetricTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/SignificantTermsTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/StatsTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/SumTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/TermsTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/TopHitsTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/ValueCountTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Base.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Bulk/ActionTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Bulk/ResponseSetTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/BulkTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/ClientTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Cluster/Health/IndexTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Cluster/Health/ShardTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Cluster/HealthTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Cluster/SettingsTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/ClusterTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Connection/ConnectionPoolTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Connection/Strategy/CallbackStrategyTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Connection/Strategy/CallbackStrategyTestHelper.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Connection/Strategy/EmptyStrategy.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Connection/Strategy/RoundRobinTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Connection/Strategy/SimpleTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Connection/Strategy/StrategyFactoryTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/ConnectionTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/DocumentTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/ExampleTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Exception/AbstractExceptionTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Exception/Bulk/Response/ActionExceptionTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Exception/Bulk/ResponseExceptionTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Exception/Bulk/UdpExceptionTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Exception/BulkExceptionTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Exception/ClientExceptionTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Exception/Connection/GuzzleExceptionTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Exception/Connection/HttpExceptionTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Exception/Connection/MemcacheExceptionTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Exception/Connection/ThriftExceptionTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Exception/ConnectionExceptionTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Exception/ElasticsearchExceptionTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Exception/InvalidExceptionTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Exception/JSONParseExceptionTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Exception/NotFoundExceptionTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Exception/NotImplementedExceptionTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Exception/PartialShardFailureExceptionTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Exception/QueryBuilderExceptionTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Exception/ResponseExceptionTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Exception/RuntimeExceptionTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Facet/DateHistogramTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Facet/FilterTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Facet/GeoClusterTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Facet/QueryTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Facet/StatisticalTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Facet/TermsStatsTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Facet/TermsTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/AbstractTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/BoolAndTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/BoolFilterTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/BoolNotTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/BoolOrTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/ExistsTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/GeoBoundingBoxTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/GeoDistanceRangeTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/GeoDistanceTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/GeoPolygonTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/GeoShapePreIndexedTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/GeoShapeProvidedTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/GeohashCellTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/HasChildTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/HasParentTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/IdsTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/IndicesTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/LimitTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/MatchAllTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/MissingTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/MultiTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/NestedFilterWithSetFilterTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/NestedTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/NumericRangeTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/PrefixTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/QueryTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/RangeTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/RegexpTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/ScriptTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/TermTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/TermsTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/TypeTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Index/SettingsTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Index/StatsTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Index/StatusTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/IndexTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/LogTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Multi/SearchTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Node/InfoTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/NodeTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/ParamTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/PercolatorTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Query/BoolQueryTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Query/BoostingTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Query/BuilderTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Query/CommonTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Query/ConstantScoreTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Query/DisMaxTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Query/EscapeStringTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Query/FilteredTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Query/FunctionScoreTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Query/FuzzyLikeThisTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Query/FuzzyTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Query/HasChildTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Query/HasParentTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Query/HighlightTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Query/IdsTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Query/ImageTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Query/MatchAllTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Query/MatchTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Query/MoreLikeThisTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Query/MultiMatchTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Query/NestedTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Query/PostFilterTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Query/PrefixTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Query/QueryStringTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Query/RangeTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Query/RegexpTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Query/RescoreTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Query/SimpleQueryStringTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Query/SimpleTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Query/TermTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Query/TermsTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Query/WildcardTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/QueryBuilder/DSL/AbstractDSLTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/QueryBuilder/DSL/AggregationTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/QueryBuilder/DSL/FilterTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/QueryBuilder/DSL/QueryTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/QueryBuilder/DSL/SuggestTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/QueryBuilder/VersionTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/QueryBuilderTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/QueryTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/RequestTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/ResponseTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/ResultSetTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/ResultTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/ScanAndScrollTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/ScriptFieldsTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/ScriptTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/ScrollTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/SearchTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/ShutdownTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/SnapshotTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/StatusTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Suggest/CompletionTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Suggest/PhraseTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Suggest/TermTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Tool/CrossIndexTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/AbstractTransportTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/GuzzleTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/HttpTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/MemcacheTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/NullTransportTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/ThriftTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/TransportBenchmarkTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/Type/MappingTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/TypeTest.php create mode 100644 vendor/ruflin/elastica/test/lib/Elastica/Test/UtilTest.php (limited to 'vendor/ruflin/elastica/test/lib/Elastica') diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/AvgTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/AvgTest.php new file mode 100644 index 00000000..650a4655 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/AvgTest.php @@ -0,0 +1,39 @@ +_createIndex(); + + $index->getType('test')->addDocuments(array( + new Document(1, array('price' => 5)), + new Document(2, array('price' => 8)), + new Document(3, array('price' => 1)), + new Document(4, array('price' => 3)), + )); + + $index->refresh(); + + return $index; + } + + /** + * @group functional + */ + public function testAvgAggregation() + { + $agg = new Avg('avg'); + $agg->setField('price'); + + $query = new Query(); + $query->addAggregation($agg); + $results = $this->_getIndexForTest()->search($query)->getAggregations(); + $this->assertEquals((5 + 8 + 1 + 3) / 4.0, $results['avg']['value']); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/BaseAggregationTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/BaseAggregationTest.php new file mode 100644 index 00000000..48003d7e --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/BaseAggregationTest.php @@ -0,0 +1,8 @@ +_createIndex(); + + $index->getType('test')->addDocuments(array( + new Document(1, array('color' => 'blue')), + new Document(2, array('color' => 'blue')), + new Document(3, array('color' => 'red')), + new Document(4, array('color' => 'green')), + )); + + $index->refresh(); + + return $index; + } + + /** + * @group functional + */ + public function testCardinalityAggregation() + { + $agg = new Cardinality('cardinality'); + $agg->setField('color'); + + $query = new Query(); + $query->addAggregation($agg); + $results = $this->_getIndexForTest()->search($query)->getAggregation('cardinality'); + + $this->assertEquals(3, $results['value']); + } + + /** + * @dataProvider invalidPrecisionThresholdProvider + * @expectedException \InvalidArgumentException + * + * @param $threshold + */ + public function testInvalidPrecisionThreshold($threshold) + { + $agg = new Cardinality('threshold'); + $agg->setPrecisionThreshold($threshold); + } + + /** + * @dataProvider validPrecisionThresholdProvider + * + * @param $threshold + */ + public function testPrecisionThreshold($threshold) + { + $agg = new Cardinality('threshold'); + $agg->setPrecisionThreshold($threshold); + + $this->assertNotNull($agg->getParam('precision_threshold')); + $this->assertInternalType('int', $agg->getParam('precision_threshold')); + } + + public function invalidPrecisionThresholdProvider() + { + return array( + 'string' => array('100'), + 'float' => array(7.8), + 'boolean' => array(true), + 'array' => array(array()), + 'object' => array(new \StdClass()), + ); + } + + public function validPrecisionThresholdProvider() + { + return array( + 'negative-int' => array(-140), + 'zero' => array(0), + 'positive-int' => array(150), + 'more-than-max' => array(40001), + ); + } + + /** + * @dataProvider validRehashProvider + * + * @param bool $rehash + */ + public function testRehash($rehash) + { + $agg = new Cardinality('rehash'); + $agg->setRehash($rehash); + + $this->assertNotNull($agg->getParam('rehash')); + $this->assertInternalType('boolean', $agg->getParam('rehash')); + } + + /** + * @dataProvider invalidRehashProvider + * @expectedException \InvalidArgumentException + * + * @param mixed $rehash + */ + public function testInvalidRehash($rehash) + { + $agg = new Cardinality('rehash'); + $agg->setRehash($rehash); + } + + public function invalidRehashProvider() + { + return array( + 'string' => array('100'), + 'int' => array(100), + 'float' => array(7.8), + 'array' => array(array()), + 'object' => array(new \StdClass()), + ); + } + + public function validRehashProvider() + { + return array( + 'true' => array(true), + 'false' => array(false), + ); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/DateHistogramTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/DateHistogramTest.php new file mode 100644 index 00000000..ca115ccc --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/DateHistogramTest.php @@ -0,0 +1,103 @@ +_createIndex(); + $type = $index->getType('test'); + + $type->setMapping(new Mapping(null, array( + 'created' => array('type' => 'date'), + ))); + + $type->addDocuments(array( + new Document(1, array('created' => '2014-01-29T00:20:00')), + new Document(2, array('created' => '2014-01-29T02:20:00')), + new Document(3, array('created' => '2014-01-29T03:20:00')), + )); + + $index->refresh(); + + return $index; + } + + /** + * @group functional + */ + public function testDateHistogramAggregation() + { + $agg = new DateHistogram('hist', 'created', '1h'); + + $query = new Query(); + $query->addAggregation($agg); + $results = $this->_getIndexForTest()->search($query)->getAggregation('hist'); + + $this->assertEquals(3, sizeof($results['buckets'])); + } + + /** + * @group unit + */ + public function testSetOffset() + { + $agg = new DateHistogram('hist', 'created', '1h'); + + $agg->setOffset('3m'); + + $expected = array( + 'date_histogram' => array( + 'field' => 'created', + 'interval' => '1h', + 'offset' => '3m', + ), + ); + + $this->assertEquals($expected, $agg->toArray()); + + $this->assertInstanceOf('Elastica\Aggregation\DateHistogram', $agg->setOffset('3m')); + } + + /** + * @group functional + */ + public function testSetOffsetWorks() + { + $agg = new DateHistogram('hist', 'created', '1m'); + $agg->setOffset('+40s'); + + $query = new Query(); + $query->addAggregation($agg); + $results = $this->_getIndexForTest()->search($query)->getAggregation('hist'); + + $this->assertEquals('2014-01-29T00:19:40.000Z', $results['buckets'][0]['key_as_string']); + } + + /** + * @group unit + */ + public function testSetTimezone() + { + $agg = new DateHistogram('hist', 'created', '1h'); + + $agg->setTimezone('-02:30'); + + $expected = array( + 'date_histogram' => array( + 'field' => 'created', + 'interval' => '1h', + 'time_zone' => '-02:30', + ), + ); + + $this->assertEquals($expected, $agg->toArray()); + + $this->assertInstanceOf('Elastica\Aggregation\DateHistogram', $agg->setTimezone('-02:30')); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/DateRangeTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/DateRangeTest.php new file mode 100644 index 00000000..b8078a4c --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/DateRangeTest.php @@ -0,0 +1,52 @@ +_createIndex(); + $type = $index->getType('test'); + + $type->setMapping(new Mapping(null, array( + 'created' => array('type' => 'date'), + ))); + + $type->addDocuments(array( + new Document(1, array('created' => 1390962135000)), + new Document(2, array('created' => 1390965735000)), + new Document(3, array('created' => 1390954935000)), + )); + + $index->refresh(); + + return $index; + } + + /** + * @group functional + */ + public function testDateRangeAggregation() + { + $agg = new DateRange('date'); + $agg->setField('created'); + $agg->addRange(1390958535000)->addRange(null, 1390958535000); + + $query = new Query(); + $query->addAggregation($agg); + $results = $this->_getIndexForTest()->search($query)->getAggregation('date'); + + foreach ($results['buckets'] as $bucket) { + if (array_key_exists('to', $bucket)) { + $this->assertEquals(1, $bucket['doc_count']); + } elseif (array_key_exists('from', $bucket)) { + $this->assertEquals(2, $bucket['doc_count']); + } + } + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/ExtendedStatsTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/ExtendedStatsTest.php new file mode 100644 index 00000000..8c336245 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/ExtendedStatsTest.php @@ -0,0 +1,45 @@ +_createIndex(); + + $index->getType('test')->addDocuments(array( + new Document(1, array('price' => 5)), + new Document(2, array('price' => 8)), + new Document(3, array('price' => 1)), + new Document(4, array('price' => 3)), + )); + + $index->refresh(); + + return $index; + } + + /** + * @group functional + */ + public function testExtendedStatsAggregation() + { + $agg = new ExtendedStats('stats'); + $agg->setField('price'); + + $query = new Query(); + $query->addAggregation($agg); + $results = $this->_getIndexForTest()->search($query)->getAggregation('stats'); + + $this->assertEquals(4, $results['count']); + $this->assertEquals(1, $results['min']); + $this->assertEquals(8, $results['max']); + $this->assertEquals((5 + 8 + 1 + 3) / 4.0, $results['avg']); + $this->assertEquals((5 + 8 + 1 + 3), $results['sum']); + $this->assertTrue(array_key_exists('sum_of_squares', $results)); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/FilterTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/FilterTest.php new file mode 100644 index 00000000..9198bb95 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/FilterTest.php @@ -0,0 +1,113 @@ +_createIndex(); + + $index->getType('test')->addDocuments(array( + new Document(1, array('price' => 5, 'color' => 'blue')), + new Document(2, array('price' => 8, 'color' => 'blue')), + new Document(3, array('price' => 1, 'color' => 'red')), + new Document(4, array('price' => 3, 'color' => 'green')), + )); + + $index->refresh(); + + return $index; + } + + /** + * @group unit + */ + public function testToArray() + { + $expected = array( + 'filter' => array('range' => array('stock' => array('gt' => 0))), + 'aggs' => array( + 'avg_price' => array('avg' => array('field' => 'price')), + ), + ); + + $agg = new Filter('in_stock_products'); + $agg->setFilter(new Range('stock', array('gt' => 0))); + $avg = new Avg('avg_price'); + $avg->setField('price'); + $agg->addAggregation($avg); + + $this->assertEquals($expected, $agg->toArray()); + } + + /** + * @group functional + */ + public function testFilterAggregation() + { + $agg = new Filter('filter'); + $agg->setFilter(new Term(array('color' => 'blue'))); + $avg = new Avg('price'); + $avg->setField('price'); + $agg->addAggregation($avg); + + $query = new Query(); + $query->addAggregation($agg); + + $results = $this->_getIndexForTest()->search($query)->getAggregation('filter'); + $results = $results['price']['value']; + + $this->assertEquals((5 + 8) / 2.0, $results); + } + + /** + * @group functional + */ + public function testFilterNoSubAggregation() + { + $agg = new Avg('price'); + $agg->setField('price'); + + $query = new Query(); + $query->addAggregation($agg); + + $results = $this->_getIndexForTest()->search($query)->getAggregation('price'); + $results = $results['value']; + + $this->assertEquals((5 + 8 + 1 + 3) / 4.0, $results); + } + + /** + * @group unit + */ + public function testConstruct() + { + $agg = new Filter('foo', new Term(array('color' => 'blue'))); + + $expected = array( + 'filter' => array( + 'term' => array( + 'color' => 'blue', + ), + ), + ); + + $this->assertEquals($expected, $agg->toArray()); + } + + /** + * @group unit + */ + public function testConstructWithoutFilter() + { + $agg = new Filter('foo'); + $this->assertEquals('foo', $agg->getName()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/FiltersTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/FiltersTest.php new file mode 100644 index 00000000..36ebcd45 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/FiltersTest.php @@ -0,0 +1,120 @@ +_createIndex('filter'); + + $index->getType('test')->addDocuments(array( + new Document(1, array('price' => 5, 'color' => 'blue')), + new Document(2, array('price' => 8, 'color' => 'blue')), + new Document(3, array('price' => 1, 'color' => 'red')), + new Document(4, array('price' => 3, 'color' => 'green')), + )); + + $index->refresh(); + + return $index; + } + + /** + * @group unit + */ + public function testToArrayUsingNamedFilters() + { + $expected = array( + 'filters' => array( + 'filters' => array( + 'blue' => array( + 'term' => array('color' => 'blue'), + ), + 'red' => array( + 'term' => array('color' => 'red'), + ), + ), + ), + 'aggs' => array( + 'avg_price' => array('avg' => array('field' => 'price')), + ), + ); + + $agg = new Filters('by_color'); + $agg->addFilter(new Term(array('color' => 'blue')), 'blue'); + $agg->addFilter(new Term(array('color' => 'red')), 'red'); + + $avg = new Avg('avg_price'); + $avg->setField('price'); + $agg->addAggregation($avg); + + $this->assertEquals($expected, $agg->toArray()); + } + + /** + * @group unit + */ + public function testToArrayUsingAnonymousFilters() + { + $expected = array( + 'filters' => array( + 'filters' => array( + array( + 'term' => array('color' => 'blue'), + ), + array( + 'term' => array('color' => 'red'), + ), + ), + ), + 'aggs' => array( + 'avg_price' => array('avg' => array('field' => 'price')), + ), + ); + + $agg = new Filters('by_color'); + $agg->addFilter(new Term(array('color' => 'blue'))); + $agg->addFilter(new Term(array('color' => 'red'))); + + $avg = new Avg('avg_price'); + $avg->setField('price'); + $agg->addAggregation($avg); + + $this->assertEquals($expected, $agg->toArray()); + } + + /** + * @group functional + */ + public function testFilterAggregation() + { + $agg = new Filters('by_color'); + $agg->addFilter(new Term(array('color' => 'blue')), 'blue'); + $agg->addFilter(new Term(array('color' => 'red')), 'red'); + + $avg = new Avg('avg_price'); + $avg->setField('price'); + $agg->addAggregation($avg); + + $query = new Query(); + $query->addAggregation($agg); + + $results = $this->_getIndexForTest()->search($query)->getAggregation('by_color'); + + $resultsForBlue = $results['buckets']['blue']; + $resultsForRed = $results['buckets']['red']; + + $this->assertEquals(2, $resultsForBlue['doc_count']); + $this->assertEquals(1, $resultsForRed['doc_count']); + + $this->assertEquals((5 + 8) / 2, $resultsForBlue['avg_price']['value']); + $this->assertEquals(1, $resultsForRed['avg_price']['value']); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/GeoDistanceTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/GeoDistanceTest.php new file mode 100644 index 00000000..f8a02d1d --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/GeoDistanceTest.php @@ -0,0 +1,46 @@ +_createIndex(); + $type = $index->getType('test'); + + $type->setMapping(new Mapping(null, array( + 'location' => array('type' => 'geo_point'), + ))); + + $type->addDocuments(array( + new Document(1, array('location' => array('lat' => 32.849437, 'lon' => -117.271732))), + new Document(2, array('location' => array('lat' => 32.798320, 'lon' => -117.246648))), + new Document(3, array('location' => array('lat' => 37.782439, 'lon' => -122.392560))), + )); + + $index->refresh(); + + return $index; + } + + /** + * @group functional + */ + public function testGeoDistanceAggregation() + { + $agg = new GeoDistance('geo', 'location', array('lat' => 32.804654, 'lon' => -117.242594)); + $agg->addRange(null, 100); + $agg->setUnit('mi'); + + $query = new Query(); + $query->addAggregation($agg); + $results = $this->_getIndexForTest()->search($query)->getAggregation('geo'); + + $this->assertEquals(2, $results['buckets'][0]['doc_count']); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/GeohashGridTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/GeohashGridTest.php new file mode 100644 index 00000000..6e0d43fd --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/GeohashGridTest.php @@ -0,0 +1,46 @@ +_createIndex(); + $type = $index->getType('test'); + + $type->setMapping(new Mapping(null, array( + 'location' => array('type' => 'geo_point'), + ))); + + $type->addDocuments(array( + new Document(1, array('location' => array('lat' => 32.849437, 'lon' => -117.271732))), + new Document(2, array('location' => array('lat' => 32.798320, 'lon' => -117.246648))), + new Document(3, array('location' => array('lat' => 37.782439, 'lon' => -122.392560))), + )); + + $index->refresh(); + + return $index; + } + + /** + * @group functional + */ + public function testGeohashGridAggregation() + { + $agg = new GeohashGrid('hash', 'location'); + $agg->setPrecision(3); + + $query = new Query(); + $query->addAggregation($agg); + $results = $this->_getIndexForTest()->search($query)->getAggregation('hash'); + + $this->assertEquals(2, $results['buckets'][0]['doc_count']); + $this->assertEquals(1, $results['buckets'][1]['doc_count']); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/GlobalAggregationTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/GlobalAggregationTest.php new file mode 100644 index 00000000..6ab086d0 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/GlobalAggregationTest.php @@ -0,0 +1,27 @@ + new \stdClass(), + 'aggs' => array( + 'avg_price' => array('avg' => array('field' => 'price')), + ), + ); + + $agg = new GlobalAggregation('all_products'); + $avg = new Avg('avg_price'); + $avg->setField('price'); + $agg->addAggregation($avg); + $this->assertEquals($expected, $agg->toArray()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/HistogramTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/HistogramTest.php new file mode 100644 index 00000000..ffdf73a4 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/HistogramTest.php @@ -0,0 +1,47 @@ +_createIndex(); + + $index->getType('test')->addDocuments(array( + new Document(1, array('price' => 5, 'color' => 'blue')), + new Document(2, array('price' => 8, 'color' => 'blue')), + new Document(3, array('price' => 1, 'color' => 'red')), + new Document(4, array('price' => 30, 'color' => 'green')), + new Document(5, array('price' => 40, 'color' => 'red')), + new Document(6, array('price' => 35, 'color' => 'green')), + new Document(7, array('price' => 42, 'color' => 'red')), + new Document(8, array('price' => 41, 'color' => 'blue')), + )); + + $index->refresh(); + + return $index; + } + + /** + * @group functional + */ + public function testHistogramAggregation() + { + $agg = new Histogram('hist', 'price', 10); + $agg->setMinimumDocumentCount(0); // should return empty buckets + + $query = new Query(); + $query->addAggregation($agg); + $results = $this->_getIndexForTest()->search($query)->getAggregation('hist'); + + $buckets = $results['buckets']; + $this->assertEquals(5, sizeof($buckets)); + $this->assertEquals(30, $buckets[3]['key']); + $this->assertEquals(2, $buckets[3]['doc_count']); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/IpRangeTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/IpRangeTest.php new file mode 100644 index 00000000..2f3099f6 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/IpRangeTest.php @@ -0,0 +1,57 @@ +_createIndex(); + $type = $index->getType('test'); + + $type->setMapping(new Mapping(null, array( + 'address' => array('type' => 'ip'), + ))); + + $type->addDocuments(array( + new Document(1, array('address' => '192.168.1.100')), + new Document(2, array('address' => '192.168.1.150')), + new Document(3, array('address' => '192.168.1.200')), + )); + + $index->refresh(); + + return $index; + } + + /** + * @group functional + */ + public function testIpRangeAggregation() + { + $agg = new IpRange('ip', 'address'); + $agg->addRange('192.168.1.101'); + $agg->addRange(null, '192.168.1.200'); + + $cidrRange = '192.168.1.0/24'; + $agg->addMaskRange($cidrRange); + + $query = new Query(); + $query->addAggregation($agg); + $results = $this->_getIndexForTest()->search($query)->getAggregation('ip'); + + foreach ($results['buckets'] as $bucket) { + if (array_key_exists('key', $bucket) && $bucket['key'] == $cidrRange) { + // the CIDR mask + $this->assertEquals(3, $bucket['doc_count']); + } else { + // the normal ip ranges + $this->assertEquals(2, $bucket['doc_count']); + } + } + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/MaxTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/MaxTest.php new file mode 100644 index 00000000..f057b81d --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/MaxTest.php @@ -0,0 +1,79 @@ +_createIndex(); + + $index->getType('test')->addDocuments(array( + new Document(1, array('price' => 5)), + new Document(2, array('price' => 8)), + new Document(3, array('price' => 1)), + new Document(4, array('price' => 3)), + )); + + $index->refresh(); + + return $index; + } + + /** + * @group unit + */ + public function testToArray() + { + $expected = array( + 'max' => array( + 'field' => 'price', + 'script' => '_value * conversion_rate', + 'params' => array( + 'conversion_rate' => 1.2, + ), + ), + 'aggs' => array( + 'subagg' => array('max' => array('field' => 'foo')), + ), + ); + + $agg = new Max('min_price_in_euros'); + $agg->setField('price'); + $agg->setScript(new Script('_value * conversion_rate', array('conversion_rate' => 1.2))); + $max = new Max('subagg'); + $max->setField('foo'); + $agg->addAggregation($max); + + $this->assertEquals($expected, $agg->toArray()); + } + + /** + * @group functional + */ + public function testMaxAggregation() + { + $index = $this->_getIndexForTest(); + + $agg = new Max('min_price'); + $agg->setField('price'); + + $query = new Query(); + $query->addAggregation($agg); + $results = $index->search($query)->getAggregation('min_price'); + + $this->assertEquals(8, $results['value']); + + // test using a script + $agg->setScript(new Script('_value * conversion_rate', array('conversion_rate' => 1.2))); + $query = new Query(); + $query->addAggregation($agg); + $results = $index->search($query)->getAggregation('min_price'); + + $this->assertEquals(8 * 1.2, $results['value']); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/MinTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/MinTest.php new file mode 100644 index 00000000..ce0ad5e7 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/MinTest.php @@ -0,0 +1,40 @@ +_createIndex(); + + $index->getType('test')->addDocuments(array( + new Document(1, array('price' => 5)), + new Document(2, array('price' => 8)), + new Document(3, array('price' => 1)), + new Document(4, array('price' => 3)), + )); + + $index->refresh(); + + return $index; + } + + /** + * @group functional + */ + public function testMinAggregation() + { + $agg = new Min('min_price'); + $agg->setField('price'); + + $query = new Query(); + $query->addAggregation($agg); + $results = $this->_getIndexForTest()->search($query)->getAggregation('min_price'); + + $this->assertEquals(1, $results['value']); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/MissingTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/MissingTest.php new file mode 100644 index 00000000..85461879 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/MissingTest.php @@ -0,0 +1,39 @@ +_createIndex(); + + $index->getType('test')->addDocuments(array( + new Document(1, array('price' => 5, 'color' => 'blue')), + new Document(2, array('price' => 8, 'color' => 'blue')), + new Document(3, array('price' => 1)), + new Document(4, array('price' => 3, 'color' => 'green')), + )); + + $index->refresh(); + + return $index; + } + + /** + * @group functional + */ + public function testMissingAggregation() + { + $agg = new Missing('missing', 'color'); + + $query = new Query(); + $query->addAggregation($agg); + $results = $this->_getIndexForTest()->search($query)->getAggregation('missing'); + + $this->assertEquals(1, $results['doc_count']); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/NestedTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/NestedTest.php new file mode 100644 index 00000000..58c5d13a --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/NestedTest.php @@ -0,0 +1,63 @@ +_createIndex(); + $type = $index->getType('test'); + + $type->setMapping(new Mapping(null, array( + 'resellers' => array( + 'type' => 'nested', + 'properties' => array( + 'name' => array('type' => 'string'), + 'price' => array('type' => 'double'), + ), + ), + ))); + + $type->addDocuments(array( + new Document(1, array( + 'resellers' => array( + 'name' => 'spacely sprockets', + 'price' => 5.55, + ), + )), + new Document(2, array( + 'resellers' => array( + 'name' => 'cogswell cogs', + 'price' => 4.98, + ), + )), + )); + + $index->refresh(); + + return $index; + } + + /** + * @group functional + */ + public function testNestedAggregation() + { + $agg = new Nested('resellers', 'resellers'); + $min = new Min('min_price'); + $min->setField('price'); + $agg->addAggregation($min); + + $query = new Query(); + $query->addAggregation($agg); + $results = $this->_getIndexForTest()->search($query)->getAggregation('resellers'); + + $this->assertEquals(4.98, $results['min_price']['value']); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/PercentilesTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/PercentilesTest.php new file mode 100644 index 00000000..ee4dd2c5 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/PercentilesTest.php @@ -0,0 +1,125 @@ +assertEquals('price_percentile', $aggr->getName()); + + $aggr = new Percentiles('price_percentile', 'price'); + $this->assertEquals('price', $aggr->getParam('field')); + } + + /** + * @group functional + */ + public function testSetField() + { + $aggr = new Percentiles('price_percentile'); + $aggr->setField('price'); + + $this->assertEquals('price', $aggr->getParam('field')); + $this->assertInstanceOf('Elastica\Aggregation\Percentiles', $aggr->setField('price')); + } + + /** + * @group functional + */ + public function testSetCompression() + { + $aggr = new Percentiles('price_percentile'); + $aggr->setCompression(200); + $this->assertEquals(200, $aggr->getParam('compression')); + $this->assertInstanceOf('Elastica\Aggregation\Percentiles', $aggr->setCompression(200)); + } + + /** + * @group functional + */ + public function testSetPercents() + { + $percents = array(1, 2, 3); + $aggr = new Percentiles('price_percentile'); + $aggr->setPercents($percents); + $this->assertEquals($percents, $aggr->getParam('percents')); + $this->assertInstanceOf('Elastica\Aggregation\Percentiles', $aggr->setPercents($percents)); + } + + /** + * @group functional + */ + public function testAddPercent() + { + $percents = array(1, 2, 3); + $aggr = new Percentiles('price_percentile'); + $aggr->setPercents($percents); + $this->assertEquals($percents, $aggr->getParam('percents')); + $aggr->addPercent(4); + $percents[] = 4; + $this->assertEquals($percents, $aggr->getParam('percents')); + $this->assertInstanceOf('Elastica\Aggregation\Percentiles', $aggr->addPercent(4)); + } + + /** + * @group functional + */ + public function testSetScript() + { + $script = 'doc["load_time"].value / 20'; + $aggr = new Percentiles('price_percentile'); + $aggr->setScript($script); + $this->assertEquals($script, $aggr->getParam('script')); + $this->assertInstanceOf('Elastica\Aggregation\Percentiles', $aggr->setScript($script)); + } + + /** + * @group functional + */ + public function testActualWork() + { + // prepare + $index = $this->_createIndex(); + $type = $index->getType('offer'); + $type->addDocuments(array( + new Document(1, array('price' => 100)), + new Document(2, array('price' => 200)), + new Document(3, array('price' => 300)), + new Document(4, array('price' => 400)), + new Document(5, array('price' => 500)), + new Document(6, array('price' => 600)), + new Document(7, array('price' => 700)), + new Document(8, array('price' => 800)), + new Document(9, array('price' => 900)), + new Document(10, array('price' => 1000)), + )); + $index->refresh(); + + // execute + $aggr = new Percentiles('price_percentile'); + $aggr->setField('price'); + + $query = new Query(); + $query->addAggregation($aggr); + + $resultSet = $type->search($query); + $aggrResult = $resultSet->getAggregation('price_percentile'); + + // hope it's ok to hardcode results... + $this->assertEquals(109.0, $aggrResult['values']['1.0']); + $this->assertEquals(145.0, $aggrResult['values']['5.0']); + $this->assertEquals(325.0, $aggrResult['values']['25.0']); + $this->assertEquals(550.0, $aggrResult['values']['50.0']); + $this->assertEquals(775.0, $aggrResult['values']['75.0']); + $this->assertEquals(955.0, $aggrResult['values']['95.0']); + $this->assertEquals(991.0, $aggrResult['values']['99.0']); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/RangeTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/RangeTest.php new file mode 100644 index 00000000..f96e4096 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/RangeTest.php @@ -0,0 +1,78 @@ +_createIndex(); + + $index->getType('test')->addDocuments(array( + new Document(1, array('price' => 5)), + new Document(2, array('price' => 8)), + new Document(3, array('price' => 1)), + new Document(4, array('price' => 3)), + new Document(5, array('price' => 1.5)), + new Document(6, array('price' => 2)), + )); + + $index->refresh(); + + return $index; + } + + /** + * @group functional + */ + public function testRangeAggregation() + { + $agg = new Range('range'); + $agg->setField('price'); + $agg->addRange(1.5, 5); + + $query = new Query(); + $query->addAggregation($agg); + $results = $this->_getIndexForTest()->search($query)->getAggregation('range'); + + $this->assertEquals(2, $results['buckets'][0]['doc_count']); + } + + /** + * @group unit + */ + public function testRangeAggregationWithKey() + { + $agg = new Range('range'); + $agg->setField('price'); + $agg->addRange(null, 50, 'cheap'); + $agg->addRange(50, 100, 'average'); + $agg->addRange(100, null, 'expensive'); + + $expected = array( + 'range' => array( + 'field' => 'price', + 'ranges' => array( + array( + 'to' => 50, + 'key' => 'cheap', + ), + array( + 'from' => 50, + 'to' => 100, + 'key' => 'average', + ), + array( + 'from' => 100, + 'key' => 'expensive', + ), + ), + ), + ); + + $this->assertEquals($expected, $agg->toArray()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/ReverseNestedTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/ReverseNestedTest.php new file mode 100644 index 00000000..0e2ed2e6 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/ReverseNestedTest.php @@ -0,0 +1,134 @@ +_createIndex(); + $mapping = new Mapping(); + $mapping->setProperties(array( + 'comments' => array( + 'type' => 'nested', + 'properties' => array( + 'name' => array('type' => 'string'), + 'body' => array('type' => 'string'), + ), + ), + )); + $type = $index->getType('test'); + $type->setMapping($mapping); + + $type->addDocuments(array( + new Document(1, array( + 'comments' => array( + array( + 'name' => 'bob', + 'body' => 'this is bobs comment', + ), + array( + 'name' => 'john', + 'body' => 'this is johns comment', + ), + ), + 'tags' => array('foo', 'bar'), + )), + new Document(2, array( + 'comments' => array( + array( + 'name' => 'bob', + 'body' => 'this is another comment from bob', + ), + array( + 'name' => 'susan', + 'body' => 'this is susans comment', + ), + ), + 'tags' => array('foo', 'baz'), + )), + )); + + $index->refresh(); + + return $index; + } + + /** + * @group unit + */ + public function testPathNotSetIfNull() + { + $agg = new ReverseNested('nested'); + $this->assertFalse($agg->hasParam('path')); + } + + /** + * @group unit + */ + public function testPathSetIfNotNull() + { + $agg = new ReverseNested('nested', 'some_field'); + $this->assertEquals('some_field', $agg->getParam('path')); + } + + /** + * @group functional + */ + public function testReverseNestedAggregation() + { + $agg = new Nested('comments', 'comments'); + $names = new Terms('name'); + $names->setField('comments.name'); + + $tags = new Terms('tags'); + $tags->setField('tags'); + + $reverseNested = new ReverseNested('main'); + $reverseNested->addAggregation($tags); + + $names->addAggregation($reverseNested); + + $agg->addAggregation($names); + + $query = new Query(); + $query->addAggregation($agg); + $results = $this->_getIndexForTest()->search($query)->getAggregation('comments'); + + $this->assertArrayHasKey('name', $results); + $nameResults = $results['name']; + + $this->assertCount(3, $nameResults['buckets']); + + // bob + $this->assertEquals('bob', $nameResults['buckets'][0]['key']); + $tags = array( + array('key' => 'foo', 'doc_count' => 2), + array('key' => 'bar', 'doc_count' => 1), + array('key' => 'baz', 'doc_count' => 1), + ); + $this->assertEquals($tags, $nameResults['buckets'][0]['main']['tags']['buckets']); + + // john + $this->assertEquals('john', $nameResults['buckets'][1]['key']); + $tags = array( + array('key' => 'bar', 'doc_count' => 1), + array('key' => 'foo', 'doc_count' => 1), + ); + $this->assertEquals($tags, $nameResults['buckets'][1]['main']['tags']['buckets']); + + // susan + $this->assertEquals('susan', $nameResults['buckets'][2]['key']); + $tags = array( + array('key' => 'baz', 'doc_count' => 1), + array('key' => 'foo', 'doc_count' => 1), + ); + $this->assertEquals($tags, $nameResults['buckets'][2]['main']['tags']['buckets']); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/ScriptTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/ScriptTest.php new file mode 100644 index 00000000..bf32b251 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/ScriptTest.php @@ -0,0 +1,87 @@ +_createIndex(); + + $index->getType('test')->addDocuments(array( + new Document('1', array('price' => 5)), + new Document('2', array('price' => 8)), + new Document('3', array('price' => 1)), + new Document('4', array('price' => 3)), + )); + + $index->refresh(); + + return $index; + } + + /** + * @group functional + */ + public function testAggregationScript() + { + $agg = new Sum('sum'); + // x = (0..1) is groovy-specific syntax, to see if lang is recognized + $script = new Script("x = (0..1); return doc['price'].value", null, 'groovy'); + $agg->setScript($script); + + $query = new Query(); + $query->addAggregation($agg); + $results = $this->_getIndexForTest()->search($query)->getAggregation('sum'); + + $this->assertEquals(5 + 8 + 1 + 3, $results['value']); + } + + /** + * @group functional + */ + public function testAggregationScriptAsString() + { + $agg = new Sum('sum'); + $agg->setScript("doc['price'].value"); + + $query = new Query(); + $query->addAggregation($agg); + $results = $this->_getIndexForTest()->search($query)->getAggregation('sum'); + + $this->assertEquals(5 + 8 + 1 + 3, $results['value']); + } + + /** + * @group unit + */ + public function testSetScript() + { + $aggregation = 'sum'; + $string = "doc['price'].value"; + $params = array( + 'param1' => 'one', + 'param2' => 1, + ); + $lang = 'groovy'; + + $agg = new Sum($aggregation); + $script = new Script($string, $params, $lang); + $agg->setScript($script); + + $array = $agg->toArray(); + + $expected = array( + $aggregation => array( + 'script' => $string, + 'params' => $params, + 'lang' => $lang, + ), + ); + $this->assertEquals($expected, $array); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/ScriptedMetricTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/ScriptedMetricTest.php new file mode 100644 index 00000000..31f5798b --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/ScriptedMetricTest.php @@ -0,0 +1,50 @@ +_createIndex(); + $type = $index->getType('test'); + + $type->setMapping(new Mapping(null, array( + 'start' => array('type' => 'long'), + 'end' => array('type' => 'long'), + ))); + + $type->addDocuments(array( + new Document(1, array('start' => 100, 'end' => 200)), + new Document(2, array('start' => 200, 'end' => 250)), + new Document(3, array('start' => 300, 'end' => 450)), + )); + + $index->refresh(); + + return $index; + } + + /** + * @group functional + */ + public function testScriptedMetricAggregation() + { + $agg = new ScriptedMetric( + 'scripted', + "_agg['durations'] = [:]", + "key = doc['start'].value+ \":\"+ doc['end'].value; _agg.durations[key] = doc['end'].value - doc['start'].value;", + 'values = []; for (item in _agg.durations) { values.add(item.value) }; return values' + ); + + $query = new Query(); + $query->addAggregation($agg); + $results = $this->_getIndexForTest()->search($query)->getAggregation('scripted'); + + $this->assertEquals(array(100, 50, 150), $results['value'][0]); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/SignificantTermsTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/SignificantTermsTest.php new file mode 100644 index 00000000..8960768b --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/SignificantTermsTest.php @@ -0,0 +1,72 @@ +_createIndex(); + $colors = array('blue', 'blue', 'red', 'red', 'green', 'yellow', 'white', 'cyan', 'magenta'); + $temperatures = array(1500, 1500, 1500, 1500, 2500, 3500, 4500, 5500, 6500, 7500, 7500, 8500, 9500); + $docs = array(); + for ($i = 0;$i < 250;$i++) { + $docs[] = new Document($i, array('color' => $colors[$i % count($colors)], 'temperature' => $temperatures[$i % count($temperatures)])); + } + $index->getType('test')->addDocuments($docs); + $index->refresh(); + + return $index; + } + + /** + * @group functional + */ + public function testSignificantTermsAggregation() + { + $agg = new SignificantTerms('significantTerms'); + $agg->setField('temperature'); + $agg->setSize(1); + + $termsQuery = new Terms(); + $termsQuery->setTerms('color', array('blue', 'red', 'green', 'yellow', 'white')); + + $query = new Query($termsQuery); + $query->addAggregation($agg); + $results = $this->_getIndexForTest()->search($query)->getAggregation('significantTerms'); + + $this->assertEquals(1, count($results['buckets'])); + $this->assertEquals(63, $results['buckets'][0]['doc_count']); + $this->assertEquals(79, $results['buckets'][0]['bg_count']); + $this->assertEquals('1500', $results['buckets'][0]['key_as_string']); + } + + /** + * @group functional + */ + public function testSignificantTermsAggregationWithBackgroundFilter() + { + $agg = new SignificantTerms('significantTerms'); + $agg->setField('temperature'); + $agg->setSize(1); + $termsFilter = new TermsFilter(); + $termsFilter->setTerms('color', array('blue', 'red', 'green', 'yellow')); + $agg->setBackgroundFilter($termsFilter); + + $termsQuery = new Terms(); + $termsQuery->setTerms('color', array('blue', 'red', 'green', 'yellow', 'white')); + + $query = new Query($termsQuery); + $query->addAggregation($agg); + $results = $this->_getIndexForTest()->search($query)->getAggregation('significantTerms'); + + $this->assertEquals(15, $results['buckets'][0]['doc_count']); + $this->assertEquals(12, $results['buckets'][0]['bg_count']); + $this->assertEquals('4500', $results['buckets'][0]['key_as_string']); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/StatsTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/StatsTest.php new file mode 100644 index 00000000..45c9d08c --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/StatsTest.php @@ -0,0 +1,44 @@ +_createIndex(); + + $index->getType('test')->addDocuments(array( + new Document(1, array('price' => 5)), + new Document(2, array('price' => 8)), + new Document(3, array('price' => 1)), + new Document(4, array('price' => 3)), + )); + + $index->refresh(); + + return $index; + } + + /** + * @group functional + */ + public function testStatsAggregation() + { + $agg = new Stats('stats'); + $agg->setField('price'); + + $query = new Query(); + $query->addAggregation($agg); + $results = $this->_getIndexForTest()->search($query)->getAggregation('stats'); + + $this->assertEquals(4, $results['count']); + $this->assertEquals(1, $results['min']); + $this->assertEquals(8, $results['max']); + $this->assertEquals((5 + 8 + 1 + 3) / 4.0, $results['avg']); + $this->assertEquals((5 + 8 + 1 + 3), $results['sum']); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/SumTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/SumTest.php new file mode 100644 index 00000000..b60e6e14 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/SumTest.php @@ -0,0 +1,40 @@ +_createIndex(); + + $index->getType('test')->addDocuments(array( + new Document(1, array('price' => 5)), + new Document(2, array('price' => 8)), + new Document(3, array('price' => 1)), + new Document(4, array('price' => 3)), + )); + + $index->refresh(); + + return $index; + } + + /** + * @group functional + */ + public function testSumAggregation() + { + $agg = new Sum('sum'); + $agg->setField('price'); + + $query = new Query(); + $query->addAggregation($agg); + $results = $this->_getIndexForTest()->search($query)->getAggregation('sum'); + + $this->assertEquals(5 + 8 + 1 + 3, $results['value']); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/TermsTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/TermsTest.php new file mode 100644 index 00000000..58eb02c2 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/TermsTest.php @@ -0,0 +1,41 @@ +_createIndex(); + + $index->getType('test')->addDocuments(array( + new Document(1, array('color' => 'blue')), + new Document(2, array('color' => 'blue')), + new Document(3, array('color' => 'red')), + new Document(4, array('color' => 'green')), + )); + + $index->refresh(); + + return $index; + } + + /** + * @group functional + */ + public function testTermsAggregation() + { + $agg = new Terms('terms'); + $agg->setField('color'); + + $query = new Query(); + $query->addAggregation($agg); + $results = $this->_getIndexForTest()->search($query)->getAggregation('terms'); + + $this->assertEquals(2, $results['buckets'][0]['doc_count']); + $this->assertEquals('blue', $results['buckets'][0]['key']); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/TopHitsTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/TopHitsTest.php new file mode 100644 index 00000000..afe23e27 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/TopHitsTest.php @@ -0,0 +1,385 @@ +_createIndex(); + + $index->getType('questions')->addDocuments(array( + new Document(1, array( + 'tags' => array('linux'), + 'last_activity_date' => '2015-01-05', + 'title' => 'Question about linux #1', + )), + new Document(2, array( + 'tags' => array('linux'), + 'last_activity_date' => '2014-12-23', + 'title' => 'Question about linux #2', + )), + new Document(3, array( + 'tags' => array('windows'), + 'last_activity_date' => '2015-01-05', + 'title' => 'Question about windows #1', + )), + new Document(4, array( + 'tags' => array('windows'), + 'last_activity_date' => '2014-12-23', + 'title' => 'Question about windows #2', + )), + new Document(5, array( + 'tags' => array('osx', 'apple'), + 'last_activity_date' => '2014-12-23', + 'title' => 'Question about osx', + )), + )); + + $index->refresh(); + + return $index; + } + + /** + * @group unit + */ + public function testSetSize() + { + $agg = new TopHits('agg_name'); + $returnValue = $agg->setSize(12); + $this->assertEquals(12, $agg->getParam('size')); + $this->assertInstanceOf('Elastica\Aggregation\TopHits', $returnValue); + } + + /** + * @group unit + */ + public function testSetFrom() + { + $agg = new TopHits('agg_name'); + $returnValue = $agg->setFrom(12); + $this->assertEquals(12, $agg->getParam('from')); + $this->assertInstanceOf('Elastica\Aggregation\TopHits', $returnValue); + } + + /** + * @group unit + */ + public function testSetSort() + { + $sort = array('last_activity_date' => array('order' => 'desc')); + $agg = new TopHits('agg_name'); + $returnValue = $agg->setSort($sort); + $this->assertEquals($sort, $agg->getParam('sort')); + $this->assertInstanceOf('Elastica\Aggregation\TopHits', $returnValue); + } + + /** + * @group unit + */ + public function testSetSource() + { + $fields = array('title', 'tags'); + $agg = new TopHits('agg_name'); + $returnValue = $agg->setSource($fields); + $this->assertEquals($fields, $agg->getParam('_source')); + $this->assertInstanceOf('Elastica\Aggregation\TopHits', $returnValue); + } + + /** + * @group unit + */ + public function testSetVersion() + { + $agg = new TopHits('agg_name'); + $returnValue = $agg->setVersion(true); + $this->assertTrue($agg->getParam('version')); + $this->assertInstanceOf('Elastica\Aggregation\TopHits', $returnValue); + + $agg->setVersion(false); + $this->assertFalse($agg->getParam('version')); + } + + /** + * @group unit + */ + public function testSetExplain() + { + $agg = new TopHits('agg_name'); + $returnValue = $agg->setExplain(true); + $this->assertTrue($agg->getParam('explain')); + $this->assertInstanceOf('Elastica\Aggregation\TopHits', $returnValue); + + $agg->setExplain(false); + $this->assertFalse($agg->getParam('explain')); + } + + /** + * @group unit + */ + public function testSetHighlight() + { + $highlight = array( + 'fields' => array( + 'title', + ), + ); + $agg = new TopHits('agg_name'); + $returnValue = $agg->setHighlight($highlight); + $this->assertEquals($highlight, $agg->getParam('highlight')); + $this->assertInstanceOf('Elastica\Aggregation\TopHits', $returnValue); + } + + /** + * @group unit + */ + public function testSetFieldDataFields() + { + $fields = array('title', 'tags'); + $agg = new TopHits('agg_name'); + $returnValue = $agg->setFieldDataFields($fields); + $this->assertEquals($fields, $agg->getParam('fielddata_fields')); + $this->assertInstanceOf('Elastica\Aggregation\TopHits', $returnValue); + } + + /** + * @group unit + */ + public function testSetScriptFields() + { + $script = new Script('1 + 2'); + $scriptFields = new ScriptFields(array('three' => $script)); + + $agg = new TopHits('agg_name'); + $returnValue = $agg->setScriptFields($scriptFields); + $this->assertEquals($scriptFields->toArray(), $agg->getParam('script_fields')); + $this->assertInstanceOf('Elastica\Aggregation\TopHits', $returnValue); + } + + /** + * @group unit + */ + public function testAddScriptField() + { + $script = new Script('2+3'); + $agg = new TopHits('agg_name'); + $returnValue = $agg->addScriptField('five', $script); + $this->assertEquals(array('five' => $script->toArray()), $agg->getParam('script_fields')); + $this->assertInstanceOf('Elastica\Aggregation\TopHits', $returnValue); + } + + protected function getOuterAggregationResult($innerAggr) + { + $outerAggr = new Terms('top_tags'); + $outerAggr->setField('tags'); + $outerAggr->setMinimumDocumentCount(2); + $outerAggr->addAggregation($innerAggr); + + $query = new Query(new MatchAll()); + $query->addAggregation($outerAggr); + + return $this->_getIndexForTest()->search($query)->getAggregation('top_tags'); + } + + /** + * @group functional + */ + public function testAggregateUpdatedRecently() + { + $aggr = new TopHits('top_tag_hits'); + $aggr->setSize(1); + $aggr->setSort(array('last_activity_date' => array('order' => 'desc'))); + + $resultDocs = array(); + $outerAggrResult = $this->getOuterAggregationResult($aggr); + foreach ($outerAggrResult['buckets'] as $bucket) { + foreach ($bucket['top_tag_hits']['hits']['hits'] as $doc) { + $resultDocs[] = $doc['_id']; + } + } + $this->assertEquals(array(1, 3), $resultDocs); + } + + /** + * @group functional + */ + public function testAggregateUpdatedFarAgo() + { + $aggr = new TopHits('top_tag_hits'); + $aggr->setSize(1); + $aggr->setSort(array('last_activity_date' => array('order' => 'asc'))); + + $resultDocs = array(); + $outerAggrResult = $this->getOuterAggregationResult($aggr); + foreach ($outerAggrResult['buckets'] as $bucket) { + foreach ($bucket['top_tag_hits']['hits']['hits'] as $doc) { + $resultDocs[] = $doc['_id']; + } + } + $this->assertEquals(array(2, 4), $resultDocs); + } + + /** + * @group functional + */ + public function testAggregateTwoDocumentPerTag() + { + $aggr = new TopHits('top_tag_hits'); + $aggr->setSize(2); + + $resultDocs = array(); + $outerAggrResult = $this->getOuterAggregationResult($aggr); + foreach ($outerAggrResult['buckets'] as $bucket) { + foreach ($bucket['top_tag_hits']['hits']['hits'] as $doc) { + $resultDocs[] = $doc['_id']; + } + } + $this->assertEquals(array(1, 2, 3, 4), $resultDocs); + } + + /** + * @group functional + */ + public function testAggregateTwoDocumentPerTagWithOffset() + { + $aggr = new TopHits('top_tag_hits'); + $aggr->setSize(2); + $aggr->setFrom(1); + $aggr->setSort(array('last_activity_date' => array('order' => 'desc'))); + + $resultDocs = array(); + $outerAggrResult = $this->getOuterAggregationResult($aggr); + foreach ($outerAggrResult['buckets'] as $bucket) { + foreach ($bucket['top_tag_hits']['hits']['hits'] as $doc) { + $resultDocs[] = $doc['_id']; + } + } + $this->assertEquals(array(2, 4), $resultDocs); + } + + /** + * @group functional + */ + public function testAggregateWithLimitedSource() + { + $aggr = new TopHits('top_tag_hits'); + $aggr->setSource(array('title')); + + $resultDocs = array(); + $outerAggrResult = $this->getOuterAggregationResult($aggr); + foreach ($outerAggrResult['buckets'] as $bucket) { + foreach ($bucket['top_tag_hits']['hits']['hits'] as $doc) { + $this->assertArrayHasKey('title', $doc['_source']); + $this->assertArrayNotHasKey('tags', $doc['_source']); + $this->assertArrayNotHasKey('last_activity_date', $doc['_source']); + } + } + } + + /** + * @group functional + */ + public function testAggregateWithVersion() + { + $aggr = new TopHits('top_tag_hits'); + $aggr->setVersion(true); + + $resultDocs = array(); + $outerAggrResult = $this->getOuterAggregationResult($aggr); + foreach ($outerAggrResult['buckets'] as $bucket) { + foreach ($bucket['top_tag_hits']['hits']['hits'] as $doc) { + $this->assertArrayHasKey('_version', $doc); + } + } + } + + /** + * @group functional + */ + public function testAggregateWithExplain() + { + $aggr = new TopHits('top_tag_hits'); + $aggr->setExplain(true); + + $resultDocs = array(); + $outerAggrResult = $this->getOuterAggregationResult($aggr); + foreach ($outerAggrResult['buckets'] as $bucket) { + foreach ($bucket['top_tag_hits']['hits']['hits'] as $doc) { + $this->assertArrayHasKey('_explanation', $doc); + } + } + } + + /** + * @group functional + */ + public function testAggregateWithScriptFields() + { + $aggr = new TopHits('top_tag_hits'); + $aggr->setSize(1); + $aggr->setScriptFields(array('three' => new Script('1 + 2'))); + $aggr->addScriptField('five', new Script('3 + 2')); + + $resultDocs = array(); + $outerAggrResult = $this->getOuterAggregationResult($aggr); + foreach ($outerAggrResult['buckets'] as $bucket) { + foreach ($bucket['top_tag_hits']['hits']['hits'] as $doc) { + $this->assertEquals(3, $doc['fields']['three'][0]); + $this->assertEquals(5, $doc['fields']['five'][0]); + } + } + } + + /** + * @group functional + */ + public function testAggregateWithHighlight() + { + $queryString = new SimpleQueryString('linux', array('title')); + + $aggr = new TopHits('top_tag_hits'); + $aggr->setHighlight(array('fields' => array('title' => new \stdClass()))); + + $query = new Query($queryString); + $query->addAggregation($aggr); + + $resultSet = $this->_getIndexForTest()->search($query); + $aggrResult = $resultSet->getAggregation('top_tag_hits'); + + foreach ($aggrResult['hits']['hits'] as $doc) { + $this->assertArrayHasKey('highlight', $doc); + $this->assertRegExp('#linux#', $doc['highlight']['title'][0]); + } + } + + /** + * @group functional + */ + public function testAggregateWithFieldData() + { + $aggr = new TopHits('top_tag_hits'); + $aggr->setFieldDataFields(array('title')); + + $query = new Query(new MatchAll()); + $query->addAggregation($aggr); + + $resultSet = $this->_getIndexForTest()->search($query); + $aggrResult = $resultSet->getAggregation('top_tag_hits'); + + foreach ($aggrResult['hits']['hits'] as $doc) { + $this->assertArrayHasKey('fields', $doc); + $this->assertArrayHasKey('title', $doc['fields']); + $this->assertArrayNotHasKey('tags', $doc['fields']); + $this->assertArrayNotHasKey('last_activity_date', $doc['fields']); + } + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/ValueCountTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/ValueCountTest.php new file mode 100644 index 00000000..21b63cbe --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/ValueCountTest.php @@ -0,0 +1,40 @@ +_createIndex(); + + $index->getType('test')->addDocuments(array( + new Document(1, array('price' => 5)), + new Document(2, array('price' => 8)), + new Document(3, array('price' => 1)), + new Document(4, array('price' => 3)), + new Document(5, array('price' => 3)), + )); + + $index->refresh(); + + return $index; + } + + /** + * @group functional + */ + public function testValueCountAggregation() + { + $agg = new ValueCount('count', 'price'); + + $query = new Query(); + $query->addAggregation($agg); + $results = $this->_getIndexForTest()->search($query)->getAggregation('count'); + + $this->assertEquals(5, $results['value']); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Base.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Base.php new file mode 100644 index 00000000..c826c747 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Base.php @@ -0,0 +1,142 @@ + $this->_getHost(), + 'port' => $this->_getPort(), + ); + + $config = array_merge($config, $params); + + return new Client($config, $callback); + } + + /** + * @return string Host to es for elastica tests + */ + protected function _getHost() + { + return getenv('ES_HOST') ?: Connection::DEFAULT_HOST; + } + + /** + * @return int Port to es for elastica tests + */ + protected function _getPort() + { + return getenv('ES_PORT') ?: Connection::DEFAULT_PORT; + } + + /** + * @return string Proxy url string + */ + protected function _getProxyUrl() + { + $proxyHost = getenv('PROXY_HOST') ?: Connection::DEFAULT_HOST; + + return 'http://'.$proxyHost.':12345'; + } + + /** + * @return string Proxy url string to proxy which returns 403 + */ + protected function _getProxyUrl403() + { + $proxyHost = getenv('PROXY_HOST') ?: Connection::DEFAULT_HOST; + + return 'http://'.$proxyHost.':12346'; + } + + /** + * @param string $name Index name + * @param bool $delete Delete index if it exists + * @param int $shards Number of shards to create + * + * @return \Elastica\Index + */ + protected function _createIndex($name = null, $delete = true, $shards = 1) + { + if (is_null($name)) { + $name = preg_replace('/[^a-z]/i', '', strtolower(get_called_class())).uniqid(); + } + + $client = $this->_getClient(); + $index = $client->getIndex('elastica_'.$name); + $index->create(array('index' => array('number_of_shards' => $shards, 'number_of_replicas' => 0)), $delete); + + return $index; + } + + protected function _waitForAllocation(Index $index) + { + do { + $settings = $index->getStatus()->get(); + $allocated = true; + foreach ($settings['shards'] as $shard) { + if ($shard[0]['routing']['state'] != 'STARTED') { + $allocated = false; + } + } + } while (!$allocated); + } + + protected function setUp() + { + parent::setUp(); + + $hasGroup = $this->_isUnitGroup() || $this->_isFunctionalGroup() || $this->_isShutdownGroup() || $this->_isBenchmarkGroup(); + $this->assertTrue($hasGroup, 'Every test must have one of "unit", "functional", "shutdown" or "benchmark" group'); + } + + protected function tearDown() + { + if ($this->_isFunctionalGroup()) { + $this->_getClient()->getIndex('_all')->delete(); + $this->_getClient()->getIndex('_all')->clearCache(); + } + + parent::tearDown(); + } + + protected function _isUnitGroup() + { + $groups = \PHPUnit_Util_Test::getGroups(get_class($this), $this->getName(false)); + + return in_array('unit', $groups); + } + + protected function _isFunctionalGroup() + { + $groups = \PHPUnit_Util_Test::getGroups(get_class($this), $this->getName(false)); + + return in_array('functional', $groups); + } + + protected function _isShutdownGroup() + { + $groups = \PHPUnit_Util_Test::getGroups(get_class($this), $this->getName(false)); + + return in_array('shutdown', $groups); + } + + protected function _isBenchmarkGroup() + { + $groups = \PHPUnit_Util_Test::getGroups(get_class($this), $this->getName(false)); + + return in_array('benchmark', $groups); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Bulk/ActionTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Bulk/ActionTest.php new file mode 100644 index 00000000..e4588e70 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Bulk/ActionTest.php @@ -0,0 +1,66 @@ +assertEquals('index', $action->getOpType()); + $this->assertFalse($action->hasSource()); + + $expected = '{"index":{}}'."\n"; + $this->assertEquals($expected, $action->toString()); + + $action->setIndex('index'); + + $expected = '{"index":{"_index":"index"}}'."\n"; + $this->assertEquals($expected, $action->toString()); + + $action->setType('type'); + + $expected = '{"index":{"_index":"index","_type":"type"}}'."\n"; + $this->assertEquals($expected, $action->toString()); + + $action->setId(1); + $expected = '{"index":{"_index":"index","_type":"type","_id":1}}'."\n"; + $this->assertEquals($expected, $action->toString()); + + $action->setRouting(1); + $expected = '{"index":{"_index":"index","_type":"type","_id":1,"_routing":1}}'."\n"; + $this->assertEquals($expected, $action->toString()); + + $client = $this->_getClient(); + $index = new Index($client, 'index2'); + $type = new Type($index, 'type2'); + + $action->setIndex($index); + + $expected = '{"index":{"_index":"index2","_type":"type","_id":1,"_routing":1}}'."\n"; + $this->assertEquals($expected, $action->toString()); + + $action->setType($type); + + $expected = '{"index":{"_index":"index2","_type":"type2","_id":1,"_routing":1}}'."\n"; + $this->assertEquals($expected, $action->toString()); + + $action->setSource(array('user' => 'name')); + + $expected = '{"index":{"_index":"index2","_type":"type2","_id":1,"_routing":1}}'."\n"; + $expected .= '{"user":"name"}'."\n"; + + $this->assertEquals($expected, $action->toString()); + $this->assertTrue($action->hasSource()); + + $this->assertFalse(Action::isValidOpType('foo')); + $this->assertTrue(Action::isValidOpType('delete')); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Bulk/ResponseSetTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Bulk/ResponseSetTest.php new file mode 100644 index 00000000..949dcf7a --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Bulk/ResponseSetTest.php @@ -0,0 +1,200 @@ +_createResponseSet($responseData, $actions); + $this->assertEquals($expected, $responseSet->isOk()); + } + + /** + * @group unit + */ + public function testGetError() + { + list($responseData, $actions) = $this->_getFixture(); + $responseData['items'][1]['index']['ok'] = false; + $responseData['items'][1]['index']['error'] = 'SomeExceptionMessage'; + $responseData['items'][2]['index']['ok'] = false; + $responseData['items'][2]['index']['error'] = 'AnotherExceptionMessage'; + + try { + $this->_createResponseSet($responseData, $actions); + $this->fail('Bulk request should fail'); + } catch (ResponseException $e) { + $responseSet = $e->getResponseSet(); + + $this->assertInstanceOf('Elastica\\Bulk\\ResponseSet', $responseSet); + + $this->assertTrue($responseSet->hasError()); + $this->assertNotEquals('AnotherExceptionMessage', $responseSet->getError()); + $this->assertEquals('SomeExceptionMessage', $responseSet->getError()); + + $actionExceptions = $e->getActionExceptions(); + $this->assertEquals(2, count($actionExceptions)); + + $this->assertInstanceOf('Elastica\Exception\Bulk\Response\ActionException', $actionExceptions[0]); + $this->assertSame($actions[1], $actionExceptions[0]->getAction()); + $this->assertContains('SomeExceptionMessage', $actionExceptions[0]->getMessage()); + $this->assertTrue($actionExceptions[0]->getResponse()->hasError()); + + $this->assertInstanceOf('Elastica\Exception\Bulk\Response\ActionException', $actionExceptions[1]); + $this->assertSame($actions[2], $actionExceptions[1]->getAction()); + $this->assertContains('AnotherExceptionMessage', $actionExceptions[1]->getMessage()); + $this->assertTrue($actionExceptions[1]->getResponse()->hasError()); + } + } + + /** + * @group unit + */ + public function testGetBulkResponses() + { + list($responseData, $actions) = $this->_getFixture(); + + $responseSet = $this->_createResponseSet($responseData, $actions); + + $bulkResponses = $responseSet->getBulkResponses(); + $this->assertInternalType('array', $bulkResponses); + $this->assertEquals(3, count($bulkResponses)); + + foreach ($bulkResponses as $i => $bulkResponse) { + $this->assertInstanceOf('Elastica\\Bulk\\Response', $bulkResponse); + $bulkResponseData = $bulkResponse->getData(); + $this->assertInternalType('array', $bulkResponseData); + $this->assertArrayHasKey('_id', $bulkResponseData); + $this->assertEquals($responseData['items'][$i]['index']['_id'], $bulkResponseData['_id']); + $this->assertSame($actions[$i], $bulkResponse->getAction()); + $this->assertEquals('index', $bulkResponse->getOpType()); + } + } + + /** + * @group unit + */ + public function testIterator() + { + list($responseData, $actions) = $this->_getFixture(); + + $responseSet = $this->_createResponseSet($responseData, $actions); + + $this->assertEquals(3, count($responseSet)); + + foreach ($responseSet as $i => $bulkResponse) { + $this->assertInstanceOf('Elastica\Bulk\Response', $bulkResponse); + $bulkResponseData = $bulkResponse->getData(); + $this->assertInternalType('array', $bulkResponseData); + $this->assertArrayHasKey('_id', $bulkResponseData); + $this->assertEquals($responseData['items'][$i]['index']['_id'], $bulkResponseData['_id']); + $this->assertSame($actions[$i], $bulkResponse->getAction()); + $this->assertEquals('index', $bulkResponse->getOpType()); + } + + $this->assertFalse($responseSet->valid()); + $this->assertNotInstanceOf('Elastica\Bulk\Response', $responseSet->current()); + $this->assertFalse($responseSet->current()); + + $responseSet->next(); + + $this->assertFalse($responseSet->valid()); + $this->assertNotInstanceOf('Elastica\Bulk\Response', $responseSet->current()); + $this->assertFalse($responseSet->current()); + + $responseSet->rewind(); + + $this->assertEquals(0, $responseSet->key()); + $this->assertTrue($responseSet->valid()); + $this->assertInstanceOf('Elastica\Bulk\Response', $responseSet->current()); + } + + public function isOkDataProvider() + { + list($responseData, $actions) = $this->_getFixture(); + + $return = array(); + $return[] = array($responseData, $actions, true); + $responseData['items'][2]['index']['ok'] = false; + $return[] = array($responseData, $actions, false); + + return $return; + } + + /** + * @param array $responseData + * @param array $actions + * + * @return \Elastica\Bulk\ResponseSet + */ + protected function _createResponseSet(array $responseData, array $actions) + { + $client = $this->getMock('Elastica\\Client', array('request')); + + $client->expects($this->once()) + ->method('request') + ->withAnyParameters() + ->will($this->returnValue(new Response($responseData))); + + $bulk = new Bulk($client); + $bulk->addActions($actions); + + return $bulk->send(); + } + + /** + * @return array + */ + protected function _getFixture() + { + $responseData = array( + 'took' => 5, + 'items' => array( + array( + 'index' => array( + '_index' => 'index', + '_type' => 'type', + '_id' => '1', + '_version' => 1, + 'ok' => true, + ), + ), + array( + 'index' => array( + '_index' => 'index', + '_type' => 'type', + '_id' => '2', + '_version' => 1, + 'ok' => true, + ), + ), + array( + 'index' => array( + '_index' => 'index', + '_type' => 'type', + '_id' => '3', + '_version' => 1, + 'ok' => true, + ), + ), + ), + ); + $bulkResponses = array( + new Action(), + new Action(), + new Action(), + ); + + return array($responseData, $bulkResponses); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/BulkTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/BulkTest.php new file mode 100644 index 00000000..c3ff15bc --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/BulkTest.php @@ -0,0 +1,792 @@ +_createIndex(); + $indexName = $index->getName(); + $type = $index->getType('bulk_test'); + $type2 = $index->getType('bulk_test2'); + $client = $index->getClient(); + + $newDocument1 = $type->createDocument(1, array('name' => 'Mister Fantastic')); + $newDocument2 = new Document(2, array('name' => 'Invisible Woman')); + $newDocument3 = $type->createDocument(3, array('name' => 'The Human Torch')); + $newDocument4 = $type->createDocument(null, array('name' => 'The Thing')); + + $newDocument3->setOpType(Document::OP_TYPE_CREATE); + + $documents = array( + $newDocument1, + $newDocument2, + $newDocument3, + $newDocument4, + ); + + $bulk = new Bulk($client); + $bulk->setType($type2); + $bulk->addDocuments($documents); + + $actions = $bulk->getActions(); + + $this->assertInstanceOf('Elastica\Bulk\Action\IndexDocument', $actions[0]); + $this->assertEquals('index', $actions[0]->getOpType()); + $this->assertSame($newDocument1, $actions[0]->getDocument()); + + $this->assertInstanceOf('Elastica\Bulk\Action\IndexDocument', $actions[1]); + $this->assertEquals('index', $actions[1]->getOpType()); + $this->assertSame($newDocument2, $actions[1]->getDocument()); + + $this->assertInstanceOf('Elastica\Bulk\Action\CreateDocument', $actions[2]); + $this->assertEquals('create', $actions[2]->getOpType()); + $this->assertSame($newDocument3, $actions[2]->getDocument()); + + $this->assertInstanceOf('Elastica\Bulk\Action\IndexDocument', $actions[3]); + $this->assertEquals('index', $actions[3]->getOpType()); + $this->assertSame($newDocument4, $actions[3]->getDocument()); + + $data = $bulk->toArray(); + + $expected = array( + array('index' => array('_index' => $indexName, '_type' => 'bulk_test', '_id' => 1)), + array('name' => 'Mister Fantastic'), + array('index' => array('_id' => 2)), + array('name' => 'Invisible Woman'), + array('create' => array('_index' => $indexName, '_type' => 'bulk_test', '_id' => 3)), + array('name' => 'The Human Torch'), + array('index' => array('_index' => $indexName, '_type' => 'bulk_test')), + array('name' => 'The Thing'), + ); + $this->assertEquals($expected, $data); + + $expected = '{"index":{"_index":"'.$indexName.'","_type":"bulk_test","_id":1}} +{"name":"Mister Fantastic"} +{"index":{"_id":2}} +{"name":"Invisible Woman"} +{"create":{"_index":"'.$indexName.'","_type":"bulk_test","_id":3}} +{"name":"The Human Torch"} +{"index":{"_index":"'.$indexName.'","_type":"bulk_test"}} +{"name":"The Thing"} +'; + + $expected = str_replace(PHP_EOL, "\n", $expected); + $this->assertEquals($expected, (string) str_replace(PHP_EOL, "\n", (string) $bulk)); + + $response = $bulk->send(); + + $this->assertInstanceOf('Elastica\Bulk\ResponseSet', $response); + + $this->assertTrue($response->isOk()); + $this->assertFalse($response->hasError()); + + foreach ($response as $i => $bulkResponse) { + $this->assertInstanceOf('Elastica\Bulk\Response', $bulkResponse); + $this->assertTrue($bulkResponse->isOk()); + $this->assertFalse($bulkResponse->hasError()); + $this->assertSame($actions[$i], $bulkResponse->getAction()); + } + + $type->getIndex()->refresh(); + $type2->getIndex()->refresh(); + + $this->assertEquals(3, $type->count()); + $this->assertEquals(1, $type2->count()); + + $bulk = new Bulk($client); + $bulk->addDocument($newDocument3, Action::OP_TYPE_DELETE); + + $data = $bulk->toArray(); + + $expected = array( + array('delete' => array('_index' => $indexName, '_type' => 'bulk_test', '_id' => 3)), + ); + $this->assertEquals($expected, $data); + + $bulk->send(); + + $type->getIndex()->refresh(); + + $this->assertEquals(2, $type->count()); + + try { + $type->getDocument(3); + $this->fail('Document #3 should be deleted'); + } catch (NotFoundException $e) { + $this->assertTrue(true); + } + } + + /** + * @group functional + */ + public function testUnicodeBulkSend() + { + $index = $this->_createIndex(); + $type = $index->getType('bulk_test'); + $type2 = $index->getType('bulk_test2'); + $client = $index->getClient(); + + $newDocument1 = $type->createDocument(1, array('name' => 'Сегодня, я вижу, особенно грустен твой взгляд,')); + $newDocument2 = new Document(2, array('name' => 'И руки особенно тонки, колени обняв.')); + $newDocument3 = $type->createDocument(3, array('name' => 'Послушай: далеко, далеко, на озере Чад / Изысканный бродит жираф.')); + + $documents = array( + $newDocument1, + $newDocument2, + $newDocument3, + ); + + $bulk = new Bulk($client); + $bulk->setType($type2); + $bulk->addDocuments($documents); + + $actions = $bulk->getActions(); + + $this->assertSame($newDocument1, $actions[0]->getDocument()); + $this->assertSame($newDocument2, $actions[1]->getDocument()); + $this->assertSame($newDocument3, $actions[2]->getDocument()); + } + + /** + * @group functional + */ + public function testSetIndexType() + { + $client = $this->_getClient(); + $index = $client->getIndex('index'); + $type = $index->getType('type'); + + $index2 = $client->getIndex('index2'); + $type2 = $index2->getType('type2'); + + $bulk = new Bulk($client); + + $this->assertFalse($bulk->hasIndex()); + $this->assertFalse($bulk->hasType()); + + $bulk->setIndex($index); + $this->assertTrue($bulk->hasIndex()); + $this->assertFalse($bulk->hasType()); + $this->assertEquals('index', $bulk->getIndex()); + + $bulk->setType($type2); + $this->assertTrue($bulk->hasIndex()); + $this->assertTrue($bulk->hasType()); + $this->assertEquals('index2', $bulk->getIndex()); + $this->assertEquals('type2', $bulk->getType()); + + $bulk->setType($type); + $this->assertTrue($bulk->hasIndex()); + $this->assertTrue($bulk->hasType()); + $this->assertEquals('index', $bulk->getIndex()); + $this->assertEquals('type', $bulk->getType()); + + $bulk->setIndex($index2); + $this->assertTrue($bulk->hasIndex()); + $this->assertTrue($bulk->hasType()); + $this->assertEquals('index2', $bulk->getIndex()); + $this->assertEquals('type', $bulk->getType()); + } + + /** + * @group unit + */ + public function testAddActions() + { + $client = $this->_getClient(); + $bulk = new Bulk($client); + + $action1 = new Action(Action::OP_TYPE_DELETE); + $action1->setIndex('index'); + $action1->setType('type'); + $action1->setId(1); + + $action2 = new Action(Action::OP_TYPE_INDEX); + $action2->setIndex('index'); + $action2->setType('type'); + $action2->setId(1); + $action2->setSource(array('name' => 'Batman')); + + $actions = array( + $action1, + $action2, + ); + + $bulk->addActions($actions); + + $getActions = $bulk->getActions(); + + $this->assertSame($action1, $getActions[0]); + $this->assertSame($action2, $getActions[1]); + } + + /** + * @group unit + */ + public function testAddRawData() + { + $bulk = new Bulk($this->_getClient()); + + $rawData = array( + array('index' => array('_index' => 'test', '_type' => 'user', '_id' => '1')), + array('user' => array('name' => 'hans')), + array('delete' => array('_index' => 'test', '_type' => 'user', '_id' => '2')), + array('delete' => array('_index' => 'test', '_type' => 'user', '_id' => '3')), + array('create' => array('_index' => 'test', '_type' => 'user', '_id' => '4')), + array('user' => array('name' => 'mans')), + array('delete' => array('_index' => 'test', '_type' => 'user', '_id' => '5')), + ); + + $bulk->addRawData($rawData); + + $actions = $bulk->getActions(); + + $this->assertInternalType('array', $actions); + $this->assertEquals(5, count($actions)); + + $this->assertInstanceOf('Elastica\Bulk\Action', $actions[0]); + $this->assertEquals('index', $actions[0]->getOpType()); + $this->assertEquals($rawData[0]['index'], $actions[0]->getMetadata()); + $this->assertTrue($actions[0]->hasSource()); + $this->assertEquals($rawData[1], $actions[0]->getSource()); + + $this->assertInstanceOf('Elastica\Bulk\Action', $actions[1]); + $this->assertEquals('delete', $actions[1]->getOpType()); + $this->assertEquals($rawData[2]['delete'], $actions[1]->getMetadata()); + $this->assertFalse($actions[1]->hasSource()); + + $this->assertInstanceOf('Elastica\Bulk\Action', $actions[2]); + $this->assertEquals('delete', $actions[2]->getOpType()); + $this->assertEquals($rawData[3]['delete'], $actions[2]->getMetadata()); + $this->assertFalse($actions[2]->hasSource()); + + $this->assertInstanceOf('Elastica\Bulk\Action', $actions[3]); + $this->assertEquals('create', $actions[3]->getOpType()); + $this->assertEquals($rawData[4]['create'], $actions[3]->getMetadata()); + $this->assertTrue($actions[3]->hasSource()); + $this->assertEquals($rawData[5], $actions[3]->getSource()); + + $this->assertInstanceOf('Elastica\Bulk\Action', $actions[4]); + $this->assertEquals('delete', $actions[4]->getOpType()); + $this->assertEquals($rawData[6]['delete'], $actions[4]->getMetadata()); + $this->assertFalse($actions[4]->hasSource()); + } + + /** + * @group unit + * @dataProvider invalidRawDataProvider + * @expectedException \Elastica\Exception\InvalidException + */ + public function testInvalidRawData($rawData, $failMessage) + { + $bulk = new Bulk($this->_getClient()); + + $bulk->addRawData($rawData); + + $this->fail($failMessage); + } + + public function invalidRawDataProvider() + { + return array( + array( + array( + array('index' => array('_index' => 'test', '_type' => 'user', '_id' => '1')), + array('user' => array('name' => 'hans')), + array('user' => array('name' => 'mans')), + ), + 'Two sources for one action', + ), + array( + array( + array('index' => array('_index' => 'test', '_type' => 'user', '_id' => '1')), + array('user' => array('name' => 'hans')), + array('upsert' => array('_index' => 'test', '_type' => 'user', '_id' => '2')), + ), + 'Invalid optype for action', + ), + array( + array( + array('user' => array('name' => 'mans')), + ), + 'Source without action', + ), + array( + array( + array(), + ), + 'Empty array', + ), + array( + array( + 'dummy', + ), + 'String as data', + ), + ); + } + + /** + * @group unit + */ + public function testCreateAbstractDocumentWithInvalidData() + { + //Wrong class type + try { + $badDocument = new \stdClass(); + AbstractDocument::create($badDocument); + $this->fail('Tried to create an abstract document with an object that is not a Document or Script, but no exception was thrown'); + } catch (\Exception $e) { + //Excepted exception was thrown. + } + + //Try to create document with a script + try { + $script = new Script(); + AbstractDocument::create($script, AbstractDocument::OP_TYPE_CREATE); + $this->fail('Tried to create an abstract document with a Script, but no exception was thrown'); + } catch (\Exception $e) { + //Excepted exception was thrown. + } + } + + /** + * @group functional + */ + public function testErrorRequest() + { + $index = $this->_createIndex(); + $type = $index->getType('bulk_test'); + $client = $index->getClient(); + + $documents = array( + $type->createDocument(1, array('name' => 'Mister Fantastic')), + $type->createDocument(2, array('name' => 'Invisible Woman')), + $type->createDocument(2, array('name' => 'The Human Torch')), + ); + + $documents[2]->setOpType(Document::OP_TYPE_CREATE); + + $bulk = new Bulk($client); + $bulk->addDocuments($documents); + + try { + $bulk->send(); + $bulk->fail('3rd document create should produce error'); + } catch (ResponseException $e) { + $this->assertContains('DocumentAlreadyExists', $e->getMessage()); + $failures = $e->getFailures(); + $this->assertInternalType('array', $failures); + $this->assertArrayHasKey(0, $failures); + $this->assertContains('DocumentAlreadyExists', $failures[0]); + } + } + + /** + * @group functional + */ + public function testRawDocumentDataRequest() + { + $index = $this->_createIndex(); + $type = $index->getType('bulk_test'); + $client = $index->getClient(); + + $documents = array( + new Document(null, '{"name":"Mister Fantastic"}'), + new Document(null, '{"name":"Invisible Woman"}'), + new Document(null, '{"name":"The Human Torch"}'), + ); + + $bulk = new Bulk($client); + $bulk->addDocuments($documents); + $bulk->setType($type); + + $expectedJson = '{"index":{}} +{"name":"Mister Fantastic"} +{"index":{}} +{"name":"Invisible Woman"} +{"index":{}} +{"name":"The Human Torch"} +'; + $expectedJson = str_replace(PHP_EOL, "\n", $expectedJson); + $this->assertEquals($expectedJson, $bulk->toString()); + + $response = $bulk->send(); + $this->assertTrue($response->isOk()); + + $type->getIndex()->refresh(); + + $response = $type->search(); + $this->assertEquals(3, $response->count()); + + foreach (array('Mister', 'Invisible', 'Torch') as $name) { + $result = $type->search($name); + $this->assertEquals(1, count($result->getResults())); + } + } + + /** + * @group functional + * @dataProvider udpDataProvider + */ + public function testUdp($clientConfig, $host, $port, $shouldFail = false) + { + if (!function_exists('socket_create')) { + $this->markTestSkipped('Function socket_create() does not exist.'); + } + $client = $this->_getClient($clientConfig); + $index = $client->getIndex('elastica_test'); + $index->create(array('index' => array('number_of_shards' => 1, 'number_of_replicas' => 0)), true); + $type = $index->getType('udp_test'); + $client = $index->getClient(); + + $type->setMapping(array('name' => array('type' => 'string'))); + + $docs = array( + $type->createDocument(1, array('name' => 'Mister Fantastic')), + $type->createDocument(2, array('name' => 'Invisible Woman')), + $type->createDocument(3, array('name' => 'The Human Torch')), + $type->createDocument(4, array('name' => 'The Thing')), + $type->createDocument(5, array('name' => 'Mole Man')), + $type->createDocument(6, array('name' => 'The Skrulls')), + ); + + $bulk = new Bulk($client); + $bulk->addDocuments($docs); + + $bulk->sendUdp($host, $port); + + $i = 0; + $limit = 20; + do { + usleep(200000); + } while ($type->count() < 6 && ++$i < $limit); + + if ($shouldFail) { + $this->assertEquals($limit, $i, 'Invalid udp connection data. Test should fail'); + } else { + $this->assertLessThan($limit, $i, 'It took too much time waiting for UDP request result'); + + foreach ($docs as $doc) { + $getDoc = $type->getDocument($doc->getId()); + $this->assertEquals($doc->getData(), $getDoc->getData()); + } + } + } + + /** + * @group functional + */ + public function testUpdate() + { + $index = $this->_createIndex(); + $type = $index->getType('bulk_test'); + $client = $index->getClient(); + + $doc1 = $type->createDocument(1, array('name' => 'John')); + $doc2 = $type->createDocument(2, array('name' => 'Paul')); + $doc3 = $type->createDocument(3, array('name' => 'George')); + $doc4 = $type->createDocument(4, array('name' => 'Ringo')); + $documents = array($doc1, $doc2, $doc3, $doc4); + + //index some documents + $bulk = new Bulk($client); + $bulk->setType($type); + $bulk->addDocuments($documents); + $response = $bulk->send(); + + $this->assertTrue($response->isOk()); + $this->assertFalse($response->hasError()); + + $index->refresh(); + + //test updating via document + $doc2 = $type->createDocument(2, array('name' => 'The Walrus')); + $bulk = new Bulk($client); + $bulk->setType($type); + $updateAction = new \Elastica\Bulk\Action\UpdateDocument($doc2); + $bulk->addAction($updateAction); + $response = $bulk->send(); + + $this->assertTrue($response->isOk()); + $this->assertFalse($response->hasError()); + + $index->refresh(); + + $doc = $type->getDocument(2); + $docData = $doc->getData(); + $this->assertEquals('The Walrus', $docData['name']); + + //test updating via script + $script = new \Elastica\Script('ctx._source.name += param1;', array('param1' => ' was Paul'), null, 2); + $doc2 = new Document(); + $script->setUpsert($doc2); + $updateAction = Action\AbstractDocument::create($script, Action::OP_TYPE_UPDATE); + $bulk = new Bulk($client); + $bulk->setType($type); + $bulk->addAction($updateAction); + $response = $bulk->send(); + + $this->assertTrue($response->isOk()); + $this->assertFalse($response->hasError()); + + $index->refresh(); + + $doc2 = $type->getDocument(2); + $this->assertEquals('The Walrus was Paul', $doc2->name); + + //test upsert + $script = new \Elastica\Script('ctx._scource.counter += count', array('count' => 1), null, 5); + $doc = new Document('', array('counter' => 1)); + $script->setUpsert($doc); + $updateAction = Action\AbstractDocument::create($script, Action::OP_TYPE_UPDATE); + $bulk = new Bulk($client); + $bulk->setType($type); + $bulk->addAction($updateAction); + $response = $bulk->send(); + + $this->assertTrue($response->isOk()); + $this->assertFalse($response->hasError()); + + $index->refresh(); + $doc = $type->getDocument(5); + $this->assertEquals(1, $doc->counter); + + //test doc_as_upsert + $doc = new \Elastica\Document(6, array('test' => 'test')); + $doc->setDocAsUpsert(true); + $updateAction = Action\AbstractDocument::create($doc, Action::OP_TYPE_UPDATE); + $bulk = new Bulk($client); + $bulk->setType($type); + $bulk->addAction($updateAction); + $response = $bulk->send(); + + $this->assertTrue($response->isOk()); + $this->assertFalse($response->hasError()); + + $index->refresh(); + $doc = $type->getDocument(6); + $this->assertEquals('test', $doc->test); + + //test doc_as_upsert with set of documents (use of addDocuments) + $doc1 = new \Elastica\Document(7, array('test' => 'test1')); + $doc1->setDocAsUpsert(true); + $doc2 = new \Elastica\Document(8, array('test' => 'test2')); + $doc2->setDocAsUpsert(true); + $docs = array($doc1, $doc2); + $bulk = new Bulk($client); + $bulk->setType($type); + $bulk->addDocuments($docs, \Elastica\Bulk\Action::OP_TYPE_UPDATE); + $response = $bulk->send(); + + $this->assertTrue($response->isOk()); + $this->assertFalse($response->hasError()); + + $index->refresh(); + $doc = $type->getDocument(7); + $this->assertEquals('test1', $doc->test); + $doc = $type->getDocument(8); + $this->assertEquals('test2', $doc->test); + + //test updating via document with json string as data + $doc3 = $type->createDocument(2); + $bulk = new Bulk($client); + $bulk->setType($type); + $doc3->setData('{"name" : "Paul it is"}'); + $updateAction = new \Elastica\Bulk\Action\UpdateDocument($doc3); + $bulk->addAction($updateAction); + $response = $bulk->send(); + + $this->assertTrue($response->isOk()); + $this->assertFalse($response->hasError()); + + $index->refresh(); + + $doc = $type->getDocument(2); + $docData = $doc->getData(); + $this->assertEquals('Paul it is', $docData['name']); + + $index->delete(); + } + + /** + * @group unit + */ + public function testGetPath() + { + $client = $this->_getClient(); + $bulk = new Bulk($client); + + $this->assertEquals('_bulk', $bulk->getPath()); + + $indexName = 'testIndex'; + + $bulk->setIndex($indexName); + $this->assertEquals($indexName.'/_bulk', $bulk->getPath()); + + $typeName = 'testType'; + $bulk->setType($typeName); + $this->assertEquals($indexName.'/'.$typeName.'/_bulk', $bulk->getPath()); + } + + /** + * @group functional + */ + public function testRetry() + { + $index = $this->_createIndex(); + $type = $index->getType('bulk_test'); + $client = $index->getClient(); + + $doc1 = $type->createDocument(1, array('name' => 'Mister Fantastic')); + $doc1->setOpType(Action::OP_TYPE_UPDATE); + $doc1->setRetryOnConflict(5); + + $bulk = new Bulk($client); + $bulk->addDocument($doc1); + + $actions = $bulk->getActions(); + + $metadata = $actions[0]->getMetadata(); + $this->assertEquals(5, $metadata[ '_retry_on_conflict' ]); + + $script = new \Elastica\Script(''); + $script->setRetryOnConflict(5); + + $bulk = new Bulk($client); + $bulk->addScript($script); + + $actions = $bulk->getActions(); + + $metadata = $actions[0]->getMetadata(); + $this->assertEquals(5, $metadata[ '_retry_on_conflict' ]); + } + + /** + * @group unit + */ + public function testSetShardTimeout() + { + $bulk = new Bulk($this->_getClient()); + $this->assertInstanceOf('Elastica\Bulk', $bulk->setShardTimeout(10)); + } + + /** + * @group unit + */ + public function testSetRequestParam() + { + $bulk = new Bulk($this->_getClient()); + $this->assertInstanceOf('Elastica\Bulk', $bulk->setRequestParam('key', 'value')); + } + + /** + * @group benchmark + */ + public function testMemoryUsage() + { + $type = $this->_createIndex()->getType('test'); + + $data = array( + 'text1' => 'Very long text for a string', + 'text2' => 'But this is not very long', + 'text3' => 'random or not random?', + ); + + $startMemory = memory_get_usage(); + + for ($n = 1; $n < 10; $n++) { + $docs = array(); + + for ($i = 1; $i <= 3000; $i++) { + $docs[] = new Document(uniqid(), $data); + } + + $type->addDocuments($docs); + $docs = array(); + } + + unset($docs); + + $endMemory = memory_get_usage(); + + $this->assertLessThan(1.3, $endMemory / $startMemory); + } + + public function udpDataProvider() + { + return array( + array( + array(), + null, + null, + ), + array( + array(), + $this->_getHost(), + null, + ), + array( + array(), + null, + 9700, + ), + array( + array(), + $this->_getHost(), + 9700, + ), + array( + array( + 'udp' => array( + 'host' => $this->_getHost(), + 'port' => 9700, + ), + ), + null, + null, + ), + array( + array( + 'udp' => array( + 'host' => $this->_getHost(), + 'port' => 9800, + ), + ), + $this->_getHost(), + 9700, + ), + array( + array( + 'udp' => array( + 'host' => $this->_getHost(), + 'port' => 9800, + ), + ), + null, + null, + true, + ), + array( + array(), + $this->_getHost(), + 9800, + true, + ), + ); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/ClientTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/ClientTest.php new file mode 100644 index 00000000..a5f2b46f --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/ClientTest.php @@ -0,0 +1,1160 @@ +_getClient(); + $this->assertCount(1, $client->getConnections()); + } + + /** + * @group functional + */ + public function testConnectionsArray() + { + // Creates a new index 'xodoa' and a type 'user' inside this index + $client = $this->_getClient(array('connections' => array(array('host' => $this->_getHost(), 'port' => 9200)))); + $index = $client->getIndex('elastica_test1'); + $index->create(array(), true); + + $type = $index->getType('user'); + + // Adds 1 document to the index + $doc1 = new Document(1, + array('username' => 'hans', 'test' => array('2', '3', '5')) + ); + $type->addDocument($doc1); + + // Adds a list of documents with _bulk upload to the index + $docs = array(); + $docs[] = new Document(2, + array('username' => 'john', 'test' => array('1', '3', '6')) + ); + $docs[] = new Document(3, + array('username' => 'rolf', 'test' => array('2', '3', '7')) + ); + $type->addDocuments($docs); + + // Refresh index + $index->refresh(); + + $resultSet = $type->search('rolf'); + } + + /** + * @group functional + */ + public function testTwoServersSame() + { + // Creates a new index 'xodoa' and a type 'user' inside this index + $client = $this->_getClient(array('connections' => array( + array('host' => $this->_getHost(), 'port' => 9200), + array('host' => $this->_getHost(), 'port' => 9200), + ))); + $index = $client->getIndex('elastica_test1'); + $index->create(array(), true); + + $type = $index->getType('user'); + + // Adds 1 document to the index + $doc1 = new Document(1, + array('username' => 'hans', 'test' => array('2', '3', '5')) + ); + $type->addDocument($doc1); + + // Adds a list of documents with _bulk upload to the index + $docs = array(); + $docs[] = new Document(2, + array('username' => 'john', 'test' => array('1', '3', '6')) + ); + $docs[] = new Document(3, + array('username' => 'rolf', 'test' => array('2', '3', '7')) + ); + $type->addDocuments($docs); + + // Refresh index + $index->refresh(); + + $resultSet = $type->search('rolf'); + } + + /** + * @group unit + */ + public function testConnectionParamsArePreparedForConnectionsOption() + { + $url = 'https://'.$this->_getHost().':9200'; + $client = $this->_getClient(array('connections' => array(array('url' => $url)))); + $connection = $client->getConnection(); + + $this->assertEquals($url, $connection->getConfig('url')); + } + + /** + * @group unit + */ + public function testConnectionParamsArePreparedForServersOption() + { + $url = 'https://'.$this->_getHost().':9200'; + $client = $this->_getClient(array('servers' => array(array('url' => $url)))); + $connection = $client->getConnection(); + + $this->assertEquals($url, $connection->getConfig('url')); + } + + /** + * @group unit + */ + public function testConnectionParamsArePreparedForDefaultOptions() + { + $url = 'https://'.$this->_getHost().':9200'; + $client = $this->_getClient(array('url' => $url)); + $connection = $client->getConnection(); + + $this->assertEquals($url, $connection->getConfig('url')); + } + + /** + * @group functional + */ + public function testBulk() + { + $client = $this->_getClient(); + + $params = array( + array('index' => array('_index' => 'test', '_type' => 'user', '_id' => '1')), + array('user' => array('name' => 'hans')), + array('index' => array('_index' => 'test', '_type' => 'user', '_id' => '2')), + array('user' => array('name' => 'peter')), + ); + + $client->bulk($params); + } + + /** + * @group functional + */ + public function testOptimizeAll() + { + $client = $this->_getClient(); + $response = $client->optimizeAll(); + + $this->assertFalse($response->hasError()); + } + + /** + * @group unit + * @expectedException \Elastica\Exception\InvalidException + */ + public function testAddDocumentsEmpty() + { + $client = $this->_getClient(); + $client->addDocuments(array()); + } + + /** + * Test bulk operations on Index. + * + * @group functional + */ + public function testBulkIndex() + { + $index = $this->_getClient()->getIndex('cryptocurrencies'); + + $anonCoin = new Document(1, array('name' => 'anoncoin'), 'altcoin'); + $ixCoin = new Document(2, array('name' => 'ixcoin'), 'altcoin'); + + $index->addDocuments(array($anonCoin, $ixCoin)); + + $this->assertEquals('anoncoin', $index->getType('altcoin')->getDocument(1)->get('name')); + $this->assertEquals('ixcoin', $index->getType('altcoin')->getDocument(2)->get('name')); + + $index->updateDocuments(array( + new Document(1, array('name' => 'AnonCoin'), 'altcoin'), + new Document(2, array('name' => 'iXcoin'), 'altcoin'), + )); + + $this->assertEquals('AnonCoin', $index->getType('altcoin')->getDocument(1)->get('name')); + $this->assertEquals('iXcoin', $index->getType('altcoin')->getDocument(2)->get('name')); + + $ixCoin->setIndex(null); // Make sure the index gets set properly if missing + $index->deleteDocuments(array($anonCoin, $ixCoin)); + + $this->setExpectedException('Elastica\Exception\NotFoundException'); + $index->getType('altcoin')->getDocument(1); + $index->getType('altcoin')->getDocument(2); + } + + /** + * Test bulk operations on Type. + * + * @group functional + */ + public function testBulkType() + { + $type = $this->_getClient()->getIndex('cryptocurrencies')->getType('altcoin'); + + $liteCoin = new Document(1, array('name' => 'litecoin')); + $nameCoin = new Document(2, array('name' => 'namecoin')); + + $type->addDocuments(array($liteCoin, $nameCoin)); + + $this->assertEquals('litecoin', $type->getDocument(1)->get('name')); + $this->assertEquals('namecoin', $type->getDocument(2)->get('name')); + + $type->updateDocuments(array( + new Document(1, array('name' => 'LiteCoin')), + new Document(2, array('name' => 'NameCoin')), + )); + + $this->assertEquals('LiteCoin', $type->getDocument(1)->get('name')); + $this->assertEquals('NameCoin', $type->getDocument(2)->get('name')); + + $nameCoin->setType(null); // Make sure the type gets set properly if missing + $type->deleteDocuments(array($liteCoin, $nameCoin)); + + $this->setExpectedException('Elastica\Exception\NotFoundException'); + $type->getDocument(1); + $type->getDocument(2); + } + + /** + * @group functional + */ + public function testUpdateDocuments() + { + $indexName = 'test'; + $typeName = 'people'; + + $client = $this->_getClient(); + $type = $client->getIndex($indexName)->getType($typeName); + + $initialValue = 28; + $modifiedValue = 27; + + $doc1 = new Document( + 1, + array('name' => 'hans', 'age' => $initialValue), + $typeName, + $indexName + ); + $doc2 = new Document( + 2, + array('name' => 'anna', 'age' => $initialValue), + $typeName, + $indexName + ); + $data = array($doc1, $doc2); + $client->addDocuments($data); + + foreach ($data as $i => $doc) { + $data[$i]->age = $modifiedValue; + } + $client->updateDocuments($data); + + $docData1 = $type->getDocument(1)->getData(); + $docData2 = $type->getDocument(2)->getData(); + + $this->assertEquals($modifiedValue, $docData1['age']); + $this->assertEquals($modifiedValue, $docData2['age']); + } + + /** + * Test deleteIds method using string parameters. + * + * This test ensures that the deleteIds method of + * the \Elastica\Client can properly accept and use + * an $index parameter and $type parameter that are + * strings + * + * This test is a bit more verbose than just sending the + * values to deleteIds and checking for exceptions or + * warnings. + * + * It will add a document, search for it, then delete it + * using the parameter types we are interested in, and then + * re-search to verify that they have been deleted + * + * @group functional + */ + public function testDeleteIdsIdxStringTypeString() + { + $data = array('username' => 'hans'); + $userSearch = 'username:hans'; + + $index = $this->_createIndex(null, true, 2); + + // Create the index, deleting it first if it already exists + $index->create(array(), true); + $type = $index->getType('user'); + + // Adds 1 document to the index + $doc = new Document(null, $data); + $doc->setRouting(1); + $result = $type->addDocument($doc); + + // Refresh index + $index->refresh(); + + $resultData = $result->getData(); + $ids = array($resultData['_id']); + + // Check to make sure the document is in the index + $resultSet = $type->search($userSearch); + $totalHits = $resultSet->getTotalHits(); + $this->assertEquals(1, $totalHits); + + // And verify that the variables we are doing to send to + // deleteIds are the type we are testing for + $idxString = $index->getName(); + $typeString = $type->getName(); + $this->assertTrue(is_string($idxString)); + $this->assertTrue(is_string($typeString)); + + // Try to delete doc with a routing value which hashes to + // a different shard then the id. + $resp = $index->getClient()->deleteIds($ids, $index, $type, 2); + + // Refresh the index + $index->refresh(); + + // Research the index to verify that the items are still there + $resultSet = $type->search($userSearch); + $totalHits = $resultSet->getTotalHits(); + $this->assertEquals(1, $totalHits); + + // Using the existing $index and $type variables which + // are \Elastica\Index and \Elastica\Type objects respectively + $resp = $index->getClient()->deleteIds($ids, $index, $type, 1); + + // Refresh the index to clear out deleted ID information + $index->refresh(); + + // Research the index to verify that the items have been deleted + $resultSet = $type->search($userSearch); + $totalHits = $resultSet->getTotalHits(); + $this->assertEquals(0, $totalHits); + } + + /** + * Test deleteIds method using string parameter for $index + * and object parameter for $type. + * + * This test ensures that the deleteIds method of + * the \Elastica\Client can properly accept and use + * an $index parameter that is a string and a $type + * parameter that is of type \Elastica\Type + * + * This test is a bit more verbose than just sending the + * values to deleteIds and checking for exceptions or + * warnings. + * + * It will add a document, search for it, then delete it + * using the parameter types we are interested in, and then + * re-search to verify that they have been deleted + * + * @group functional + */ + public function testDeleteIdsIdxStringTypeObject() + { + $data = array('username' => 'hans'); + $userSearch = 'username:hans'; + + $index = $this->_createIndex(); + + // Create the index, deleting it first if it already exists + $index->create(array(), true); + $type = $index->getType('user'); + + // Adds 1 document to the index + $doc = new Document(null, $data); + $result = $type->addDocument($doc); + + // Refresh index + $index->refresh(); + + $resultData = $result->getData(); + $ids = array($resultData['_id']); + + // Check to make sure the document is in the index + $resultSet = $type->search($userSearch); + $totalHits = $resultSet->getTotalHits(); + $this->assertEquals(1, $totalHits); + + // And verify that the variables we are doing to send to + // deleteIds are the type we are testing for + $idxString = $index->getName(); + $this->assertTrue(is_string($idxString)); + $this->assertInstanceOf('Elastica\Type', $type); + + // Using the existing $index and $type variables which + // are \Elastica\Index and \Elastica\Type objects respectively + $resp = $index->getClient()->deleteIds($ids, $index, $type); + + // Refresh the index to clear out deleted ID information + $index->refresh(); + + // Research the index to verify that the items have been deleted + $resultSet = $type->search($userSearch); + $totalHits = $resultSet->getTotalHits(); + $this->assertEquals(0, $totalHits); + } + + /** + * Test deleteIds method using object parameter for $index + * and string parameter for $type. + * + * This test ensures that the deleteIds method of + * the \Elastica\Client can properly accept and use + * an $index parameter that is of type Elasitca_Index + * and a $type parameter that is a string + * + * This test is a bit more verbose than just sending the + * values to deleteIds and checking for exceptions or + * warnings. + * + * It will add a document, search for it, then delete it + * using the parameter types we are interested in, and then + * re-search to verify that they have been deleted + * + * @group functional + */ + public function testDeleteIdsIdxObjectTypeString() + { + $data = array('username' => 'hans'); + $userSearch = 'username:hans'; + + $index = $this->_createIndex(); + + // Create the index, deleting it first if it already exists + $index->create(array(), true); + $type = $index->getType('user'); + + // Adds 1 document to the index + $doc = new Document(null, $data); + $result = $type->addDocument($doc); + + // Refresh index + $index->refresh(); + + $resultData = $result->getData(); + $ids = array($resultData['_id']); + + // Check to make sure the document is in the index + $resultSet = $type->search($userSearch); + $totalHits = $resultSet->getTotalHits(); + $this->assertEquals(1, $totalHits); + + // And verify that the variables we are doing to send to + // deleteIds are the type we are testing for + $typeString = $type->getName(); + $this->assertInstanceOf('Elastica\Index', $index); + $this->assertTrue(is_string($typeString)); + + // Using the existing $index and $type variables which + // are \Elastica\Index and \Elastica\Type objects respectively + $resp = $index->getClient()->deleteIds($ids, $index, $type); + + // Refresh the index to clear out deleted ID information + $index->refresh(); + + // Research the index to verify that the items have been deleted + $resultSet = $type->search($userSearch); + $totalHits = $resultSet->getTotalHits(); + $this->assertEquals(0, $totalHits); + } + + /** + * Test deleteIds method using object parameter for $index + * and object parameter for $type. + * + * This test ensures that the deleteIds method of + * the \Elastica\Client can properly accept and use + * an $index parameter that is an object and a $type + * parameter that is of type \Elastica\Type + * + * This test is a bit more verbose than just sending the + * values to deleteIds and checking for exceptions or + * warnings. + * + * It will add a document, search for it, then delete it + * using the parameter types we are interested in, and then + * re-search to verify that they have been deleted + * + * @group functional + */ + public function testDeleteIdsIdxObjectTypeObject() + { + $data = array('username' => 'hans'); + $userSearch = 'username:hans'; + + $index = $this->_createIndex(); + + // Create the index, deleting it first if it already exists + $index->create(array(), true); + $type = $index->getType('user'); + + // Adds 1 document to the index + $doc = new Document(null, $data); + $result = $type->addDocument($doc); + + // Refresh index + $index->refresh(); + + $resultData = $result->getData(); + $ids = array($resultData['_id']); + + // Check to make sure the document is in the index + $resultSet = $type->search($userSearch); + $totalHits = $resultSet->getTotalHits(); + $this->assertEquals(1, $totalHits); + + // And verify that the variables we are doing to send to + // deleteIds are the type we are testing for + $this->assertInstanceOf('Elastica\Index', $index); + $this->assertInstanceOf('Elastica\Type', $type); + + // Using the existing $index and $type variables which + // are \Elastica\Index and \Elastica\Type objects respectively + $resp = $index->getClient()->deleteIds($ids, $index, $type); + + // Refresh the index to clear out deleted ID information + $index->refresh(); + + // Research the index to verify that the items have been deleted + $resultSet = $type->search($userSearch); + $totalHits = $resultSet->getTotalHits(); + $this->assertEquals(0, $totalHits); + } + + /** + * @group functional + */ + public function testOneInvalidConnection() + { + $client = $this->_getClient(); + + // First connection work, second should not work + $connection1 = new Connection(array('port' => '9100', 'timeout' => 2, 'host' => $this->_getHost())); + $connection2 = new Connection(array('port' => '9200', 'timeout' => 2, 'host' => $this->_getHost())); + + $client->setConnections(array($connection1, $connection2)); + + $client->request('_status', Request::GET); + + $connections = $client->getConnections(); + + // two connections are setup + $this->assertEquals(2, count($connections)); + + // One connection has to be disabled + $this->assertTrue($connections[0]->isEnabled() == false || $connections[1]->isEnabled() == false); + } + + /** + * @group functional + */ + public function testTwoInvalidConnection() + { + $client = $this->_getClient(); + + // First connection work, second should not work + $connection1 = new Connection(array('port' => '9101', 'timeout' => 2)); + $connection2 = new Connection(array('port' => '9102', 'timeout' => 2)); + + $client->setConnections(array($connection1, $connection2)); + + try { + $client->request('_status', Request::GET); + $this->fail('Should throw exception as no connection valid'); + } catch (HttpException $e) { + } + + $connections = $client->getConnections(); + + // two connections are setup + $this->assertEquals(2, count($connections)); + + // One connection has to be disabled + $this->assertTrue($connections[0]->isEnabled() == false || $connections[1]->isEnabled() == false); + } + + /** + * Tests if the callback works in case a connection is down. + * + * @group functional + */ + public function testCallback() + { + $count = 0; + $object = $this; + + // Callback function which verifies that disabled connection objects are returned + $callback = function ($connection, $exception, $client) use (&$object, &$count) { + $object->assertInstanceOf('Elastica\Connection', $connection); + $object->assertInstanceOf('Elastica\Exception\ConnectionException', $exception); + $object->assertInstanceOf('Elastica\Client', $client); + $object->assertFalse($connection->isEnabled()); + $count++; + }; + + $client = $this->_getClient(array(), $callback); + + // First connection work, second should not work + $connection1 = new Connection(array('port' => '9101', 'timeout' => 2)); + $connection2 = new Connection(array('port' => '9102', 'timeout' => 2)); + + $client->setConnections(array($connection1, $connection2)); + + $this->assertEquals(0, $count); + + try { + $client->request('_status', Request::GET); + $this->fail('Should throw exception as no connection valid'); + } catch (HttpException $e) { + $this->assertTrue(true); + } + + // Two disabled connections (from closure call) + $this->assertEquals(2, $count); + } + + /** + * @group functional + */ + public function testUrlConstructor() + { + $url = 'http://'.$this->_getHost().':9200/'; + + // Url should overwrite invalid host + $client = $this->_getClient(array('url' => $url, 'port' => '9101', 'timeout' => 2)); + + $response = $client->request('_status'); + $this->assertInstanceOf('Elastica\Response', $response); + } + + /** + * @group functional + */ + public function testUpdateDocumentByDocument() + { + $index = $this->_createIndex(); + $type = $index->getType('test'); + $client = $index->getClient(); + + $newDocument = new Document(1, array('field1' => 'value1', 'field2' => 'value2')); + $type->addDocument($newDocument); + + $updateDocument = new Document(1, array('field2' => 'value2changed', 'field3' => 'value3added')); + $client->updateDocument(1, $updateDocument, $index->getName(), $type->getName()); + + $document = $type->getDocument(1); + + $this->assertInstanceOf('Elastica\Document', $document); + $data = $document->getData(); + $this->assertArrayHasKey('field1', $data); + $this->assertEquals('value1', $data['field1']); + $this->assertArrayHasKey('field2', $data); + $this->assertEquals('value2changed', $data['field2']); + $this->assertArrayHasKey('field3', $data); + $this->assertEquals('value3added', $data['field3']); + } + + /** + * @group functional + */ + public function testUpdateDocumentByScript() + { + $index = $this->_createIndex(); + $type = $index->getType('test'); + $client = $index->getClient(); + + $newDocument = new Document(1, array('field1' => 'value1', 'field2' => 10, 'field3' => 'should be removed', 'field4' => 'should be changed')); + $type->addDocument($newDocument); + + $script = new Script('ctx._source.field2 += 5; ctx._source.remove("field3"); ctx._source.field4 = "changed"'); + $client->updateDocument(1, $script, $index->getName(), $type->getName()); + + $document = $type->getDocument(1); + + $this->assertInstanceOf('Elastica\Document', $document); + $data = $document->getData(); + $this->assertArrayHasKey('field1', $data); + $this->assertEquals('value1', $data['field1']); + $this->assertArrayHasKey('field2', $data); + $this->assertEquals(15, $data['field2']); + $this->assertArrayHasKey('field2', $data); + $this->assertEquals('changed', $data['field4']); + $this->assertArrayNotHasKey('field3', $data); + } + + /** + * @group functional + */ + public function testUpdateDocumentByScriptWithUpsert() + { + $index = $this->_createIndex(); + $type = $index->getType('test'); + $client = $index->getClient(); + + $script = new Script('ctx._source.field2 += count; ctx._source.remove("field3"); ctx._source.field4 = "changed"'); + $script->setParam('count', 5); + $script->setUpsert(array('field1' => 'value1', 'field2' => 10, 'field3' => 'should be removed', 'field4' => 'value4')); + + // should use document fields because document does not exist, script is avoided + $client->updateDocument(1, $script, $index->getName(), $type->getName(), array('fields' => '_source')); + + $document = $type->getDocument(1); + + $this->assertInstanceOf('Elastica\Document', $document); + $data = $document->getData(); + $this->assertArrayHasKey('field1', $data); + $this->assertEquals('value1', $data['field1']); + $this->assertArrayHasKey('field2', $data); + $this->assertEquals(10, $data['field2']); + $this->assertArrayHasKey('field3', $data); + $this->assertEquals('should be removed', $data['field3']); + $this->assertArrayHasKey('field4', $data); + $this->assertEquals('value4', $data['field4']); + + // should use script because document exists, document values are ignored + $client->updateDocument(1, $script, $index->getName(), $type->getName(), array('fields' => '_source')); + + $document = $type->getDocument(1); + + $this->assertInstanceOf('Elastica\Document', $document); + $data = $document->getData(); + $this->assertArrayHasKey('field1', $data); + $this->assertEquals('value1', $data['field1']); + $this->assertArrayHasKey('field2', $data); + $this->assertEquals(15, $data['field2']); + $this->assertArrayHasKey('field4', $data); + $this->assertEquals('changed', $data['field4']); + $this->assertArrayNotHasKey('field3', $data); + } + + /** + * @group functional + */ + public function testUpdateDocumentByRawData() + { + $index = $this->_createIndex(); + $type = $index->getType('test'); + $client = $index->getClient(); + + $newDocument = new Document(1, array('field1' => 'value1')); + $type->addDocument($newDocument); + + $rawData = array( + 'doc' => array( + 'field2' => 'value2', + ), + ); + + $response = $client->updateDocument(1, $rawData, $index->getName(), $type->getName(), array('retry_on_conflict' => 1)); + $this->assertTrue($response->isOk()); + + $document = $type->getDocument(1); + + $this->assertInstanceOf('Elastica\Document', $document); + $data = $document->getData(); + $this->assertArrayHasKey('field1', $data); + $this->assertEquals('value1', $data['field1']); + $this->assertArrayHasKey('field2', $data); + $this->assertEquals('value2', $data['field2']); + } + + /** + * @group functional + */ + public function testUpdateDocumentByDocumentWithUpsert() + { + $index = $this->_createIndex(); + $type = $index->getType('test'); + $client = $index->getClient(); + + $newDocument = new Document(1, array('field1' => 'value1updated', 'field2' => 'value2updated')); + $upsert = new Document(1, array('field1' => 'value1', 'field2' => 'value2')); + $newDocument->setUpsert($upsert); + $client->updateDocument(1, $newDocument, $index->getName(), $type->getName(), array('fields' => '_source')); + + $document = $type->getDocument(1); + $this->assertInstanceOf('Elastica\Document', $document); + $data = $document->getData(); + $this->assertArrayHasKey('field1', $data); + $this->assertEquals('value1', $data['field1']); + $this->assertArrayHasKey('field2', $data); + $this->assertEquals('value2', $data['field2']); + + // should use update document because document exists, upsert document values are ignored + $client->updateDocument(1, $newDocument, $index->getName(), $type->getName(), array('fields' => '_source')); + + $document = $type->getDocument(1); + $this->assertInstanceOf('Elastica\Document', $document); + $data = $document->getData(); + $this->assertArrayHasKey('field1', $data); + $this->assertEquals('value1updated', $data['field1']); + $this->assertArrayHasKey('field2', $data); + $this->assertEquals('value2updated', $data['field2']); + } + + /** + * @group functional + */ + public function testDocAsUpsert() + { + $index = $this->_createIndex(); + $type = $index->getType('test'); + $client = $index->getClient(); + + //Confirm document one does not exist + try { + $document = $type->getDocument(1); + $this->fail('Exception was not thrown. Maybe the document exists?'); + } catch (\Exception $e) { + //Ignore the exception because we expect the document to not exist. + } + + $newDocument = new Document(1, array('field1' => 'value1', 'field2' => 'value2')); + $newDocument->setDocAsUpsert(true); + $client->updateDocument(1, $newDocument, $index->getName(), $type->getName(), array('fields' => '_source')); + + $document = $type->getDocument(1); + $this->assertInstanceOf('Elastica\Document', $document); + $data = $document->getData(); + $this->assertArrayHasKey('field1', $data); + $this->assertEquals('value1', $data['field1']); + $this->assertArrayHasKey('field2', $data); + $this->assertEquals('value2', $data['field2']); + } + + /** + * @group functional + */ + public function testUpdateWithInvalidType() + { + $index = $this->_createIndex(); + $type = $index->getType('test'); + $client = $index->getClient(); + + //Try to update using a stdClass object + $badDocument = new \stdClass(); + + try { + $client->updateDocument(1, $badDocument, $index->getName(), $type->getName()); + $this->fail('Tried to update using an object that is not a Document or a Script but no exception was thrown'); + } catch (\Exception $e) { + //Good. An exception was thrown. + } + } + + /** + * @group functional + */ + public function testDeleteDocuments() + { + $index = $this->_createIndex(); + $type = $index->getType('test'); + $client = $index->getClient(); + + $docs = array( + new Document(1, array('field' => 'value1'), $type, $index), + new Document(2, array('field' => 'value2'), $type, $index), + new Document(3, array('field' => 'value3'), $type, $index), + ); + + $response = $client->addDocuments($docs); + + $this->assertInstanceOf('Elastica\Bulk\ResponseSet', $response); + $this->assertEquals(3, count($response)); + $this->assertTrue($response->isOk()); + $this->assertFalse($response->hasError()); + $this->assertEquals('', $response->getError()); + + $index->refresh(); + + $this->assertEquals(3, $type->count()); + + $deleteDocs = array( + $docs[0], + $docs[2], + ); + + $response = $client->deleteDocuments($deleteDocs); + + $this->assertInstanceOf('Elastica\Bulk\ResponseSet', $response); + $this->assertEquals(2, count($response)); + $this->assertTrue($response->isOk()); + $this->assertFalse($response->hasError()); + $this->assertEquals('', $response->getError()); + + $index->refresh(); + + $this->assertEquals(1, $type->count()); + } + + /** + * @group functional + */ + public function testLastRequestResponse() + { + $client = $this->_getClient(); + $response = $client->request('_status'); + + $this->assertInstanceOf('Elastica\Response', $response); + + $lastRequest = $client->getLastRequest(); + + $this->assertInstanceOf('Elastica\Request', $lastRequest); + $this->assertEquals('_status', $lastRequest->getPath()); + + $lastResponse = $client->getLastResponse(); + $this->assertInstanceOf('Elastica\Response', $lastResponse); + $this->assertSame($response, $lastResponse); + } + + /** + * @group functional + */ + public function testUpdateDocumentPopulateFields() + { + $index = $this->_createIndex(); + $type = $index->getType('test'); + $client = $index->getClient(); + + $newDocument = new Document(1, array('field1' => 'value1', 'field2' => 10, 'field3' => 'should be removed', 'field4' => 'value4')); + $newDocument->setAutoPopulate(); + $type->addDocument($newDocument); + + $script = new Script('ctx._source.field2 += count; ctx._source.remove("field3"); ctx._source.field4 = "changed"'); + $script->setParam('count', 5); + $script->setUpsert($newDocument); + + $client->updateDocument( + 1, + $script, + $index->getName(), + $type->getName(), + array('fields' => '_source') + ); + + $data = $type->getDocument(1)->getData(); + $this->assertArrayHasKey('field1', $data); + $this->assertEquals('value1', $data['field1']); + $this->assertArrayHasKey('field2', $data); + $this->assertEquals(15, $data['field2']); + $this->assertArrayHasKey('field4', $data); + $this->assertEquals('changed', $data['field4']); + $this->assertArrayNotHasKey('field3', $data); + + $script = new Script('ctx._source.field2 += count; ctx._source.remove("field4"); ctx._source.field1 = field1;'); + $script->setParam('count', 5); + $script->setParam('field1', 'updated'); + $script->setUpsert($newDocument); + + $client->updateDocument( + 1, + $script, + $index->getName(), + $type->getName(), + array('fields' => 'field2,field4') + ); + + $document = $type->getDocument(1); + + $data = $document->getData(); + + $this->assertArrayHasKey('field1', $data); + $this->assertEquals('updated', $data['field1']); + $this->assertArrayHasKey('field2', $data); + $this->assertEquals(20, $data['field2']); + $this->assertArrayNotHasKey('field3', $data); + $this->assertArrayNotHasKey('field4', $data); + } + + /** + * @group functional + */ + public function testAddDocumentsWithoutIds() + { + $docs = array(); + for ($i = 0; $i < 10; $i++) { + $docs[] = new Document(null, array('pos' => $i)); + } + + foreach ($docs as $doc) { + $this->assertFalse($doc->hasId()); + } + + $index = $this->_createIndex(); + + $client = $index->getClient(); + $client->setConfigValue('document', array('autoPopulate' => true)); + + $type = $index->getType('pos'); + $type->addDocuments($docs); + + foreach ($docs as $doc) { + $this->assertTrue($doc->hasId()); + $this->assertTrue($doc->hasVersion()); + $this->assertEquals(1, $doc->getVersion()); + } + } + + /** + * @group unit + */ + public function testConfigValue() + { + $config = array( + 'level1' => array( + 'level2' => array( + 'level3' => 'value3', + ), + 'level21' => 'value21', + ), + 'level11' => 'value11', + ); + $client = $this->_getClient($config); + + $this->assertNull($client->getConfigValue('level12')); + $this->assertFalse($client->getConfigValue('level12', false)); + $this->assertEquals(10, $client->getConfigValue('level12', 10)); + + $this->assertEquals('value11', $client->getConfigValue('level11')); + $this->assertNotNull($client->getConfigValue('level11')); + $this->assertNotEquals(false, $client->getConfigValue('level11', false)); + $this->assertNotEquals(10, $client->getConfigValue('level11', 10)); + + $this->assertEquals('value3', $client->getConfigValue(array('level1', 'level2', 'level3'))); + $this->assertInternalType('array', $client->getConfigValue(array('level1', 'level2'))); + } + + /** + * @group functional + */ + public function testArrayQuery() + { + $client = $this->_getClient(); + + $index = $client->getIndex('test'); + $index->create(array(), true); + $type = $index->getType('test'); + $type->addDocument(new Document(1, array('username' => 'ruflin'))); + $index->refresh(); + + $query = array( + 'query' => array( + 'query_string' => array( + 'query' => 'ruflin', + ), + ), + ); + + $path = $index->getName().'/'.$type->getName().'/_search'; + + $response = $client->request($path, Request::GET, $query); + $responseArray = $response->getData(); + + $this->assertEquals(1, $responseArray['hits']['total']); + } + + /** + * @group functional + */ + public function testJSONQuery() + { + $client = $this->_getClient(); + + $index = $client->getIndex('test'); + $index->create(array(), true); + $type = $index->getType('test'); + $type->addDocument(new Document(1, array('username' => 'ruflin'))); + $index->refresh(); + + $query = '{"query":{"query_string":{"query":"ruflin"}}}'; + + $path = $index->getName().'/'.$type->getName().'/_search'; + + $response = $client->request($path, Request::GET, $query); + $responseArray = $response->getData(); + + $this->assertEquals(1, $responseArray['hits']['total']); + } + + /** + * @group unit + */ + public function testAddHeader() + { + $client = $this->_getClient(); + + // add one header + $client->addHeader('foo', 'bar'); + $this->assertEquals(array('foo' => 'bar'), $client->getConfigValue('headers')); + + // check class + $this->assertInstanceOf('Elastica\Client', $client->addHeader('foo', 'bar')); + + // check invalid parameters + try { + $client->addHeader(new \stdClass(), 'foo'); + $this->fail('Header name is not a string but exception not thrown'); + } catch (InvalidException $ex) { + } + + try { + $client->addHeader('foo', new \stdClass()); + $this->fail('Header value is not a string but exception not thrown'); + } catch (InvalidException $ex) { + } + } + + /** + * @group unit + */ + public function testRemoveHeader() + { + $client = $this->_getClient(); + + // set headers + $headers = array( + 'first' => 'first value', + 'second' => 'second value', + ); + foreach ($headers as $key => $value) { + $client->addHeader($key, $value); + } + $this->assertEquals($headers, $client->getConfigValue('headers')); + + // remove one + $client->removeHeader('first'); + unset($headers['first']); + $this->assertEquals($headers, $client->getConfigValue('headers')); + + // check class + $this->assertInstanceOf('Elastica\Client', $client->removeHeader('second')); + + // check invalid parameter + try { + $client->removeHeader(new \stdClass()); + $this->fail('Header name is not a string but exception not thrown'); + } catch (InvalidException $ex) { + } + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Cluster/Health/IndexTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Cluster/Health/IndexTest.php new file mode 100644 index 00000000..00a121fc --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Cluster/Health/IndexTest.php @@ -0,0 +1,144 @@ + 'yellow', + 'number_of_shards' => 1, + 'number_of_replicas' => 2, + 'active_primary_shards' => 3, + 'active_shards' => 4, + 'relocating_shards' => 5, + 'initializing_shards' => 6, + 'unassigned_shards' => 7, + 'shards' => array( + '0' => array( + 'status' => 'yellow', + 'primary_active' => false, + 'active_shards' => 0, + 'relocating_shards' => 1, + 'initializing_shards' => 0, + 'unassigned_shards' => 1, + ), + '1' => array( + 'status' => 'yellow', + 'primary_active' => true, + 'active_shards' => 1, + 'relocating_shards' => 0, + 'initializing_shards' => 0, + 'unassigned_shards' => 1, + ), + '2' => array( + 'status' => 'green', + 'primary_active' => true, + 'active_shards' => 1, + 'relocating_shards' => 0, + 'initializing_shards' => 0, + 'unassigned_shards' => 0, + ), + ), + ); + + $this->_index = new HealthIndex('test', $data); + } + + /** + * @group unit + */ + public function testGetName() + { + $this->assertEquals('test', $this->_index->getName()); + } + + /** + * @group unit + */ + public function testGetStatus() + { + $this->assertEquals('yellow', $this->_index->getStatus()); + } + + /** + * @group unit + */ + public function testGetNumberOfShards() + { + $this->assertEquals(1, $this->_index->getNumberOfShards()); + } + + /** + * @group unit + */ + public function testGetNumberOfReplicas() + { + $this->assertEquals(2, $this->_index->getNumberOfReplicas()); + } + + /** + * @group unit + */ + public function testGetActivePrimaryShards() + { + $this->assertEquals(3, $this->_index->getActivePrimaryShards()); + } + + /** + * @group unit + */ + public function testGetActiveShards() + { + $this->assertEquals(4, $this->_index->getActiveShards()); + } + + /** + * @group unit + */ + public function testGetRelocatingShards() + { + $this->assertEquals(5, $this->_index->getRelocatingShards()); + } + + /** + * @group unit + */ + public function testGetInitializingShards() + { + $this->assertEquals(6, $this->_index->getInitializingShards()); + } + + /** + * @group unit + */ + public function testGetUnassignedShards() + { + $this->assertEquals(7, $this->_index->getUnassignedShards()); + } + + /** + * @group unit + */ + public function testGetShards() + { + $shards = $this->_index->getShards(); + + $this->assertInternalType('array', $shards); + $this->assertEquals(3, count($shards)); + + foreach ($shards as $shard) { + $this->assertInstanceOf('Elastica\Cluster\Health\Shard', $shard); + } + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Cluster/Health/ShardTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Cluster/Health/ShardTest.php new file mode 100644 index 00000000..5a436623 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Cluster/Health/ShardTest.php @@ -0,0 +1,85 @@ + 'red', + 'primary_active' => true, + 'active_shards' => 1, + 'relocating_shards' => 0, + 'initializing_shards' => 0, + 'unassigned_shards' => 1, + ); + + $this->_shard = new HealthShard(2, $shardData); + } + + /** + * @group unit + */ + public function testGetShardNumber() + { + $this->assertEquals(2, $this->_shard->getShardNumber()); + } + + /** + * @group unit + */ + public function testGetStatus() + { + $this->assertEquals('red', $this->_shard->getStatus()); + } + + /** + * @group unit + */ + public function testisPrimaryActive() + { + $this->assertTrue($this->_shard->isPrimaryActive()); + } + + /** + * @group unit + */ + public function testIsActive() + { + $this->assertTrue($this->_shard->isActive()); + } + + /** + * @group unit + */ + public function testIsRelocating() + { + $this->assertFalse($this->_shard->isRelocating()); + } + + /** + * @group unit + */ + public function testIsInitialized() + { + $this->assertFalse($this->_shard->isInitialized()); + } + + /** + * @group unit + */ + public function testIsUnassigned() + { + $this->assertTrue($this->_shard->isUnassigned()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Cluster/HealthTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Cluster/HealthTest.php new file mode 100644 index 00000000..bb3e82ec --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Cluster/HealthTest.php @@ -0,0 +1,147 @@ + 'test_cluster', + 'status' => 'green', + 'timed_out' => false, + 'number_of_nodes' => 10, + 'number_of_data_nodes' => 8, + 'active_primary_shards' => 3, + 'active_shards' => 4, + 'relocating_shards' => 2, + 'initializing_shards' => 7, + 'unassigned_shards' => 5, + 'indices' => array( + 'index_one' => array( + ), + 'index_two' => array( + ), + ), + ); + + $health = $this + ->getMockBuilder('Elastica\Cluster\Health') + ->setConstructorArgs(array($this->_getClient())) + ->setMethods(array('_retrieveHealthData')) + ->getMock(); + + $health + ->expects($this->any()) + ->method('_retrieveHealthData') + ->will($this->returnValue($data)); + + // need to explicitly refresh because the mocking won't refresh the data in the constructor + $health->refresh(); + + $this->_health = $health; + } + + /** + * @group unit + */ + public function testGetClusterName() + { + $this->assertEquals('test_cluster', $this->_health->getClusterName()); + } + + /** + * @group unit + */ + public function testGetStatus() + { + $this->assertEquals('green', $this->_health->getStatus()); + } + + /** + * @group unit + */ + public function testGetTimedOut() + { + $this->assertFalse($this->_health->getTimedOut()); + } + + /** + * @group unit + */ + public function testGetNumberOfNodes() + { + $this->assertEquals(10, $this->_health->getNumberOfNodes()); + } + + /** + * @group unit + */ + public function testGetNumberOfDataNodes() + { + $this->assertEquals(8, $this->_health->getNumberOfDataNodes()); + } + + /** + * @group unit + */ + public function testGetActivePrimaryShards() + { + $this->assertEquals(3, $this->_health->getActivePrimaryShards()); + } + + /** + * @group unit + */ + public function testGetActiveShards() + { + $this->assertEquals(4, $this->_health->getActiveShards()); + } + + /** + * @group unit + */ + public function testGetRelocatingShards() + { + $this->assertEquals(2, $this->_health->getRelocatingShards()); + } + + /** + * @group unit + */ + public function testGetInitializingShards() + { + $this->assertEquals(7, $this->_health->getInitializingShards()); + } + + /** + * @group unit + */ + public function testGetUnassignedShards() + { + $this->assertEquals(5, $this->_health->getUnassignedShards()); + } + + /** + * @group unit + */ + public function testGetIndices() + { + $indices = $this->_health->getIndices(); + + $this->assertInternalType('array', $indices); + $this->assertEquals(2, count($indices)); + + foreach ($indices as $index) { + $this->assertInstanceOf('Elastica\Cluster\Health\Index', $index); + } + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Cluster/SettingsTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Cluster/SettingsTest.php new file mode 100644 index 00000000..2a52fc8f --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Cluster/SettingsTest.php @@ -0,0 +1,117 @@ +_createIndex(); + + if (count($index->getClient()->getCluster()->getNodes()) < 2) { + $this->markTestSkipped('At least two master nodes have to be running for this test'); + } + + $settings = new Settings($index->getClient()); + + $settings->setTransient('discovery.zen.minimum_master_nodes', 2); + $data = $settings->get(); + $this->assertEquals(2, $data['transient']['discovery']['zen']['minimum_master_nodes']); + + $settings->setTransient('discovery.zen.minimum_master_nodes', 1); + $data = $settings->get(); + $this->assertEquals(1, $data['transient']['discovery']['zen']['minimum_master_nodes']); + } + + /** + * @group functional + */ + public function testSetPersistent() + { + $index = $this->_createIndex(); + + if (count($index->getClient()->getCluster()->getNodes()) < 2) { + $this->markTestSkipped('At least two master nodes have to be running for this test'); + } + + $settings = new Settings($index->getClient()); + + $settings->setPersistent('discovery.zen.minimum_master_nodes', 2); + $data = $settings->get(); + $this->assertEquals(2, $data['persistent']['discovery']['zen']['minimum_master_nodes']); + + $settings->setPersistent('discovery.zen.minimum_master_nodes', 1); + $data = $settings->get(); + $this->assertEquals(1, $data['persistent']['discovery']['zen']['minimum_master_nodes']); + } + + /** + * @group functional + */ + public function testSetReadOnly() + { + // Create two indices to check that the complete cluster is read only + $settings = new Settings($this->_getClient()); + $settings->setReadOnly(false); + $index1 = $this->_createIndex(); + $index2 = $this->_createIndex(); + + $doc1 = new Document(null, array('hello' => 'world')); + $doc2 = new Document(null, array('hello' => 'world')); + $doc3 = new Document(null, array('hello' => 'world')); + $doc4 = new Document(null, array('hello' => 'world')); + $doc5 = new Document(null, array('hello' => 'world')); + $doc6 = new Document(null, array('hello' => 'world')); + + // Check that adding documents work + $index1->getType('test')->addDocument($doc1); + $index2->getType('test')->addDocument($doc2); + + $response = $settings->setReadOnly(true); + $this->assertFalse($response->hasError()); + $setting = $settings->getTransient('cluster.blocks.read_only'); + $this->assertEquals('true', $setting); + + // Make sure both index are read only + try { + $index1->getType('test')->addDocument($doc3); + $this->fail('should throw read only exception'); + } catch (ResponseException $e) { + $message = $e->getMessage(); + $this->assertContains('ClusterBlockException', $message); + $this->assertContains('cluster read-only', $message); + } + + try { + $index2->getType('test')->addDocument($doc4); + $this->fail('should throw read only exception'); + } catch (ResponseException $e) { + $message = $e->getMessage(); + $this->assertContains('ClusterBlockException', $message); + $this->assertContains('cluster read-only', $message); + } + + $response = $settings->setReadOnly(false); + $this->assertFalse($response->hasError()); + $setting = $settings->getTransient('cluster.blocks.read_only'); + $this->assertEquals('false', $setting); + + // Check that adding documents works again + $index1->getType('test')->addDocument($doc5); + $index2->getType('test')->addDocument($doc6); + + $index1->refresh(); + $index2->refresh(); + + // 2 docs should be in each index + $this->assertEquals(2, $index1->count()); + $this->assertEquals(2, $index2->count()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/ClusterTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/ClusterTest.php new file mode 100644 index 00000000..ebd89f01 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/ClusterTest.php @@ -0,0 +1,83 @@ +_getClient(); + + $cluster = new Cluster($client); + + foreach ($cluster->getNodeNames() as $name) { + $this->assertEquals('Elastica', $name); + } + } + + /** + * @group functional + */ + public function testGetNodes() + { + $client = $this->_getClient(); + $cluster = $client->getCluster(); + + $nodes = $cluster->getNodes(); + + foreach ($nodes as $node) { + $this->assertInstanceOf('Elastica\Node', $node); + } + + $this->assertGreaterThan(0, count($nodes)); + } + + /** + * @group functional + */ + public function testGetState() + { + $client = $this->_getClient(); + $cluster = $client->getCluster(); + $state = $cluster->getState(); + $this->assertInternalType('array', $state); + } + + /** + * @group functional + */ + public function testGetIndexNames() + { + $client = $this->_getClient(); + $cluster = $client->getCluster(); + + $index = $this->_createIndex(); + $index->delete(); + $cluster->refresh(); + + // Checks that index does not exist + $indexNames = $cluster->getIndexNames(); + $this->assertNotContains($index->getName(), $indexNames); + + $index = $this->_createIndex(); + $cluster->refresh(); + + // Now index should exist + $indexNames = $cluster->getIndexNames(); + $this->assertContains($index->getname(), $indexNames); + } + + /** + * @group functional + */ + public function testGetHealth() + { + $client = $this->_getClient(); + $this->assertInstanceOf('Elastica\Cluster\Health', $client->getCluster()->getHealth()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Connection/ConnectionPoolTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Connection/ConnectionPoolTest.php new file mode 100644 index 00000000..ee03587a --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Connection/ConnectionPoolTest.php @@ -0,0 +1,112 @@ +createPool(); + + $this->assertEquals($this->getConnections(), $pool->getConnections()); + } + + /** + * @group unit + */ + public function testSetConnections() + { + $pool = $this->createPool(); + + $connections = $this->getConnections(5); + + $pool->setConnections($connections); + + $this->assertEquals($connections, $pool->getConnections()); + + $this->assertInstanceOf('Elastica\Connection\ConnectionPool', $pool->setConnections($connections)); + } + + /** + * @group unit + */ + public function testAddConnection() + { + $pool = $this->createPool(); + $pool->setConnections(array()); + + $connections = $this->getConnections(5); + + foreach ($connections as $connection) { + $pool->addConnection($connection); + } + + $this->assertEquals($connections, $pool->getConnections()); + + $this->assertInstanceOf('Elastica\Connection\ConnectionPool', $pool->addConnection($connections[0])); + } + + /** + * @group unit + */ + public function testHasConnection() + { + $pool = $this->createPool(); + + $this->assertTrue($pool->hasConnection()); + } + + /** + * @group unit + */ + public function testFailHasConnections() + { + $pool = $this->createPool(); + + $pool->setConnections(array()); + + $this->assertFalse($pool->hasConnection()); + } + + /** + * @group unit + */ + public function testGetConnection() + { + $pool = $this->createPool(); + + $this->assertInstanceOf('Elastica\Connection', $pool->getConnection()); + } + + protected function getConnections($quantity = 1) + { + $params = array(); + $connections = array(); + + for ($i = 0; $i < $quantity; $i++) { + $connections[] = new Connection($params); + } + + return $connections; + } + + protected function createPool() + { + $connections = $this->getConnections(); + $strategy = StrategyFactory::create('Simple'); + + $pool = new ConnectionPool($connections, $strategy); + + return $pool; + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Connection/Strategy/CallbackStrategyTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Connection/Strategy/CallbackStrategyTest.php new file mode 100644 index 00000000..de8290ae --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Connection/Strategy/CallbackStrategyTest.php @@ -0,0 +1,97 @@ +getConnection(array()); + + $this->assertEquals(1, $count); + } + + /** + * @group unit + */ + public function testIsValid() + { + // closure is valid + $isValid = CallbackStrategy::isValid(function () {}); + $this->assertTrue($isValid); + + // object implementing __invoke + $isValid = CallbackStrategy::isValid(new CallbackStrategyTestHelper()); + $this->assertTrue($isValid); + + // static method as string + $isValid = CallbackStrategy::isValid('Elastica\Test\Connection\Strategy\CallbackStrategyTestHelper::getFirstConnectionStatic'); + $this->assertTrue($isValid); + + // static method as array + $isValid = CallbackStrategy::isValid(array('Elastica\Test\Connection\Strategy\CallbackStrategyTestHelper', 'getFirstConnectionStatic')); + $this->assertTrue($isValid); + + // object method + $isValid = CallbackStrategy::isValid(array(new CallbackStrategyTestHelper(), 'getFirstConnectionStatic')); + $this->assertTrue($isValid); + + // function name + $isValid = CallbackStrategy::isValid('array_pop'); + $this->assertTrue($isValid); + } + + /** + * @group unit + */ + public function testFailIsValid() + { + $isValid = CallbackStrategy::isValid(new \stdClass()); + $this->assertFalse($isValid); + + $isValid = CallbackStrategy::isValid('array_pop_pop_pop_pop_pop_pop'); + $this->assertFalse($isValid); + } + + /** + * @group functional + */ + public function testConnection() + { + $count = 0; + + $config = array('connectionStrategy' => function ($connections) use (&$count) { + ++$count; + + return current($connections); + }); + + $client = $this->_getClient($config); + $response = $client->request('/_aliases'); + + $this->assertEquals(1, $count); + + $this->assertTrue($response->isOk()); + + $strategy = $client->getConnectionStrategy(); + + $this->assertInstanceOf('Elastica\Connection\Strategy\CallbackStrategy', $strategy); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Connection/Strategy/CallbackStrategyTestHelper.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Connection/Strategy/CallbackStrategyTestHelper.php new file mode 100644 index 00000000..b15efe91 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Connection/Strategy/CallbackStrategyTestHelper.php @@ -0,0 +1,20 @@ + 'RoundRobin'); + $client = $this->_getClient($config); + $response = $client->request('/_aliases'); + /* @var $response Response */ + + $this->_checkResponse($response); + + $this->_checkStrategy($client); + } + + /** + * @group unit + */ + public function testOldStrategySetted() + { + $config = array('roundRobin' => true); + $client = $this->_getClient($config); + + $this->_checkStrategy($client); + } + + /** + * @group functional + * @expectedException \Elastica\Exception\ConnectionException + */ + public function testFailConnection() + { + $config = array('connectionStrategy' => 'RoundRobin', 'host' => '255.255.255.0', 'timeout' => $this->_timeout); + $client = $this->_getClient($config); + + $this->_checkStrategy($client); + + $client->request('/_aliases'); + } + + /** + * @group functional + */ + public function testWithOneFailConnection() + { + $connections = array( + new Connection(array('host' => '255.255.255.0', 'timeout' => $this->_timeout)), + new Connection(array('host' => $this->_getHost(), 'timeout' => $this->_timeout)), + ); + + $count = 0; + $callback = function ($connection, $exception, $client) use (&$count) { + ++$count; + }; + + $client = $this->_getClient(array('connectionStrategy' => 'RoundRobin'), $callback); + $client->setConnections($connections); + + $response = $client->request('/_aliases'); + /* @var $response Response */ + + $this->_checkResponse($response); + + $this->_checkStrategy($client); + + $this->assertLessThan(count($connections), $count); + } + + /** + * @group functional + */ + public function testWithNoValidConnection() + { + $connections = array( + new Connection(array('host' => '255.255.255.0', 'timeout' => $this->_timeout)), + new Connection(array('host' => '45.45.45.45', 'port' => '80', 'timeout' => $this->_timeout)), + new Connection(array('host' => '10.123.213.123', 'timeout' => $this->_timeout)), + ); + + $count = 0; + $client = $this->_getClient(array('roundRobin' => true), function () use (&$count) { + ++$count; + }); + + $client->setConnections($connections); + + try { + $client->request('/_aliases'); + $this->fail('Should throw exception as no connection valid'); + } catch (ConnectionException $e) { + $this->assertEquals(count($connections), $count); + $this->_checkStrategy($client); + } + } + + protected function _checkStrategy($client) + { + $strategy = $client->getConnectionStrategy(); + + $this->assertInstanceOf('Elastica\Connection\Strategy\RoundRobin', $strategy); + } + + protected function _checkResponse($response) + { + $this->assertTrue($response->isOk()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Connection/Strategy/SimpleTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Connection/Strategy/SimpleTest.php new file mode 100644 index 00000000..3b6ac89d --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Connection/Strategy/SimpleTest.php @@ -0,0 +1,113 @@ +_getClient(); + $response = $client->request('/_aliases'); + /* @var $response \Elastica\Response */ + + $this->_checkResponse($response); + + $this->_checkStrategy($client); + } + + /** + * @group functional + * @expectedException \Elastica\Exception\ConnectionException + */ + public function testFailConnection() + { + $config = array('host' => '255.255.255.0', 'timeout' => $this->_timeout); + $client = $this->_getClient($config); + + $this->_checkStrategy($client); + + $client->request('/_aliases'); + } + + /** + * @group functional + */ + public function testWithOneFailConnection() + { + $connections = array( + new Connection(array('host' => '255.255.255.0', 'timeout' => $this->_timeout)), + new Connection(array('host' => $this->_getHost(), 'timeout' => $this->_timeout)), + ); + + $count = 0; + $callback = function ($connection, $exception, $client) use (&$count) { + ++$count; + }; + + $client = $this->_getClient(array(), $callback); + $client->setConnections($connections); + + $response = $client->request('/_aliases'); + /* @var $response Response */ + + $this->_checkResponse($response); + + $this->_checkStrategy($client); + + $this->assertLessThan(count($connections), $count); + } + + /** + * @group functional + */ + public function testWithNoValidConnection() + { + $connections = array( + new Connection(array('host' => '255.255.255.0', 'timeout' => $this->_timeout)), + new Connection(array('host' => '45.45.45.45', 'port' => '80', 'timeout' => $this->_timeout)), + new Connection(array('host' => '10.123.213.123', 'timeout' => $this->_timeout)), + ); + + $count = 0; + $client = $this->_getClient(array(), function () use (&$count) { + ++$count; + }); + + $client->setConnections($connections); + + try { + $client->request('/_aliases'); + $this->fail('Should throw exception as no connection valid'); + } catch (ConnectionException $e) { + $this->assertEquals(count($connections), $count); + } + } + + protected function _checkStrategy($client) + { + $strategy = $client->getConnectionStrategy(); + + $this->assertInstanceOf('Elastica\Connection\Strategy\Simple', $strategy); + } + + protected function _checkResponse($response) + { + $this->assertTrue($response->isOk()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Connection/Strategy/StrategyFactoryTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Connection/Strategy/StrategyFactoryTest.php new file mode 100644 index 00000000..978f8fd5 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Connection/Strategy/StrategyFactoryTest.php @@ -0,0 +1,84 @@ +assertInstanceOf('Elastica\Connection\Strategy\CallbackStrategy', $strategy); + } + + /** + * @group unit + */ + public function testCreateByName() + { + $strategyName = 'Simple'; + + $strategy = StrategyFactory::create($strategyName); + + $this->assertInstanceOf('Elastica\Connection\Strategy\Simple', $strategy); + } + + /** + * @group unit + */ + public function testCreateByClass() + { + $strategy = new EmptyStrategy(); + + $this->assertEquals($strategy, StrategyFactory::create($strategy)); + } + + /** + * @group unit + */ + public function testCreateByClassName() + { + $strategyName = '\\Elastica\Test\Connection\Strategy\\EmptyStrategy'; + + $strategy = StrategyFactory::create($strategyName); + + $this->assertInstanceOf($strategyName, $strategy); + } + + /** + * @group unit + * @expectedException \InvalidArgumentException + */ + public function testFailCreate() + { + $strategy = new \stdClass(); + + StrategyFactory::create($strategy); + } + + /** + * @group unit + */ + public function testNoCollisionWithGlobalNamespace() + { + // create collision + if (!class_exists('Simple')) { + class_alias('Elastica\Util', 'Simple'); + } + $strategy = StrategyFactory::create('Simple'); + $this->assertInstanceOf('Elastica\Connection\Strategy\Simple', $strategy); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/ConnectionTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/ConnectionTest.php new file mode 100644 index 00000000..7600524b --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/ConnectionTest.php @@ -0,0 +1,121 @@ +assertEquals(Connection::DEFAULT_HOST, $connection->getHost()); + $this->assertEquals(Connection::DEFAULT_PORT, $connection->getPort()); + $this->assertEquals(Connection::DEFAULT_TRANSPORT, $connection->getTransport()); + $this->assertInstanceOf('Elastica\Transport\AbstractTransport', $connection->getTransportObject()); + $this->assertEquals(Connection::TIMEOUT, $connection->getTimeout()); + $this->assertEquals(Connection::CONNECT_TIMEOUT, $connection->getConnectTimeout()); + $this->assertEquals(array(), $connection->getConfig()); + $this->assertTrue($connection->isEnabled()); + } + + /** + * @group unit + */ + public function testEnabledDisable() + { + $connection = new Connection(); + $this->assertTrue($connection->isEnabled()); + $connection->setEnabled(false); + $this->assertFalse($connection->isEnabled()); + $connection->setEnabled(true); + $this->assertTrue($connection->isEnabled()); + } + + /** + * @group unit + * @expectedException \Elastica\Exception\ConnectionException + */ + public function testInvalidConnection() + { + $connection = new Connection(array('port' => 9999)); + + $request = new Request('_status', Request::GET); + $request->setConnection($connection); + + // Throws exception because no valid connection + $request->send(); + } + + /** + * @group unit + */ + public function testCreate() + { + $connection = Connection::create(); + $this->assertInstanceOf('Elastica\Connection', $connection); + + $connection = Connection::create(array()); + $this->assertInstanceOf('Elastica\Connection', $connection); + + $port = 9999; + $connection = Connection::create(array('port' => $port)); + $this->assertInstanceOf('Elastica\Connection', $connection); + $this->assertEquals($port, $connection->getPort()); + } + + /** + * @group unit + * @expectedException \Elastica\Exception\InvalidException + * @expectedException \Elastica\Exception\InvalidException + */ + public function testCreateInvalid() + { + Connection::create('test'); + } + + /** + * @group unit + */ + public function testGetConfig() + { + $url = 'test'; + $connection = new Connection(array('config' => array('url' => $url))); + $this->assertTrue($connection->hasConfig('url')); + $this->assertEquals($url, $connection->getConfig('url')); + } + + /** + * @group unit + */ + public function testGetConfigWithArrayUsedForTransport() + { + $connection = new Connection(array('transport' => array('type' => 'Http'))); + $this->assertInstanceOf('Elastica\Transport\Http', $connection->getTransportObject()); + } + + /** + * @group unit + * @expectedException Elastica\Exception\InvalidException + * @expectedExceptionMessage Invalid transport + */ + public function testGetInvalidConfigWithArrayUsedForTransport() + { + $connection = new Connection(array('transport' => array('type' => 'invalidtransport'))); + $connection->getTransportObject(); + } + + /** + * @group unit + * @expectedException \Elastica\Exception\InvalidException + */ + public function testGetConfigInvalidValue() + { + $connection = new Connection(); + $connection->getConfig('url'); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/DocumentTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/DocumentTest.php new file mode 100644 index 00000000..969c5b22 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/DocumentTest.php @@ -0,0 +1,349 @@ +markTestSkipped("File {$fileName} does not exist."); + } + $doc = new Document(); + $returnValue = $doc->addFile('key', $fileName); + $this->assertInstanceOf('Elastica\Document', $returnValue); + } + + /** + * @group unit + */ + public function testAddGeoPoint() + { + $doc = new Document(); + $returnValue = $doc->addGeoPoint('point', 38.89859, -77.035971); + $this->assertInstanceOf('Elastica\Document', $returnValue); + } + + /** + * @group unit + */ + public function testSetData() + { + $doc = new Document(); + $returnValue = $doc->setData(array('data')); + $this->assertInstanceOf('Elastica\Document', $returnValue); + } + + /** + * @group unit + */ + public function testToArray() + { + $id = 17; + $data = array('hello' => 'world'); + $type = 'testtype'; + $index = 'textindex'; + + $doc = new Document($id, $data, $type, $index); + + $result = array('_index' => $index, '_type' => $type, '_id' => $id, '_source' => $data); + $this->assertEquals($result, $doc->toArray()); + } + + /** + * @group unit + */ + public function testSetType() + { + $document = new Document(); + $document->setType('type'); + + $this->assertEquals('type', $document->getType()); + + $index = new Index($this->_getClient(), 'index'); + $type = $index->getType('type'); + + $document->setIndex('index2'); + $this->assertEquals('index2', $document->getIndex()); + + $document->setType($type); + + $this->assertEquals('index', $document->getIndex()); + $this->assertEquals('type', $document->getType()); + } + + /** + * @group unit + */ + public function testSetIndex() + { + $document = new Document(); + $document->setIndex('index2'); + $document->setType('type2'); + + $this->assertEquals('index2', $document->getIndex()); + $this->assertEquals('type2', $document->getType()); + + $index = new Index($this->_getClient(), 'index'); + + $document->setIndex($index); + + $this->assertEquals('index', $document->getIndex()); + $this->assertEquals('type2', $document->getType()); + } + + /** + * @group unit + */ + public function testHasId() + { + $document = new Document(); + $this->assertFalse($document->hasId()); + $document->setId(''); + $this->assertFalse($document->hasId()); + $document->setId(0); + $this->assertTrue($document->hasId()); + $document->setId('hello'); + $this->assertTrue($document->hasId()); + } + + /** + * @group unit + */ + public function testGetOptions() + { + $document = new Document(); + $document->setIndex('index'); + $document->setOpType('create'); + $document->setParent('2'); + $document->setId(1); + + $options = $document->getOptions(array('index', 'type', 'id', 'parent')); + + $this->assertInternalType('array', $options); + $this->assertEquals(3, count($options)); + $this->assertArrayHasKey('index', $options); + $this->assertArrayHasKey('id', $options); + $this->assertArrayHasKey('parent', $options); + $this->assertEquals('index', $options['index']); + $this->assertEquals(1, $options['id']); + $this->assertEquals('2', $options['parent']); + $this->assertArrayNotHasKey('type', $options); + $this->assertArrayNotHasKey('op_type', $options); + $this->assertArrayNotHasKey('_index', $options); + $this->assertArrayNotHasKey('_id', $options); + $this->assertArrayNotHasKey('_parent', $options); + + $options = $document->getOptions(array('parent', 'op_type', 'percolate'), true); + + $this->assertInternalType('array', $options); + $this->assertEquals(2, count($options)); + $this->assertArrayHasKey('_parent', $options); + $this->assertArrayHasKey('_op_type', $options); + $this->assertEquals('2', $options['_parent']); + $this->assertEquals('create', $options['_op_type']); + $this->assertArrayNotHasKey('percolate', $options); + $this->assertArrayNotHasKey('op_type', $options); + $this->assertArrayNotHasKey('parent', $options); + } + + /** + * @group unit + */ + public function testGetSetHasRemove() + { + $document = new Document(1, array('field1' => 'value1', 'field2' => 'value2', 'field3' => 'value3', 'field4' => null)); + + $this->assertEquals('value1', $document->get('field1')); + $this->assertEquals('value2', $document->get('field2')); + $this->assertEquals('value3', $document->get('field3')); + $this->assertNull($document->get('field4')); + try { + $document->get('field5'); + $this->fail('Undefined field get should throw exception'); + } catch (InvalidException $e) { + $this->assertTrue(true); + } + + $this->assertTrue($document->has('field1')); + $this->assertTrue($document->has('field2')); + $this->assertTrue($document->has('field3')); + $this->assertTrue($document->has('field4')); + $this->assertFalse($document->has('field5'), 'Field5 should not be isset, because it is not set'); + + $data = $document->getData(); + + $this->assertArrayHasKey('field1', $data); + $this->assertEquals('value1', $data['field1']); + $this->assertArrayHasKey('field2', $data); + $this->assertEquals('value2', $data['field2']); + $this->assertArrayHasKey('field3', $data); + $this->assertEquals('value3', $data['field3']); + $this->assertArrayHasKey('field4', $data); + $this->assertNull($data['field4']); + + $returnValue = $document->set('field1', 'changed1'); + $this->assertInstanceOf('Elastica\Document', $returnValue); + $returnValue = $document->remove('field3'); + $this->assertInstanceOf('Elastica\Document', $returnValue); + try { + $document->remove('field5'); + $this->fail('Undefined field unset should throw exception'); + } catch (InvalidException $e) { + $this->assertTrue(true); + } + + $this->assertEquals('changed1', $document->get('field1')); + $this->assertFalse($document->has('field3')); + + $newData = $document->getData(); + + $this->assertNotEquals($data, $newData); + } + + /** + * @group unit + */ + public function testDataPropertiesOverloading() + { + $document = new Document(1, array('field1' => 'value1', 'field2' => 'value2', 'field3' => 'value3', 'field4' => null)); + + $this->assertEquals('value1', $document->field1); + $this->assertEquals('value2', $document->field2); + $this->assertEquals('value3', $document->field3); + $this->assertNull($document->field4); + try { + $document->field5; + $this->fail('Undefined field get should throw exception'); + } catch (InvalidException $e) { + $this->assertTrue(true); + } + + $this->assertTrue(isset($document->field1)); + $this->assertTrue(isset($document->field2)); + $this->assertTrue(isset($document->field3)); + $this->assertFalse(isset($document->field4), 'Field4 should not be isset, because it is null'); + $this->assertFalse(isset($document->field5), 'Field5 should not be isset, because it is not set'); + + $data = $document->getData(); + + $this->assertArrayHasKey('field1', $data); + $this->assertEquals('value1', $data['field1']); + $this->assertArrayHasKey('field2', $data); + $this->assertEquals('value2', $data['field2']); + $this->assertArrayHasKey('field3', $data); + $this->assertEquals('value3', $data['field3']); + $this->assertArrayHasKey('field4', $data); + $this->assertNull($data['field4']); + + $document->field1 = 'changed1'; + unset($document->field3); + try { + unset($document->field5); + $this->fail('Undefined field unset should throw exception'); + } catch (InvalidException $e) { + $this->assertTrue(true); + } + + $this->assertEquals('changed1', $document->field1); + $this->assertFalse(isset($document->field3)); + + $newData = $document->getData(); + + $this->assertNotEquals($data, $newData); + } + + /** + * @group unit + */ + public function testSetTtl() + { + $document = new Document(); + + $this->assertFalse($document->hasTtl()); + $options = $document->getOptions(); + $this->assertArrayNotHasKey('ttl', $options); + + $document->setTtl('1d'); + + $newOptions = $document->getOptions(); + + $this->assertArrayHasKey('ttl', $newOptions); + $this->assertEquals('1d', $newOptions['ttl']); + $this->assertNotEquals($options, $newOptions); + + $this->assertTrue($document->hasTtl()); + $this->assertEquals('1d', $document->getTtl()); + } + + /** + * @group unit + */ + public function testSerializedData() + { + $data = '{"user":"rolf"}'; + $document = new Document(1, $data); + + $this->assertFalse($document->has('user')); + + try { + $document->get('user'); + $this->fail('User field should not be available'); + } catch (InvalidException $e) { + $this->assertTrue(true); + } + + try { + $document->remove('user'); + $this->fail('User field should not be available for removal'); + } catch (InvalidException $e) { + $this->assertTrue(true); + } + + try { + $document->set('name', 'shawn'); + $this->fail('Document should not allow to set new data'); + } catch (InvalidException $e) { + $this->assertTrue(true); + } + } + + /** + * @group unit + */ + public function testUpsert() + { + $document = new Document(); + + $upsert = new Document(); + $upsert->setData(array('someproperty' => 'somevalue')); + + $this->assertFalse($document->hasUpsert()); + + $document->setUpsert($upsert); + + $this->assertTrue($document->hasUpsert()); + $this->assertSame($upsert, $document->getUpsert()); + } + + /** + * @group unit + */ + public function testDocAsUpsert() + { + $document = new Document(); + + $this->assertFalse($document->getDocAsUpsert()); + $this->assertSame($document, $document->setDocAsUpsert(true)); + $this->assertTrue($document->getDocAsUpsert()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/ExampleTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/ExampleTest.php new file mode 100644 index 00000000..efdc04eb --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/ExampleTest.php @@ -0,0 +1,61 @@ +_getClient(); + $index = $client->getIndex('ruflin'); + $type = $index->getType('users'); + + $id = 2; + $data = array('firstname' => 'Nicolas', 'lastname' => 'Ruflin'); + $doc = new Document($id, $data); + + $type->addDocument($doc); + } + + /** + * @group functional + */ + public function testExample() + { + // Creates a new index 'xodoa' and a type 'user' inside this index + $client = $this->_getClient(); + $index = $client->getIndex('elastica_test'); + $index->create(array(), true); + + $type = $index->getType('user'); + + // Adds 1 document to the index + $doc1 = new Document(1, + array('username' => 'hans', 'test' => array('2', '3', '5')) + ); + $type->addDocument($doc1); + + // Adds a list of documents with _bulk upload to the index + $docs = array(); + $docs[] = new Document(2, + array('username' => 'john', 'test' => array('1', '3', '6')) + ); + $docs[] = new Document(3, + array('username' => 'rolf', 'test' => array('2', '3', '7')) + ); + $type->addDocuments($docs); + + // Refresh index + $index->refresh(); + + $resultSet = $type->search('rolf'); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Exception/AbstractExceptionTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Exception/AbstractExceptionTest.php new file mode 100644 index 00000000..ea84218a --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Exception/AbstractExceptionTest.php @@ -0,0 +1,31 @@ + Elastica\Exception\RuntimeExceptionTest + $name = preg_replace('/^Elastica\\\\Test/', 'Elastica', $reflection->getName()); + + // Elastica\Exception\RuntimeExceptionTest => Elastica\Exception\RuntimeException + $name = preg_replace('/Test$/', '', $name); + + return $name; + } + + /** + * @group unit + */ + public function testInheritance() + { + $className = $this->_getExceptionClass(); + $reflection = new \ReflectionClass($className); + $this->assertTrue($reflection->isSubclassOf('Exception')); + $this->assertTrue($reflection->implementsInterface('Elastica\Exception\ExceptionInterface')); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Exception/Bulk/Response/ActionExceptionTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Exception/Bulk/Response/ActionExceptionTest.php new file mode 100644 index 00000000..38a2f873 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Exception/Bulk/Response/ActionExceptionTest.php @@ -0,0 +1,8 @@ +assertEquals($message, $exception->getMessage()); + $this->assertEquals($code, $exception->getCode()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Exception/PartialShardFailureExceptionTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Exception/PartialShardFailureExceptionTest.php new file mode 100644 index 00000000..afcca1ec --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Exception/PartialShardFailureExceptionTest.php @@ -0,0 +1,54 @@ +_getClient(); + $index = $client->getIndex('elastica_partial_failure'); + $index->create(array( + 'index' => array( + 'number_of_shards' => 5, + 'number_of_replicas' => 0, + ), + ), true); + + $type = $index->getType('folks'); + + $type->addDocument(new Document('', array('name' => 'ruflin'))); + $type->addDocument(new Document('', array('name' => 'bobrik'))); + $type->addDocument(new Document('', array('name' => 'kimchy'))); + + $index->refresh(); + + $query = Query::create(array( + 'query' => array( + 'filtered' => array( + 'filter' => array( + 'script' => array( + 'script' => 'doc["undefined"] > 8', // compiles, but doesn't work + ), + ), + ), + ), + )); + + try { + $index->search($query); + + $this->fail('PartialShardFailureException should have been thrown'); + } catch (PartialShardFailureException $e) { + $resultSet = new ResultSet($e->getResponse(), $query); + $this->assertEquals(0, count($resultSet->getResults())); + } + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Exception/QueryBuilderExceptionTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Exception/QueryBuilderExceptionTest.php new file mode 100644 index 00000000..375731ba --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Exception/QueryBuilderExceptionTest.php @@ -0,0 +1,6 @@ +_createIndex('woo', true); + + try { + $this->_createIndex('woo', false); + $this->fail('Index created when it should fail'); + } catch (ResponseException $ex) { + $this->assertEquals('IndexAlreadyExistsException', $ex->getElasticsearchException()->getExceptionName()); + $this->assertEquals(400, $ex->getElasticsearchException()->getCode()); + } + } + + /** + * @group functional + */ + public function testBadType() + { + $index = $this->_createIndex(); + $type = $index->getType('test'); + + $type->setMapping(array( + 'num' => array( + 'type' => 'long', + ), + )); + + try { + $type->addDocument(new Document('', array( + 'num' => 'not number at all', + ))); + $this->fail('Indexing with wrong type should fail'); + } catch (ResponseException $ex) { + $this->assertEquals('MapperParsingException', $ex->getElasticsearchException()->getExceptionName()); + $this->assertEquals(400, $ex->getElasticsearchException()->getCode()); + } + } + + /** + * @group functional + */ + public function testWhatever() + { + $index = $this->_createIndex(); + $index->delete(); + + try { + $index->search(); + } catch (ResponseException $ex) { + $this->assertEquals('IndexMissingException', $ex->getElasticsearchException()->getExceptionName()); + $this->assertEquals(404, $ex->getElasticsearchException()->getCode()); + } + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Exception/RuntimeExceptionTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Exception/RuntimeExceptionTest.php new file mode 100644 index 00000000..9dd18951 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Exception/RuntimeExceptionTest.php @@ -0,0 +1,6 @@ +assertInstanceOf('Elastica\Facet\Histogram', $facet); + $this->assertInstanceOf('Elastica\Facet\AbstractFacet', $facet); + unset($facet); + } + + /** + * @group functional + */ + public function testQuery() + { + $client = $this->_getClient(); + $index = $client->getIndex('test'); + $index->create(array(), true); + $type = $index->getType('helloworld'); + + $mapping = new Mapping($type, array( + 'name' => array('type' => 'string', 'store' => 'no'), + 'dtmPosted' => array('type' => 'date', 'store' => 'no', 'format' => 'yyyy-MM-dd HH:mm:ss'), + )); + $type->setMapping($mapping); + + $doc = new Document(1, array('name' => 'nicolas ruflin', 'dtmPosted' => '2011-06-23 21:53:00')); + $type->addDocument($doc); + $doc = new Document(2, array('name' => 'raul martinez jr', 'dtmPosted' => '2011-06-23 09:53:00')); + $type->addDocument($doc); + $doc = new Document(3, array('name' => 'rachelle clemente', 'dtmPosted' => '2011-07-08 08:53:00')); + $type->addDocument($doc); + $doc = new Document(4, array('name' => 'elastica search', 'dtmPosted' => '2011-07-08 01:53:00')); + $type->addDocument($doc); + + $facet = new DateHistogram('dateHist1'); + $facet->setInterval('day'); + $facet->setField('dtmPosted'); + + $query = new Query(); + $query->addFacet($facet); + $query->setQuery(new MatchAll()); + $index->refresh(); + + $response = $type->search($query); + $facets = $response->getFacets(); + + $this->assertEquals(4, $response->getTotalHits()); + $this->assertEquals(2, count($facets['dateHist1']['entries'])); + } + + /** + * @group functional + */ + public function testFactor() + { + $client = $this->_getClient(); + $index = $client->getIndex('test'); + $index->create(array(), true); + $type = $index->getType('helloworld'); + + $mapping = new Mapping($type, array( + 'name' => array('type' => 'string', 'store' => 'no'), + 'dtmPosted' => array('type' => 'long', 'store' => 'no'), + )); + $type->setMapping($mapping); + + $doc = new Document(1, array('name' => 'nicolas ruflin', 'dtmPosted' => 1308865980)); + $type->addDocument($doc); + $doc = new Document(2, array('name' => 'raul martinez jr', 'dtmPosted' => 1308822780)); + $type->addDocument($doc); + $doc = new Document(3, array('name' => 'rachelle clemente', 'dtmPosted' => 1310115180)); + $type->addDocument($doc); + $doc = new Document(4, array('name' => 'elastica search', 'dtmPosted' => 1310089980)); + $type->addDocument($doc); + + $facet = new DateHistogram('dateHist1'); + $facet->setInterval('day'); + $facet->setField('dtmPosted'); + $facet->setFactor(1000); + + $query = new Query(); + $query->addFacet($facet); + $query->setQuery(new MatchAll()); + $index->refresh(); + + $response = $type->search($query); + $facets = $response->getFacets(); + + $this->assertEquals(4, $response->getTotalHits()); + $this->assertEquals(2, count($facets['dateHist1']['entries'])); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Facet/FilterTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Facet/FilterTest.php new file mode 100644 index 00000000..622923fe --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Facet/FilterTest.php @@ -0,0 +1,42 @@ +_getClient(); + $index = $client->getIndex('test'); + $index->create(array(), true); + $type = $index->getType('helloworld'); + + $type->addDocument(new Document(1, array('color' => 'red'))); + $type->addDocument(new Document(2, array('color' => 'green'))); + $type->addDocument(new Document(3, array('color' => 'blue'))); + + $index->refresh(); + + $filter = new Term(array('color' => 'red')); + + $facet = new Filter('test'); + $facet->setFilter($filter); + + $query = new Query(); + $query->addFacet($facet); + + $resultSet = $type->search($query); + + $facets = $resultSet->getFacets(); + + $this->assertEquals(1, $facets['test']['count']); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Facet/GeoClusterTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Facet/GeoClusterTest.php new file mode 100644 index 00000000..f771ac32 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Facet/GeoClusterTest.php @@ -0,0 +1,57 @@ +_getClient(); + $nodes = $client->getCluster()->getNodes(); + if (!$nodes[0]->getInfo()->hasPlugin('geocluster-facet')) { + $this->markTestSkipped('geocluster-facet plugin not installed'); + } + + $index = $this->_createIndex(); + $type = $index->getType('testQuery'); + $geoField = 'location'; + + $type->setMapping(new Mapping($type, array( + $geoField => array('type' => 'geo_point', 'lat_lon' => true), + ))); + + $doc = new Document(1, array('name' => 'item1', 'location' => array(20, 20))); + $type->addDocument($doc); + + $doc = new Document(2, array('name' => 'item2', 'location' => array(20, 20))); + $type->addDocument($doc); + + $doc = new Document(3, array('name' => 'item3', 'location' => array(20, 20))); + $type->addDocument($doc); + + $index->refresh(); + + $facet = new GeoCluster('clusters'); + $facet + ->setField($geoField) + ->setFactor(1) + ->setShowIds(false); + $query = new Query(); + $query->setFacets(array($facet)); + + $response = $type->search($query); + $facets = $response->getFacets(); + + $this->assertEquals(1, count($facets['clusters']['clusters'])); + + $index->delete(); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Facet/QueryTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Facet/QueryTest.php new file mode 100644 index 00000000..8e0231aa --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Facet/QueryTest.php @@ -0,0 +1,42 @@ +_getClient(); + $index = $client->getIndex('test'); + $index->create(array(), true); + $type = $index->getType('helloworld'); + + $type->addDocument(new Document(1, array('color' => 'red'))); + $type->addDocument(new Document(2, array('color' => 'green'))); + $type->addDocument(new Document(3, array('color' => 'blue'))); + + $index->refresh(); + + $termQuery = new Term(array('color' => 'red')); + + $facet = new FacetQuery('test'); + $facet->setQuery($termQuery); + + $query = new Query(); + $query->addFacet($facet); + + $resultSet = $type->search($query); + + $facets = $resultSet->getFacets(); + + $this->assertEquals(1, $facets['test']['count']); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Facet/StatisticalTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Facet/StatisticalTest.php new file mode 100644 index 00000000..dc374289 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Facet/StatisticalTest.php @@ -0,0 +1,82 @@ +_getClient(); + $index = $client->getIndex('test'); + $index->create(array(), true); + $type = $index->getType('helloworld'); + + $doc = new Document(1, array('price' => 10)); + $type->addDocument($doc); + $doc = new Document(2, array('price' => 35)); + $type->addDocument($doc); + $doc = new Document(2, array('price' => 45)); + $type->addDocument($doc); + + $facet = new Statistical('stats'); + $facet->setField('price'); + + $query = new Query(); + $query->addFacet($facet); + $query->setQuery(new MatchAll()); + + $index->refresh(); + + $response = $type->search($query); + $facets = $response->getFacets(); + + $this->assertEquals(55, $facets['stats']['total']); + $this->assertEquals(10, $facets['stats']['min']); + $this->assertEquals(45, $facets['stats']['max']); + } + + /** + * @group functional + */ + public function testStatisticalWithSetFields() + { + $client = $this->_getClient(); + $index = $client->getIndex('test'); + $index->create(array(), true); + $type = $index->getType('helloworld'); + + $doc = new Document(1, array('price' => 10, 'price2' => 20)); + $type->addDocument($doc); + $doc = new Document(2, array('price' => 35, 'price2' => 70)); + $type->addDocument($doc); + $doc = new Document(2, array('price' => 45, 'price2' => 90)); + $type->addDocument($doc); + + $facet = new Statistical('stats'); + $facet->setFields(array('price', 'price2')); + + $query = new Query(); + $query->addFacet($facet); + $query->setQuery(new MatchAll()); + + $index->refresh(); + + $response = $type->search($query); + $facets = $response->getFacets(); + + $this->assertEquals(165, $facets['stats']['total']); + $this->assertEquals(10, $facets['stats']['min']); + $this->assertEquals(90, $facets['stats']['max']); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Facet/TermsStatsTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Facet/TermsStatsTest.php new file mode 100644 index 00000000..e3377930 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Facet/TermsStatsTest.php @@ -0,0 +1,113 @@ +_getClient(); + $index = $client->getIndex('test'); + $index->create(array(), true); + $type = $index->getType('helloworld'); + + $doc = new Document(1, array('name' => 'tom', 'paid' => 7)); + $type->addDocument($doc); + $doc = new Document(2, array('name' => 'tom', 'paid' => 2)); + $type->addDocument($doc); + $doc = new Document(3, array('name' => 'tom', 'paid' => 5)); + $type->addDocument($doc); + $doc = new Document(4, array('name' => 'mike', 'paid' => 13)); + $type->addDocument($doc); + $doc = new Document(5, array('name' => 'mike', 'paid' => 1)); + $type->addDocument($doc); + $doc = new Document(6, array('name' => 'mike', 'paid' => 15)); + $type->addDocument($doc); + + $facet = new TermsStats('test'); + $facet->setKeyField('name'); + $facet->setValueField('paid'); + $facet->setOrder('reverse_total'); + + $query = new Query(); + $query->addFacet($facet); + $query->setQuery(new MatchAll()); + + $index->refresh(); + + $response = $type->search($query); + $facets = $response->getFacets(); + + $this->assertEquals(14, $facets[ 'test' ][ 'terms' ][0]['total']); + $this->assertEquals(29, $facets[ 'test' ][ 'terms' ][1]['total']); + } + + /** + * @group functional + */ + public function testQuery() + { + $client = $this->_getClient(); + $index = $client->getIndex('test'); + $index->create(array(), true); + $type = $index->getType('helloworld'); + + $doc = new Document(1, array('name' => 'tom', 'paid' => 7)); + $type->addDocument($doc); + $doc = new Document(2, array('name' => 'tom', 'paid' => 2)); + $type->addDocument($doc); + $doc = new Document(3, array('name' => 'tom', 'paid' => 5)); + $type->addDocument($doc); + $doc = new Document(4, array('name' => 'mike', 'paid' => 13)); + $type->addDocument($doc); + $doc = new Document(5, array('name' => 'mike', 'paid' => 1)); + $type->addDocument($doc); + $doc = new Document(6, array('name' => 'mike', 'paid' => 15)); + $type->addDocument($doc); + + $facet = new TermsStats('test'); + $facet->setKeyField('name'); + $facet->setValueField('paid'); + + $query = new Query(); + $query->addFacet($facet); + $query->setQuery(new MatchAll()); + + $index->refresh(); + + $response = $type->search($query); + $facets = $response->getFacets(); + + $this->assertEquals(2, count($facets[ 'test' ][ 'terms' ])); + foreach ($facets[ 'test' ][ 'terms' ] as $facet) { + if ($facet[ 'term' ] === 'tom') { + $this->assertEquals(14, $facet[ 'total' ]); + } + if ($facet[ 'term' ] === 'mike') { + $this->assertEquals(29, $facet[ 'total' ]); + } + } + } + + /** + * @group unit + */ + public function testSetSize() + { + $facet = new TermsStats('test'); + $facet->setSize(100); + + $data = $facet->toArray(); + + $this->assertArrayHasKey('size', $data['terms_stats']); + $this->assertEquals(100, $data['terms_stats']['size']); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Facet/TermsTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Facet/TermsTest.php new file mode 100644 index 00000000..f4c95796 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Facet/TermsTest.php @@ -0,0 +1,74 @@ +_getClient(); + $index = $client->getIndex('test'); + $index->create(array(), true); + $type = $index->getType('helloworld'); + + $doc = new Document(1, array('name' => 'nicolas ruflin')); + $type->addDocument($doc); + $doc = new Document(2, array('name' => 'ruflin test')); + $type->addDocument($doc); + $doc = new Document(2, array('name' => 'nicolas helloworld')); + $type->addDocument($doc); + + $facet = new Terms('test'); + $facet->setField('name'); + + $query = new Query(); + $query->addFacet($facet); + $query->setQuery(new MatchAll()); + + $index->refresh(); + + $response = $type->search($query); + $facets = $response->getFacets(); + + $this->assertEquals(3, count($facets['test']['terms'])); + } + + /** + * @group functional + */ + public function testFacetScript() + { + $client = $this->_getClient(); + $index = $client->getIndex('test'); + $index->create(array(), true); + $type = $index->getType('helloworld'); + + $doc = new Document(1, array('name' => 'rodolfo', 'last_name' => 'moraes')); + $type->addDocument($doc); + $doc = new Document(2, array('name' => 'jose', 'last_name' => 'honjoya')); + $type->addDocument($doc); + + $facet = new Terms('test'); + $facet->setField('name'); + $facet->setScript('term + " "+doc["last_name"].value'); + + $query = new Query(); + $query->addFacet($facet); + $query->setQuery(new MatchAll()); + + $index->refresh(); + + $response = $type->search($query); + $facets = $response->getFacets(); + + $this->assertEquals(2, count($facets['test']['terms'])); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/AbstractTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/AbstractTest.php new file mode 100644 index 00000000..3ba64dd1 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/AbstractTest.php @@ -0,0 +1,81 @@ +getStub(); + + $stubFilter->setCached(true); + $arrayFilter = current($stubFilter->toArray()); + $this->assertTrue($arrayFilter['_cache']); + + $stubFilter->setCached(false); + $arrayFilter = current($stubFilter->toArray()); + $this->assertFalse($arrayFilter['_cache']); + } + + /** + * @group unit + */ + public function testSetCachedDefaultValue() + { + $stubFilter = $this->getStub(); + + $stubFilter->setCached(); + $arrayFilter = current($stubFilter->toArray()); + $this->assertTrue($arrayFilter['_cache']); + } + + /** + * @group unit + */ + public function testSetCacheKey() + { + $stubFilter = $this->getStub(); + + $cacheKey = 'myCacheKey'; + + $stubFilter->setCacheKey($cacheKey); + $arrayFilter = current($stubFilter->toArray()); + $this->assertEquals($cacheKey, $arrayFilter['_cache_key']); + } + + /** + * @group unit + * @expectedException \Elastica\Exception\InvalidException + */ + public function testSetCacheKeyEmptyKey() + { + $stubFilter = $this->getStub(); + + $cacheKey = ''; + + $stubFilter->setCacheKey($cacheKey); + } + + /** + * @group unit + */ + public function testSetName() + { + $stubFilter = $this->getStub(); + + $name = 'myFilter'; + + $stubFilter->setName($name); + $arrayFilter = current($stubFilter->toArray()); + $this->assertEquals($name, $arrayFilter['_name']); + } + + private function getStub() + { + return $this->getMockForAbstractClass('Elastica\Filter\AbstractFilter'); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/BoolAndTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/BoolAndTest.php new file mode 100644 index 00000000..a8f47633 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/BoolAndTest.php @@ -0,0 +1,86 @@ +assertEquals(array('and' => array()), $and->toArray()); + + $idsFilter = new Ids(); + $idsFilter->setIds(12); + + $and->addFilter($idsFilter); + $and->addFilter($idsFilter); + + $expectedArray = array( + 'and' => array( + $idsFilter->toArray(), + $idsFilter->toArray(), + ), + ); + + $this->assertEquals($expectedArray, $and->toArray()); + } + + /** + * @group functional + */ + public function testSetCache() + { + $client = $this->_getClient(); + $index = $client->getIndex('test'); + $index->create(array(), true); + $type = $index->getType('test'); + + $type->addDocuments(array( + new Document(1, array('name' => 'hello world')), + new Document(2, array('name' => 'nicolas ruflin')), + new Document(3, array('name' => 'ruflin')), + )); + + $and = new BoolAnd(); + + $idsFilter1 = new Ids(); + $idsFilter1->setIds(1); + + $idsFilter2 = new Ids(); + $idsFilter2->setIds(1); + + $and->addFilter($idsFilter1); + $and->addFilter($idsFilter2); + + $index->refresh(); + $and->setCached(true); + + $resultSet = $type->search($and); + + $this->assertEquals(1, $resultSet->count()); + } + + /** + * @group unit + */ + public function testConstruct() + { + $ids1 = new Ids('foo', array(1, 2)); + $ids2 = new Ids('bar', array(3, 4)); + + $and1 = new BoolAnd(array($ids1, $ids2)); + + $and2 = new BoolAnd(); + $and2->addFilter($ids1); + $and2->addFilter($ids2); + + $this->assertEquals($and1->toArray(), $and2->toArray()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/BoolFilterTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/BoolFilterTest.php new file mode 100644 index 00000000..ec7728af --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/BoolFilterTest.php @@ -0,0 +1,200 @@ +setIds(1); + $idsFilter2 = new Ids(); + $idsFilter2->setIds(2); + $idsFilter3 = new Ids(); + $idsFilter3->setIds(3); + + $childBool = new BoolFilter(); + $childBool->addShould(array($idsFilter1, $idsFilter2)); + $mainBool->addShould(array($childBool, $idsFilter3)); + + $expectedArray = array( + 'bool' => array( + 'should' => array( + array( + array( + 'bool' => array( + 'should' => array( + array( + $idsFilter1->toArray(), + $idsFilter2->toArray(), + ), + ), + ), + ), + $idsFilter3->toArray(), + ), + ), + ), + ); + $out[] = array($mainBool, $expectedArray); + + // case #1 _cache parameter should be supported + $bool = new BoolFilter(); + $terms = new Terms('field1', array('value1', 'value2')); + $termsNot = new Terms('field2', array('value1', 'value2')); + $bool->addMust($terms); + $bool->addMustNot($termsNot); + $bool->setCached(true); + $bool->setCacheKey('my-cache-key'); + $expected = array( + 'bool' => array( + 'must' => array( + $terms->toArray(), + ), + 'must_not' => array( + $termsNot->toArray(), + ), + '_cache' => true, + '_cache_key' => 'my-cache-key', + ), + ); + $out[] = array($bool, $expected); + + return $out; + } + + /** + * @group unit + * @dataProvider getTestToArrayData() + * + * @param Bool $bool + * @param array $expectedArray + */ + public function testToArray(BoolFilter $bool, $expectedArray) + { + $this->assertEquals($expectedArray, $bool->toArray()); + } + + /** + * @group functional + */ + public function testBoolFilter() + { + $index = $this->_createIndex(); + $type = $index->getType('book'); + + //index some test data + $type->addDocuments(array( + new Document(1, array('author' => 'Michael Shermer', 'title' => 'The Believing Brain', 'publisher' => 'Robinson')), + new Document(2, array('author' => 'Jared Diamond', 'title' => 'Guns, Germs and Steel', 'publisher' => 'Vintage')), + new Document(3, array('author' => 'Jared Diamond', 'title' => 'Collapse', 'publisher' => 'Penguin')), + new Document(4, array('author' => 'Richard Dawkins', 'title' => 'The Selfish Gene', 'publisher' => 'OUP Oxford')), + new Document(5, array('author' => 'Anthony Burges', 'title' => 'A Clockwork Orange', 'publisher' => 'Penguin')), + )); + + $index->refresh(); + + //use the terms lookup feature to query for some data + //build query + //must + // should + // author = jared + // author = richard + // must_not + // publisher = penguin + + //construct the query + $query = new Query(); + $mainBoolFilter = new BoolFilter(); + $shouldFilter = new BoolFilter(); + $authorFilter1 = new Term(); + $authorFilter1->setTerm('author', 'jared'); + $authorFilter2 = new Term(); + $authorFilter2->setTerm('author', 'richard'); + $shouldFilter->addShould(array($authorFilter1, $authorFilter2)); + + $mustNotFilter = new BoolFilter(); + $publisherFilter = new Term(); + $publisherFilter->setTerm('publisher', 'penguin'); + $mustNotFilter->addMustNot($publisherFilter); + + $mainBoolFilter->addMust(array($shouldFilter, $mustNotFilter)); + $query->setPostFilter($mainBoolFilter); + //execute the query + $results = $index->search($query); + + //check the number of results + $this->assertEquals($results->count(), 2, 'Bool filter with child Bool filters: number of results check'); + + //count compare the id's + $ids = array(); + /** @var \Elastica\Result $result **/ + foreach ($results as $result) { + $ids[] = $result->getId(); + } + $this->assertEquals($ids, array('2', '4'), 'Bool filter with child Bool filters: result ID check'); + + $index->delete(); + } + + /** + * @group unit + * @expectedException \Elastica\Exception\InvalidException + */ + public function testAddMustInvalidException() + { + $filter = new BoolFilter(); + $filter->addMust('fail!'); + } + + /** + * @group unit + * @expectedException \Elastica\Exception\InvalidException + */ + public function testAddMustNotInvalidException() + { + $filter = new BoolFilter(); + $filter->addMustNot('fail!'); + } + + /** + * @group unit + * @expectedException \Elastica\Exception\InvalidException + */ + public function testAddShouldInvalidException() + { + $filter = new BoolFilter(); + $filter->addShould('fail!'); + } + + /** + * Small unit test to check if also the old object name works. + * + * @group unit + * @expectedException \Elastica\Exception\InvalidException + */ + public function testOldObject() + { + if (version_compare(phpversion(), 7, '>=')) { + self::markTestSkipped('These objects are not supported in PHP 7'); + } + + $filter = new \Elastica\Filter\Bool(); + $filter->addShould('fail!'); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/BoolNotTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/BoolNotTest.php new file mode 100644 index 00000000..75461496 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/BoolNotTest.php @@ -0,0 +1,27 @@ +setIds(12); + $filter = new BoolNot($idsFilter); + + $expectedArray = array( + 'not' => array( + 'filter' => $idsFilter->toArray(), + ), + ); + + $this->assertEquals($expectedArray, $filter->toArray()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/BoolOrTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/BoolOrTest.php new file mode 100644 index 00000000..6462d1fa --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/BoolOrTest.php @@ -0,0 +1,90 @@ +getMockForAbstractClass('Elastica\Filter\AbstractFilter'); + $orFilter = new BoolOr(); + $returnValue = $orFilter->addFilter($filter); + $this->assertInstanceOf('Elastica\Filter\BoolOr', $returnValue); + } + + /** + * @group unit + */ + public function testToArray() + { + $orFilter = new BoolOr(); + + $filter1 = new Ids(); + $filter1->setIds('1'); + + $filter2 = new Ids(); + $filter2->setIds('2'); + + $orFilter->addFilter($filter1); + $orFilter->addFilter($filter2); + + $expectedArray = array( + 'or' => array( + $filter1->toArray(), + $filter2->toArray(), + ), + ); + + $this->assertEquals($expectedArray, $orFilter->toArray()); + } + + /** + * @group unit + */ + public function testConstruct() + { + $ids1 = new Ids('foo', array(1, 2)); + $ids2 = new Ids('bar', array(3, 4)); + + $and1 = new BoolOr(array($ids1, $ids2)); + + $and2 = new BoolOr(); + $and2->addFilter($ids1); + $and2->addFilter($ids2); + + $this->assertEquals($and1->toArray(), $and2->toArray()); + } + + /** + * @group functional + */ + public function testOrFilter() + { + $index = $this->_createIndex(); + $type = $index->getType('test'); + + $doc1 = new Document('', array('categoryId' => 1)); + $doc2 = new Document('', array('categoryId' => 2)); + $doc3 = new Document('', array('categoryId' => 3)); + + $type->addDocument($doc1); + $type->addDocument($doc2); + $type->addDocument($doc3); + + $index->refresh(); + + $boolOr = new \Elastica\Filter\BoolOr(); + $boolOr->addFilter(new \Elastica\Filter\Term(array('categoryId' => '1'))); + $boolOr->addFilter(new \Elastica\Filter\Term(array('categoryId' => '2'))); + + $resultSet = $type->search($boolOr); + $this->assertEquals(2, $resultSet->count()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/ExistsTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/ExistsTest.php new file mode 100644 index 00000000..25afa724 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/ExistsTest.php @@ -0,0 +1,36 @@ + array('field' => $field)); + $this->assertEquals($expectedArray, $filter->toArray()); + } + + /** + * @group unit + */ + public function testSetField() + { + $field = 'test'; + $filter = new Exists($field); + + $this->assertEquals($field, $filter->getParam('field')); + + $newField = 'hello world'; + $this->assertInstanceOf('Elastica\Filter\Exists', $filter->setField($newField)); + + $this->assertEquals($newField, $filter->getParam('field')); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/GeoBoundingBoxTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/GeoBoundingBoxTest.php new file mode 100644 index 00000000..8fdde965 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/GeoBoundingBoxTest.php @@ -0,0 +1,55 @@ +addCoordinates($key, $coords); + $expectedArray = array('top_left' => $coords[0], 'bottom_right' => $coords[1]); + $this->assertEquals($expectedArray, $filter->getParam($key)); + + $returnValue = $filter->addCoordinates($key, $coords); + $this->assertInstanceOf('Elastica\Filter\GeoBoundingBox', $returnValue); + } + + /** + * @group unit + * @expectedException \Elastica\Exception\InvalidException + */ + public function testAddCoordinatesInvalidException() + { + $filter = new GeoBoundingBox('foo', array()); + } + + /** + * @group unit + */ + public function testToArray() + { + $key = 'pin.location'; + $coords = array('40.73, -74.1', '40.01, -71.12'); + $filter = new GeoBoundingBox($key, $coords); + + $expectedArray = array( + 'geo_bounding_box' => array( + $key => array( + 'top_left' => $coords[0], + 'bottom_right' => $coords[1], + ), + ), + ); + + $this->assertEquals($expectedArray, $filter->toArray()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/GeoDistanceRangeTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/GeoDistanceRangeTest.php new file mode 100644 index 00000000..203a45de --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/GeoDistanceRangeTest.php @@ -0,0 +1,220 @@ +_createIndex(); + $type = $index->getType('test'); + + // Set mapping + $type->setMapping(array('point' => array('type' => 'geo_point'))); + + // Add doc 1 + $doc1 = new Document(1, + array( + 'name' => 'ruflin', + ) + ); + + $doc1->addGeoPoint('point', 17, 19); + $type->addDocument($doc1); + + // Add doc 2 + $doc2 = new Document(2, + array( + 'name' => 'ruflin', + ) + ); + + $doc2->addGeoPoint('point', 30, 40); + $type->addDocument($doc2); + + $index->optimize(); + $index->refresh(); + + // Only one point should be in radius + $query = new Query(); + $geoFilter = new GeoDistanceRange( + 'point', + array('lat' => 30, 'lon' => 40), + array('from' => '0km', 'to' => '2km') + ); + + $query = new Query(new MatchAll()); + $query->setPostFilter($geoFilter); + $this->assertEquals(1, $type->search($query)->count()); + + // Both points should be inside + $query = new Query(); + $geoFilter = new GeoDistanceRange( + 'point', + array('lat' => 30, 'lon' => 40), + array('gte' => '0km', 'lte' => '40000km') + ); + $query = new Query(new MatchAll()); + $query->setPostFilter($geoFilter); + $index->refresh(); + + $this->assertEquals(2, $type->search($query)->count()); + } + + /** + * @group unit + * @expectedException \Elastica\Exception\InvalidException + */ + public function testInvalidRange() + { + $geoFilter = new GeoDistanceRange( + 'point', + array('lat' => 30, 'lon' => 40), + array('invalid' => '0km', 'lte' => '40000km') + ); + } + + /** + * @group unit + * @dataProvider invalidLocationDataProvider + * @expectedException \Elastica\Exception\InvalidException + */ + public function testInvalidLocation($location) + { + $geoFilter = new GeoDistanceRange( + 'point', + $location, + array('gt' => '0km', 'lte' => '40000km') + ); + } + + /** + * @group unit + * @dataProvider constructDataProvider + */ + public function testConstruct($key, $location, $ranges, $expected) + { + $filter = new GeoDistanceRange($key, $location, $ranges); + + $data = $filter->toArray(); + + $this->assertEquals($expected, $data); + } + + public function invalidLocationDataProvider() + { + return array( + array( + array('lat' => 1.0), + ), + array( + array('lon' => 1.0), + ), + array( + array(), + ), + array( + new \stdClass(), + ), + array( + null, + ), + array( + true, + ), + array( + false, + ), + ); + } + + public function constructDataProvider() + { + return array( + array( + 'location', + 'u09tvqx', + array( + 'from' => '10km', + 'to' => '20km', + ), + array( + 'geo_distance_range' => array( + 'from' => '10km', + 'to' => '20km', + 'location' => 'u09tvqx', + ), + ), + ), + array( + 'location', + 'u09tvqx', + array( + 'to' => '20km', + 'include_upper' => 0, + 'from' => '10km', + 'include_lower' => 1, + ), + array( + 'geo_distance_range' => array( + 'to' => '20km', + 'include_upper' => false, + 'from' => '10km', + 'include_lower' => true, + 'location' => 'u09tvqx', + ), + ), + ), + array( + 'location', + array( + 'lon' => 2.35, + 'lat' => 48.86, + ), + array( + 'lte' => '20km', + 'gt' => '10km', + ), + array( + 'geo_distance_range' => array( + 'lte' => '20km', + 'gt' => '10km', + 'location' => array( + 'lat' => 48.86, + 'lon' => 2.35, + ), + ), + ), + ), + array( + 'location', + array( + 'lat' => 48.86, + 'lon' => 2.35, + ), + array( + 'lt' => '20km', + 'gte' => '10km', + ), + array( + 'geo_distance_range' => array( + 'lt' => '20km', + 'gte' => '10km', + 'location' => array( + 'lat' => 48.86, + 'lon' => 2.35, + ), + ), + ), + ), + ); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/GeoDistanceTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/GeoDistanceTest.php new file mode 100644 index 00000000..b61b4adc --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/GeoDistanceTest.php @@ -0,0 +1,141 @@ +_createIndex(); + + $type = $index->getType('test'); + + // Set mapping + $type->setMapping(array('point' => array('type' => 'geo_point'))); + + // Add doc 1 + $doc1 = new Document(1, + array( + 'name' => 'ruflin', + ) + ); + + $doc1->addGeoPoint('point', 17, 19); + $type->addDocument($doc1); + + // Add doc 2 + $doc2 = new Document(2, + array( + 'name' => 'ruflin', + ) + ); + + $doc2->addGeoPoint('point', 30, 40); + $type->addDocument($doc2); + + $index->optimize(); + $index->refresh(); + + // Only one point should be in radius + $query = new Query(); + $geoFilter = new GeoDistance('point', array('lat' => 30, 'lon' => 40), '1km'); + + $query = new Query(new MatchAll()); + $query->setPostFilter($geoFilter); + $this->assertEquals(1, $type->search($query)->count()); + + // Both points should be inside + $query = new Query(); + $geoFilter = new GeoDistance('point', array('lat' => 30, 'lon' => 40), '40000km'); + $query = new Query(new MatchAll()); + $query->setPostFilter($geoFilter); + $index->refresh(); + + $this->assertEquals(2, $type->search($query)->count()); + } + + /** + * @group unit + */ + public function testConstructLatlon() + { + $key = 'location'; + $location = array( + 'lat' => 48.86, + 'lon' => 2.35, + ); + $distance = '10km'; + + $filter = new GeoDistance($key, $location, $distance); + + $expected = array( + 'geo_distance' => array( + $key => $location, + 'distance' => $distance, + ), + ); + + $data = $filter->toArray(); + + $this->assertEquals($expected, $data); + } + + /** + * @group unit + */ + public function testConstructGeohash() + { + $key = 'location'; + $location = 'u09tvqx'; + $distance = '10km'; + + $filter = new GeoDistance($key, $location, $distance); + + $expected = array( + 'geo_distance' => array( + $key => $location, + 'distance' => $distance, + ), + ); + + $data = $filter->toArray(); + + $this->assertEquals($expected, $data); + } + + /** + * @group unit + */ + public function testSetDistanceType() + { + $filter = new GeoDistance('location', array('lat' => 48.86, 'lon' => 2.35), '10km'); + $distanceType = GeoDistance::DISTANCE_TYPE_ARC; + $filter->setDistanceType($distanceType); + + $data = $filter->toArray(); + + $this->assertEquals($distanceType, $data['geo_distance']['distance_type']); + } + + /** + * @group unit + */ + public function testSetOptimizeBbox() + { + $filter = new GeoDistance('location', array('lat' => 48.86, 'lon' => 2.35), '10km'); + $optimizeBbox = GeoDistance::OPTIMIZE_BBOX_MEMORY; + $filter->setOptimizeBbox($optimizeBbox); + + $data = $filter->toArray(); + + $this->assertEquals($optimizeBbox, $data['geo_distance']['optimize_bbox']); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/GeoPolygonTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/GeoPolygonTest.php new file mode 100644 index 00000000..b56f73b0 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/GeoPolygonTest.php @@ -0,0 +1,65 @@ +_createIndex(); + + $type = $index->getType('test'); + + // Set mapping + $type->setMapping(array('location' => array('type' => 'geo_point'))); + + // Add doc 1 + $doc1 = new Document(1, + array( + 'name' => 'ruflin', + ) + ); + + $doc1->addGeoPoint('location', 17, 19); + $type->addDocument($doc1); + + // Add doc 2 + $doc2 = new Document(2, + array( + 'name' => 'ruflin', + ) + ); + + $doc2->addGeoPoint('location', 30, 40); + $type->addDocument($doc2); + + $index->refresh(); + + // Only one point should be in polygon + $query = new Query(); + $points = array(array(16, 16), array(16, 20), array(20, 20), array(20, 16), array(16, 16)); + $geoFilter = new GeoPolygon('location', $points); + + $query = new Query(new MatchAll()); + $query->setPostFilter($geoFilter); + $this->assertEquals(1, $type->search($query)->count()); + + // Both points should be inside + $query = new Query(); + $points = array(array(16, 16), array(16, 40), array(40, 40), array(40, 16), array(16, 16)); + $geoFilter = new GeoPolygon('location', $points); + + $query = new Query(new MatchAll()); + $query->setPostFilter($geoFilter); + + $this->assertEquals(2, $type->search($query)->count()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/GeoShapePreIndexedTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/GeoShapePreIndexedTest.php new file mode 100644 index 00000000..96453527 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/GeoShapePreIndexedTest.php @@ -0,0 +1,102 @@ +_createIndex(); + $indexName = $index->getName(); + $type = $index->getType('type'); + $otherType = $index->getType('other_type'); + + // create mapping + $mapping = new \Elastica\Type\Mapping($type, array( + 'location' => array( + 'type' => 'geo_shape', + ), + )); + $type->setMapping($mapping); + + // create other type mapping + $otherMapping = new \Elastica\Type\Mapping($type, array( + 'location' => array( + 'type' => 'geo_shape', + ), + )); + $otherType->setMapping($otherMapping); + + // add type docs + $type->addDocument(new \Elastica\Document('1', array( + 'location' => array( + 'type' => 'envelope', + 'coordinates' => array( + array(0.0, 50.0), + array(50.0, 0.0), + ), + ), + ))); + + // add other type docs + $otherType->addDocument(new \Elastica\Document('2', array( + 'location' => array( + 'type' => 'envelope', + 'coordinates' => array( + array(25.0, 75.0), + array(75.0, 25.0), + ), + ), + ))); + + $index->optimize(); + $index->refresh(); + + $gsp = new GeoShapePreIndexed( + 'location', '1', 'type', $indexName, 'location' + ); + $gsp->setRelation(AbstractGeoShape::RELATION_INTERSECT); + + $expected = array( + 'geo_shape' => array( + 'location' => array( + 'indexed_shape' => array( + 'id' => '1', + 'type' => 'type', + 'index' => $indexName, + 'path' => 'location', + ), + 'relation' => $gsp->getRelation(), + ), + ), + ); + + $this->assertEquals($expected, $gsp->toArray()); + + $query = new Filtered(new MatchAll(), $gsp); + $results = $index->getType('type')->search($query); + + $this->assertEquals(1, $results->count()); + + $index->delete(); + } + + /** + * @group unit + */ + public function testSetRelation() + { + $gsp = new GeoShapePreIndexed('location', '1', 'type', 'indexName', 'location'); + $gsp->setRelation(AbstractGeoShape::RELATION_INTERSECT); + $this->assertEquals(AbstractGeoShape::RELATION_INTERSECT, $gsp->getRelation()); + $this->assertInstanceOf('Elastica\Filter\GeoShapePreIndexed', $gsp->setRelation(AbstractGeoShape::RELATION_INTERSECT)); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/GeoShapeProvidedTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/GeoShapeProvidedTest.php new file mode 100644 index 00000000..d631e44e --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/GeoShapeProvidedTest.php @@ -0,0 +1,103 @@ +_createIndex(); + $type = $index->getType('test'); + + // create mapping + $mapping = new Mapping($type, array( + 'location' => array( + 'type' => 'geo_shape', + ), + )); + $type->setMapping($mapping); + + // add docs + $type->addDocument(new Document(1, array( + 'location' => array( + 'type' => 'envelope', + 'coordinates' => array( + array(-50.0, 50.0), + array(50.0, -50.0), + ), + ), + ))); + + $index->optimize(); + $index->refresh(); + + $envelope = array( + array(25.0, 75.0), + array(75.0, 25.0), + ); + $gsp = new GeoShapeProvided('location', $envelope); + + $expected = array( + 'geo_shape' => array( + 'location' => array( + 'shape' => array( + 'type' => GeoShapeProvided::TYPE_ENVELOPE, + 'coordinates' => $envelope, + ), + 'relation' => AbstractGeoShape::RELATION_INTERSECT, + ), + ), + ); + + $this->assertEquals($expected, $gsp->toArray()); + + $query = new Filtered(new MatchAll(), $gsp); + $results = $type->search($query); + + $this->assertEquals(1, $results->count()); + } + + /** + * @group unit + */ + public function testConstructPolygon() + { + $polygon = array(array(102.0, 2.0), array(103.0, 2.0), array(103.0, 3.0), array(103.0, 3.0), array(102.0, 2.0)); + $gsp = new GeoShapeProvided('location', $polygon, GeoShapeProvided::TYPE_POLYGON); + + $expected = array( + 'geo_shape' => array( + 'location' => array( + 'shape' => array( + 'type' => GeoShapeProvided::TYPE_POLYGON, + 'coordinates' => $polygon, + ), + 'relation' => $gsp->getRelation(), + ), + ), + ); + + $this->assertEquals($expected, $gsp->toArray()); + } + + /** + * @group unit + */ + public function testSetRelation() + { + $gsp = new GeoShapeProvided('location', array(array(25.0, 75.0), array(75.0, 25.0))); + $gsp->setRelation(AbstractGeoShape::RELATION_INTERSECT); + $this->assertEquals(AbstractGeoShape::RELATION_INTERSECT, $gsp->getRelation()); + $this->assertInstanceOf('Elastica\Filter\GeoShapeProvided', $gsp->setRelation(AbstractGeoShape::RELATION_INTERSECT)); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/GeohashCellTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/GeohashCellTest.php new file mode 100644 index 00000000..7ef0d04f --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/GeohashCellTest.php @@ -0,0 +1,68 @@ + 37.789018, 'lon' => -122.391506), '50m'); + $expected = array( + 'geohash_cell' => array( + 'pin' => array( + 'lat' => 37.789018, + 'lon' => -122.391506, + ), + 'precision' => '50m', + 'neighbors' => false, + ), + ); + $this->assertEquals($expected, $filter->toArray()); + } + + /** + * @group functional + */ + public function testFilter() + { + $index = $this->_createIndex(); + $type = $index->getType('test'); + $mapping = new Mapping($type, array( + 'pin' => array( + 'type' => 'geo_point', + 'geohash' => true, + 'geohash_prefix' => true, + ), + )); + $type->setMapping($mapping); + + $type->addDocument(new Document(1, array('pin' => '9q8yyzm0zpw8'))); + $type->addDocument(new Document(2, array('pin' => '9mudgb0yued0'))); + $index->refresh(); + + $filter = new GeohashCell('pin', array('lat' => 32.828326, 'lon' => -117.255854)); + $query = new Query(); + $query->setPostFilter($filter); + $results = $type->search($query); + + $this->assertEquals(1, $results->count()); + + //test precision parameter + $filter = new GeohashCell('pin', '9', 1); + $query = new Query(); + $query->setPostFilter($filter); + $results = $type->search($query); + + $this->assertEquals(2, $results->count()); + + $index->delete(); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/HasChildTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/HasChildTest.php new file mode 100644 index 00000000..00af1def --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/HasChildTest.php @@ -0,0 +1,213 @@ + array( + 'query' => $q->toArray(), + 'type' => $type, + ), + ); + + $this->assertEquals($expectedArray, $filter->toArray()); + } + + /** + * @group functional + */ + public function testSetType() + { + $index = $this->prepareSearchData(); + + $filter = new HasChild(new MatchAll(), 'type_name'); + $this->assertEquals('type_name', $filter->getParam('type')); + + $filter->setType('new_type_name'); + $this->assertEquals('new_type_name', $filter->getParam('type')); + + $type = $index->getType('foo'); + $filter = new HasChild(new MatchAll(), $type); + $this->assertEquals('foo', $filter->getParam('type')); + + $type = $index->getType('bar'); + $filter->setType($type); + $this->assertEquals('bar', $filter->getParam('type')); + + $returnValue = $filter->setType('last'); + $this->assertInstanceOf('Elastica\Filter\HasChild', $returnValue); + } + + /** + * @group unit + */ + public function testSetMinimumChildrenCount() + { + $query = new MatchAll(); + $filter = new HasChild($query, 'test'); + + $filter->setMinimumChildrenCount(2); + $this->assertEquals(2, $filter->getParam('min_children')); + + $returnValue = $filter->setMinimumChildrenCount(2); + $this->assertInstanceOf('Elastica\Filter\HasChild', $returnValue); + } + + /** + * @group unit + */ + public function testSetMaximumChildrenCount() + { + $query = new MatchAll(); + $filter = new HasChild($query, 'test'); + + $filter->setMaximumChildrenCount(10); + $this->assertEquals(10, $filter->getParam('max_children')); + + $returnValue = $filter->setMaximumChildrenCount(10); + $this->assertInstanceOf('Elastica\Filter\HasChild', $returnValue); + } + + /** + * @group unit + */ + public function testFilterInsideHasChild() + { + $f = new \Elastica\Filter\MatchAll(); + + $type = 'test'; + + $filter = new HasChild($f, $type); + + $expectedArray = array( + 'has_child' => array( + 'filter' => $f->toArray(), + 'type' => $type, + ), + ); + + $this->assertEquals($expectedArray, $filter->toArray()); + } + + /** + * @group functional + */ + public function testFilterInsideHasChildSearch() + { + $index = $this->prepareSearchData(); + + $f = new \Elastica\Filter\Term(); + $f->setTerm('user', 'child1'); + $filter = new HasChild($f, 'child'); + + $searchQuery = new \Elastica\Query(); + $searchQuery->setPostFilter($filter); + $searchResults = $index->search($searchQuery); + + $this->assertEquals(1, $searchResults->count()); + + $result = $searchResults->current()->getData(); + $expected = array('id' => 'parent1', 'user' => 'parent1', 'email' => 'parent1@test.com'); + + $this->assertEquals($expected, $result); + } + + /** + * @group functional + */ + public function testQueryInsideHasChildSearch() + { + $index = $this->prepareSearchData(); + + $f = new \Elastica\Query\Term(); + $f->setTerm('user', 'child1'); + $filter = new HasChild($f, 'child'); + + $searchQuery = new \Elastica\Query(); + $searchQuery->setPostFilter($filter); + $searchResults = $index->search($searchQuery); + + $this->assertEquals(1, $searchResults->count()); + + $result = $searchResults->current()->getData(); + $expected = array('id' => 'parent1', 'user' => 'parent1', 'email' => 'parent1@test.com'); + + $this->assertEquals($expected, $result); + } + + /** + * @group functional + */ + public function testTypeInsideHasChildSearch() + { + $index = $this->prepareSearchData(); + + $f = new \Elastica\Query\Match(); + $f->setField('alt.name', 'testname'); + $filter = new HasChild($f, 'child'); + + $searchQuery = new \Elastica\Query(); + $searchQuery->setPostFilter($filter); + $searchResults = $index->search($searchQuery); + + $this->assertEquals(1, $searchResults->count()); + + $result = $searchResults->current()->getData(); + $expected = array('id' => 'parent2', 'user' => 'parent2', 'email' => 'parent2@test.com'); + + $this->assertEquals($expected, $result); + } + + private function prepareSearchData() + { + $client = $this->_getClient(); + $index = $client->getIndex('has_child_test'); + $index->create(array(), true); + + $parentType = $index->getType('parent'); + + $childType = $index->getType('child'); + $childMapping = new \Elastica\Type\Mapping($childType); + $childMapping->setParent('parent'); + $childMapping->send(); + + $altType = $index->getType('alt'); + $altDoc = new Document('alt1', array('name' => 'altname')); + $altType->addDocument($altDoc); + + $parent1 = new Document('parent1', array('id' => 'parent1', 'user' => 'parent1', 'email' => 'parent1@test.com')); + $parentType->addDocument($parent1); + $parent2 = new Document('parent2', array('id' => 'parent2', 'user' => 'parent2', 'email' => 'parent2@test.com')); + $parentType->addDocument($parent2); + + $child1 = new Document('child1', array('id' => 'child1', 'user' => 'child1', 'email' => 'child1@test.com')); + $child1->setParent('parent1'); + $childType->addDocument($child1); + $child2 = new Document('child2', array('id' => 'child2', 'user' => 'child2', 'email' => 'child2@test.com')); + $child2->setParent('parent2'); + $childType->addDocument($child2); + $child3 = new Document('child3', array('id' => 'child3', 'user' => 'child3', 'email' => 'child3@test.com', 'alt' => array(array('name' => 'testname')))); + $child3->setParent('parent2'); + $childType->addDocument($child3); + + $index->refresh(); + + return $index; + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/HasParentTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/HasParentTest.php new file mode 100644 index 00000000..50143dda --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/HasParentTest.php @@ -0,0 +1,153 @@ + array( + 'query' => $q->toArray(), + 'type' => $type, + ), + ); + + $this->assertEquals($expectedArray, $filter->toArray()); + } + + /** + * @group functional + */ + public function testSetType() + { + $index = $this->prepareSearchData(); + + $filter = new HasParent(new MatchAll(), 'type_name'); + $this->assertEquals('type_name', $filter->getParam('type')); + + $filter->setType('new_type_name'); + $this->assertEquals('new_type_name', $filter->getParam('type')); + + $type = $index->getType('foo'); + $filter = new HasParent(new MatchAll(), $type); + $this->assertEquals('foo', $filter->getParam('type')); + + $type = $index->getType('bar'); + $filter->setType($type); + $this->assertEquals('bar', $filter->getParam('type')); + + $returnValue = $filter->setType('last'); + $this->assertInstanceOf('Elastica\Filter\HasParent', $returnValue); + } + + /** + * @group unit + */ + public function testFilterInsideHasParent() + { + $f = new \Elastica\Filter\MatchAll(); + + $type = 'test'; + + $filter = new HasParent($f, $type); + + $expectedArray = array( + 'has_parent' => array( + 'filter' => $f->toArray(), + 'type' => $type, + ), + ); + + $this->assertEquals($expectedArray, $filter->toArray()); + } + + /** + * @group functional + */ + public function testFilterInsideHasParentSearch() + { + $index = $this->prepareSearchData(); + + $f = new \Elastica\Filter\Term(); + $f->setTerm('user', 'parent1'); + $filter = new HasParent($f, 'parent'); + + $searchQuery = new \Elastica\Query(); + $searchQuery->setPostFilter($filter); + $searchResults = $index->search($searchQuery); + + $this->assertEquals(1, $searchResults->count()); + + $result = $searchResults->current()->getData(); + $expected = array('id' => 'child1', 'user' => 'child1', 'email' => 'child1@test.com'); + + $this->assertEquals($expected, $result); + } + + /** + * @group functional + */ + public function testQueryInsideHasParentSearch() + { + $index = $this->prepareSearchData(); + + $f = new \Elastica\Query\Term(); + $f->setTerm('user', 'parent1'); + $filter = new HasParent($f, 'parent'); + + $searchQuery = new \Elastica\Query(); + $searchQuery->setPostFilter($filter); + $searchResults = $index->search($searchQuery); + + $this->assertEquals(1, $searchResults->count()); + + $result = $searchResults->current()->getData(); + $expected = array('id' => 'child1', 'user' => 'child1', 'email' => 'child1@test.com'); + + $this->assertEquals($expected, $result); + } + + private function prepareSearchData() + { + $client = $this->_getClient(); + $index = $client->getIndex('has_parent_test'); + $index->create(array(), true); + + $parentType = $index->getType('parent'); + + $childType = $index->getType('child'); + $childMapping = new \Elastica\Type\Mapping($childType); + $childMapping->setParent('parent'); + $childMapping->send(); + + $parent1 = new Document('parent1', array('id' => 'parent1', 'user' => 'parent1', 'email' => 'parent1@test.com')); + $parentType->addDocument($parent1); + $parent2 = new Document('parent2', array('id' => 'parent2', 'user' => 'parent2', 'email' => 'parent2@test.com')); + $parentType->addDocument($parent2); + + $child1 = new Document('child1', array('id' => 'child1', 'user' => 'child1', 'email' => 'child1@test.com')); + $child1->setParent('parent1'); + $childType->addDocument($child1); + $child2 = new Document('child2', array('id' => 'child2', 'user' => 'child2', 'email' => 'child2@test.com')); + $child2->setParent('parent2'); + $childType->addDocument($child2); + + $index->refresh(); + + return $index; + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/IdsTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/IdsTest.php new file mode 100644 index 00000000..8395f39a --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/IdsTest.php @@ -0,0 +1,244 @@ +_createIndex(); + + // Add documents to first type + $docs = array(); + for ($i = 1; $i < 100; $i++) { + $docs[] = new Document($i, array('name' => 'ruflin')); + } + $index->getType('helloworld1')->addDocuments($docs); + + // Add documents to second type + $docs = array(); + for ($i = 1; $i < 100; $i++) { + $docs[] = new Document($i, array('name' => 'ruflin')); + } + // This is a special id that will only be in the second type + $docs[] = new Document(101, array('name' => 'ruflin')); + $index->getType('helloworld2')->addDocuments($docs); + + $index->optimize(); + $index->refresh(); + + return $index; + } + + protected function _getTypeForTest() + { + return $this->_getIndexForTest()->getType('helloworld1'); + } + + /** + * @group functional + */ + public function testSetIdsSearchSingle() + { + $filter = new Ids(); + $filter->setIds('1'); + + $query = Query::create($filter); + $resultSet = $this->_getTypeForTest()->search($query); + + $this->assertEquals(1, $resultSet->count()); + } + + /** + * @group functional + */ + public function testSetIdsSearchArray() + { + $filter = new Ids(); + $filter->setIds(array(1, 7, 13)); + + $query = Query::create($filter); + $resultSet = $this->_getTypeForTest()->search($query); + + $this->assertEquals(3, $resultSet->count()); + } + + /** + * @group functional + */ + public function testAddIdsSearchSingle() + { + $filter = new Ids(); + $filter->addId('39'); + + $query = Query::create($filter); + $resultSet = $this->_getTypeForTest()->search($query); + + $this->assertEquals(1, $resultSet->count()); + } + + /** + * @group functional + */ + public function testAddIdsSearchSingleNotInType() + { + $filter = new Ids(); + $filter->addId('39'); + + // Add an ID that is not in the index + $filter->addId(104); + + $query = Query::create($filter); + $resultSet = $this->_getTypeForTest()->search($query); + + $this->assertEquals(1, $resultSet->count()); + } + + /** + * @group functional + */ + public function testComboIdsSearchArray() + { + $filter = new Ids(); + $filter->setIds(array(1, 7, 13)); + $filter->addId('39'); + + $query = Query::create($filter); + $resultSet = $this->_getTypeForTest()->search($query); + + $this->assertEquals(4, $resultSet->count()); + } + + /** + * @group functional + */ + public function testSetTypeSingleSearchSingle() + { + $filter = new Ids(); + $filter->setIds('1'); + $filter->setType('helloworld1'); + + $query = Query::create($filter); + $resultSet = $this->_getIndexForTest()->search($query); + + $this->assertEquals(1, $resultSet->count()); + } + + /** + * @group functional + */ + public function testSetTypeSingleSearchArray() + { + $filter = new Ids(); + $filter->setIds(array('1', '2')); + $filter->setType('helloworld1'); + + $query = Query::create($filter); + $resultSet = $this->_getIndexForTest()->search($query); + + $this->assertEquals(2, $resultSet->count()); + } + + /** + * @group functional + */ + public function testSetTypeSingleSearchSingleDocInOtherType() + { + $filter = new Ids(); + + // Doc 4 is in the second type... + $filter->setIds('101'); + $filter->setType('helloworld1'); + + $query = Query::create($filter); + $resultSet = $this->_getTypeForTest()->search($query); + + // ...therefore 0 results should be returned + $this->assertEquals(0, $resultSet->count()); + } + + /** + * @group functional + */ + public function testSetTypeSingleSearchArrayDocInOtherType() + { + $filter = new Ids(); + + // Doc 4 is in the second type... + $filter->setIds(array('1', '101')); + $filter->setType('helloworld1'); + + $query = Query::create($filter); + $resultSet = $this->_getTypeForTest()->search($query); + + // ...therefore only 1 result should be returned + $this->assertEquals(1, $resultSet->count()); + } + + /** + * @group functional + */ + public function testSetTypeArraySearchArray() + { + $filter = new Ids(); + $filter->setIds(array('1', '4')); + $filter->setType(array('helloworld1', 'helloworld2')); + + $query = Query::create($filter); + $resultSet = $this->_getIndexForTest()->search($query); + + $this->assertEquals(4, $resultSet->count()); + } + + /** + * @group functional + */ + public function testSetTypeArraySearchSingle() + { + $filter = new Ids(); + $filter->setIds('4'); + $filter->setType(array('helloworld1', 'helloworld2')); + + $query = Query::create($filter); + $resultSet = $this->_getIndexForTest()->search($query); + + $this->assertEquals(2, $resultSet->count()); + } + + /** + * @group unit + */ + public function testFilterTypeAndTypeCollision() + { + // This test ensures that Elastica\Type and Elastica\Filter\Type + // do not collide when used together, which at one point + // happened because of a use statement in Elastica\Filter\Ids + // Test goal is to make sure a Fatal Error is not triggered + $filterType = new Type(); + $filter = new Ids(); + } + + /** + * @group unit + */ + public function testAddType() + { + $type = $this->_getClient()->getIndex('indexname')->getType('typename'); + + $filter = new Ids(); + + $filter->addType('foo'); + $this->assertEquals(array('foo'), $filter->getParam('type')); + + $filter->addType($type); + $this->assertEquals(array('foo', $type->getName()), $filter->getParam('type')); + + $returnValue = $filter->addType('bar'); + $this->assertInstanceOf('Elastica\Filter\Ids', $returnValue); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/IndicesTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/IndicesTest.php new file mode 100644 index 00000000..bc78aa6b --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/IndicesTest.php @@ -0,0 +1,125 @@ + array( + 'indices' => array('index1', 'index2'), + 'filter' => array( + 'term' => array('tag' => 'wow'), + ), + 'no_match_filter' => array( + 'term' => array('tag' => 'such filter'), + ), + ), + ); + $filter = new Indices(new Term(array('tag' => 'wow')), array('index1', 'index2')); + $filter->setNoMatchFilter(new Term(array('tag' => 'such filter'))); + $this->assertEquals($expected, $filter->toArray()); + } + + /** + * @group functional + */ + public function testIndicesFilter() + { + $docs = array( + new Document(1, array('color' => 'blue')), + new Document(2, array('color' => 'green')), + new Document(3, array('color' => 'blue')), + new Document(4, array('color' => 'yellow')), + ); + + $index1 = $this->_createIndex(); + $index1->addAlias('indices_filter'); + $index1->getType('test')->addDocuments($docs); + $index1->refresh(); + + $index2 = $this->_createIndex(); + $index2->addAlias('indices_filter'); + $index2->getType('test')->addDocuments($docs); + $index2->refresh(); + + $filter = new Indices(new BoolNot(new Term(array('color' => 'blue'))), array($index1->getName())); + $filter->setNoMatchFilter(new BoolNot(new Term(array('color' => 'yellow')))); + $query = new Query(); + $query->setPostFilter($filter); + + // search over the alias + $index = $this->_getClient()->getIndex('indices_filter'); + $results = $index->search($query); + + // ensure that the proper docs have been filtered out for each index + $this->assertEquals(5, $results->count()); + foreach ($results->getResults() as $result) { + $data = $result->getData(); + $color = $data['color']; + if ($result->getIndex() === $index1->getName()) { + $this->assertNotEquals('blue', $color); + } else { + $this->assertNotEquals('yellow', $color); + } + } + } + + /** + * @group unit + */ + public function testSetIndices() + { + $client = $this->_getClient(); + $index1 = $client->getIndex('index1'); + $index2 = $client->getIndex('index2'); + + $indices = array('one', 'two'); + $filter = new Indices(new Term(array('color' => 'blue')), $indices); + $this->assertEquals($indices, $filter->getParam('indices')); + + $indices[] = 'three'; + $filter->setIndices($indices); + $this->assertEquals($indices, $filter->getParam('indices')); + + $filter->setIndices(array($index1, $index2)); + $expected = array($index1->getName(), $index2->getName()); + $this->assertEquals($expected, $filter->getParam('indices')); + + $returnValue = $filter->setIndices($indices); + $this->assertInstanceOf('Elastica\Filter\Indices', $returnValue); + } + + /** + * @group unit + */ + public function testAddIndex() + { + $client = $this->_getClient(); + $index = $client->getIndex('someindex'); + + $filter = new Indices(new Term(array('color' => 'blue')), array()); + + $filter->addIndex($index); + $expected = array($index->getName()); + $this->assertEquals($expected, $filter->getParam('indices')); + + $filter->addIndex('foo'); + $expected = array($index->getName(), 'foo'); + $this->assertEquals($expected, $filter->getParam('indices')); + + $returnValue = $filter->addIndex('bar'); + $this->assertInstanceOf('Elastica\Filter\Indices', $returnValue); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/LimitTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/LimitTest.php new file mode 100644 index 00000000..0cdfee39 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/LimitTest.php @@ -0,0 +1,34 @@ +assertEquals(10, $filter->getParam('value')); + + $this->assertInstanceOf('Elastica\Filter\Limit', $filter->setLimit(20)); + $this->assertEquals(20, $filter->getParam('value')); + } + + /** + * @group unit + */ + public function testToArray() + { + $filter = new Limit(15); + + $expectedArray = array( + 'limit' => array('value' => 15), + ); + + $this->assertEquals($expectedArray, $filter->toArray()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/MatchAllTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/MatchAllTest.php new file mode 100644 index 00000000..9bfd511f --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/MatchAllTest.php @@ -0,0 +1,20 @@ + new \stdClass()); + + $this->assertEquals($expectedArray, $filter->toArray()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/MissingTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/MissingTest.php new file mode 100644 index 00000000..f87df790 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/MissingTest.php @@ -0,0 +1,78 @@ + array('field' => 'field_name')); + $this->assertEquals($expectedArray, $filter->toArray()); + + $filter = new Missing('field_name'); + $filter->setExistence(true); + $expectedArray = array('missing' => array('field' => 'field_name', 'existence' => true)); + $this->assertEquals($expectedArray, $filter->toArray()); + + $filter = new Missing('field_name'); + $filter->setNullValue(true); + $expectedArray = array('missing' => array('field' => 'field_name', 'null_value' => true)); + $this->assertEquals($expectedArray, $filter->toArray()); + } + + /** + * @group unit + */ + public function testSetField() + { + $filter = new Missing('field_name'); + + $this->assertEquals('field_name', $filter->getParam('field')); + + $filter->setField('new_field_name'); + $this->assertEquals('new_field_name', $filter->getParam('field')); + + $returnValue = $filter->setField('very_new_field_name'); + $this->assertInstanceOf('Elastica\Filter\Missing', $returnValue); + } + + /** + * @group unit + */ + public function testSetExistence() + { + $filter = new Missing('field_name'); + + $filter->setExistence(true); + $this->assertTrue($filter->getParam('existence')); + + $filter->setExistence(false); + $this->assertFalse($filter->getParam('existence')); + + $returnValue = $filter->setExistence(true); + $this->assertInstanceOf('Elastica\Filter\Missing', $returnValue); + } + + /** + * @group unit + */ + public function testSetNullValue() + { + $filter = new Missing('field_name'); + + $filter->setNullValue(true); + $this->assertTrue($filter->getParam('null_value')); + + $filter->setNullValue(false); + $this->assertFalse($filter->getParam('null_value')); + + $returnValue = $filter->setNullValue(true); + $this->assertInstanceOf('Elastica\Filter\Missing', $returnValue); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/MultiTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/MultiTest.php new file mode 100644 index 00000000..cc8a56e8 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/MultiTest.php @@ -0,0 +1,109 @@ +getStub(); + + $this->assertEmpty($stub->getFilters()); + } + + /** + * @group unit + */ + public function testAddFilter() + { + $stub = $this->getStub(); + + $filter = new MatchAll(); + $stub->addFilter($filter); + + $expected = array( + $filter->toArray(), + ); + + $this->assertEquals($expected, $stub->getFilters()); + } + + /** + * @group unit + */ + public function testSetFilters() + { + $stub = $this->getStub(); + + $filter = new MatchAll(); + $stub->setFilters(array($filter)); + + $expected = array( + $filter->toArray(), + ); + + $this->assertEquals($expected, $stub->getFilters()); + } + + /** + * @group unit + */ + public function testToArray() + { + $stub = $this->getStub(); + + $filter = new MatchAll(); + $stub->addFilter($filter); + + $expected = array( + $stub->getBaseName() => array( + $filter->toArray(), + ), + ); + + $this->assertEquals($expected, $stub->toArray()); + } + + /** + * @group unit + */ + public function testToArrayWithParam() + { + $stub = $this->getStub(); + + $stub->setCached(true); + + $filter = new MatchAll(); + $stub->addFilter($filter); + + $expected = array( + $stub->getBaseName() => array( + '_cache' => true, + 'filters' => array( + $filter->toArray(), + ), + ), + ); + + $this->assertEquals($expected, $stub->toArray()); + } + + private function getStub() + { + return $this->getMockForAbstractClass('Elastica\Test\Filter\AbstractMultiDebug'); + } +} + +class AbstractMultiDebug extends AbstractMulti +{ + public function getBaseName() + { + return parent::_getBaseName(); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/NestedFilterWithSetFilterTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/NestedFilterWithSetFilterTest.php new file mode 100644 index 00000000..96a49a77 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/NestedFilterWithSetFilterTest.php @@ -0,0 +1,110 @@ +_createIndex(); + $type = $index->getType('user'); + + $type->setMapping(new Mapping(null, array( + 'firstname' => array('type' => 'string', 'store' => 'yes'), + // default is store => no expected + 'lastname' => array('type' => 'string'), + 'hobbies' => array( + 'type' => 'nested', + 'include_in_parent' => true, + 'properties' => array('hobby' => array('type' => 'string')), + ), + ))); + + $type->addDocuments(array( + new Document(1, array( + 'firstname' => 'Nicolas', + 'lastname' => 'Ruflin', + 'hobbies' => array( + array('hobby' => 'opensource'), + ), + )), + new Document(2, array( + 'firstname' => 'Nicolas', + 'lastname' => 'Ippolito', + 'hobbies' => array( + array('hobby' => 'opensource'), + array('hobby' => 'guitar'), + ), + )), + )); + + $index->refresh(); + + return $index; + } + + /** + * @group unit + */ + public function testToArray() + { + $filter = new Nested(); + $this->assertEquals(array('nested' => array()), $filter->toArray()); + $query = new Terms(); + $query->setTerms('hobby', array('guitar')); + $filter->setPath('hobbies'); + $filter->setFilter($query); + + $expectedArray = array( + 'nested' => array( + 'path' => 'hobbies', + 'filter' => array('terms' => array( + 'hobby' => array('guitar'), + )), + ), + ); + + $this->assertEquals($expectedArray, $filter->toArray()); + } + + /** + * @group functional + */ + public function testShouldReturnTheRightNumberOfResult() + { + $filter = new Nested(); + $this->assertEquals(array('nested' => array()), $filter->toArray()); + $query = new Terms(); + $query->setTerms('hobby', array('guitar')); + $filter->setPath('hobbies'); + $filter->setFilter($query); + + $client = $this->_getClient(); + $search = new Search($client); + $index = $this->_getIndexForTest(); + $search->addIndex($index); + $resultSet = $search->search($filter); + + $this->assertEquals(1, $resultSet->getTotalHits()); + + $filter = new Nested(); + $this->assertEquals(array('nested' => array()), $filter->toArray()); + $query = new Terms(); + $query->setTerms('hobby', array('opensource')); + $filter->setPath('hobbies'); + $filter->setFilter($query); + + $client = $this->_getClient(); + $search = new Search($client); + $index = $this->_getIndexForTest(); + $search->addIndex($index); + $resultSet = $search->search($filter); + $this->assertEquals(2, $resultSet->getTotalHits()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/NestedTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/NestedTest.php new file mode 100644 index 00000000..8eb42d37 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/NestedTest.php @@ -0,0 +1,127 @@ +_createIndex('elastica_test_filter_nested'); + $type = $index->getType('user'); + $mapping = new Mapping(); + $mapping->setProperties( + array( + 'firstname' => array('type' => 'string', 'store' => 'yes'), + // default is store => no expected + 'lastname' => array('type' => 'string'), + 'hobbies' => array( + 'type' => 'nested', + 'include_in_parent' => true, + 'properties' => array('hobby' => array('type' => 'string')), + ), + ) + ); + $type->setMapping($mapping); + + $response = $type->addDocuments(array( + new Document(1, + array( + 'firstname' => 'Nicolas', + 'lastname' => 'Ruflin', + 'hobbies' => array( + array('hobby' => 'opensource'), + ), + ) + ), + new Document(2, + array( + 'firstname' => 'Nicolas', + 'lastname' => 'Ippolito', + 'hobbies' => array( + array('hobby' => 'opensource'), + array('hobby' => 'guitar'), + ), + ) + ), + )); + + $index->refresh(); + + return $index; + } + + /** + * @group unit + */ + public function testToArray() + { + $filter = new Nested(); + $this->assertEquals(array('nested' => array()), $filter->toArray()); + $query = new Terms(); + $query->setTerms('hobby', array('guitar')); + $filter->setPath('hobbies'); + $filter->setQuery($query); + + $expectedArray = array( + 'nested' => array( + 'path' => 'hobbies', + 'query' => array('terms' => array( + 'hobby' => array('guitar'), + )), + ), + ); + + $this->assertEquals($expectedArray, $filter->toArray()); + } + + /** + * @group functional + */ + public function testShouldReturnTheRightNumberOfResult() + { + $filter = new Nested(); + $this->assertEquals(array('nested' => array()), $filter->toArray()); + $query = new Terms(); + $query->setTerms('hobby', array('guitar')); + $filter->setPath('hobbies'); + $filter->setQuery($query); + + $search = new Search($this->_getClient()); + $search->addIndex($this->_getIndexForTest()); + $resultSet = $search->search($filter); + $this->assertEquals(1, $resultSet->getTotalHits()); + + $filter = new Nested(); + $this->assertEquals(array('nested' => array()), $filter->toArray()); + $query = new Terms(); + $query->setTerms('hobby', array('opensource')); + $filter->setPath('hobbies'); + $filter->setQuery($query); + + $search = new Search($this->_getClient()); + $search->addIndex($this->_getIndexForTest()); + $resultSet = $search->search($filter); + $this->assertEquals(2, $resultSet->getTotalHits()); + } + + /** + * @group unit + */ + public function testSetJoin() + { + $filter = new Nested(); + + $this->assertTrue($filter->setJoin(true)->getParam('join')); + + $this->assertFalse($filter->setJoin(false)->getParam('join')); + + $returnValue = $filter->setJoin(true); + $this->assertInstanceOf('Elastica\Filter\Nested', $returnValue); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/NumericRangeTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/NumericRangeTest.php new file mode 100644 index 00000000..590b5137 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/NumericRangeTest.php @@ -0,0 +1,37 @@ +addField('fieldName', array('to' => 'value')); + $this->assertInstanceOf('Elastica\Filter\NumericRange', $returnValue); + } + + /** + * @group unit + */ + public function testToArray() + { + $filter = new NumericRange(); + + $fromTo = array('from' => 'ra', 'to' => 'ru'); + $filter->addField('name', $fromTo); + + $expectedArray = array( + 'numeric_range' => array( + 'name' => $fromTo, + ), + ); + + $this->assertEquals($expectedArray, $filter->toArray()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/PrefixTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/PrefixTest.php new file mode 100644 index 00000000..aab37615 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/PrefixTest.php @@ -0,0 +1,138 @@ + array( + $field => $prefix, + ), + ); + + $this->assertequals($expectedArray, $filter->toArray()); + } + + /** + * @group functional + */ + public function testDifferentPrefixes() + { + $client = $this->_getClient(); + $index = $client->getIndex('test'); + + $index->create(array(), true); + $type = $index->getType('test'); + + $mapping = new Mapping($type, array( + 'name' => array('type' => 'string', 'store' => 'no', 'index' => 'not_analyzed'), + ) + ); + $type->setMapping($mapping); + $type->addDocuments(array( + new Document(1, array('name' => 'Basel-Stadt')), + new Document(2, array('name' => 'New York')), + new Document(3, array('name' => 'Baden')), + new Document(4, array('name' => 'Baden Baden')), + new Document(5, array('name' => 'New Orleans')), + )); + + $index->refresh(); + + $query = new Prefix('name', 'Ba'); + $resultSet = $index->search($query); + $this->assertEquals(3, $resultSet->count()); + + // Lower case should not return a result + $query = new Prefix('name', 'ba'); + $resultSet = $index->search($query); + $this->assertEquals(0, $resultSet->count()); + + $query = new Prefix('name', 'Baden'); + $resultSet = $index->search($query); + $this->assertEquals(2, $resultSet->count()); + + $query = new Prefix('name', 'Baden B'); + $resultSet = $index->search($query); + $this->assertEquals(1, $resultSet->count()); + + $query = new Prefix('name', 'Baden Bas'); + $resultSet = $index->search($query); + $this->assertEquals(0, $resultSet->count()); + } + + /** + * @group functional + */ + public function testDifferentPrefixesLowercase() + { + $client = $this->_getClient(); + $index = $client->getIndex('test'); + + $indexParams = array( + 'analysis' => array( + 'analyzer' => array( + 'lw' => array( + 'type' => 'custom', + 'tokenizer' => 'keyword', + 'filter' => array('lowercase'), + ), + ), + ), + ); + + $index->create($indexParams, true); + $type = $index->getType('test'); + + $mapping = new Mapping($type, array( + 'name' => array('type' => 'string', 'store' => 'no', 'analyzer' => 'lw'), + ) + ); + $type->setMapping($mapping); + + $type->addDocuments(array( + new Document(1, array('name' => 'Basel-Stadt')), + new Document(2, array('name' => 'New York')), + new Document(3, array('name' => 'Baden')), + new Document(4, array('name' => 'Baden Baden')), + new Document(5, array('name' => 'New Orleans')), + )); + + $index->refresh(); + + $query = new Prefix('name', 'ba'); + $resultSet = $index->search($query); + $this->assertEquals(3, $resultSet->count()); + + // Upper case should not return a result + $query = new Prefix('name', 'Ba'); + $resultSet = $index->search($query); + $this->assertEquals(0, $resultSet->count()); + + $query = new Prefix('name', 'baden'); + $resultSet = $index->search($query); + $this->assertEquals(2, $resultSet->count()); + + $query = new Prefix('name', 'baden b'); + $resultSet = $index->search($query); + $this->assertEquals(1, $resultSet->count()); + + $query = new Prefix('name', 'baden bas'); + $resultSet = $index->search($query); + $this->assertEquals(0, $resultSet->count()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/QueryTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/QueryTest.php new file mode 100644 index 00000000..23754510 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/QueryTest.php @@ -0,0 +1,51 @@ + array( + 'query_string' => array( + 'query' => 'foo bar', + ), + ), + ); + + $this->assertEquals($expected, $filter->toArray()); + } + + /** + * @group unit + */ + public function testExtended() + { + $query = new QueryString('foo bar'); + $filter = new Query($query); + $filter->setCached(true); + + $expected = array( + 'fquery' => array( + 'query' => array( + 'query_string' => array( + 'query' => 'foo bar', + ), + ), + '_cache' => true, + ), + ); + + $this->assertEquals($expected, $filter->toArray()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/RangeTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/RangeTest.php new file mode 100644 index 00000000..b7cad3ac --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/RangeTest.php @@ -0,0 +1,61 @@ +addField('fieldName', array('to' => 'value')); + $this->assertInstanceOf('Elastica\Filter\Range', $returnValue); + } + + /** + * @group unit + */ + public function testToArray() + { + $field = 'field_name'; + $range = array('gte' => 10, 'lte' => 99); + + $filter = new Range(); + $filter->addField($field, $range); + $expectedArray = array('range' => array($field => $range)); + $this->assertEquals($expectedArray, $filter->toArray()); + } + + /** + * @group unit + */ + public function testSetExecution() + { + $field = 'field_name'; + $range = array('gte' => 10, 'lte' => 99); + $filter = new Range('field_name', $range); + + $filter->setExecution('fielddata'); + $this->assertEquals('fielddata', $filter->getParam('execution')); + + $returnValue = $filter->setExecution('index'); + $this->assertInstanceOf('Elastica\Filter\Range', $returnValue); + } + + /** + * Tests that parent fields are not overwritten by the toArray method. + * + * @group unit + */ + public function testSetCachedNotOverwritten() + { + $filter = new Range('field_name', array()); + $filter->setCached(true); + $array = $filter->toArray(); + $this->assertTrue($array['range']['_cache']); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/RegexpTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/RegexpTest.php new file mode 100644 index 00000000..6e3a0395 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/RegexpTest.php @@ -0,0 +1,162 @@ + array( + $field => $regexp, + ), + ); + + $this->assertequals($expectedArray, $filter->toArray()); + } + + /** + * @group unit + */ + public function testToArrayWithOptions() + { + $field = 'name'; + $regexp = 'ruf'; + $options = array( + 'flags' => 'ALL', + ); + + $filter = new Regexp($field, $regexp, $options); + + $expectedArray = array( + 'regexp' => array( + $field => array( + 'value' => $regexp, + 'flags' => 'ALL', + ), + ), + ); + + $this->assertequals($expectedArray, $filter->toArray()); + } + + /** + * @group functional + */ + public function testDifferentRegexp() + { + $client = $this->_getClient(); + $index = $client->getIndex('test'); + + $index->create(array(), true); + $type = $index->getType('test'); + + $mapping = new Mapping($type, array( + 'name' => array('type' => 'string', 'store' => 'no', 'index' => 'not_analyzed'), + ) + ); + $type->setMapping($mapping); + $type->addDocuments(array( + new Document(1, array('name' => 'Basel-Stadt')), + new Document(2, array('name' => 'New York')), + new Document(3, array('name' => 'Baden')), + new Document(4, array('name' => 'Baden Baden')), + new Document(5, array('name' => 'New Orleans')), + )); + + $index->refresh(); + + $query = new Regexp('name', 'Ba.*'); + $resultSet = $index->search($query); + $this->assertEquals(3, $resultSet->count()); + + // Lower case should not return a result + $query = new Regexp('name', 'ba.*'); + $resultSet = $index->search($query); + $this->assertEquals(0, $resultSet->count()); + + $query = new Regexp('name', 'Baden.*'); + $resultSet = $index->search($query); + $this->assertEquals(2, $resultSet->count()); + + $query = new Regexp('name', 'Baden B.*'); + $resultSet = $index->search($query); + $this->assertEquals(1, $resultSet->count()); + + $query = new Regexp('name', 'Baden Bas.*'); + $resultSet = $index->search($query); + $this->assertEquals(0, $resultSet->count()); + } + + /** + * @group functional + */ + public function testDifferentRegexpLowercase() + { + $client = $this->_getClient(); + $index = $client->getIndex('test'); + + $indexParams = array( + 'analysis' => array( + 'analyzer' => array( + 'lw' => array( + 'type' => 'custom', + 'tokenizer' => 'keyword', + 'filter' => array('lowercase'), + ), + ), + ), + ); + + $index->create($indexParams, true); + $type = $index->getType('test'); + + $mapping = new Mapping($type, array( + 'name' => array('type' => 'string', 'store' => 'no', 'analyzer' => 'lw'), + ) + ); + $type->setMapping($mapping); + $type->addDocuments(array( + new Document(1, array('name' => 'Basel-Stadt')), + new Document(2, array('name' => 'New York')), + new Document(3, array('name' => 'Baden')), + new Document(4, array('name' => 'Baden Baden')), + new Document(5, array('name' => 'New Orleans')), + )); + + $index->refresh(); + + $query = new Regexp('name', 'ba.*'); + $resultSet = $index->search($query); + $this->assertEquals(3, $resultSet->count()); + + // Upper case should not return a result + $query = new Regexp('name', 'Ba.*'); + $resultSet = $index->search($query); + $this->assertEquals(0, $resultSet->count()); + + $query = new Regexp('name', 'baden.*'); + $resultSet = $index->search($query); + $this->assertEquals(2, $resultSet->count()); + + $query = new Regexp('name', 'baden b.*'); + $resultSet = $index->search($query); + $this->assertEquals(1, $resultSet->count()); + + $query = new Regexp('name', 'baden bas.*'); + $resultSet = $index->search($query); + $this->assertEquals(0, $resultSet->count()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/ScriptTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/ScriptTest.php new file mode 100644 index 00000000..fdec0381 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/ScriptTest.php @@ -0,0 +1,57 @@ +toArray(); + $this->assertInternalType('array', $array); + + $expected = array( + 'script' => array( + 'script' => $string, + ), + ); + $this->assertEquals($expected, $array); + } + + /** + * @group unit + */ + public function testSetScript() + { + $string = '_score * 2.0'; + $params = array( + 'param1' => 'one', + 'param2' => 1, + ); + $lang = 'mvel'; + $script = new Script($string, $params, $lang); + + $filter = new ScriptFilter(); + $filter->setScript($script); + + $array = $filter->toArray(); + + $expected = array( + 'script' => array( + 'script' => $string, + 'params' => $params, + 'lang' => $lang, + ), + ); + $this->assertEquals($expected, $array); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/TermTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/TermTest.php new file mode 100644 index 00000000..9c8f5a2a --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/TermTest.php @@ -0,0 +1,24 @@ +setTerm($key, $value); + + $data = $query->toArray(); + + $this->assertInternalType('array', $data['term']); + $this->assertEquals(array($key => $value), $data['term']); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/TermsTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/TermsTest.php new file mode 100644 index 00000000..bb37ba96 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/TermsTest.php @@ -0,0 +1,129 @@ +_createIndex(); + $type1 = $index->getType('musicians'); + $type2 = $index->getType('bands'); + + //index some test data + $type1->addDocuments(array( + new Document(1, array('name' => 'robert', 'lastName' => 'plant')), + new Document(2, array('name' => 'jimmy', 'lastName' => 'page')), + new Document(3, array('name' => 'john paul', 'lastName' => 'jones')), + new Document(4, array('name' => 'john', 'lastName' => 'bonham')), + new Document(5, array('name' => 'jimi', 'lastName' => 'hendrix')), + )); + + $type2->addDocument(new Document('led zeppelin', array('members' => array('plant', 'page', 'jones', 'bonham')))); + $index->refresh(); + + //use the terms lookup feature to query for some data + $termsFilter = new Terms(); + $termsFilter->setLookup('lastName', $type2, 'led zeppelin', 'members', null); + $query = new Query(); + $query->setPostFilter($termsFilter); + $results = $index->search($query); + $this->assertEquals($results->count(), 4, 'Terms lookup with null index'); + + $termsFilter->setLookup('lastName', $type2, 'led zeppelin', 'members', $index); + $query->setPostFilter($termsFilter); + $results = $index->search($query); + $this->assertEquals($results->count(), 4, 'Terms lookup with index as object'); + + //Query with index given as string + $termsFilter->setLookup('lastName', $type2, 'led zeppelin', 'members', $index->getName()); + $query->setPostFilter($termsFilter); + $results = $index->search($query); + $this->assertEquals($results->count(), 4, 'Terms lookup with index as string'); + + //Query with array of options + $termsFilter->setLookup('lastName', $type2, 'led zeppelin', 'members', array('index' => $index, 'cache' => false)); + $query->setPostFilter($termsFilter); + $results = $index->search($query); + $this->assertEquals($results->count(), 4, 'Terms lookup with options array'); + + $index->delete(); + } + + /** + * @group unit + */ + public function testSetExecution() + { + $filter = new Terms('color', array('blue', 'green')); + + $filter->setExecution('bool'); + $this->assertEquals('bool', $filter->getParam('execution')); + + $returnValue = $filter->setExecution('bool'); + $this->assertInstanceOf('Elastica\Filter\Terms', $returnValue); + } + + /** + * @group unit + */ + public function testSetTerms() + { + $field = 'color'; + $terms = array('blue', 'green'); + + $filter = new Terms(); + $filter->setTerms($field, $terms); + $expected = array('terms' => array($field => $terms)); + $this->assertEquals($expected, $filter->toArray()); + + $returnValue = $filter->setTerms($field, $terms); + $this->assertInstanceOf('Elastica\Filter\Terms', $returnValue); + } + + /** + * @group unit + */ + public function testAddTerm() + { + $filter = new Terms('color', array('blue')); + + $filter->addTerm('green'); + $expected = array('terms' => array('color' => array('blue', 'green'))); + $this->assertEquals($expected, $filter->toArray()); + + $returnValue = $filter->addTerm('cyan'); + $this->assertInstanceOf('Elastica\Filter\Terms', $returnValue); + } + + /** + * @group unit + */ + public function testToArray() + { + $filter = new Terms('color', array()); + $expected = array('terms' => array('color' => array())); + $this->assertEquals($expected, $filter->toArray()); + + $filter = new Terms('color', array('cyan')); + $expected = array('terms' => array('color' => array('cyan'))); + $this->assertEquals($expected, $filter->toArray()); + } + + /** + * @group unit + * @expectedException \Elastica\Exception\InvalidException + */ + public function testToArrayInvalidException() + { + $filter = new Terms(); + $filter->toArray(); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/TypeTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/TypeTest.php new file mode 100644 index 00000000..8da19273 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/TypeTest.php @@ -0,0 +1,32 @@ +setType('type_name'); + $this->assertInstanceOf('Elastica\Filter\Type', $returnValue); + } + + /** + * @group unit + */ + public function testToArray() + { + $typeFilter = new Type('type_name'); + + $expectedArray = array( + 'type' => array('value' => 'type_name'), + ); + + $this->assertEquals($expectedArray, $typeFilter->toArray()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Index/SettingsTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Index/SettingsTest.php new file mode 100644 index 00000000..1562c7a0 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Index/SettingsTest.php @@ -0,0 +1,338 @@ +_getClient(); + $index = $client->getIndex($indexName); + $index->create(array(), true); + $index->refresh(); + $settings = $index->getSettings(); + + $this->assertInternalType('array', $settings->get()); + $this->assertNotNull($settings->get('number_of_replicas')); + $this->assertNotNull($settings->get('number_of_shards')); + $this->assertNull($settings->get('kjqwerjlqwer')); + + $index->delete(); + } + + /** + * @group functional + */ + public function testGetWithAlias() + { + $indexName = 'elasticatest'; + $aliasName = 'elasticatest_alias'; + + $client = $this->_getClient(); + $index = $client->getIndex($indexName); + $index->create(array(), true); + $index->refresh(); + + $index->addAlias($aliasName); + $index = $client->getIndex($aliasName); + $settings = $index->getSettings(); + + $this->assertInternalType('array', $settings->get()); + $this->assertNotNull($settings->get('number_of_replicas')); + $this->assertNotNull($settings->get('number_of_shards')); + $this->assertNull($settings->get('kjqwerjlqwer')); + + $index->delete(); + } + + /** + * @group functional + */ + public function testSetNumberOfReplicas() + { + $indexName = 'test'; + + $client = $this->_getClient(); + $index = $client->getIndex($indexName); + $index->create(array(), true); + $settings = $index->getSettings(); + + $settings->setNumberOfReplicas(2); + $index->refresh(); + $this->assertEquals(2, $settings->get('number_of_replicas')); + + $settings->setNumberOfReplicas(3); + $index->refresh(); + $this->assertEquals(3, $settings->get('number_of_replicas')); + + $index->delete(); + } + + /** + * @group functional + */ + public function testSetRefreshInterval() + { + $indexName = 'test'; + + $client = $this->_getClient(); + $index = $client->getIndex($indexName); + $index->create(array(), true); + + $settings = $index->getSettings(); + + $settings->setRefreshInterval('2s'); + $index->refresh(); + $this->assertEquals('2s', $settings->get('refresh_interval')); + + $settings->setRefreshInterval('5s'); + $index->refresh(); + $this->assertEquals('5s', $settings->get('refresh_interval')); + + $index->delete(); + } + + /** + * @group functional + */ + public function testGetRefreshInterval() + { + $indexName = 'test'; + + $client = $this->_getClient(); + $index = $client->getIndex($indexName); + $index->create(array(), true); + + $settings = $index->getSettings(); + + $this->assertEquals(IndexSettings::DEFAULT_REFRESH_INTERVAL, $settings->getRefreshInterval()); + + $interval = '2s'; + $settings->setRefreshInterval($interval); + $index->refresh(); + $this->assertEquals($interval, $settings->getRefreshInterval()); + $this->assertEquals($interval, $settings->get('refresh_interval')); + + $index->delete(); + } + + /** + * @group functional + */ + public function testSetMergePolicy() + { + $indexName = 'test'; + + $client = $this->_getClient(); + $index = $client->getIndex($indexName); + $index->create(array(), true); + //wait for the shards to be allocated + $this->_waitForAllocation($index); + + $settings = $index->getSettings(); + + $settings->setMergePolicy('expunge_deletes_allowed', 15); + $this->assertEquals(15, $settings->getMergePolicy('expunge_deletes_allowed')); + + $settings->setMergePolicy('expunge_deletes_allowed', 10); + $this->assertEquals(10, $settings->getMergePolicy('expunge_deletes_allowed')); + + $index->delete(); + } + + /** + * @group functional + */ + public function testSetMergeFactor() + { + $indexName = 'test'; + + $client = $this->_getClient(); + $index = $client->getIndex($indexName); + $index->create(array(), true); + + //wait for the shards to be allocated + $this->_waitForAllocation($index); + + $settings = $index->getSettings(); + + $response = $settings->setMergePolicy('merge_factor', 15); + $this->assertEquals(15, $settings->getMergePolicy('merge_factor')); + $this->assertInstanceOf('Elastica\Response', $response); + $this->assertTrue($response->isOk()); + + $settings->setMergePolicy('merge_factor', 10); + $this->assertEquals(10, $settings->getMergePolicy('merge_factor')); + + $index->delete(); + } + + /** + * @group functional + */ + public function testSetMergePolicyType() + { + $indexName = 'test'; + + $client = $this->_getClient(); + $index = $client->getIndex($indexName); + $index->create(array(), true); + + //wait for the shards to be allocated + $this->_waitForAllocation($index); + + $settings = $index->getSettings(); + + $settings->setMergePolicyType('log_byte_size'); + $this->assertEquals('log_byte_size', $settings->getMergePolicyType()); + + $response = $settings->setMergePolicy('merge_factor', 15); + $this->assertEquals(15, $settings->getMergePolicy('merge_factor')); + $this->assertInstanceOf('Elastica\Response', $response); + $this->assertTrue($response->isOk()); + + $index->delete(); + } + + /** + * @group functional + */ + public function testSetReadOnly() + { + $index = $this->_createIndex(); + //wait for the shards to be allocated + $this->_waitForAllocation($index); + $index->getSettings()->setReadOnly(false); + + // Add document to normal index + $doc1 = new Document(null, array('hello' => 'world')); + $doc2 = new Document(null, array('hello' => 'world')); + $doc3 = new Document(null, array('hello' => 'world')); + + $type = $index->getType('test'); + $type->addDocument($doc1); + $this->assertFalse($index->getSettings()->getReadOnly()); + + // Try to add doc to read only index + $index->getSettings()->setReadOnly(true); + $this->assertTrue($index->getSettings()->getReadOnly()); + + try { + $type->addDocument($doc2); + $this->fail('Should throw exception because of read only'); + } catch (ResponseException $e) { + $message = $e->getMessage(); + $this->assertContains('ClusterBlockException', $message); + $this->assertContains('index write', $message); + } + + // Remove read only, add document + $response = $index->getSettings()->setReadOnly(false); + $this->assertTrue($response->isOk()); + + $type->addDocument($doc3); + $index->refresh(); + + $this->assertEquals(2, $type->count()); + + $index->delete(); + } + + /** + * @group functional + */ + public function testGetSetBlocksRead() + { + $index = $this->_createIndex(); + $index->refresh(); + $settings = $index->getSettings(); + + $this->assertFalse($settings->getBlocksRead()); + + $settings->setBlocksRead(true); + $this->assertTrue($settings->getBlocksRead()); + + $settings->setBlocksRead(false); + $this->assertFalse($settings->getBlocksRead()); + + $settings->setBlocksRead(); + $this->assertTrue($settings->getBlocksRead()); + + $index->delete(); + } + + /** + * @group functional + */ + public function testGetSetBlocksWrite() + { + $index = $this->_createIndex(); + $index->refresh(); + $settings = $index->getSettings(); + + $this->assertFalse($settings->getBlocksWrite()); + + $settings->setBlocksWrite(true); + $this->assertTrue($settings->getBlocksWrite()); + + $settings->setBlocksWrite(false); + $this->assertFalse($settings->getBlocksWrite()); + + $settings->setBlocksWrite(); + $this->assertTrue($settings->getBlocksWrite()); + + $index->delete(); + } + + /** + * @group functional + */ + public function testGetSetBlocksMetadata() + { + $index = $this->_createIndex(); + $index->refresh(); + $settings = $index->getSettings(); + + $this->assertFalse($settings->getBlocksMetadata()); + + $settings->setBlocksMetadata(true); + $this->assertTrue($settings->getBlocksMetadata()); + + $settings->setBlocksMetadata(false); + $this->assertFalse($settings->getBlocksMetadata()); + + $settings->setBlocksMetadata(); + $this->assertTrue($settings->getBlocksMetadata()); + + $settings->setBlocksMetadata(false); // Cannot delete index otherwise + $index->delete(); + } + + /** + * @group functional + */ + public function testNotFoundIndex() + { + $client = $this->_getClient(); + $index = $client->getIndex('not_found_index'); + //wait for the shards to be allocated + + try { + $settings = $index->getSettings()->get(); + $this->fail('Should throw exception because of index not found'); + } catch (ResponseException $e) { + $message = $e->getMessage(); + $this->assertContains('IndexMissingException', $message); + } + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Index/StatsTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Index/StatsTest.php new file mode 100644 index 00000000..d0bb7838 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Index/StatsTest.php @@ -0,0 +1,24 @@ +_getClient(); + $index = $client->getIndex($indexName); + $index->create(array(), true); + $stats = $index->getStats(); + $this->assertInstanceOf('Elastica\Index\Stats', $stats); + + $this->assertTrue($stats->getResponse()->isOk()); + $this->assertEquals(0, $stats->get('_all', 'indices', 'test', 'primaries', 'docs', 'count')); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Index/StatusTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Index/StatusTest.php new file mode 100644 index 00000000..24f22ffd --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Index/StatusTest.php @@ -0,0 +1,74 @@ +_getClient(); + $index = $client->getIndex($indexName); + $index->create(array(), true); + + $status = new IndexStatus($index); + + $aliases = $status->getAliases(); + + $this->assertTrue(empty($aliases)); + $this->assertInternalType('array', $aliases); + + $index->addAlias($aliasName); + $status->refresh(); + + $aliases = $status->getAliases(); + + $this->assertTrue(in_array($aliasName, $aliases)); + } + + /** + * @group functional + */ + public function testHasAlias() + { + $indexName = 'test'; + $aliasName = 'test-alias'; + + $client = $this->_getClient(); + $index = $client->getIndex($indexName); + $index->create(array(), true); + + $status = new IndexStatus($index); + + $this->assertFalse($status->hasAlias($aliasName)); + + $index->addAlias($aliasName); + $status->refresh(); + + $this->assertTrue($status->hasAlias($aliasName)); + } + + /** + * @group functional + */ + public function testGetSettings() + { + $indexName = 'test'; + + $client = $this->_getClient(); + $index = $client->getIndex($indexName); + $index->create(array(), true); + $status = $index->getStatus(); + + $settings = $status->getSettings(); + $this->assertInternalType('array', $settings); + $this->assertTrue(isset($settings['index']['number_of_shards'])); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/IndexTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/IndexTest.php new file mode 100644 index 00000000..25ac53c2 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/IndexTest.php @@ -0,0 +1,901 @@ +_createIndex(); + $doc = new Document(1, array('id' => 1, 'email' => 'test@test.com', 'username' => 'hanswurst', 'test' => array('2', '3', '5'))); + + $type = $index->getType('test'); + + $mapping = array('id' => array('type' => 'integer', 'store' => true), 'email' => array('type' => 'string', 'store' => 'no'), + 'username' => array('type' => 'string', 'store' => 'no'), 'test' => array('type' => 'integer', 'store' => 'no'),); + $type->setMapping($mapping); + + $type->addDocument($doc); + $index->optimize(); + + $storedMapping = $index->getMapping(); + + $this->assertEquals($storedMapping['test']['properties']['id']['type'], 'integer'); + $this->assertEquals($storedMapping['test']['properties']['id']['store'], true); + $this->assertEquals($storedMapping['test']['properties']['email']['type'], 'string'); + $this->assertEquals($storedMapping['test']['properties']['username']['type'], 'string'); + $this->assertEquals($storedMapping['test']['properties']['test']['type'], 'integer'); + + $result = $type->search('hanswurst'); + } + + /** + * @group functional + */ + public function testGetMappingAlias() + { + $index = $this->_createIndex(); + $indexName = $index->getName(); + + $aliasName = 'test-mapping-alias'; + $index->addAlias($aliasName); + + $type = new Type($index, 'test'); + $mapping = new Mapping($type, array( + 'id' => array('type' => 'integer', 'store' => 'yes'), + )); + $type->setMapping($mapping); + + $client = $index->getClient(); + + // Index mapping + $mapping1 = $client->getIndex($indexName)->getMapping(); + + // Alias mapping + $mapping2 = $client->getIndex($aliasName)->getMapping(); + + // Make sure, a mapping is set + $this->assertNotEmpty($mapping1); + + // Alias and index mapping should be identical + $this->assertEquals($mapping1, $mapping2); + } + + /** + * @group functional + */ + public function testParent() + { + $index = $this->_createIndex(); + + $typeBlog = new Type($index, 'blog'); + + $typeComment = new Type($index, 'comment'); + + $mapping = new Mapping(); + $mapping->setParam('_parent', array('type' => 'blog')); + $typeComment->setMapping($mapping); + + $entry1 = new Document(1); + $entry1->set('title', 'Hello world'); + $typeBlog->addDocument($entry1); + + $entry2 = new Document(2); + $entry2->set('title', 'Foo bar'); + $typeBlog->addDocument($entry2); + + $entry3 = new Document(3); + $entry3->set('title', 'Till dawn'); + $typeBlog->addDocument($entry3); + + $comment = new Document(1); + $comment->set('author', 'Max'); + $comment->setParent(2); // Entry Foo bar + $typeComment->addDocument($comment); + + $index->optimize(); + + $query = new HasChild('Max', 'comment'); + $resultSet = $typeBlog->search($query); + $this->assertEquals(1, $resultSet->count()); + $this->assertEquals(array('title' => 'Foo bar'), $resultSet->current()->getData()); + } + + /** + * @group functional + */ + public function testAddPdfFile() + { + $this->_checkAttachmentsPlugin(); + $indexMapping = array('file' => array('type' => 'attachment', 'store' => 'no'), 'text' => array('type' => 'string', 'store' => 'no')); + + $indexParams = array('index' => array('number_of_shards' => 1, 'number_of_replicas' => 0)); + + $index = $this->_createIndex(); + $type = new Type($index, 'test'); + + $index->create($indexParams, true); + $type->setMapping($indexMapping); + + $doc1 = new Document(1); + $doc1->addFile('file', BASE_PATH.'/data/test.pdf', 'application/pdf'); + $doc1->set('text', 'basel world'); + $type->addDocument($doc1); + + $doc2 = new Document(2); + $doc2->set('text', 'running in basel'); + $type->addDocument($doc2); + + $index->optimize(); + + $resultSet = $type->search('xodoa'); + $this->assertEquals(1, $resultSet->count()); + + $resultSet = $type->search('basel'); + $this->assertEquals(2, $resultSet->count()); + + // Author is ruflin + $resultSet = $type->search('ruflin'); + $this->assertEquals(1, $resultSet->count()); + + // String does not exist in file + $resultSet = $type->search('guschti'); + $this->assertEquals(0, $resultSet->count()); + } + + /** + * @group functional + */ + public function testAddPdfFileContent() + { + $this->_checkAttachmentsPlugin(); + $indexMapping = array('file' => array('type' => 'attachment', 'store' => 'no'), 'text' => array('type' => 'string', 'store' => 'no')); + + $indexParams = array('index' => array('number_of_shards' => 1, 'number_of_replicas' => 0)); + + $index = $this->_createIndex(); + $type = new Type($index, 'test'); + + $index->create($indexParams, true); + $type->setMapping($indexMapping); + + $doc1 = new Document(1); + $doc1->addFileContent('file', file_get_contents(BASE_PATH.'/data/test.pdf')); + $doc1->set('text', 'basel world'); + $type->addDocument($doc1); + + $doc2 = new Document(2); + $doc2->set('text', 'running in basel'); + $type->addDocument($doc2); + + $index->optimize(); + + $resultSet = $type->search('xodoa'); + $this->assertEquals(1, $resultSet->count()); + + $resultSet = $type->search('basel'); + $this->assertEquals(2, $resultSet->count()); + + // Author is ruflin + $resultSet = $type->search('ruflin'); + $this->assertEquals(1, $resultSet->count()); + + // String does not exist in file + $resultSet = $type->search('guschti'); + $this->assertEquals(0, $resultSet->count()); + } + + /** + * @group functional + */ + public function testAddWordxFile() + { + $this->_checkAttachmentsPlugin(); + $indexMapping = array('file' => array('type' => 'attachment'), 'text' => array('type' => 'string', 'store' => 'no')); + + $indexParams = array('index' => array('number_of_shards' => 1, 'number_of_replicas' => 0)); + + $index = $this->_createIndex(); + $type = new Type($index, 'content'); + + $index->create($indexParams, true); + $type->setMapping($indexMapping); + + $doc1 = new Document(1); + $doc1->addFile('file', BASE_PATH.'/data/test.docx'); + $doc1->set('text', 'basel world'); + $type->addDocument($doc1); + + $index->optimize(); + $index->refresh(); + + $doc2 = new Document(2); + $doc2->set('text', 'running in basel'); + $type->addDocument($doc2); + + $index->optimize(); + $index->refresh(); + + $resultSet = $type->search('basel'); + $this->assertEquals(2, $resultSet->count()); + + $resultSet = $type->search('ruflin'); + $this->assertEquals(0, $resultSet->count()); + + $resultSet = $type->search('xodoa'); + $this->assertEquals(1, $resultSet->count()); + } + + /** + * @group functional + */ + public function testExcludeFileSource() + { + $this->_checkAttachmentsPlugin(); + $indexMapping = array('file' => array('type' => 'attachment', 'store' => 'yes'), 'text' => array('type' => 'string', 'store' => 'yes'), + 'title' => array('type' => 'string', 'store' => 'yes'),); + + $indexParams = array('index' => array('number_of_shards' => 1, 'number_of_replicas' => 0)); + + $index = $this->_createIndex(); + $type = new Type($index, 'content'); + + $mapping = Mapping::create($indexMapping); + $mapping->setSource(array('excludes' => array('file'))); + + $mapping->setType($type); + + $index->create($indexParams, true); + $type->setMapping($mapping); + + $docId = 1; + $text = 'Basel World'; + $title = 'No Title'; + + $doc1 = new Document($docId); + $doc1->addFile('file', BASE_PATH.'/data/test.docx'); + $doc1->set('text', $text); + $doc1->set('title', $title); + $type->addDocument($doc1); + + // Optimization necessary, as otherwise source still in realtime get + $index->optimize(); + + $data = $type->getDocument($docId)->getData(); + $this->assertEquals($data['title'], $title); + $this->assertEquals($data['text'], $text); + $this->assertFalse(isset($data['file'])); + } + + /** + * @group functional + * @expectedException \Elastica\Exception\ResponseException + */ + public function testAddRemoveAlias() + { + $client = $this->_getClient(); + + $indexName1 = 'test1'; + $aliasName = 'test-alias'; + $typeName = 'test'; + + $index = $client->getIndex($indexName1); + $index->create(array('index' => array('number_of_shards' => 1, 'number_of_replicas' => 0)), true); + + $doc = new Document(1, array('id' => 1, 'email' => 'test@test.com', 'username' => 'ruflin')); + + $type = $index->getType($typeName); + $type->addDocument($doc); + $index->refresh(); + + $resultSet = $type->search('ruflin'); + + $this->assertEquals(1, $resultSet->count()); + + $data = $index->addAlias($aliasName, true)->getData(); + $this->assertTrue($data['acknowledged']); + + $index2 = $client->getIndex($aliasName); + $type2 = $index2->getType($typeName); + + $resultSet2 = $type2->search('ruflin'); + $this->assertEquals(1, $resultSet2->count()); + + $response = $index->removeAlias($aliasName)->getData(); + $this->assertTrue($response['acknowledged']); + + $client->getIndex($aliasName)->getType($typeName)->search('ruflin'); + } + + /** + * @group functional + */ + public function testCount() + { + $index = $this->_createIndex(); + + // Add document to normal index + $doc1 = new Document(null, array('name' => 'ruflin')); + $doc2 = new Document(null, array('name' => 'nicolas')); + + $type = $index->getType('test'); + $type->addDocument($doc1); + $type->addDocument($doc2); + + $index->refresh(); + + $this->assertEquals(2, $index->count()); + + $query = new Term(); + $key = 'name'; + $value = 'nicolas'; + $query->setTerm($key, $value); + + $this->assertEquals(1, $index->count($query)); + } + + /** + * @group functional + */ + public function testDeleteByQueryWithQueryString() + { + $index = $this->_createIndex(); + $type1 = new Type($index, 'test1'); + $type1->addDocument(new Document(1, array('name' => 'ruflin nicolas'))); + $type1->addDocument(new Document(2, array('name' => 'ruflin'))); + $type2 = new Type($index, 'test2'); + $type2->addDocument(new Document(1, array('name' => 'ruflin nicolas'))); + $type2->addDocument(new Document(2, array('name' => 'ruflin'))); + $index->refresh(); + + $response = $index->search('ruflin*'); + $this->assertEquals(4, $response->count()); + + $response = $index->search('nicolas'); + $this->assertEquals(2, $response->count()); + + // Delete first document + $response = $index->deleteByQuery('nicolas'); + $this->assertTrue($response->isOk()); + + $index->refresh(); + + // Makes sure, document is deleted + $response = $index->search('ruflin*'); + $this->assertEquals(2, $response->count()); + + $response = $index->search('nicolas'); + $this->assertEquals(0, $response->count()); + } + + /** + * @group functional + */ + public function testDeleteByQueryWithQuery() + { + $index = $this->_createIndex(); + $type1 = new Type($index, 'test1'); + $type1->addDocument(new Document(1, array('name' => 'ruflin nicolas'))); + $type1->addDocument(new Document(2, array('name' => 'ruflin'))); + $type2 = new Type($index, 'test2'); + $type2->addDocument(new Document(1, array('name' => 'ruflin nicolas'))); + $type2->addDocument(new Document(2, array('name' => 'ruflin'))); + $index->refresh(); + + $response = $index->search('ruflin*'); + $this->assertEquals(4, $response->count()); + + $response = $index->search('nicolas'); + $this->assertEquals(2, $response->count()); + + // Delete first document + $response = $index->deleteByQuery(new SimpleQueryString('nicolas')); + $this->assertTrue($response->isOk()); + + $index->refresh(); + + // Makes sure, document is deleted + $response = $index->search('ruflin*'); + $this->assertEquals(2, $response->count()); + + $response = $index->search('nicolas'); + $this->assertEquals(0, $response->count()); + } + + /** + * @group functional + */ + public function testDeleteByQueryWithQueryAndOptions() + { + $index = $this->_createIndex(null, true, 2); + $type1 = new Type($index, 'test1'); + $type1->addDocument(new Document(1, array('name' => 'ruflin nicolas'))); + $type1->addDocument(new Document(2, array('name' => 'ruflin'))); + $type2 = new Type($index, 'test2'); + $type2->addDocument(new Document(1, array('name' => 'ruflin nicolas'))); + $type2->addDocument(new Document(2, array('name' => 'ruflin'))); + $index->refresh(); + + $response = $index->search('ruflin*'); + $this->assertEquals(4, $response->count()); + + $response = $index->search('nicolas'); + $this->assertEquals(2, $response->count()); + + // Route to the wrong document id; should not delete + $response = $index->deleteByQuery(new SimpleQueryString('nicolas'), array('routing' => '2')); + $this->assertTrue($response->isOk()); + + $index->refresh(); + + $response = $index->search('ruflin*'); + $this->assertEquals(4, $response->count()); + + $response = $index->search('nicolas'); + $this->assertEquals(2, $response->count()); + + // Delete first document + $response = $index->deleteByQuery(new SimpleQueryString('nicolas'), array('routing' => '1')); + $this->assertTrue($response->isOk()); + + $index->refresh(); + + // Makes sure, document is deleted + $response = $index->search('ruflin*'); + $this->assertEquals(2, $response->count()); + + $response = $index->search('nicolas'); + $this->assertEquals(0, $response->count()); + } + + /** + * @group functional + */ + public function testDeleteIndexDeleteAlias() + { + $indexName = 'test'; + $aliasName = 'test-aliase'; + + $client = $this->_getClient(); + $index = $client->getIndex($indexName); + + $index->create(array(), true); + $index->addAlias($aliasName); + + $status = new Status($client); + $this->assertTrue($status->indexExists($indexName)); + $this->assertTrue($status->aliasExists($aliasName)); + + // Deleting index should also remove alias + $index->delete(); + + $status->refresh(); + $this->assertFalse($status->indexExists($indexName)); + $this->assertFalse($status->aliasExists($aliasName)); + } + + /** + * @group functional + */ + public function testAddAliasTwoIndices() + { + $indexName1 = 'test1'; + $indexName2 = 'test2'; + $aliasName = 'test-alias'; + + $client = $this->_getClient(); + $index1 = $client->getIndex($indexName1); + $index2 = $client->getIndex($indexName2); + + $index1->create(array(), true); + $this->_waitForAllocation($index1); + $index1->addAlias($aliasName); + $index2->create(array(), true); + $this->_waitForAllocation($index2); + + $index1->refresh(); + $index2->refresh(); + $index1->optimize(); + $index2->optimize(); + + $status = new Status($client); + + $this->assertTrue($status->indexExists($indexName1)); + $this->assertTrue($status->indexExists($indexName2)); + + $this->assertTrue($status->aliasExists($aliasName)); + $this->assertTrue($index1->getStatus()->hasAlias($aliasName)); + $this->assertFalse($index2->getStatus()->hasAlias($aliasName)); + + $index2->addAlias($aliasName); + $this->assertTrue($index1->getStatus()->hasAlias($aliasName)); + $this->assertTrue($index2->getStatus()->hasAlias($aliasName)); + } + + /** + * @group functional + */ + public function testReplaceAlias() + { + $indexName1 = 'test1'; + $indexName2 = 'test2'; + $aliasName = 'test-alias'; + + $client = $this->_getClient(); + $index1 = $client->getIndex($indexName1); + $index2 = $client->getIndex($indexName2); + + $index1->create(array(), true); + $index1->addAlias($aliasName); + $index2->create(array(), true); + + $index1->refresh(); + $index2->refresh(); + + $status = new Status($client); + + $this->assertTrue($status->indexExists($indexName1)); + $this->assertTrue($status->indexExists($indexName2)); + $this->assertTrue($status->aliasExists($aliasName)); + $this->assertTrue($index1->getStatus()->hasAlias($aliasName)); + $this->assertFalse($index2->getStatus()->hasAlias($aliasName)); + + $index2->addAlias($aliasName, true); + $this->assertFalse($index1->getStatus()->hasAlias($aliasName)); + $this->assertTrue($index2->getStatus()->hasAlias($aliasName)); + } + + /** + * @group functional + */ + public function testAddDocumentVersion() + { + $client = $this->_getClient(); + $index = $client->getIndex('test'); + $index->create(array(), true); + $type = new Type($index, 'test'); + + $doc1 = new Document(1); + $doc1->set('title', 'Hello world'); + + $return = $type->addDocument($doc1); + $data = $return->getData(); + $this->assertEquals(1, $data['_version']); + + $return = $type->addDocument($doc1); + $data = $return->getData(); + $this->assertEquals(2, $data['_version']); + } + + /** + * @group functional + */ + public function testClearCache() + { + $index = $this->_createIndex(); + $response = $index->clearCache(); + $this->assertFalse($response->hasError()); + } + + /** + * @group functional + */ + public function testFlush() + { + $index = $this->_createIndex(); + $response = $index->flush(); + $this->assertFalse($response->hasError()); + } + + /** + * @group functional + */ + public function testExists() + { + $index = $this->_createIndex(); + + $this->assertTrue($index->exists()); + + $index->delete(); + + $this->assertFalse($index->exists()); + } + + /** + * Test $index->delete() return value for unknown index. + * + * Tests if deleting an index that does not exist in Elasticsearch, + * correctly returns a boolean true from the hasError() method of + * the \Elastica\Response object + * + * @group functional + */ + public function testDeleteMissingIndexHasError() + { + $client = $this->_getClient(); + $index = $client->getIndex('index_does_not_exist'); + + try { + $index->delete(); + $this->fail('This should never be reached. Deleting an unknown index will throw an exception'); + } catch (ResponseException $error) { + $response = $error->getResponse(); + $this->assertTrue($response->hasError()); + $request = $error->getRequest(); + $this->assertInstanceOf('Elastica\Request', $request); + } + } + + /** + * Tests to see if the test type mapping exists when calling $index->getMapping(). + * + * @group functional + */ + public function testIndexGetMapping() + { + $index = $this->_createIndex(); + $type = $index->getType('test'); + + $mapping = array('id' => array('type' => 'integer', 'store' => true), 'email' => array('type' => 'string', 'store' => 'no'), + 'username' => array('type' => 'string', 'store' => 'no'), 'test' => array('type' => 'integer', 'store' => 'no'),); + + $type->setMapping($mapping); + $index->refresh(); + $indexMappings = $index->getMapping(); + + $this->assertEquals($indexMappings['test']['properties']['id']['type'], 'integer'); + $this->assertEquals($indexMappings['test']['properties']['id']['store'], true); + $this->assertEquals($indexMappings['test']['properties']['email']['type'], 'string'); + $this->assertEquals($indexMappings['test']['properties']['username']['type'], 'string'); + $this->assertEquals($indexMappings['test']['properties']['test']['type'], 'integer'); + } + + /** + * Tests to see if the index is empty when there are no types set. + * + * @group functional + */ + public function testEmptyIndexGetMapping() + { + $index = $this->_createIndex(); + $indexMappings = $index->getMapping(); + + $this->assertTrue(empty($indexMappings['elastica_test'])); + } + + /** + * Test to see if search Default Limit works. + * + * @group functional + */ + public function testLimitDefaultIndex() + { + $client = $this->_getClient(); + $index = $client->getIndex('zero'); + $index->create(array('index' => array('number_of_shards' => 1, 'number_of_replicas' => 0)), true); + + $docs = array(); + + $docs[] = new Document(1, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')); + $docs[] = new Document(2, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')); + $docs[] = new Document(3, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')); + $docs[] = new Document(4, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')); + $docs[] = new Document(5, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')); + $docs[] = new Document(6, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')); + $docs[] = new Document(7, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')); + $docs[] = new Document(8, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')); + $docs[] = new Document(9, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')); + $docs[] = new Document(10, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')); + $docs[] = new Document(11, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')); + + $type = $index->getType('zeroType'); + $type->addDocuments($docs); + $index->refresh(); + + // default limit results (default limit is 10) + $resultSet = $index->search('farrelley'); + $this->assertEquals(10, $resultSet->count()); + + // limit = 1 + $resultSet = $index->search('farrelley', 1); + $this->assertEquals(1, $resultSet->count()); + } + + /** + * @expectedException \Elastica\Exception\InvalidException + * + * @group functional + */ + public function testCreateArray() + { + $client = $this->_getClient(); + $indexName = 'test'; + + //Testing recreate (backward compatibility) + $index = $client->getIndex($indexName); + $index->create(array(), true); + $this->_waitForAllocation($index); + $status = new Status($client); + $this->assertTrue($status->indexExists($indexName)); + + //Testing create index with array options + $opts = array('recreate' => true, 'routing' => 'r1,r2'); + $index->create(array(), $opts); + $this->_waitForAllocation($index); + $status = new Status($client); + $this->assertTrue($status->indexExists($indexName)); + + //Testing invalid options + $opts = array('recreate' => true, 'routing' => 'r1,r2', 'testing_invalid_option' => true); + $index->create(array(), $opts); + $this->_waitForAllocation($index); + $status = new Status($client); + $this->assertTrue($status->indexExists($indexName)); + } + + /** + * @group functional + */ + public function testCreateSearch() + { + $client = $this->_getClient(); + $index = new Index($client, 'test'); + + $query = new QueryString('test'); + $options = 5; + + $search = $index->createSearch($query, $options); + + $expected = array( + 'query' => array( + 'query_string' => array( + 'query' => 'test', + ), + ), + 'size' => 5, + ); + $this->assertEquals($expected, $search->getQuery()->toArray()); + $this->assertEquals(array('test'), $search->getIndices()); + $this->assertTrue($search->hasIndices()); + $this->assertTrue($search->hasIndex('test')); + $this->assertTrue($search->hasIndex($index)); + $this->assertEquals(array(), $search->getTypes()); + $this->assertFalse($search->hasTypes()); + $this->assertFalse($search->hasType('test_type')); + + $type = new Type($index, 'test_type2'); + $this->assertFalse($search->hasType($type)); + } + + /** + * @group functional + */ + public function testSearch() + { + $index = $this->_createIndex(); + + $type = new Type($index, 'user'); + + $docs = array(); + $docs[] = new Document(1, array('username' => 'hans', 'test' => array('2', '3', '5'))); + $docs[] = new Document(2, array('username' => 'john', 'test' => array('1', '3', '6'))); + $docs[] = new Document(3, array('username' => 'rolf', 'test' => array('2', '3', '7'))); + $type->addDocuments($docs); + $index->refresh(); + + $resultSet = $index->search('rolf'); + $this->assertEquals(1, $resultSet->count()); + + $count = $index->count('rolf'); + $this->assertEquals(1, $count); + + // Test if source is returned + $result = $resultSet->current(); + $this->assertEquals(3, $result->getId()); + $data = $result->getData(); + $this->assertEquals('rolf', $data['username']); + + $count = $index->count(); + $this->assertEquals(3, $count); + } + + /** + * @group functional + */ + public function testOptimize() + { + $index = $this->_createIndex(); + + $type = new Type($index, 'optimize'); + + $docs = array(); + $docs[] = new Document(1, array('foo' => 'bar')); + $docs[] = new Document(2, array('foo' => 'bar')); + $type->addDocuments($docs); + $index->refresh(); + + $stats = $index->getStats()->getData(); + $this->assertEquals(0, $stats['_all']['primaries']['docs']['deleted']); + + $type->deleteById(1); + $index->refresh(); + + $stats = $index->getStats()->getData(); + $this->assertEquals(1, $stats['_all']['primaries']['docs']['deleted']); + + $index->optimize(array('max_num_segments' => 1)); + + $stats = $index->getStats()->getData(); + $this->assertEquals(0, $stats['_all']['primaries']['docs']['deleted']); + } + + /** + * @group functional + */ + public function testAnalyze() + { + $index = $this->_createIndex(); + $index->optimize(); + sleep(2); + $returnedTokens = $index->analyze('foo'); + + $tokens = array( + array( + 'token' => 'foo', + 'start_offset' => 0, + 'end_offset' => 3, + 'type' => '', + 'position' => 1, + ), + ); + + $this->assertEquals($tokens, $returnedTokens); + } + + /** + * @group unit + * @expectedException \Elastica\Exception\InvalidException + */ + public function testThrowExceptionIfNotScalar() + { + $client = $this->_getClient(); + $client->getIndex(new \stdClass()); + } + + /** + * @group unit + */ + public function testConvertScalarsToString() + { + $client = $this->_getClient(); + $index = $client->getIndex(1); + + $this->assertEquals('1', $index->getName()); + $this->assertInternalType('string', $index->getName()); + } + + /** + * Check for the presence of the mapper-attachments plugin and skip the current test if it is not found. + */ + protected function _checkAttachmentsPlugin() + { + $nodes = $this->_getClient()->getCluster()->getNodes(); + if (!$nodes[0]->getInfo()->hasPlugin('mapper-attachments')) { + $this->markTestSkipped('mapper-attachments plugin not installed'); + } + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/LogTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/LogTest.php new file mode 100644 index 00000000..fdfc5c00 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/LogTest.php @@ -0,0 +1,196 @@ +assertInstanceOf('Psr\Log\LoggerInterface', $log); + } + + /** + * @group unit + */ + public function testSetLogConfigPath() + { + $logPath = '/tmp/php.log'; + $client = $this->_getClient(array('log' => $logPath)); + $this->assertEquals($logPath, $client->getConfig('log')); + } + + /** + * @group unit + */ + public function testSetLogConfigEnable() + { + $client = $this->_getClient(array('log' => true)); + $this->assertTrue($client->getConfig('log')); + } + + /** + * @group unit + */ + public function testSetLogConfigEnable1() + { + $client = $this->_getClient(); + $client->setLogger(new Log()); + $this->assertFalse($client->getConfig('log')); + } + + /** + * @group unit + */ + public function testEmptyLogConfig() + { + $client = $this->_getClient(); + $this->assertEmpty($client->getConfig('log')); + } + + /** + * @group unit + */ + public function testGetLastMessage() + { + $log = new Log('/tmp/php.log'); + + $log->log(LogLevel::DEBUG, $this->_message, $this->_context); + + $this->_context['error_message'] = $this->_message; + $message = json_encode($this->_context); + + $this->assertEquals($message, $log->getLastMessage()); + } + + /** + * @group unit + */ + public function testGetLastMessage2() + { + $client = $this->_getClient(array('log' => true)); + $log = new Log($client); + + // Set log path temp path as otherwise test fails with output + $errorLog = ini_get('error_log'); + ini_set('error_log', sys_get_temp_dir().DIRECTORY_SEPARATOR.'php.log'); + + $this->_context['error_message'] = $this->_message; + $message = json_encode($this->_context); + + $log->log(LogLevel::DEBUG, $this->_message, $this->_context); + ini_set('error_log', $errorLog); + + $this->assertEquals($message, $log->getLastMessage()); + } + + /** + * @group unit + */ + public function testGetLastMessageInfo() + { + $log = $this->initLog(); + $log->info($this->_message, $this->_context); + $this->assertEquals($this->getMessage(), $log->getLastMessage()); + } + + /** + * @group unit + */ + public function testGetLastMessageCritical() + { + $log = $this->initLog(); + $log->critical($this->_message, $this->_context); + $this->assertEquals($this->getMessage(), $log->getLastMessage()); + } + + /** + * @group unit + */ + public function testGetLastMessageAlert() + { + $log = $this->initLog(); + $log->alert($this->_message, $this->_context); + $this->assertEquals($this->getMessage(), $log->getLastMessage()); + } + + /** + * @group unit + */ + public function testGetLastMessageDebug() + { + $log = $this->initLog(); + $log->debug($this->_message, $this->_context); + $this->assertEquals($this->getMessage(), $log->getLastMessage()); + } + + /** + * @group unit + */ + public function testGetLastMessageEmergency() + { + $log = $this->initLog(); + $log->emergency($this->_message, $this->_context); + $this->assertEquals($this->getMessage(), $log->getLastMessage()); + } + + /** + * @group unit + */ + public function testGetLastMessageError() + { + $log = $this->initLog(); + $log->error($this->_message, $this->_context); + $this->assertEquals($this->getMessage(), $log->getLastMessage()); + } + + /** + * @group unit + */ + public function testGetLastMessageNotice() + { + $log = $this->initLog(); + $log->notice($this->_message, $this->_context); + $this->assertEquals($this->getMessage(), $log->getLastMessage()); + } + + /** + * @group unit + */ + public function testGetLastMessageWarning() + { + $log = $this->initLog(); + $log->warning($this->_message, $this->_context); + $this->assertEquals($this->getMessage(), $log->getLastMessage()); + } + + private function initLog() + { + $log = new Log('/tmp/php.log'); + + return $log; + } + + private function getMessage() + { + $this->_context['error_message'] = $this->_message; + + return json_encode($this->_context); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Multi/SearchTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Multi/SearchTest.php new file mode 100644 index 00000000..765c8c1d --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Multi/SearchTest.php @@ -0,0 +1,575 @@ +_getClient(); + + $index = $client->getIndex('zero'); + $index->create(array('index' => array('number_of_shards' => 1, 'number_of_replicas' => 0)), true); + + $docs = array(); + $docs[] = new Document(1, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')); + $docs[] = new Document(2, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')); + $docs[] = new Document(3, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')); + $docs[] = new Document(4, array('id' => 1, 'email' => 'test@test.com', 'username' => 'kate')); + $docs[] = new Document(5, array('id' => 1, 'email' => 'test@test.com', 'username' => 'kate')); + $docs[] = new Document(6, array('id' => 1, 'email' => 'test@test.com', 'username' => 'bunny')); + $docs[] = new Document(7, array('id' => 1, 'email' => 'test@test.com', 'username' => 'bunny')); + $docs[] = new Document(8, array('id' => 1, 'email' => 'test@test.com', 'username' => 'bunny')); + $docs[] = new Document(9, array('id' => 1, 'email' => 'test@test.com', 'username' => 'bunny')); + $docs[] = new Document(10, array('id' => 1, 'email' => 'test@test.com', 'username' => 'bunny')); + $docs[] = new Document(11, array('id' => 1, 'email' => 'test@test.com', 'username' => 'bunny')); + $type = $index->getType('zeroType'); + $type->addDocuments($docs); + $index->refresh(); + + return $type; + } + + /** + * @group unit + */ + public function testConstruct() + { + $client = $this->_getClient(); + $multiSearch = new MultiSearch($client); + + $this->assertInstanceOf('Elastica\Multi\Search', $multiSearch); + $this->assertSame($client, $multiSearch->getClient()); + } + + /** + * @group unit + */ + public function testSetSearches() + { + $client = $this->_getClient(); + $multiSearch = new MultiSearch($client); + + $search1 = new Search($client); + $search2 = new Search($client); + $search3 = new Search($client); + + $multiSearch->setSearches(array($search1, $search2, $search3)); + + $searches = $multiSearch->getSearches(); + + $this->assertInternalType('array', $searches); + $this->assertCount(3, $searches); + $this->assertArrayHasKey(0, $searches); + $this->assertSame($search1, $searches[0]); + $this->assertArrayHasKey(1, $searches); + $this->assertSame($search2, $searches[1]); + $this->assertArrayHasKey(2, $searches); + $this->assertSame($search3, $searches[2]); + + $multiSearch->clearSearches(); + $searches = $multiSearch->getSearches(); + + $this->assertInternalType('array', $searches); + $this->assertCount(0, $searches); + } + + /** + * @group unit + */ + public function testSetSearchesByKeys() + { + $client = $this->_getClient(); + $multiSearch = new MultiSearch($client); + + $search1 = new Search($client); + $search2 = new Search($client); + $search3 = new Search($client); + + $multiSearch->setSearches(array('search1' => $search1, 'search2' => $search2, $search3)); + + $searches = $multiSearch->getSearches(); + + $this->assertInternalType('array', $searches); + $this->assertCount(3, $searches); + $this->assertArrayHasKey('search1', $searches); + $this->assertSame($search1, $searches['search1']); + $this->assertArrayHasKey('search2', $searches); + $this->assertSame($search2, $searches['search2']); + $this->assertArrayHasKey(0, $searches); + $this->assertSame($search3, $searches[0]); + + $multiSearch->clearSearches(); + $searches = $multiSearch->getSearches(); + + $this->assertInternalType('array', $searches); + $this->assertCount(0, $searches); + } + + /** + * @group functional + */ + public function testSearch() + { + $type = $this->_createType(); + $index = $type->getIndex(); + $client = $index->getClient(); + + $multiSearch = new MultiSearch($client); + + $search1 = new Search($client); + $search1->addIndex($index)->addType($type); + $query1 = new Query(); + $termQuery1 = new Term(); + $termQuery1->setTerm('username', 'farrelley'); + $query1->setQuery($termQuery1); + $query1->setSize(2); + $search1->setQuery($query1); + + $multiSearch->addSearch($search1); + + $this->assertCount(1, $multiSearch->getSearches()); + + $search2 = new Search($client); + $search2->addIndex($index)->addType($type); + $query2 = new Query(); + $termQuery2 = new Term(); + $termQuery2->setTerm('username', 'bunny'); + $query2->setQuery($termQuery2); + $query2->setSize(3); + $search2->setQuery($query2); + + $multiSearch->addSearch($search2); + + $this->assertCount(2, $multiSearch->getSearches()); + + $searches = $multiSearch->getSearches(); + $this->assertSame($search1, $searches[0]); + $this->assertSame($search2, $searches[1]); + + $multiResultSet = $multiSearch->search(); + + $this->assertInstanceOf('Elastica\Multi\ResultSet', $multiResultSet); + $this->assertCount(2, $multiResultSet); + $this->assertInstanceOf('Elastica\Response', $multiResultSet->getResponse()); + + foreach ($multiResultSet as $resultSet) { + $this->assertInstanceOf('Elastica\ResultSet', $resultSet); + } + + $resultSets = $multiResultSet->getResultSets(); + + $this->assertInternalType('array', $resultSets); + + $this->assertArrayHasKey(0, $resultSets); + $this->assertInstanceOf('Elastica\ResultSet', $resultSets[0]); + $this->assertCount(2, $resultSets[0]); + $this->assertSame($query1, $resultSets[0]->getQuery()); + $this->assertEquals(3, $resultSets[0]->getTotalHits()); + + $this->assertArrayHasKey(1, $resultSets); + $this->assertInstanceOf('Elastica\ResultSet', $resultSets[1]); + $this->assertCount(3, $resultSets[1]); + $this->assertSame($query2, $resultSets[1]->getQuery()); + $this->assertEquals(6, $resultSets[1]->getTotalHits()); + + $this->assertFalse($multiResultSet->hasError()); + + $search1->setOption(Search::OPTION_SEARCH_TYPE, Search::OPTION_SEARCH_TYPE_COUNT); + $search2->setOption(Search::OPTION_SEARCH_TYPE, Search::OPTION_SEARCH_TYPE_COUNT); + + $multiResultSet = $multiSearch->search(); + + $this->assertInstanceOf('Elastica\Multi\ResultSet', $multiResultSet); + $this->assertCount(2, $multiResultSet); + $this->assertInstanceOf('Elastica\Response', $multiResultSet->getResponse()); + + $resultSets = $multiResultSet->getResultSets(); + + $this->assertInternalType('array', $resultSets); + + $this->assertArrayHasKey(0, $resultSets); + $this->assertInstanceOf('Elastica\ResultSet', $resultSets[0]); + $this->assertCount(0, $resultSets[0]); + $this->assertSame($query1, $resultSets[0]->getQuery()); + $this->assertEquals(3, $resultSets[0]->getTotalHits()); + + $this->assertArrayHasKey(1, $resultSets); + $this->assertInstanceOf('Elastica\ResultSet', $resultSets[1]); + $this->assertCount(0, $resultSets[1]); + $this->assertSame($query2, $resultSets[1]->getQuery()); + $this->assertEquals(6, $resultSets[1]->getTotalHits()); + } + + /** + * @group functional + */ + public function testSearchWithKeys() + { + $type = $this->_createType(); + $index = $type->getIndex(); + $client = $index->getClient(); + + $multiSearch = new MultiSearch($client); + + $search1 = new Search($client); + $search1->addIndex($index)->addType($type); + $query1 = new Query(); + $termQuery1 = new Term(); + $termQuery1->setTerm('username', 'farrelley'); + $query1->setQuery($termQuery1); + $query1->setSize(2); + $search1->setQuery($query1); + + $multiSearch->addSearch($search1, 'search1'); + + $this->assertCount(1, $multiSearch->getSearches()); + + $search2 = new Search($client); + $search2->addIndex($index)->addType($type); + $query2 = new Query(); + $termQuery2 = new Term(); + $termQuery2->setTerm('username', 'bunny'); + $query2->setQuery($termQuery2); + $query2->setSize(3); + $search2->setQuery($query2); + + $multiSearch->addSearch($search2, 'search2'); + + $this->assertCount(2, $multiSearch->getSearches()); + + $searches = $multiSearch->getSearches(); + $this->assertSame($search1, $searches['search1']); + $this->assertSame($search2, $searches['search2']); + + $multiResultSet = $multiSearch->search(); + + $this->assertInstanceOf('Elastica\Multi\ResultSet', $multiResultSet); + $this->assertCount(2, $multiResultSet); + $this->assertInstanceOf('Elastica\Response', $multiResultSet->getResponse()); + + foreach ($multiResultSet as $resultSet) { + $this->assertInstanceOf('Elastica\ResultSet', $resultSet); + } + + $this->assertInstanceOf('Elastica\ResultSet', $multiResultSet['search1']); + $this->assertInstanceOf('Elastica\ResultSet', $multiResultSet['search2']); + + $resultSets = $multiResultSet->getResultSets(); + + $this->assertInternalType('array', $resultSets); + + $this->assertArrayHasKey('search1', $resultSets); + $this->assertInstanceOf('Elastica\ResultSet', $resultSets['search1']); + $this->assertCount(2, $resultSets['search1']); + $this->assertSame($query1, $resultSets['search1']->getQuery()); + $this->assertEquals(3, $resultSets['search1']->getTotalHits()); + + $this->assertArrayHasKey('search2', $resultSets); + $this->assertInstanceOf('Elastica\ResultSet', $resultSets['search2']); + $this->assertCount(3, $resultSets['search2']); + $this->assertSame($query2, $resultSets['search2']->getQuery()); + $this->assertEquals(6, $resultSets['search2']->getTotalHits()); + + $this->assertFalse($multiResultSet->hasError()); + + $search1->setOption(Search::OPTION_SEARCH_TYPE, Search::OPTION_SEARCH_TYPE_COUNT); + $search2->setOption(Search::OPTION_SEARCH_TYPE, Search::OPTION_SEARCH_TYPE_COUNT); + + $multiResultSet = $multiSearch->search(); + + $this->assertInstanceOf('Elastica\Multi\ResultSet', $multiResultSet); + $this->assertCount(2, $multiResultSet); + $this->assertInstanceOf('Elastica\Response', $multiResultSet->getResponse()); + + $resultSets = $multiResultSet->getResultSets(); + + $this->assertInternalType('array', $resultSets); + + $this->assertArrayHasKey('search1', $resultSets); + $this->assertInstanceOf('Elastica\ResultSet', $resultSets['search1']); + $this->assertCount(0, $resultSets['search1']); + $this->assertSame($query1, $resultSets['search1']->getQuery()); + $this->assertEquals(3, $resultSets['search1']->getTotalHits()); + + $this->assertArrayHasKey('search2', $resultSets); + $this->assertInstanceOf('Elastica\ResultSet', $resultSets['search2']); + $this->assertCount(0, $resultSets['search2']); + $this->assertSame($query2, $resultSets['search2']->getQuery()); + $this->assertEquals(6, $resultSets['search2']->getTotalHits()); + } + + /** + * @group functional + */ + public function testSearchWithError() + { + $type = $this->_createType(); + $index = $type->getIndex(); + $client = $index->getClient(); + + $multiSearch = new MultiSearch($client); + + $searchGood = new Search($client); + $searchGood->setQuery('bunny'); + $searchGood->addIndex($index)->addType($type); + + $multiSearch->addSearch($searchGood); + + $searchBad = new Search($client); + $searchBadQuery = new Range(); + $searchBadQuery->addField('bad', array('from' => 0)); + $searchBadQuery->setParam('_cache', true); + $searchBad->setQuery($searchBadQuery); + $searchBad->addIndex($index)->addType($type); + + $multiSearch->addSearch($searchBad); + + $multiResultSet = $multiSearch->search(); + + $this->assertInstanceOf('Elastica\Multi\ResultSet', $multiResultSet); + $resultSets = $multiResultSet->getResultSets(); + $this->assertInternalType('array', $resultSets); + + $this->assertArrayHasKey(0, $resultSets); + $this->assertInstanceOf('Elastica\ResultSet', $resultSets[0]); + $this->assertSame($searchGood->getQuery(), $resultSets[0]->getQuery()); + $this->assertSame(6, $resultSets[0]->getTotalHits()); + $this->assertCount(6, $resultSets[0]); + + $this->assertArrayHasKey(1, $resultSets); + $this->assertInstanceOf('Elastica\ResultSet', $resultSets[1]); + $this->assertSame($searchBad->getQuery(), $resultSets[1]->getQuery()); + $this->assertSame(0, $resultSets[1]->getTotalHits()); + $this->assertCount(0, $resultSets[1]); + $this->assertTrue($resultSets[1]->getResponse()->hasError()); + + $this->assertTrue($multiResultSet->hasError()); + } + + /** + * @group functional + */ + public function testSearchWithErrorWithKeys() + { + $type = $this->_createType(); + $index = $type->getIndex(); + $client = $index->getClient(); + + $multiSearch = new MultiSearch($client); + + $searchGood = new Search($client); + $searchGood->setQuery('bunny'); + $searchGood->addIndex($index)->addType($type); + + $multiSearch->addSearch($searchGood, 'search1'); + + $searchBad = new Search($client); + $searchBadQuery = new Range(); + $searchBadQuery->addField('bad', array('from' => 0)); + $searchBadQuery->setParam('_cache', true); + $searchBad->setQuery($searchBadQuery); + $searchBad->addIndex($index)->addType($type); + + $multiSearch->addSearch($searchBad); + + $multiResultSet = $multiSearch->search(); + + $this->assertInstanceOf('Elastica\Multi\ResultSet', $multiResultSet); + $resultSets = $multiResultSet->getResultSets(); + $this->assertInternalType('array', $resultSets); + + $this->assertArrayHasKey('search1', $resultSets); + $this->assertInstanceOf('Elastica\ResultSet', $resultSets['search1']); + $this->assertSame($searchGood->getQuery(), $resultSets['search1']->getQuery()); + $this->assertSame(6, $resultSets['search1']->getTotalHits()); + $this->assertCount(6, $resultSets['search1']); + + $this->assertArrayHasKey(0, $resultSets); + $this->assertInstanceOf('Elastica\ResultSet', $resultSets[0]); + $this->assertSame($searchBad->getQuery(), $resultSets[0]->getQuery()); + $this->assertSame(0, $resultSets[0]->getTotalHits()); + $this->assertCount(0, $resultSets[0]); + $this->assertTrue($resultSets[0]->getResponse()->hasError()); + + $this->assertTrue($multiResultSet->hasError()); + } + + /** + * @group functional + */ + public function testGlobalSearchTypeSearch() + { + $type = $this->_createType(); + $index = $type->getIndex(); + $client = $index->getClient(); + + $multiSearch = new MultiSearch($client); + + $search1 = new Search($client); + $search1->addIndex($index)->addType($type); + $query1 = new Query(); + $termQuery1 = new Term(); + $termQuery1->setTerm('username', 'farrelley'); + $query1->setQuery($termQuery1); + $query1->setSize(2); + $search1->setQuery($query1); + + $multiSearch->addSearch($search1); + + $this->assertCount(1, $multiSearch->getSearches()); + + $search2 = new Search($client); + $search2->addIndex($index)->addType($type); + $query2 = new Query(); + $termQuery2 = new Term(); + $termQuery2->setTerm('username', 'bunny'); + $query2->setQuery($termQuery2); + $query2->setSize(3); + $search2->setQuery($query2); + + $multiSearch->addSearch($search2); + + $multiSearch->setSearchType(Search::OPTION_SEARCH_TYPE_COUNT); + + $multiResultSet = $multiSearch->search(); + + $this->assertInstanceOf('Elastica\Multi\ResultSet', $multiResultSet); + $this->assertCount(2, $multiResultSet); + $this->assertInstanceOf('Elastica\Response', $multiResultSet->getResponse()); + + $resultSets = $multiResultSet->getResultSets(); + + $this->assertInternalType('array', $resultSets); + + $this->assertArrayHasKey(0, $resultSets); + $this->assertInstanceOf('Elastica\ResultSet', $resultSets[0]); + $this->assertCount(0, $resultSets[0]); + $this->assertSame($query1, $resultSets[0]->getQuery()); + $this->assertEquals(3, $resultSets[0]->getTotalHits()); + + $this->assertArrayHasKey(1, $resultSets); + $this->assertInstanceOf('Elastica\ResultSet', $resultSets[1]); + $this->assertCount(0, $resultSets[1]); + $this->assertSame($query2, $resultSets[1]->getQuery()); + $this->assertEquals(6, $resultSets[1]->getTotalHits()); + + $search1->setOption(Search::OPTION_SEARCH_TYPE, Search::OPTION_SEARCH_TYPE_QUERY_AND_FETCH); + + $multiResultSet = $multiSearch->search(); + + $this->assertInstanceOf('Elastica\Multi\ResultSet', $multiResultSet); + $this->assertCount(2, $multiResultSet); + $this->assertInstanceOf('Elastica\Response', $multiResultSet->getResponse()); + + $resultSets = $multiResultSet->getResultSets(); + + $this->assertInternalType('array', $resultSets); + + $this->assertArrayHasKey(0, $resultSets); + $this->assertInstanceOf('Elastica\ResultSet', $resultSets[0]); + $this->assertCount(2, $resultSets[0]); + $this->assertSame($query1, $resultSets[0]->getQuery()); + $this->assertEquals(3, $resultSets[0]->getTotalHits()); + + $this->assertArrayHasKey(1, $resultSets); + $this->assertInstanceOf('Elastica\ResultSet', $resultSets[1]); + $this->assertCount(0, $resultSets[1]); + $this->assertSame($query2, $resultSets[1]->getQuery()); + $this->assertEquals(6, $resultSets[1]->getTotalHits()); + } + + /** + * @group functional + */ + public function testGlobalSearchTypeSearchWithKeys() + { + $type = $this->_createType(); + $index = $type->getIndex(); + $client = $index->getClient(); + + $multiSearch = new MultiSearch($client); + + $search1 = new Search($client); + $search1->addIndex($index)->addType($type); + $query1 = new Query(); + $termQuery1 = new Term(); + $termQuery1->setTerm('username', 'farrelley'); + $query1->setQuery($termQuery1); + $query1->setSize(2); + $search1->setQuery($query1); + + $multiSearch->addSearch($search1); + + $this->assertCount(1, $multiSearch->getSearches()); + + $search2 = new Search($client); + $search2->addIndex($index)->addType($type); + $query2 = new Query(); + $termQuery2 = new Term(); + $termQuery2->setTerm('username', 'bunny'); + $query2->setQuery($termQuery2); + $query2->setSize(3); + $search2->setQuery($query2); + + $multiSearch->addSearch($search2); + + $multiSearch->setSearchType(Search::OPTION_SEARCH_TYPE_COUNT); + + $multiResultSet = $multiSearch->search(); + + $this->assertInstanceOf('Elastica\Multi\ResultSet', $multiResultSet); + $this->assertCount(2, $multiResultSet); + $this->assertInstanceOf('Elastica\Response', $multiResultSet->getResponse()); + + $resultSets = $multiResultSet->getResultSets(); + + $this->assertInternalType('array', $resultSets); + + $this->assertArrayHasKey(0, $resultSets); + $this->assertInstanceOf('Elastica\ResultSet', $resultSets[0]); + $this->assertCount(0, $resultSets[0]); + $this->assertSame($query1, $resultSets[0]->getQuery()); + $this->assertEquals(3, $resultSets[0]->getTotalHits()); + + $this->assertArrayHasKey(1, $resultSets); + $this->assertInstanceOf('Elastica\ResultSet', $resultSets[1]); + $this->assertCount(0, $resultSets[1]); + $this->assertSame($query2, $resultSets[1]->getQuery()); + $this->assertEquals(6, $resultSets[1]->getTotalHits()); + + $search1->setOption(Search::OPTION_SEARCH_TYPE, Search::OPTION_SEARCH_TYPE_QUERY_AND_FETCH); + + $multiResultSet = $multiSearch->search(); + + $this->assertInstanceOf('Elastica\Multi\ResultSet', $multiResultSet); + $this->assertCount(2, $multiResultSet); + $this->assertInstanceOf('Elastica\Response', $multiResultSet->getResponse()); + + $resultSets = $multiResultSet->getResultSets(); + + $this->assertInternalType('array', $resultSets); + + $this->assertArrayHasKey(0, $resultSets); + $this->assertInstanceOf('Elastica\ResultSet', $resultSets[0]); + $this->assertCount(2, $resultSets[0]); + $this->assertSame($query1, $resultSets[0]->getQuery()); + $this->assertEquals(3, $resultSets[0]->getTotalHits()); + + $this->assertArrayHasKey(1, $resultSets); + $this->assertInstanceOf('Elastica\ResultSet', $resultSets[1]); + $this->assertCount(0, $resultSets[1]); + $this->assertSame($query2, $resultSets[1]->getQuery()); + $this->assertEquals(6, $resultSets[1]->getTotalHits()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Node/InfoTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Node/InfoTest.php new file mode 100644 index 00000000..812f141e --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Node/InfoTest.php @@ -0,0 +1,79 @@ +_getClient(); + $names = $client->getCluster()->getNodeNames(); + $name = reset($names); + + $node = new Node($name, $client); + $info = new NodeInfo($node); + + $this->assertNull($info->get('os', 'mem', 'total')); + + // Load os infos + $info = new NodeInfo($node, array('os')); + + $this->assertNotNull($info->get('os', 'mem', 'total_in_bytes')); + $this->assertInternalType('array', $info->get('os', 'mem')); + $this->assertNull($info->get('test', 'notest', 'notexist')); + } + + /** + * @group functional + */ + public function testHasPlugin() + { + $client = $this->_getClient(); + $nodes = $client->getCluster()->getNodes(); + $node = $nodes[0]; + $info = $node->getInfo(); + + $pluginName = 'mapper-attachments'; + + $this->assertTrue($info->hasPlugin($pluginName)); + $this->assertFalse($info->hasPlugin('foo')); + } + + /** + * @group functional + */ + public function testGetId() + { + $client = $this->_getClient(); + $nodes = $client->getCluster()->getNodes(); + + $ids = array(); + + foreach ($nodes as $node) { + $id = $node->getInfo()->getId(); + + // Checks that the ids are unique + $this->assertFalse(in_array($id, $ids)); + $ids[] = $id; + } + } + + /** + * @group functional + */ + public function testGetName() + { + $client = $this->_getClient(); + $nodes = $client->getCluster()->getNodes(); + + foreach ($nodes as $node) { + $this->assertEquals('Elastica', $node->getInfo()->getName()); + } + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/NodeTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/NodeTest.php new file mode 100644 index 00000000..fbd2d297 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/NodeTest.php @@ -0,0 +1,75 @@ +_getClient(); + $names = $client->getCluster()->getNodeNames(); + $name = reset($names); + + $node = new Node($name, $client); + $this->assertInstanceOf('Elastica\Node', $node); + } + + /** + * @group functional + */ + public function testGetInfo() + { + $client = $this->_getClient(); + $names = $client->getCluster()->getNodeNames(); + $name = reset($names); + + $node = new Node($name, $client); + + $info = $node->getInfo(); + + $this->assertInstanceOf('Elastica\Node\Info', $info); + } + + /** + * @group functional + */ + public function testGetStats() + { + $client = $this->_getClient(); + $names = $client->getCluster()->getNodeNames(); + $name = reset($names); + + $node = new Node($name, $client); + + $stats = $node->getStats(); + + $this->assertInstanceOf('Elastica\Node\Stats', $stats); + } + + /** + * @group functional + */ + public function testGetName() + { + $nodes = $this->_getClient()->getCluster()->getNodes(); + // At least 1 instance must exist + $this->assertGreaterThan(0, $nodes); + + foreach ($nodes as $node) { + $this->assertEquals($node->getName(), 'Elastica'); + } + } + + /** + * @group functional + */ + public function testGetId() + { + $node = new Node('Elastica', $this->_getClient()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/ParamTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/ParamTest.php new file mode 100644 index 00000000..eade8118 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/ParamTest.php @@ -0,0 +1,115 @@ +assertInstanceOf('Elastica\Param', $param); + $this->assertEquals(array($this->_getFilterName($param) => array()), $param->toArray()); + } + + /** + * @group unit + */ + public function testSetParams() + { + $param = new Param(); + $params = array('hello' => 'word', 'nicolas' => 'ruflin'); + $param->setParams($params); + + $this->assertInstanceOf('Elastica\Param', $param); + $this->assertEquals(array($this->_getFilterName($param) => $params), $param->toArray()); + } + + /** + * @group unit + */ + public function testSetGetParam() + { + $param = new Param(); + + $key = 'name'; + $value = 'nicolas ruflin'; + + $params = array($key => $value); + $param->setParam($key, $value); + + $this->assertEquals($params, $param->getParams()); + $this->assertEquals($value, $param->getParam($key)); + } + + /** + * @group unit + */ + public function testAddParam() + { + $param = new Param(); + + $key = 'name'; + $value = 'nicolas ruflin'; + + $param->addParam($key, $value); + + $this->assertEquals(array($key => array($value)), $param->getParams()); + $this->assertEquals(array($value), $param->getParam($key)); + } + + /** + * @group unit + */ + public function testAddParam2() + { + $param = new Param(); + + $key = 'name'; + $value1 = 'nicolas'; + $value2 = 'ruflin'; + + $param->addParam($key, $value1); + $param->addParam($key, $value2); + + $this->assertEquals(array($key => array($value1, $value2)), $param->getParams()); + $this->assertEquals(array($value1, $value2), $param->getParam($key)); + } + + /** + * @group unit + * @expectedException \Elastica\Exception\InvalidException + */ + public function testGetParamInvalid() + { + $param = new Param(); + + $param->getParam('notest'); + } + + /** + * @group unit + */ + public function testHasParam() + { + $param = new Param(); + + $key = 'name'; + $value = 'nicolas ruflin'; + + $this->assertFalse($param->hasParam($key)); + + $param->setParam($key, $value); + $this->assertTrue($param->hasParam($key)); + } + + protected function _getFilterName($filter) + { + return Util::getParamName($filter); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/PercolatorTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/PercolatorTest.php new file mode 100644 index 00000000..2a5a88c3 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/PercolatorTest.php @@ -0,0 +1,328 @@ +_createIndex(); + $percolatorName = $index->getName(); + + $percolator = new Percolator($index); + + $query = new Term(array('field1' => 'value1')); + $response = $percolator->registerQuery($percolatorName, $query); + + $data = $response->getData(); + + $expectedArray = array( + '_type' => '.percolator', + '_index' => $index->getName(), + '_id' => $percolatorName, + '_version' => 1, + 'created' => 1, + ); + + $this->assertEquals($expectedArray, $data); + + $index->delete(); + } + + /** + * @group functional + */ + public function testMatchDoc() + { + $index = $this->_createIndex(); + + $percolator = new Percolator($index); + + $percolatorName = $index->getName(); + + $query = new Term(array('name' => 'ruflin')); + $response = $percolator->registerQuery($percolatorName, $query); + + $this->assertTrue($response->isOk()); + $this->assertFalse($response->hasError()); + + $doc1 = new Document(); + $doc1->set('name', 'ruflin'); + + $doc2 = new Document(); + $doc2->set('name', 'nicolas'); + + $index->refresh(); + + $matches1 = $percolator->matchDoc($doc1); + + $this->assertCount(1, $matches1); + $firstPercolatorFound = false; + foreach ($matches1 as $match) { + if ($match['_id'] == $percolatorName) { + $firstPercolatorFound = true; + } + } + $this->assertTrue($firstPercolatorFound); + + $matches2 = $percolator->matchDoc($doc2); + $this->assertEmpty($matches2); + + $index->delete(); + } + + /** + * Test case for using filtered percolator queries based on the Elasticsearch documentation examples. + * + * @group functional + */ + public function testFilteredMatchDoc() + { + // step one: register create index and setup the percolator query from the ES documentation. + $index = $this->_createIndex(); + $percolator = new Percolator($index); + $baseQuery = new Term(array('field1' => 'value1')); + $fields = array('color' => 'blue'); + + $response = $percolator->registerQuery('kuku', $baseQuery, $fields); + + $this->assertTrue($response->isOk()); + $this->assertFalse($response->hasError()); + + // refreshing is required in order to ensure the query is really ready for execution. + $index->refresh(); + + // step two: match a document which should match the kuku query when filtered on the blue color + $doc = new Document(); + $doc->set('field1', 'value1'); + + $matches = $percolator->matchDoc($doc, new Term(array('color' => 'blue'))); + $this->assertCount(1, $matches, 'No or too much registered query matched.'); + $this->assertEquals('kuku', $matches[0]['_id'], 'A wrong registered query has matched.'); + + // step three: validate that using a different color, no registered query matches. + $matches = $percolator->matchDoc($doc, new Term(array('color' => 'green'))); + $this->assertCount(0, $matches, 'A registered query matched, although nothing should match at all.'); + + $index->delete(); + } + + /** + * Test case for using filtered percolator queries based on the Elasticsearch documentation examples. + * + * @group functional + */ + public function testRegisterAndUnregisterPercolator() + { + // step one: register create index and setup the percolator query from the ES documentation. + $index = $this->_createIndex(); + $percolator = new Percolator($index); + $baseQuery = new Term(array('field1' => 'value1')); + $fields = array('color' => 'blue'); + + $response = $percolator->registerQuery('kuku', $baseQuery, $fields); + + $this->assertTrue($response->isOk()); + $this->assertFalse($response->hasError()); + + // refreshing is required in order to ensure the query is really ready for execution. + $index->refresh(); + + // step two: match a document which should match the kuku query when filtered on the blue color + $doc = new Document(); + $doc->set('field1', 'value1'); + + $matches = $percolator->matchDoc($doc, new Term(array('color' => 'blue'))); + $this->assertCount(1, $matches, 'No or too much registered query matched.'); + $this->assertEquals('kuku', $matches[0]['_id'], 'A wrong registered query has matched.'); + + // step three: validate that using a different color, no registered query matches. + $matches = $percolator->matchDoc($doc, new Term(array('color' => 'green'))); + $this->assertCount(0, $matches, 'A registered query matched, although nothing should match at all.'); + + // unregister percolator query + $response = $percolator->unregisterQuery('kuku'); + + $this->assertTrue($response->isOk()); + $this->assertFalse($response->hasError()); + + // refreshing is required in order to ensure the query is really ready for execution. + $index->refresh(); + + $matches = $percolator->matchDoc($doc, new Term(array('color' => 'blue'))); + $this->assertCount(0, $matches, 'Percolator query did not get deleted.'); + + $index->delete(); + } + + protected function _getDefaultPercolator($percolatorName = 'existingDoc') + { + $index = $this->_createIndex(); + $percolator = new Percolator($index); + + $query = new Term(array('name' => 'foobar')); + $percolator->registerQuery($percolatorName, $query, array('field1' => array('tag1', 'tag2'))); + + return $percolator; + } + + protected function _addDefaultDocuments($index, $type = 'testing') + { + $type = $index->getType('testing'); + $type->addDocuments(array( + new Document(1, array('name' => 'foobar')), + new Document(2, array('name' => 'barbaz')), + )); + $index->refresh(); + + return $type; + } + + /** + * @group functional + */ + public function testPercolateExistingDocWithoutAnyParameter() + { + $percolator = $this->_getDefaultPercolator(); + $index = $percolator->getIndex(); + $type = $this->_addDefaultDocuments($index); + + $matches = $percolator->matchExistingDoc(1, $type->getName()); + + $this->assertCount(1, $matches); + $this->assertEquals('existingDoc', $matches[0]['_id']); + $index->delete(); + } + + /** + * @group functional + */ + public function testPercolateExistingDocWithPercolateFormatIds() + { + $percolator = $this->_getDefaultPercolator(); + $index = $percolator->getIndex(); + $type = $this->_addDefaultDocuments($index); + + $parameter = array('percolate_format' => 'ids'); + $matches = $percolator->matchExistingDoc(1, $type->getName(), null, $parameter); + + $this->assertCount(1, $matches); + $this->assertEquals('existingDoc', $matches[0]); + $index->delete(); + } + + /** + * @group functional + */ + public function testPercolateExistingDocWithIdThatShouldBeUrlEncoded() + { + $percolator = $this->_getDefaultPercolator(); + $index = $percolator->getIndex(); + $type = $this->_addDefaultDocuments($index); + + // id with whitespace, should be urlencoded + $id = 'foo bar 1'; + + $type->addDocument(new Document($id, array('name' => 'foobar'))); + $index->refresh(); + + $matches = $percolator->matchExistingDoc($id, $type->getName()); + + $this->assertCount(1, $matches); + $index->delete(); + } + + /** + * @group functional + */ + public function testPercolateWithAdditionalRequestBodyOptions() + { + $index = $this->_createIndex(); + $percolator = new Percolator($index); + + $query = new Term(array('name' => 'foo')); + $response = $percolator->registerQuery('percotest', $query, array('field1' => array('tag1', 'tag2'))); + + $this->assertTrue($response->isOk()); + $this->assertFalse($response->hasError()); + + $query = new Term(array('name' => 'foo')); + $response = $percolator->registerQuery('percotest1', $query, array('field1' => array('tag2'))); + + $this->assertTrue($response->isOk()); + $this->assertFalse($response->hasError()); + + $doc1 = new Document(); + $doc1->set('name', 'foo'); + + $index->refresh(); + + $options = array( + 'track_scores' => true, + 'sort' => array('_score' => 'desc'), + 'size' => 1, + ); + + $matches = $percolator->matchDoc($doc1, new Term(array('field1' => 'tag2')), 'type', $options); + + $this->assertCount(1, $matches); + $this->assertEquals('percotest1', $matches[0]['_id']); + $this->assertArrayHasKey('_score', $matches[0]); + } + + /** + * @group functional + */ + public function testPercolateExistingDocWithAdditionalRequestBodyOptions() + { + $percolatorName = 'existingDoc'; + $percolator = $this->_getDefaultPercolator($percolatorName); + + $query = new Term(array('name' => 'foobar')); + $percolator->registerQuery($percolatorName.'1', $query, array('field1' => array('tag2'))); + + $index = $percolator->getIndex(); + $type = $this->_addDefaultDocuments($index); + + $options = array( + 'track_scores' => true, + 'sort' => array('_score' => 'desc'), + 'size' => 1, + ); + + $matches = $percolator->matchExistingDoc(1, $type->getName(), new Term(array('field1' => 'tag2')), $options); + + $this->assertCount(1, $matches); + $this->assertEquals('existingDoc1', $matches[0]['_id']); + $this->assertArrayHasKey('_score', $matches[0]); + $index->delete(); + } + + protected function _createIndex($name = null, $delete = true, $shards = 1) + { + $index = parent::_createIndex($name, $delete, $shards); + $type = $index->getType('.percolator'); + + $mapping = new Type\Mapping($type, + array( + 'name' => array('type' => 'string'), + 'field1' => array('type' => 'string'), + ) + ); + $mapping->disableSource(); + + $type->setMapping($mapping); + + return $index; + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/BoolQueryTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/BoolQueryTest.php new file mode 100644 index 00000000..211d0c23 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/BoolQueryTest.php @@ -0,0 +1,171 @@ +setIds(1); + + $idsQuery2 = new Ids(); + $idsQuery2->setIds(2); + + $idsQuery3 = new Ids(); + $idsQuery3->setIds(3); + + $boost = 1.2; + $minMatch = 2; + + $query->setBoost($boost); + $query->setMinimumNumberShouldMatch($minMatch); + $query->addMust($idsQuery1); + $query->addMustNot($idsQuery2); + $query->addShould($idsQuery3->toArray()); + + $expectedArray = array( + 'bool' => array( + 'must' => array($idsQuery1->toArray()), + 'should' => array($idsQuery3->toArray()), + 'minimum_number_should_match' => $minMatch, + 'must_not' => array($idsQuery2->toArray()), + 'boost' => $boost, + ), + ); + + $this->assertEquals($expectedArray, $query->toArray()); + } + + /** + * Test to resolve the following issue. + * + * @link https://groups.google.com/forum/?fromgroups#!topic/elastica-php-client/zK_W_hClfvU + * + * @group unit + */ + public function testToArrayStructure() + { + $boolQuery = new BoolQuery(); + + $term1 = new Term(); + $term1->setParam('interests', 84); + + $term2 = new Term(); + $term2->setParam('interests', 92); + + $boolQuery->addShould($term1)->addShould($term2); + + $jsonString = '{"bool":{"should":[{"term":{"interests":84}},{"term":{"interests":92}}]}}'; + $this->assertEquals($jsonString, json_encode($boolQuery->toArray())); + } + + /** + * @group functional + */ + public function testSearch() + { + $client = $this->_getClient(); + $index = new Index($client, 'test'); + $index->create(array(), true); + + $type = new Type($index, 'helloworld'); + + $doc = new Document(1, array('id' => 1, 'email' => 'hans@test.com', 'username' => 'hans', 'test' => array('2', '3', '5'))); + $type->addDocument($doc); + $doc = new Document(2, array('id' => 2, 'email' => 'emil@test.com', 'username' => 'emil', 'test' => array('1', '3', '6'))); + $type->addDocument($doc); + $doc = new Document(3, array('id' => 3, 'email' => 'ruth@test.com', 'username' => 'ruth', 'test' => array('2', '3', '7'))); + $type->addDocument($doc); + + // Refresh index + $index->refresh(); + + $boolQuery = new BoolQuery(); + $termQuery1 = new Term(array('test' => '2')); + $boolQuery->addMust($termQuery1); + $resultSet = $type->search($boolQuery); + + $this->assertEquals(2, $resultSet->count()); + + $termQuery2 = new Term(array('test' => '5')); + $boolQuery->addMust($termQuery2); + $resultSet = $type->search($boolQuery); + + $this->assertEquals(1, $resultSet->count()); + + $termQuery3 = new Term(array('username' => 'hans')); + $boolQuery->addMust($termQuery3); + $resultSet = $type->search($boolQuery); + + $this->assertEquals(1, $resultSet->count()); + + $termQuery4 = new Term(array('username' => 'emil')); + $boolQuery->addMust($termQuery4); + $resultSet = $type->search($boolQuery); + + $this->assertEquals(0, $resultSet->count()); + } + + /** + * @group functional + */ + public function testEmptyBoolQuery() + { + $index = $this->_createIndex(); + $type = new Type($index, 'test'); + + $docNumber = 3; + for ($i = 0; $i < $docNumber; $i++) { + $doc = new Document($i, array('email' => 'test@test.com')); + $type->addDocument($doc); + } + + $index->refresh(); + + $boolQuery = new BoolQuery(); + + $resultSet = $type->search($boolQuery); + + $this->assertEquals($resultSet->count(), $docNumber); + } + + /** + * @group functional + */ + public function testOldObject() + { + if (version_compare(phpversion(), 7, '>=')) { + self::markTestSkipped('These objects are not supported in PHP 7'); + } + + $index = $this->_createIndex(); + $type = new Type($index, 'test'); + + $docNumber = 3; + for ($i = 0; $i < $docNumber; $i++) { + $doc = new Document($i, array('email' => 'test@test.com')); + $type->addDocument($doc); + } + + $index->refresh(); + + $boolQuery = new \Elastica\Query\Bool(); + + $resultSet = $type->search($boolQuery); + + $this->assertEquals($resultSet->count(), $docNumber); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/BoostingTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/BoostingTest.php new file mode 100644 index 00000000..8133fd37 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/BoostingTest.php @@ -0,0 +1,89 @@ + 'Vital Lama', 'price' => 5.2), + array('name' => 'Vital Match', 'price' => 2.1), + array('name' => 'Mercury Vital', 'price' => 7.5), + array('name' => 'Fist Mercury', 'price' => 3.8), + array('name' => 'Lama Vital 2nd', 'price' => 3.2), + ); + + protected function _getTestIndex() + { + $index = $this->_createIndex(); + $type = $index->getType('test'); + $type->setMapping(array( + 'name' => array('type' => 'string', 'index' => 'analyzed'), + 'price' => array('type' => 'float'), + )); + $docs = array(); + foreach ($this->sampleData as $key => $value) { + $docs[] = new Document($key, $value); + } + $type->addDocuments($docs); + + $index->refresh(); + + return $index; + } + + /** + * @group unit + */ + public function testToArray() + { + $keyword = 'vital'; + $negativeKeyword = 'Mercury'; + + $query = new Boosting(); + $positiveQuery = new Term(array('name' => $keyword)); + $negativeQuery = new Term(array('name' => $negativeKeyword)); + $query->setPositiveQuery($positiveQuery); + $query->setNegativeQuery($negativeQuery); + $query->setNegativeBoost(0.3); + + $expected = array( + 'boosting' => array( + 'positive' => $positiveQuery->toArray(), + 'negative' => $negativeQuery->toArray(), + 'negative_boost' => 0.3, + ), + ); + $this->assertEquals($expected, $query->toArray()); + } + + /** + * @group functional + */ + public function testNegativeBoost() + { + $keyword = 'vital'; + $negativeKeyword = 'mercury'; + + $query = new Boosting(); + $positiveQuery = new Term(array('name' => $keyword)); + $negativeQuery = new Term(array('name' => $negativeKeyword)); + $query->setPositiveQuery($positiveQuery); + $query->setNegativeQuery($negativeQuery); + $query->setNegativeBoost(0.2); + + $response = $this->_getTestIndex()->search($query); + $results = $response->getResults(); + + $this->assertEquals($response->getTotalHits(), 4); + + $lastResult = $results[3]->getData(); + $this->assertEquals($lastResult['name'], $this->sampleData[2]['name']); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/BuilderTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/BuilderTest.php new file mode 100644 index 00000000..a96e8b3a --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/BuilderTest.php @@ -0,0 +1,273 @@ +assertInstanceOf( + 'Elastica\Query\Builder', + Builder::factory('some string') + ); + } + + public function getQueryData() + { + return array( + array('allowLeadingWildcard', false, '{"allow_leading_wildcard":"false"}'), + array('allowLeadingWildcard', true, '{"allow_leading_wildcard":"true"}'), + array('analyzeWildcard', false, '{"analyze_wildcard":"false"}'), + array('analyzeWildcard', true, '{"analyze_wildcard":"true"}'), + array('analyzer', 'someAnalyzer', '{"analyzer":"someAnalyzer"}'), + array('autoGeneratePhraseQueries', true, '{"auto_generate_phrase_queries":"true"}'), + array('autoGeneratePhraseQueries', false, '{"auto_generate_phrase_queries":"false"}'), + array('boost', 2, '{"boost":"2"}'), + array('boost', 4.2, '{"boost":"4.2"}'), + array('defaultField', 'fieldName', '{"default_field":"fieldName"}'), + array('defaultOperator', 'OR', '{"default_operator":"OR"}'), + array('defaultOperator', 'AND', '{"default_operator":"AND"}'), + array('enablePositionIncrements', true, '{"enable_position_increments":"true"}'), + array('enablePositionIncrements', false, '{"enable_position_increments":"false"}'), + array('explain', true, '{"explain":"true"}'), + array('explain', false, '{"explain":"false"}'), + array('from', 42, '{"from":"42"}'), + array('fuzzyMinSim', 4.2, '{"fuzzy_min_sim":"4.2"}'), + array('fuzzyPrefixLength', 2, '{"fuzzy_prefix_length":"2"}'), + array('gt', 10, '{"gt":"10"}'), + array('gte', 11, '{"gte":"11"}'), + array('lowercaseExpandedTerms', true, '{"lowercase_expanded_terms":"true"}'), + array('lt', 10, '{"lt":"10"}'), + array('lte', 11, '{"lte":"11"}'), + array('minimumNumberShouldMatch', 21, '{"minimum_number_should_match":"21"}'), + array('phraseSlop', 6, '{"phrase_slop":"6"}'), + array('size', 7, '{"size":"7"}'), + array('tieBreakerMultiplier', 7, '{"tie_breaker_multiplier":"7"}'), + array('matchAll', 1.1, '{"match_all":{"boost":"1.1"}}'), + array('fields', array('age', 'sex', 'location'), '{"fields":["age","sex","location"]}'), + ); + } + + /** + * @group unit + * @dataProvider getQueryData + * @covers \Elastica\Query\Builder::__toString + * @covers \Elastica\Query\Builder::allowLeadingWildcard + * @covers \Elastica\Query\Builder::analyzeWildcard + * @covers \Elastica\Query\Builder::analyzer + * @covers \Elastica\Query\Builder::autoGeneratePhraseQueries + * @covers \Elastica\Query\Builder::boost + * @covers \Elastica\Query\Builder::defaultField + * @covers \Elastica\Query\Builder::defaultOperator + * @covers \Elastica\Query\Builder::enablePositionIncrements + * @covers \Elastica\Query\Builder::explain + * @covers \Elastica\Query\Builder::from + * @covers \Elastica\Query\Builder::fuzzyMinSim + * @covers \Elastica\Query\Builder::fuzzyPrefixLength + * @covers \Elastica\Query\Builder::gt + * @covers \Elastica\Query\Builder::gte + * @covers \Elastica\Query\Builder::lowercaseExpandedTerms + * @covers \Elastica\Query\Builder::lt + * @covers \Elastica\Query\Builder::lte + * @covers \Elastica\Query\Builder::minimumNumberShouldMatch + * @covers \Elastica\Query\Builder::phraseSlop + * @covers \Elastica\Query\Builder::size + * @covers \Elastica\Query\Builder::tieBreakerMultiplier + * @covers \Elastica\Query\Builder::matchAll + * @covers \Elastica\Query\Builder::fields + */ + public function testAllowLeadingWildcard($method, $argument, $result) + { + $builder = new Builder(); + $this->assertSame($builder, $builder->$method($argument)); + $this->assertSame($result, (string) $builder); + } + + public function getQueryTypes() + { + return array( + array('bool', 'bool'), + array('constantScore', 'constant_score'), + array('disMax', 'dis_max'), + array('facets', 'facets'), + array('filter', 'filter'), + array('filteredQuery', 'filtered'), + array('must', 'must'), + array('mustNot', 'must_not'), + array('prefix', 'prefix'), + array('query', 'query'), + array('queryString', 'query_string'), + array('range', 'range'), + array('should', 'should'), + array('sort', 'sort'), + array('term', 'term'), + array('textPhrase', 'text_phrase'), + array('wildcard', 'wildcard'), + ); + } + + /** + * @group unit + * @dataProvider getQueryTypes + * @covers \Elastica\Query\Builder::fieldClose + * @covers \Elastica\Query\Builder::close + * @covers \Elastica\Query\Builder::bool + * @covers \Elastica\Query\Builder::boolClose + * @covers \Elastica\Query\Builder::constantScore + * @covers \Elastica\Query\Builder::constantScoreClose + * @covers \Elastica\Query\Builder::disMax + * @covers \Elastica\Query\Builder::disMaxClose + * @covers \Elastica\Query\Builder::facets + * @covers \Elastica\Query\Builder::facetsClose + * @covers \Elastica\Query\Builder::filter + * @covers \Elastica\Query\Builder::filterClose + * @covers \Elastica\Query\Builder::filteredQuery + * @covers \Elastica\Query\Builder::filteredQueryClose + * @covers \Elastica\Query\Builder::must + * @covers \Elastica\Query\Builder::mustClose + * @covers \Elastica\Query\Builder::mustNot + * @covers \Elastica\Query\Builder::mustNotClose + * @covers \Elastica\Query\Builder::prefix + * @covers \Elastica\Query\Builder::prefixClose + * @covers \Elastica\Query\Builder::query + * @covers \Elastica\Query\Builder::queryClose + * @covers \Elastica\Query\Builder::queryString + * @covers \Elastica\Query\Builder::queryStringClose + * @covers \Elastica\Query\Builder::range + * @covers \Elastica\Query\Builder::rangeClose + * @covers \Elastica\Query\Builder::should + * @covers \Elastica\Query\Builder::shouldClose + * @covers \Elastica\Query\Builder::sort + * @covers \Elastica\Query\Builder::sortClose + * @covers \Elastica\Query\Builder::term + * @covers \Elastica\Query\Builder::termClose + * @covers \Elastica\Query\Builder::textPhrase + * @covers \Elastica\Query\Builder::textPhraseClose + * @covers \Elastica\Query\Builder::wildcard + * @covers \Elastica\Query\Builder::wildcardClose + */ + public function testQueryTypes($method, $queryType) + { + $builder = new Builder(); + $this->assertSame($builder, $builder->$method()); // open + $this->assertSame($builder, $builder->{$method.'Close'}()); // close + $this->assertSame('{"'.$queryType.'":{}}', (string) $builder); + } + + /** + * @group unit + * @covers \Elastica\Query\Builder::fieldOpen + * @covers \Elastica\Query\Builder::fieldClose + * @covers \Elastica\Query\Builder::open + * @covers \Elastica\Query\Builder::close + */ + public function testFieldOpenAndClose() + { + $builder = new Builder(); + $this->assertSame($builder, $builder->fieldOpen('someField')); + $this->assertSame($builder, $builder->fieldClose()); + $this->assertSame('{"someField":{}}', (string) $builder); + } + + /** + * @group unit + * @covers \Elastica\Query\Builder::sortField + */ + public function testSortField() + { + $builder = new Builder(); + $this->assertSame($builder, $builder->sortField('name', true)); + $this->assertSame('{"sort":{"name":{"reverse":"true"}}}', (string) $builder); + } + + /** + * @group unit + * @covers \Elastica\Query\Builder::sortFields + */ + public function testSortFields() + { + $builder = new Builder(); + $this->assertSame($builder, $builder->sortFields(array('field1' => 'asc', 'field2' => 'desc', 'field3' => 'asc'))); + $this->assertSame('{"sort":[{"field1":"asc"},{"field2":"desc"},{"field3":"asc"}]}', (string) $builder); + } + + /** + * @group unit + * @covers \Elastica\Query\Builder::queries + */ + public function testQueries() + { + $queries = array(); + + $builder = new Builder(); + $b1 = clone $builder; + $b2 = clone $builder; + + $queries[] = $b1->term()->field('age', 34)->termClose(); + $queries[] = $b2->term()->field('name', 'christer')->termClose(); + + $this->assertSame($builder, $builder->queries($queries)); + $this->assertSame('{"queries":[{"term":{"age":"34"}},{"term":{"name":"christer"}}]}', (string) $builder); + } + + public function getFieldData() + { + return array( + array('name', 'value', '{"name":"value"}'), + array('name', true, '{"name":"true"}'), + array('name', false, '{"name":"false"}'), + array('name', array(1, 2, 3), '{"name":["1","2","3"]}'), + array('name', array('foo', 'bar', 'baz'), '{"name":["foo","bar","baz"]}'), + ); + } + + /** + * @group unit + * @dataProvider getFieldData + * @covers \Elastica\Query\Builder::field + */ + public function testField($name, $value, $result) + { + $builder = new Builder(); + $this->assertSame($builder, $builder->field($name, $value)); + $this->assertSame($result, (string) $builder); + } + + /** + * @group unit + * @expectedException \Elastica\Exception\InvalidException + * @expectedExceptionMessage The produced query is not a valid json string : "{{}" + * @covers \Elastica\Query\Builder::toArray + */ + public function testToArrayWithInvalidData() + { + $builder = new Builder(); + $builder->open('foo'); + $builder->toArray(); + } + + /** + * @group unit + * @covers \Elastica\Query\Builder::toArray + */ + public function testToArray() + { + $builder = new Builder(); + $builder->query()->term()->field('category.id', array(1, 2, 3))->termClose()->queryClose(); + $expected = array( + 'query' => array( + 'term' => array( + 'category.id' => array(1, 2, 3), + ), + ), + ); + $this->assertEquals($expected, $builder->toArray()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/CommonTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/CommonTest.php new file mode 100644 index 00000000..016213a8 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/CommonTest.php @@ -0,0 +1,63 @@ +setLowFrequencyOperator(Common::OPERATOR_AND); + + $expected = array( + 'common' => array( + 'body' => array( + 'query' => 'test query', + 'cutoff_frequency' => .001, + 'low_freq_operator' => 'and', + ), + ), + ); + + $this->assertEquals($expected, $query->toArray()); + } + + /** + * @group functional + */ + public function testQuery() + { + $index = $this->_createIndex(); + $type = $index->getType('test'); + + $docs = array( + new Document(1, array('body' => 'foo baz')), + new Document(2, array('body' => 'foo bar baz')), + new Document(3, array('body' => 'foo bar baz bat')), + ); + //add documents to create common terms + for ($i = 4; $i < 24; $i++) { + $docs[] = new Document($i, array('body' => 'foo bar')); + } + $type->addDocuments($docs); + $index->refresh(); + + $query = new Common('body', 'foo bar baz bat', .5); + $results = $type->search($query)->getResults(); + + //documents containing only common words should not be returned + $this->assertEquals(3, sizeof($results)); + + $query->setMinimumShouldMatch(2); + $results = $type->search($query); + + //only the document containing both low frequency terms should match + $this->assertEquals(1, $results->count()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/ConstantScoreTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/ConstantScoreTest.php new file mode 100644 index 00000000..a3a213b9 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/ConstantScoreTest.php @@ -0,0 +1,162 @@ + array( + 'filter' => array( + 'term' => array( + 'foo', + 'bar', + ), + ), + ), + ), + ), + array( + array( + 'and' => array( + array( + 'query' => array( + 'query_string' => array( + 'query' => 'foo', + 'default_field' => 'something', + ), + ), + ), + array( + 'query' => array( + 'query_string' => array( + 'query' => 'bar', + 'default_field' => 'something', + ), + ), + ), + ), + ), + '{"constant_score":{"filter":{"and":[{"query":{"query_string":{"query":"foo","default_field":"something"}}},{"query":{"query_string":{"query":"bar","default_field":"something"}}}]}}}', + ), + ); + } + /** + * @group unit + * @dataProvider dataProviderSampleQueries + */ + public function testSimple($filter, $expected) + { + $query = new ConstantScore(); + $query->setFilter($filter); + if (is_string($expected)) { + $expected = json_decode($expected, true); + } + $this->assertEquals($expected, $query->toArray()); + } + + /** + * @group unit + */ + public function testToArray() + { + $query = new ConstantScore(); + + $boost = 1.2; + $filter = new Ids(); + $filter->setIds(array(1)); + + $query->setFilter($filter); + $query->setBoost($boost); + + $expectedArray = array( + 'constant_score' => array( + 'filter' => $filter->toArray(), + 'boost' => $boost, + ), + ); + + $this->assertEquals($expectedArray, $query->toArray()); + } + + /** + * @group unit + */ + public function testConstruct() + { + $filter = new Ids(); + $filter->setIds(array(1)); + + $query = new ConstantScore($filter); + + $expectedArray = array( + 'constant_score' => array( + 'filter' => $filter->toArray(), + ), + ); + + $this->assertEquals($expectedArray, $query->toArray()); + } + + /** + * @group functional + */ + public function testQuery() + { + $index = $this->_createIndex(); + + $type = $index->getType('constant_score'); + $type->addDocuments(array( + new Document(1, array('id' => 1, 'email' => 'hans@test.com', 'username' => 'hans')), + new Document(2, array('id' => 2, 'email' => 'emil@test.com', 'username' => 'emil')), + new Document(3, array('id' => 3, 'email' => 'ruth@test.com', 'username' => 'ruth')), + )); + + // Refresh index + $index->refresh(); + + $boost = 1.3; + $query_match = new MatchAll(); + + $query = new ConstantScore(); + $query->setQuery($query_match); + $query->setBoost($boost); + + $expectedArray = array( + 'constant_score' => array( + 'query' => $query_match->toArray(), + 'boost' => $boost, + ), + ); + + $this->assertEquals($expectedArray, $query->toArray()); + $resultSet = $type->search($query); + + $results = $resultSet->getResults(); + + $this->assertEquals($resultSet->count(), 3); + $this->assertEquals($results[1]->getScore(), 1); + } + + /** + * @group unit + */ + public function testConstructEmpty() + { + $query = new ConstantScore(); + $expectedArray = array('constant_score' => array()); + + $this->assertEquals($expectedArray, $query->toArray()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/DisMaxTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/DisMaxTest.php new file mode 100644 index 00000000..2c128018 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/DisMaxTest.php @@ -0,0 +1,84 @@ +setIds(1); + + $idsQuery2 = new Ids(); + $idsQuery2->setIds(2); + + $idsQuery3 = new Ids(); + $idsQuery3->setIds(3); + + $boost = 1.2; + $tieBreaker = 2; + + $query->setBoost($boost); + $query->setTieBreaker($tieBreaker); + $query->addQuery($idsQuery1); + $query->addQuery($idsQuery2); + $query->addQuery($idsQuery3->toArray()); + + $expectedArray = array( + 'dis_max' => array( + 'tie_breaker' => $tieBreaker, + 'boost' => $boost, + 'queries' => array( + $idsQuery1->toArray(), + $idsQuery2->toArray(), + $idsQuery3->toArray(), + ), + ), + ); + + $this->assertEquals($expectedArray, $query->toArray()); + } + + /** + * @group functional + */ + public function testQuery() + { + $index = $this->_createIndex(); + $type = $index->getType('test'); + + $type->addDocuments(array( + new Document(1, array('name' => 'Basel-Stadt')), + new Document(2, array('name' => 'New York')), + new Document(3, array('name' => 'Baden')), + new Document(4, array('name' => 'Baden Baden')), + )); + + $index->refresh(); + + $queryString1 = new QueryString('Bade*'); + $queryString2 = new QueryString('Base*'); + + $boost = 1.2; + $tieBreaker = 2; + + $query = new DisMax(); + $query->setBoost($boost); + $query->setTieBreaker($tieBreaker); + $query->addQuery($queryString1); + $query->addQuery($queryString2); + $resultSet = $type->search($query); + + $this->assertEquals(3, $resultSet->count()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/EscapeStringTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/EscapeStringTest.php new file mode 100644 index 00000000..536a7bb3 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/EscapeStringTest.php @@ -0,0 +1,36 @@ +_createIndex(); + $index->getSettings()->setNumberOfReplicas(0); + + $type = new Type($index, 'helloworld'); + + $doc = new Document(1, array( + 'email' => 'test@test.com', 'username' => 'test 7/6 123', 'test' => array('2', '3', '5'), ) + ); + $type->addDocument($doc); + + // Refresh index + $index->refresh(); + + $queryString = new QueryString(Util::escapeTerm('test 7/6')); + $resultSet = $type->search($queryString); + + $this->assertEquals(1, $resultSet->count()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/FilteredTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/FilteredTest.php new file mode 100644 index 00000000..c4bea6f2 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/FilteredTest.php @@ -0,0 +1,124 @@ +_createIndex(); + $type = $index->getType('helloworld'); + + $type->addDocuments(array( + new Document(1, array('id' => 1, 'email' => 'test@test.com', 'username' => 'hanswurst', 'test' => array('2', '3', '5'))), + new Document(2, array('id' => 2, 'email' => 'test@test.com', 'username' => 'peter', 'test' => array('2', '3', '5'))), + )); + + $queryString = new QueryString('test*'); + + $filter1 = new Term(); + $filter1->setTerm('username', 'peter'); + + $filter2 = new Term(); + $filter2->setTerm('username', 'qwerqwer'); + + $query1 = new Filtered($queryString, $filter1); + $query2 = new Filtered($queryString, $filter2); + $index->refresh(); + + $resultSet = $type->search($queryString); + $this->assertEquals(2, $resultSet->count()); + + $resultSet = $type->search($query1); + $this->assertEquals(1, $resultSet->count()); + + $resultSet = $type->search($query2); + $this->assertEquals(0, $resultSet->count()); + } + + /** + * @group unit + */ + public function testFilteredGetter() + { + $queryString = new QueryString('test*'); + + $filter1 = new Term(); + $filter1->setTerm('username', 'peter'); + + $filter2 = new Term(); + $filter2->setTerm('username', 'qwerqwer'); + + $query1 = new Filtered($queryString, $filter1); + $query2 = new Filtered($queryString, $filter2); + + $this->assertEquals($query1->getQuery(), $queryString); + $this->assertEquals($query2->getQuery(), $queryString); + $this->assertEquals($query1->getFilter(), $filter1); + $this->assertEquals($query2->getFilter(), $filter2); + } + + /** + * @group unit + * @expectedException \Elastica\Exception\InvalidException + */ + public function testFilteredWithoutArgumentsShouldRaiseException() + { + $query = new Filtered(); + $query->toArray(); + } + + /** + * @group functional + */ + public function testFilteredSearchNoQuery() + { + $index = $this->_createIndex(); + $type = $index->getType('helloworld'); + + $type->addDocuments(array( + new Document(1, array('id' => 1, 'email' => 'test@test.com', 'username' => 'hanswurst', 'test' => array('2', '3', '5'))), + new Document(2, array('id' => 2, 'email' => 'test@test.com', 'username' => 'peter', 'test' => array('2', '3', '5'))), + )); + + $index->refresh(); + + $filter = new Term(); + $filter->setTerm('username', 'peter'); + + $query = new Filtered(null, $filter); + + $resultSet = $type->search($query); + $this->assertEquals(1, $resultSet->count()); + } + + /** + * @group functional + */ + public function testFilteredSearchNoFilter() + { + $index = $this->_createIndex(); + $type = $index->getType('helloworld'); + + $doc = new Document(1, array('id' => 1, 'email' => 'test@test.com', 'username' => 'hanswurst', 'test' => array('2', '3', '5'))); + $type->addDocument($doc); + $doc = new Document(2, array('id' => 2, 'email' => 'test@test.com', 'username' => 'peter', 'test' => array('2', '3', '5'))); + $type->addDocument($doc); + + $queryString = new QueryString('hans*'); + + $query = new Filtered($queryString); + $index->refresh(); + + $resultSet = $type->search($query); + $this->assertEquals(1, $resultSet->count()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/FunctionScoreTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/FunctionScoreTest.php new file mode 100644 index 00000000..25600034 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/FunctionScoreTest.php @@ -0,0 +1,324 @@ +_createIndex(); + $type = $index->getType('test'); + + $type->setMapping(array( + 'name' => array('type' => 'string', 'index' => 'not_analyzed'), + 'location' => array('type' => 'geo_point'), + 'price' => array('type' => 'float'), + )); + + $type->addDocuments(array( + new Document(1, array( + 'name' => "Mr. Frostie's", + 'location' => array('lat' => 32.799605, 'lon' => -117.243027), + 'price' => 4.5, + )), + new Document(2, array( + 'name' => "Miller's Field", + 'location' => array('lat' => 32.795964, 'lon' => -117.255028), + 'price' => 9.5, + )), + )); + + $index->refresh(); + + return $index; + } + + /** + * @group unit + */ + public function testToArray() + { + $priceOrigin = 0; + $locationScale = '2mi'; + $priceScale = 9.25; + $query = new FunctionScore(); + $childQuery = new MatchAll(); + $query->setQuery($childQuery); + $query->addDecayFunction(FunctionScore::DECAY_GAUSS, 'location', $this->locationOrigin, $locationScale); + $query->addDecayFunction(FunctionScore::DECAY_GAUSS, 'price', $priceOrigin, $priceScale); + $expected = array( + 'function_score' => array( + 'query' => $childQuery->toArray(), + 'functions' => array( + array( + 'gauss' => array( + 'location' => array( + 'origin' => $this->locationOrigin, + 'scale' => $locationScale, + ), + ), + ), + array( + 'gauss' => array( + 'price' => array( + 'origin' => $priceOrigin, + 'scale' => $priceScale, + ), + ), + ), + ), + ), + ); + $this->assertEquals($expected, $query->toArray()); + } + + /** + * @group unit + */ + public function testDecayWeight() + { + $priceOrigin = 0; + $locationScale = '2mi'; + $priceScale = 9.25; + $query = new FunctionScore(); + $childQuery = new MatchAll(); + $query->setQuery($childQuery); + $query->addDecayFunction( + FunctionScore::DECAY_GAUSS, + 'location', + $this->locationOrigin, + $locationScale, + null, + null, + .5 + ); + $query->addDecayFunction(FunctionScore::DECAY_GAUSS, 'price', $priceOrigin, $priceScale, null, null, 2); + $expected = array( + 'function_score' => array( + 'query' => $childQuery->toArray(), + 'functions' => array( + array( + 'gauss' => array( + 'location' => array( + 'origin' => $this->locationOrigin, + 'scale' => $locationScale, + ), + ), + 'weight' => .5, + ), + array( + 'gauss' => array( + 'price' => array( + 'origin' => $priceOrigin, + 'scale' => $priceScale, + ), + ), + 'weight' => 2, + ), + ), + ), + ); + $this->assertEquals($expected, $query->toArray()); + } + + /** + * @group functional + */ + public function testGauss() + { + $query = new FunctionScore(); + $query->addDecayFunction(FunctionScore::DECAY_GAUSS, 'location', $this->locationOrigin, '4mi'); + $query->addDecayFunction(FunctionScore::DECAY_GAUSS, 'price', 0, 10); + $response = $this->_getIndexForTest()->search($query); + $results = $response->getResults(); + + // the document with the closest location and lowest price should be scored highest + $result0 = $results[0]->getData(); + $this->assertEquals("Mr. Frostie's", $result0['name']); + } + + /** + * @group functional + */ + public function testWeight() + { + $filter = new Term(array('price' => 4.5)); + $query = new FunctionScore(); + $query->addBoostFactorFunction(5.0, $filter); + $expected = array( + 'function_score' => array( + 'functions' => array( + array( + 'weight' => 5.0, + 'filter' => array( + 'term' => array( + 'price' => 4.5, + ), + ), + ), + ), + ), + ); + + $this->assertEquals($expected, $query->toArray()); + + $response = $this->_getIndexForTest()->search($query); + $results = $response->getResults(); + + // the document with price = 4.5 should be scored highest + $result0 = $results[0]->getData(); + $this->assertEquals("Mr. Frostie's", $result0['name']); + } + + /** + * @group functional + */ + public function testRandomScore() + { + $filter = new Term(array('price' => 4.5)); + $query = new FunctionScore(); + $query->addRandomScoreFunction(2, $filter); + $expected = array( + 'function_score' => array( + 'functions' => array( + array( + 'random_score' => array( + 'seed' => 2, + ), + 'filter' => array( + 'term' => array( + 'price' => 4.5, + ), + ), + ), + ), + ), + ); + + $this->assertEquals($expected, $query->toArray()); + + $response = $this->_getIndexForTest()->search($query); + $results = $response->getResults(); + + // the document with the random score should have a score > 1, means it is the first result + $result0 = $results[0]->getData(); + + $this->assertEquals("Miller's Field", $result0['name']); + } + + /** + * @group unit + */ + public function testRandomScoreWeight() + { + $filter = new Term(array('price' => 4.5)); + $query = new FunctionScore(); + $query->addRandomScoreFunction(2, $filter, 2); + $expected = array( + 'function_score' => array( + 'functions' => array( + array( + 'random_score' => array( + 'seed' => 2, + ), + 'filter' => array( + 'term' => array( + 'price' => 4.5, + ), + ), + 'weight' => 2, + ), + ), + ), + ); + + $this->assertEquals($expected, $query->toArray()); + } + + /** + * @group functional + */ + public function testRandomScoreWithoutSeed() + { + $query = new FunctionScore(); + $query->setRandomScore(); + + $response = $this->_getIndexForTest()->search($query); + + $this->assertEquals(2, $response->count()); + } + + /** + * @group functional + */ + public function testScriptScore() + { + $scriptString = "_score * doc['price'].value"; + $script = new Script($scriptString); + $query = new FunctionScore(); + $query->addScriptScoreFunction($script); + $expected = array( + 'function_score' => array( + 'functions' => array( + array( + 'script_score' => array( + 'script' => $scriptString, + ), + ), + ), + ), + ); + + $this->assertEquals($expected, $query->toArray()); + + $response = $this->_getIndexForTest()->search($query); + $results = $response->getResults(); + + // the document the highest price should be scored highest + $result0 = $results[0]->getData(); + $this->assertEquals("Miller's Field", $result0['name']); + } + + /** + * @group functional + */ + public function testSetMinScore() + { + $expected = array( + 'function_score' => array( + 'min_score' => 0.8, + 'functions' => array( + array( + 'gauss' => array( + 'price' => array( + 'origin' => 0, + 'scale' => 10, + ), + ), + ), + ), + ), + ); + + $query = new FunctionScore(); + $query->addDecayFunction(FunctionScore::DECAY_GAUSS, 'price', 0, 10); + $returnedValue = $query->setMinScore(0.8); + + $this->assertEquals($expected, $query->toArray()); + $this->assertInstanceOf('Elastica\Query\FunctionScore', $returnedValue); + + $response = $this->_getIndexForTest()->search($query); + $results = $response->getResults(); + + $this->assertCount(1, $results); + $this->assertEquals(1, $results[0]->getId()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/FuzzyLikeThisTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/FuzzyLikeThisTest.php new file mode 100644 index 00000000..fe11e788 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/FuzzyLikeThisTest.php @@ -0,0 +1,300 @@ +_getClient(); + $index = new Index($client, 'test'); + $index->create(array(), true); + $index->getSettings()->setNumberOfReplicas(0); + //$index->getSettings()->setNumberOfShards(1); + + $type = new Type($index, 'helloworldfuzzy'); + $mapping = new Mapping($type, array( + 'email' => array('store' => 'yes', 'type' => 'string', 'index' => 'analyzed'), + 'content' => array('store' => 'yes', 'type' => 'string', 'index' => 'analyzed'), + )); + + $mapping->setSource(array('enabled' => false)); + $type->setMapping($mapping); + + $doc = new Document(1000, array('email' => 'testemail@gmail.com', 'content' => 'This is a sample post. Hello World Fuzzy Like This!')); + $type->addDocument($doc); + + // Refresh index + $index->refresh(); + + $fltQuery = new FuzzyLikeThis(); + $fltQuery->setLikeText('sample gmail'); + $fltQuery->addFields(array('email', 'content')); + $fltQuery->setMinSimilarity(0.3); + $fltQuery->setMaxQueryTerms(3); + $resultSet = $type->search($fltQuery); + $this->assertEquals(1, $resultSet->count()); + } + + /** + * @group unit + */ + public function testSetPrefixLength() + { + $query = new FuzzyLikeThis(); + + $length = 3; + $query->setPrefixLength($length); + + $data = $query->toArray(); + + $this->assertEquals($length, $data['fuzzy_like_this']['prefix_length']); + } + + /** + * @group unit + */ + public function testAddFields() + { + $query = new FuzzyLikeThis(); + + $fields = array('test1', 'test2'); + $query->addFields($fields); + + $data = $query->toArray(); + + $this->assertEquals($fields, $data['fuzzy_like_this']['fields']); + } + + /** + * @group unit + */ + public function testSetLikeText() + { + $query = new FuzzyLikeThis(); + + $text = ' hello world'; + $query->setLikeText($text); + + $data = $query->toArray(); + + $this->assertEquals(trim($text), $data['fuzzy_like_this']['like_text']); + } + + /** + * @group unit + */ + public function testSetIgnoreTF() + { + $query = new FuzzyLikeThis(); + + $ignoreTF = false; + $query->setIgnoreTF($ignoreTF); + $data = $query->toArray(); + $this->assertEquals($ignoreTF, $data['fuzzy_like_this']['ignore_tf']); + + $ignoreTF = true; + $query->setIgnoreTF($ignoreTF); + $data = $query->toArray(); + $this->assertEquals($ignoreTF, $data['fuzzy_like_this']['ignore_tf']); + } + + /** + * @group unit + */ + public function testSetIgnoreTFDefault() + { + $query = new FuzzyLikeThis(); + + $data = $query->toArray(); + + $defaultIgnoreTF = false; + $this->assertEquals($defaultIgnoreTF, $data['fuzzy_like_this']['ignore_tf']); + } + + /** + * @group unit + */ + public function testSetMinSimilarity() + { + $query = new FuzzyLikeThis(); + + $similarity = 2; + $query->setMinSimilarity($similarity); + + $data = $query->toArray(); + + $this->assertEquals($similarity, $data['fuzzy_like_this']['min_similarity']); + } + + /** + * @group unit + */ + public function testSetBoost() + { + $query = new FuzzyLikeThis(); + + $boost = 2.2; + $query->setBoost($boost); + + $data = $query->toArray(); + + $this->assertEquals($boost, $data['fuzzy_like_this']['boost']); + } + + /** + * @group unit + */ + public function testAddAnalyzerViasetParam() + { + $analyzer = 'snowball'; + + $query = new FuzzyLikeThis(); + $query->setParam('analyzer', $analyzer); + + $data = $query->toArray(); + $this->assertEquals($analyzer, $data['fuzzy_like_this']['analyzer']); + } + + /** + * @group unit + */ + public function testSetAnalyzer() + { + $analyzer = 'snowball'; + + $query = new FuzzyLikeThis(); + $query->setAnalyzer($analyzer); + + $data = $query->toArray(); + $this->assertEquals($analyzer, $data['fuzzy_like_this']['analyzer']); + } + + /** + * @group unit + */ + public function testAnalyzerNotPresentInArrayToMaintainDefaultOfField() + { + $query = new FuzzyLikeThis(); + + $data = $query->toArray(); + $this->assertArrayNotHasKey('analyzer', $data); + } + + /** + * @group unit + */ + public function testArgArrayFieldsOverwrittenBySetParams() + { + $query = new FuzzyLikeThis(); + $query->setMaxQueryTerms(100); + $query->setParam('max_query_terms', 200); + + $data = $query->toArray(); + $this->assertEquals(200, $data['fuzzy_like_this']['max_query_terms']); + } + + /** + * @group functional + */ + public function testSearchSetAnalyzer() + { + $client = $this->_getClient(); + $index = new Index($client, 'test'); + $index->create(array('analysis' => array( + 'analyzer' => array( + 'searchAnalyzer' => array( + 'type' => 'custom', + 'tokenizer' => 'standard', + 'filter' => array('myStopWords'), + ), + ), + 'filter' => array( + 'myStopWords' => array( + 'type' => 'stop', + 'stopwords' => array('The'), + ), + ), + )), true); + + $index->getSettings()->setNumberOfReplicas(0); + //$index->getSettings()->setNumberOfShards(1); + + $type = new Type($index, 'helloworldfuzzy'); + $mapping = new Mapping($type, array( + 'email' => array('store' => 'yes', 'type' => 'string', 'index' => 'analyzed'), + 'content' => array('store' => 'yes', 'type' => 'string', 'index' => 'analyzed'), + )); + + $mapping->setSource(array('enabled' => false)); + $type->setMapping($mapping); + + $type->addDocuments(array( + new Document(1000, array('email' => 'testemail@gmail.com', 'content' => 'The Fuzzy Test!')), + new Document(1001, array('email' => 'testemail@gmail.com', 'content' => 'Elastica Fuzzy Test')), + )); + + // Refresh index + $index->refresh(); + + $fltQuery = new FuzzyLikeThis(); + $fltQuery->addFields(array('email', 'content')); + $fltQuery->setLikeText('The'); + + $fltQuery->setMinSimilarity(0.1); + $fltQuery->setMaxQueryTerms(3); + + // Test before analyzer applied, should return 1 result + $resultSet = $type->search($fltQuery); + $this->assertEquals(1, $resultSet->count()); + + $fltQuery->setParam('analyzer', 'searchAnalyzer'); + + $resultSet = $type->search($fltQuery); + $this->assertEquals(0, $resultSet->count()); + } + + /** + * @group functional + */ + public function testNoLikeTextProvidedShouldReturnNoResults() + { + $client = $this->_getClient(); + $index = new Index($client, 'test'); + $index->create(array(), true); + $index->getSettings()->setNumberOfReplicas(0); + + $type = new Type($index, 'helloworldfuzzy'); + $mapping = new Mapping($type, array( + 'email' => array('store' => 'yes', 'type' => 'string', 'index' => 'analyzed'), + 'content' => array('store' => 'yes', 'type' => 'string', 'index' => 'analyzed'), + )); + + $mapping->setSource(array('enabled' => false)); + $type->setMapping($mapping); + + $doc = new Document(1000, array('email' => 'testemail@gmail.com', 'content' => 'This is a sample post. Hello World Fuzzy Like This!')); + $type->addDocument($doc); + + // Refresh index + $index->refresh(); + + $fltQuery = new FuzzyLikeThis(); + $fltQuery->setLikeText(''); + $fltQuery->addFields(array('email', 'content')); + $fltQuery->setMinSimilarity(0.3); + $fltQuery->setMaxQueryTerms(3); + $resultSet = $type->search($fltQuery); + + $this->assertEquals(0, $resultSet->count()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/FuzzyTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/FuzzyTest.php new file mode 100644 index 00000000..e9107232 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/FuzzyTest.php @@ -0,0 +1,136 @@ +addField('user', array('value' => 'Nicolas', 'boost' => 1.0)); + $expectedArray = array( + 'fuzzy' => array( + 'user' => array( + 'value' => 'Nicolas', + 'boost' => 1.0, + ), + ), + ); + $this->assertEquals($expectedArray, $fuzzy->toArray(), 'Deprecated method failed'); + + $fuzzy = new Fuzzy('user', 'Nicolas'); + $expectedArray = array( + 'fuzzy' => array( + 'user' => array( + 'value' => 'Nicolas', + ), + ), + ); + $this->assertEquals($expectedArray, $fuzzy->toArray()); + + $fuzzy = new Fuzzy(); + $fuzzy->setField('user', 'Nicolas')->setFieldOption('boost', 1.0); + $expectedArray = array( + 'fuzzy' => array( + 'user' => array( + 'value' => 'Nicolas', + 'boost' => 1.0, + ), + ), + ); + $this->assertEquals($expectedArray, $fuzzy->toArray()); + } + + /** + * @group functional + */ + public function testQuery() + { + $client = $this->_getClient(); + $index = $client->getIndex('test'); + $index->create(array(), true); + $type = $index->getType('test'); + + $type->addDocuments(array( + new Document(1, array('name' => 'Basel-Stadt')), + new Document(2, array('name' => 'New York')), + new Document(3, array('name' => 'Baden')), + new Document(4, array('name' => 'Baden Baden')), + )); + + $index->refresh(); + + $field = 'name'; + + $query = new Fuzzy(); + $query->setField($field, 'Baden'); + + $resultSet = $index->search($query); + + $this->assertEquals(2, $resultSet->count()); + } + + /** + * @group unit + */ + public function testBadArguments() + { + $this->setExpectedException('Elastica\Exception\InvalidException'); + $query = new Fuzzy(); + $query->addField('name', array(array('value' => 'Baden'))); + + $this->setExpectedException('Elastica\Exception\InvalidException'); + $query = new Fuzzy(); + $query->setField('name', array()); + + $this->setExpectedException('Elastica\Exception\InvalidException'); + $query = new Fuzzy(); + $query->setField('name', 'value'); + $query->setField('name1', 'value1'); + } + + /** + * @group functional + */ + public function testFuzzyWithFacets() + { + $index = $this->_createIndex(); + $type = $index->getType('test'); + + $type->addDocuments(array( + new Document(1, array('name' => 'Basel-Stadt')), + new Document(2, array('name' => 'New York')), + new Document(3, array('name' => 'Baden')), + new Document(4, array('name' => 'Baden Baden')), + )); + + $index->refresh(); + + $field = 'name'; + + $fuzzyQuery = new Fuzzy(); + $fuzzyQuery->setField($field, 'Baden'); + + $facet = new \Elastica\Facet\Terms('test'); + $facet->setField('name'); + + $query = new \Elastica\Query($fuzzyQuery); + $query->addFacet($facet); + + $resultSet = $index->search($query); + + // Assert query worked ok + $this->assertEquals(2, $resultSet->count()); + + // Check Facets + $this->assertTrue($resultSet->hasFacets()); + $facets = $resultSet->getFacets(); + $this->assertEquals(2, $facets['test']['total']); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/HasChildTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/HasChildTest.php new file mode 100644 index 00000000..a717b1ab --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/HasChildTest.php @@ -0,0 +1,117 @@ + array( + 'query' => $q->toArray(), + 'type' => $type, + ), + ); + + $this->assertEquals($expectedArray, $query->toArray()); + } + + /** + * @group unit + */ + public function testSetScope() + { + $q = new MatchAll(); + + $type = 'test'; + + $scope = 'foo'; + + $query = new HasChild($q, $type); + $query->setScope($scope); + + $expectedArray = array( + 'has_child' => array( + 'query' => $q->toArray(), + 'type' => $type, + '_scope' => $scope, + ), + ); + + $this->assertEquals($expectedArray, $query->toArray()); + } + + /** + * @group functional + */ + public function testTypeInsideHasChildSearch() + { + $index = $this->_getTestIndex(); + + $f = new Match(); + $f->setField('alt.name', 'testname'); + $query = new HasChild($f, 'child'); + + $searchQuery = new Query(); + $searchQuery->setQuery($query); + $searchResults = $index->search($searchQuery); + + $this->assertEquals(1, $searchResults->count()); + + $result = $searchResults->current()->getData(); + $expected = array('id' => 'parent2', 'user' => 'parent2', 'email' => 'parent2@test.com'); + + $this->assertEquals($expected, $result); + } + + protected function _getTestIndex() + { + $index = $this->_createIndex('has_child_test'); + + $parentType = $index->getType('parent'); + + $childType = $index->getType('child'); + $childMapping = new Mapping($childType); + $childMapping->setParent('parent'); + $childMapping->send(); + + $altType = $index->getType('alt'); + $altDoc = new Document('alt1', array('name' => 'altname')); + $altType->addDocument($altDoc); + + $parent1 = new Document('parent1', array('id' => 'parent1', 'user' => 'parent1', 'email' => 'parent1@test.com')); + $parentType->addDocument($parent1); + $parent2 = new Document('parent2', array('id' => 'parent2', 'user' => 'parent2', 'email' => 'parent2@test.com')); + $parentType->addDocument($parent2); + + $child1 = new Document('child1', array('id' => 'child1', 'user' => 'child1', 'email' => 'child1@test.com')); + $child1->setParent('parent1'); + $childType->addDocument($child1); + $child2 = new Document('child2', array('id' => 'child2', 'user' => 'child2', 'email' => 'child2@test.com')); + $child2->setParent('parent2'); + $childType->addDocument($child2); + $child3 = new Document('child3', array('id' => 'child3', 'user' => 'child3', 'email' => 'child3@test.com', 'alt' => array(array('name' => 'testname')))); + $child3->setParent('parent2'); + $childType->addDocument($child3); + + $index->refresh(); + + return $index; + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/HasParentTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/HasParentTest.php new file mode 100644 index 00000000..31a89852 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/HasParentTest.php @@ -0,0 +1,108 @@ + array( + 'query' => $q->toArray(), + 'type' => $type, + ), + ); + + $this->assertEquals($expectedArray, $query->toArray()); + } + + /** + * @group unit + */ + public function testSetScope() + { + $q = new MatchAll(); + + $type = 'test'; + + $scope = 'foo'; + + $query = new HasParent($q, $type); + $query->setScope($scope); + + $expectedArray = array( + 'has_parent' => array( + 'query' => $q->toArray(), + 'type' => $type, + '_scope' => $scope, + ), + ); + + $this->assertEquals($expectedArray, $query->toArray()); + } + + /** + * @group functional + */ + public function testHasParent() + { + $index = $this->_createIndex(); + + $shopType = $index->getType('shop'); + $productType = $index->getType('product'); + $mapping = new Mapping(); + $mapping->setParent('shop'); + $productType->setMapping($mapping); + + $shopType->addDocuments( + array( + new Document('zurich', array('brand' => 'google')), + new Document('london', array('brand' => 'apple')), + ) + ); + + $doc1 = new Document(1, array('device' => 'chromebook')); + $doc1->setParent('zurich'); + + $doc2 = new Document(2, array('device' => 'macmini')); + $doc2->setParent('london'); + + $productType->addDocument($doc1); + $productType->addDocument($doc2); + + $index->refresh(); + + // All documents + $parentQuery = new HasParent(new MatchAll(), $shopType->getName()); + $search = new Search($index->getClient()); + $results = $search->search($parentQuery); + $this->assertEquals(2, $results->count()); + + $match = new Match(); + $match->setField('brand', 'google'); + + $parentQuery = new HasParent($match, $shopType->getName()); + $search = new Search($index->getClient()); + $results = $search->search($parentQuery); + $this->assertEquals(1, $results->count()); + $result = $results->current(); + $data = $result->getData(); + $this->assertEquals($data['device'], 'chromebook'); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/HighlightTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/HighlightTest.php new file mode 100644 index 00000000..c6850019 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/HighlightTest.php @@ -0,0 +1,48 @@ +_createIndex(); + $type = $index->getType('helloworld'); + + $phrase = 'My name is ruflin'; + + $type->addDocuments(array( + new Document(1, array('id' => 1, 'phrase' => $phrase, 'username' => 'hanswurst', 'test' => array('2', '3', '5'))), + new Document(2, array('id' => 2, 'phrase' => $phrase, 'username' => 'peter', 'test' => array('2', '3', '5'))), + )); + + $queryString = new QueryString('rufl*'); + $query = new Query($queryString); + $query->setHighlight(array( + 'pre_tags' => array(''), + 'post_tags' => array(''), + 'fields' => array( + 'phrase' => array( + 'fragment_size' => 200, + 'number_of_fragments' => 1, + ), + ), + )); + + $index->refresh(); + + $resultSet = $type->search($query); + foreach ($resultSet as $result) { + $highlight = $result->getHighlights(); + $this->assertEquals(array('phrase' => array(0 => 'My name is ruflin')), $highlight); + } + $this->assertEquals(2, $resultSet->count()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/IdsTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/IdsTest.php new file mode 100644 index 00000000..7b39020f --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/IdsTest.php @@ -0,0 +1,187 @@ +_createIndex(); + + $type1 = $index->getType('helloworld1'); + $type2 = $index->getType('helloworld2'); + + $doc = new Document(1, array('name' => 'hello world')); + $type1->addDocument($doc); + + $doc = new Document(2, array('name' => 'nicolas ruflin')); + $type1->addDocument($doc); + + $doc = new Document(3, array('name' => 'ruflin')); + $type1->addDocument($doc); + + $doc = new Document(4, array('name' => 'hello world again')); + $type2->addDocument($doc); + + $index->refresh(); + + $this->_type = $type1; + $this->_index = $index; + } + + /** + * @group functional + */ + public function testSetIdsSearchSingle() + { + $query = new Ids(); + $query->setIds('1'); + + $resultSet = $this->_type->search($query); + + $this->assertEquals(1, $resultSet->count()); + } + + /** + * @group functional + */ + public function testSetIdsSearchArray() + { + $query = new Ids(); + $query->setIds(array('1', '2')); + + $resultSet = $this->_type->search($query); + + $this->assertEquals(2, $resultSet->count()); + } + + /** + * @group functional + */ + public function testAddIdsSearchSingle() + { + $query = new Ids(); + $query->addId('3'); + + $resultSet = $this->_type->search($query); + + $this->assertEquals(1, $resultSet->count()); + } + + /** + * @group functional + */ + public function testComboIdsSearchArray() + { + $query = new Ids(); + + $query->setIds(array('1', '2')); + $query->addId('3'); + + $resultSet = $this->_type->search($query); + + $this->assertEquals(3, $resultSet->count()); + } + + /** + * @group functional + */ + public function testSetTypeSingleSearchSingle() + { + $query = new Ids(); + + $query->setIds('1'); + $query->setType('helloworld1'); + + $resultSet = $this->_index->search($query); + + $this->assertEquals(1, $resultSet->count()); + } + + /** + * @group functional + */ + public function testSetTypeSingleSearchArray() + { + $query = new Ids(); + + $query->setIds(array('1', '2')); + $query->setType('helloworld1'); + + $resultSet = $this->_index->search($query); + + $this->assertEquals(2, $resultSet->count()); + } + + /** + * @group functional + */ + public function testSetTypeSingleSearchSingleDocInOtherType() + { + $query = new Ids(); + + // Doc 4 is in the second type... + $query->setIds('4'); + $query->setType('helloworld1'); + + $resultSet = $this->_index->search($query); + + // ...therefore 0 results should be returned + $this->assertEquals(0, $resultSet->count()); + } + + /** + * @group functional + */ + public function testSetTypeSingleSearchArrayDocInOtherType() + { + $query = new Ids(); + + // Doc 4 is in the second type... + $query->setIds(array('1', '4')); + $query->setType('helloworld1'); + + $resultSet = $this->_index->search($query); + + // ...therefore only 1 result should be returned + $this->assertEquals(1, $resultSet->count()); + } + + /** + * @group functional + */ + public function testSetTypeArraySearchArray() + { + $query = new Ids(); + + $query->setIds(array('1', '4')); + $query->setType(array('helloworld1', 'helloworld2')); + + $resultSet = $this->_index->search($query); + + $this->assertEquals(2, $resultSet->count()); + } + + /** + * @group functional + */ + public function testSetTypeArraySearchSingle() + { + $query = new Ids(); + + $query->setIds('4'); + $query->setType(array('helloworld1', 'helloworld2')); + + $resultSet = $this->_index->search($query); + + $this->assertEquals(1, $resultSet->count()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/ImageTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/ImageTest.php new file mode 100644 index 00000000..989e4e4a --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/ImageTest.php @@ -0,0 +1,159 @@ +_testFileContent = base64_encode(file_get_contents(BASE_PATH.'/data/test.jpg')); + } + + /** + * @group unit + */ + public function testToArrayFromReference() + { + $client = $this->_getClient(); + $index = new Index($client, 'test'); + $type = new Type($index, 'helloworld'); + $field = 'image'; + + $query = new Image(); + $query->setFieldFeature($field, 'CEDD'); + $query->setFieldHash($field, 'BIT_SAMPLING'); + $query->setFieldBoost($field, 100); + + $query->setImageByReference($field, $index->getName(), $type->getName(), 10); + + $jsonString = '{"image":{"image":{"feature":"CEDD","hash":"BIT_SAMPLING","boost":100,"index":"test","type":"helloworld","id":10,"path":"image"}}}'; + $this->assertEquals($jsonString, json_encode($query->toArray())); + } + + /** + * @group unit + */ + public function testToArrayFromImage() + { + $field = 'image'; + + $query = new Image(); + $query->setFieldFeature($field, 'CEDD'); + $query->setFieldHash($field, 'BIT_SAMPLING'); + $query->setFieldBoost($field, 100); + + $query->setFieldImage($field, BASE_PATH.'/data/test.jpg'); + + $jsonString = '{"image":{"image":{"feature":"CEDD","hash":"BIT_SAMPLING","boost":100,"image":"\/9j\/4QAYRXhpZgAASUkqAAgAAAAAAAAAAAAAAP\/sABFEdWNreQABAAQAAAA8AAD\/4QN6aHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wLwA8P3hwYWNrZXQgYmVnaW49Iu+7vyIgaWQ9Ilc1TTBNcENlaGlIenJlU3pOVGN6a2M5ZCI\/PiA8eDp4bXBtZXRhIHhtbG5zOng9ImFkb2JlOm5zOm1ldGEvIiB4OnhtcHRrPSJBZG9iZSBYTVAgQ29yZSA1LjUtYzAyMSA3OS4xNTQ5MTEsIDIwMTMvMTAvMjktMTE6NDc6MTYgICAgICAgICI+IDxyZGY6UkRGIHhtbG5zOnJkZj0iaHR0cDovL3d3dy53My5vcmcvMTk5OS8wMi8yMi1yZGYtc3ludGF4LW5zIyI+IDxyZGY6RGVzY3JpcHRpb24gcmRmOmFib3V0PSIiIHhtbG5zOnhtcE1NPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvbW0vIiB4bWxuczpzdFJlZj0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL3NUeXBlL1Jlc291cmNlUmVmIyIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIiB4bXBNTTpPcmlnaW5hbERvY3VtZW50SUQ9InhtcC5kaWQ6OWQ4MjQ5N2MtNzViMS0wYzQ5LTg4ZjMtMDdiNmRhMjU0ZWRhIiB4bXBNTTpEb2N1bWVudElEPSJ4bXAuZGlkOjA4NjBGM0Y1QkJGQTExRTM4MjQ0QzMzNjU2MjUxOEJGIiB4bXBNTTpJbnN0YW5jZUlEPSJ4bXAuaWlkOjA4NjBGM0Y0QkJGQTExRTM4MjQ0QzMzNjU2MjUxOEJGIiB4bXA6Q3JlYXRvclRvb2w9IkFkb2JlIFBob3Rvc2hvcCBDQyAoV2luZG93cykiPiA8eG1wTU06RGVyaXZlZEZyb20gc3RSZWY6aW5zdGFuY2VJRD0ieG1wLmlpZDo5ZDgyNDk3Yy03NWIxLTBjNDktODhmMy0wN2I2ZGEyNTRlZGEiIHN0UmVmOmRvY3VtZW50SUQ9InhtcC5kaWQ6OWQ4MjQ5N2MtNzViMS0wYzQ5LTg4ZjMtMDdiNmRhMjU0ZWRhIi8+IDwvcmRmOkRlc2NyaXB0aW9uPiA8L3JkZjpSREY+IDwveDp4bXBtZXRhPiA8P3hwYWNrZXQgZW5kPSJyIj8+\/+4ADkFkb2JlAGTAAAAAAf\/bAIQABgQEBAUEBgUFBgkGBQYJCwgGBggLDAoKCwoKDBAMDAwMDAwQDA4PEA8ODBMTFBQTExwbGxscHx8fHx8fHx8fHwEHBwcNDA0YEBAYGhURFRofHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8f\/8AAEQgAZABkAwERAAIRAQMRAf\/EAKwAAAEFAQEBAQAAAAAAAAAAAAgAAwQFBwYCAQkBAQEAAwEBAAAAAAAAAAAAAAABAgQFAwYQAAEDAgMCBQwOCQQDAAAAAAIBAwQABRESBiEHMUFhExRRcYEisnPDNIQVRRaRodEyQlJikiOzVGYnCLFyo9NklKQlGIIzQ2VTdhcRAAIBAgQEBAYDAQAAAAAAAAABAhEDITESBEFRMgVhcZFCgaHR4SITsVJiFf\/aAAwDAQACEQMRAD8AJyHDhlDYImG1JWwVVUBVVVRTkqge6DC+ztfMH3KgF0GF9na+YPuUAugwvs7XzB9ygF0GF9na+YPuUBR6n1NpDTTAuXUmgdcTFmK22JvuYfFbRMcOVdnLXje3MLa\/JmEpqOZmty32GbiparFGaa24OS1QzXlyNoiJ85a50+5y9q9Tyd7kiva3v6jQ8xwraY\/E5gh9tDrBdxueBP2s6ax74rK6YtXq0DFx2LJjIjzafrAooaJ1sa2LfcU+pUM1eXE0i3nY7jEblwUjyYzqYg62gEK+wnDyV0IzUlVOqPVNMkdBhfZ2vmD7lZFF0GF9na+YPuUBD6JF88c3zIc30fNkyphjnwxwwqkJkHxKP3oO5SoUfoBUAqA47eXr5rSlqEY6C7eJuIwmC2iKJ755xE+AGPZXZWpu9z+qOHU8jzuXNK8Qe813vd2UlV243WaWJmvbOGqcarsQQHsCKVw4Qnclh+UmamLZ3dn3UKQCd1lkprwx42CCnIrpIqqvWSuva7Uve\/Q9la5l0W6rT6hgCSQPiLnlL2iTCth9ss+PqX9aOcv27W725spEEimsDtJpRyvoicaInan2NvJWjf7dKKrB6l8\/uYuDRWaT1fdNN3BJcIlNhxU6XCVcG3hTh\/VcT4Jdhdlali+7bqsiRk0ETZrvBvFsj3KCfORZI5wVdipxKJJxEK7FSu9bmpxTWRtJ1VSbWZSF6b8m8JVA9B8Sj96DuUqAfoDNdab+dKaZvDtnGPIuk6MqDMSNkRtolTHIpuEKKaIqYoOOHHtoCz3bb07frsriMO3yIPm7ms\/SCbLPz2bDLkIuDJx0Bh2u9SHf9VXG5kalHEyYhp8WOwqiOCfKXE+zXzO4u67jkaM5VdTSt3ulAtdrB54E84zBFyUfGIr2wMovUFOHqlX0Gy2ytQ\/08\/oe8I0R3LTIinBW6ZEphGxLthx61Ggiw6Mw8zwIQrXk20zMxXexpMLZLG7xRysSTyShRMERxdouf68MC5a5HcLCi9a45+fP4nhcjTEsdx2oTbuEywun9DIBZcUV4nAVBdRP1kUS7FTt12jcfiWzLgbJXWNghem\/JvCVQPQfEo\/eg7lKgH6AC1m2u6h3hnajeVty63h9hySqZlFDkGpHgvCqCi4VQEvp3dvYNA2a+yLI7KcclxlN1ZTgubY7ZqCjlEMPfLjXncdIvyI8gfbLGR+4W1hzaDzzAn1lMca+b28azinzRoxWISEIUwxw4Vxr642mTxSqQfbCowTI+IqvUVNtYSM4nI70GG39JXNC4W2VcFeoQEhItae9VbMjG50syPdfKNnXtmUOFxxxov1TZPH9FcfaOl2J423+SCVr6A2yF6b8m8JVA9B8Sj96DuUqAfoAOtGF+MtrT\/v3vrXaoDAmRhlRH4x+9fbJsusYqK\/prGSqqBgjgj9ul82YqMm3vZCBeHPHcwVOyoV8zGsJeMX\/AAaGQRdjlNTobctgkJl4BdbVOMTTH2uCvrIyUkmsmbWZbtJjtrMhKbGsWyj2wRxrBmSM73v3lqHpd+Pm+muBDHaHjUcUJxewKVz+43KW6cZYfUwuukfM4PczbHJuuor+XFm3tOyHC6iqPNB7Z1ztjGt1eB5WlWQRdd42yF6b8m8JVA9B8Sj96DuUqA5jeTvJtWhLSzMnMPSn5hmzBjsiioTwgpojhqqIA7Npe1QA2borLf77vMtFwYiOOMx5x3C5TEAkjtoucyTnFTLiRnlEccaoDBqAH7fhpB60X71jihjbLqSJLUU2NS8MMS6gvImxfjJypXG31jTLWsma16NHUh7ud4Q2VUt1wNUtxkqsv7V5kiXFUJE282q7dnvV5Kz2W9UFon08Hy+xjCdDaIdyYkNi82Ym04mIOAqEJIvGhJsrtp1VVkexYDLbRMcajZUVWotV2qzQilXGQLDSJ2grtM1+K2HCS14Xr0barJ\/V+RXJLMH7VWp52qr10lwVajtpzcONjjzbeOOK4cJku0q+d3O4c5apYcvBGpOdXU3XdTo0tPWJX5LeS43DK48K++BtE+jbXl2qq8q11e3bdwhql1S+S4GzZhRVZ21dA9SF6b8m8JVA9B8Sj96DuUqA5feRu0tOvLbGh3CVIhlCcJ+K7GUNjhAoduJiWYUx4NnXoAbdz+p9R6f3m2yyx5riwJVwdt1wgqZLGcwU2+dFtVwE0IEJCTbxLilUBg1AVmpVsnmKYF7Fpy1uN83IbfVEA0NUERxXjIlRE5axkk1R5ElSmIKWoLQ9Y7m9HUSSOhKraEqkQCq4oKl8LBNmbjrlbrt8oOsMY\/NGq7b4Huz6juduPC2z3IxLtVts+1XrguI+1WjC7KHS3EwToXbu8PWxt5EupgnxgFsS9lBr1e8vP3My1y5lCy1e7\/cubYGTd7ia4YBmePsrtQE66ola6UpvCsn6mGLZtu7XdAtoNu66gyOXAVQmIIKhtslxEZcBmnFh2qcvDXT23b6PVcz4I2LdmmLNTrqnuKgIXpvybwlUD0HxKP3oO5SoB+gAo0Qf43WlPvC\/9c9VAa9QGC79NZjcLoOnIp5oVtJHJypwHKVO1DlRoVxX5S8lcff39UtCyWZq35VwMrLUN3Pmbc235yJ0xYhxDxJxTNcoNtn75MVWvXbbqcaLqRhCbQTunN2OloOmIlquFriy3hHnJTjrYuKr57XMDVMcEXYmHEldCduM+pI29KeZIa3XbvWjQwsETMm3aGZPYVVSvJbS1\/VE\/XHkYlvp3zav3fa8LTWlQtsK2JDjyBZKIiqrjqmhe8NtPgJxV7xilkZJULrcV+Ya86v1H6ranjRxnyGnHrdPiCTYuKymZxpxsiPAsmJCQrhs4KpTfKAVAQvTfk3hKoHoPiUfvQdylQD9ABHoYvxxtP8A7E\/9c9VAW28PV7WltNPzhUSnO\/QW5lfhPmi5VVPignbFyJWvub6twb48DCc9KqChcZZqpm44rjpkRuOltIzJcxEvKSrjXz0at1ZpGk\/l10Ot0vD2sZ7eMO2kUe0iSYoclUwdewX\/AMQrkH5Sr1K7Gzte5nvZhxCMroGyKgOe1ToPR2o4stLzZ4cx6QwrBynGGykICIuXK8qZxUVXEcF2LQAafl6JR3zaYRSXY7KDMvCuEV5NvXwoA7KAVAQvTfk3hKoHoPiUfvQdylQD9AA9oUvx0tKfeN\/656qAjN48yyXqZJ09emnIc+P9JalLATNvKirJil71xMVyOBwphtTai15XbMbkaSMZxTWJiZ7utS3a+x7PazbkdKcQFliuVGWse3dMC29oO3BMcV2Vzf8Anzi+aNd2WFhp2w27T9jhWW3BzcKA0LLI8aonCRdUiXElXq11IxSVEbKVFQsayKKgESISKK7UXYqUAAmu7A9uz1+o2K\/xJsq2yykQXoh85IikJqoty21Hm0MUVRIcxIqcOHBQBhblNb3bW+7i16juwMNz5ZPg8MUSFr6F82kVBMjVMUDFdvDQHc0BC9N+TeEqgeg+JR+9B3KVAP0ADOgy\/Ha0J95H\/rnqoDT1Fpiwajt62+9wm50VVQxFxFQgNOA2zFUNs04iBUWoCn0Xu4tGkpEx+HMmTSlZRBZ7ovkyCYrkBzKJqir8dSXZw0B1dAKgFQHl1VFo1HhQVVOvhQH567obLA1XvYsNnvIrJhzpjzs0CVfpUaBx9RNeHAyDAuSgP0Et9ut9tiNwrdFZhQ2UwajR2xaaBFXHtQBEFNvUSgJFAQvTfk3hKoHoPiUfvQdylQD9AABrKPedG7y7o2RlBu1tujs6A+qIiqJPq8w+CFsICEk9tKoO2\/yy3qIiY+aNibV6M5t5f9+oD5\/lpvV\/6j+Wc\/f0B5X82u9Xi80fyzn7+gPJfm43qim3zR\/LOfv6AZP83+9ZODzP\/LO\/v6Ag3P8ANvvZm2+TC5y2xkktk0siPHNHQQ0wUm1N0xQuoqitARPyqafn3XfDbJ0dslhWRqRJmPInaijjJstipcGYzc2Jx4L1KAO2gFQEL035N4SqBmJ546KzzfR+byDkzZ8cMEwxwoQe\/vf8N+0oU5bWfqdjH9c\/V7NgvRfO3NZsMdvN8\/twx4cKA5j8BuP1K\/pKA+fgL9yv6OgF+An3J\/o6A8r\/APAOP1I7PQ6A8F\/j1x+o\/Z6FQHgv8deP1F7PQqA7rRnq\/wCal9S\/MfmrOubzRzfMc5gmObo\/a5sOrtoC\/wD73\/DftKAX97\/hv2lARP7p50\/4Of5j5eTJn9nHGhD\/2Q=="}}}'; + $this->assertEquals($jsonString, json_encode($query->toArray())); + } + + /** + * @group functional + */ + public function testFromReference() + { + $this->markTestSkipped('Tests skipped as plugin not working properly with ES 1.6.0. See https://github.com/ruflin/Elastica/pull/881'); + $field = 'image'; + + $client = $this->_getClient(); + $index = $client->getIndex('test'); + $index->create(array(), true); + + $type = $index->getType('test'); + + $mapping = new Mapping($type, array( + $field => array( + 'type' => 'image', + 'store' => false, + 'include_in_all' => false, + 'feature' => array( + 'CEDD' => array( + 'hash' => 'BIT_SAMPLING', + ), + ), + ), + ) + ); + + $type->setMapping($mapping); + + $type->addDocuments(array( + new Document(1, array($field => $this->_testFileContent)), + new Document(2, array($field => $this->_testFileContent)), + new Document(3, array($field => $this->_testFileContent)), + )); + + $index->refresh(); + + $query = new Image(); + $query->setFieldFeature($field, 'CEDD'); + $query->setFieldHash($field, 'BIT_SAMPLING'); + $query->setFieldBoost($field, 100); + $query->setImageByReference($field, $index->getName(), $type->getName(), 1); + + $resultSet = $index->search($query); + $this->assertEquals(3, $resultSet->count()); + } + + /** + * @group functional + */ + public function testFromImage() + { + $this->markTestSkipped('Tests skipped as plugin not working properly with ES 1.6.0. See https://github.com/ruflin/Elastica/pull/881'); + + $field = 'image'; + + $client = $this->_getClient(); + $index = $client->getIndex('test'); + $index->create(array(), true); + + $type = $index->getType('test'); + + $mapping = new Mapping($type, array( + $field => array( + 'type' => 'image', + 'store' => false, + 'include_in_all' => false, + 'feature' => array( + 'CEDD' => array( + 'hash' => 'BIT_SAMPLING', + ), + ), + ), + ) + ); + + $type->setMapping($mapping); + + $type->addDocuments(array( + new Document(1, array($field => $this->_testFileContent)), + new Document(2, array($field => $this->_testFileContent)), + new Document(3, array($field => $this->_testFileContent)), + )); + + $index->refresh(); + + $query = new Image(); + $query->setFieldFeature($field, 'CEDD'); + $query->setFieldHash($field, 'BIT_SAMPLING'); + $query->setFieldBoost($field, 100); + $query->setFieldImage($field, BASE_PATH.'/data/test.jpg'); + + $resultSet = $index->search($query); + $this->assertEquals(3, $resultSet->count()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/MatchAllTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/MatchAllTest.php new file mode 100644 index 00000000..7eb0fae0 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/MatchAllTest.php @@ -0,0 +1,49 @@ + new \stdClass()); + + $this->assertEquals($expectedArray, $query->toArray()); + } + + /** + * @group functional + */ + public function testMatchAllIndicesTypes() + { + $index1 = $this->_createIndex(); + $index2 = $this->_createIndex(); + + $client = $index1->getClient(); + + $search1 = new Search($client); + $resultSet1 = $search1->search(new MatchAll()); + + $doc1 = new Document(1, array('name' => 'ruflin')); + $doc2 = new Document(1, array('name' => 'ruflin')); + $index1->getType('test')->addDocument($doc1); + $index2->getType('test')->addDocument($doc2); + + $index1->refresh(); + $index2->refresh(); + + $search2 = new Search($client); + $resultSet2 = $search2->search(new MatchAll()); + + $this->assertEquals($resultSet1->getTotalHits() + 2, $resultSet2->getTotalHits()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/MatchTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/MatchTest.php new file mode 100644 index 00000000..98fdf26a --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/MatchTest.php @@ -0,0 +1,339 @@ +setFieldQuery($field, $testQuery); + $query->setFieldType($field, $type); + $query->setFieldOperator($field, $operator); + $query->setFieldAnalyzer($field, $analyzer); + $query->setFieldBoost($field, $boost); + $query->setFieldMinimumShouldMatch($field, $minimumShouldMatch); + $query->setFieldFuzziness($field, $fuzziness); + $query->setFieldFuzzyRewrite($field, $fuzzyRewrite); + $query->setFieldPrefixLength($field, $prefixLength); + $query->setFieldMaxExpansions($field, $maxExpansions); + + $expectedArray = array( + 'match' => array( + $field => array( + 'query' => $testQuery, + 'type' => $type, + 'operator' => $operator, + 'analyzer' => $analyzer, + 'boost' => $boost, + 'minimum_should_match' => $minimumShouldMatch, + 'fuzziness' => $fuzziness, + 'fuzzy_rewrite' => $fuzzyRewrite, + 'prefix_length' => $prefixLength, + 'max_expansions' => $maxExpansions, + ), + ), + ); + + $this->assertEquals($expectedArray, $query->toArray()); + } + + /** + * @group functional + */ + public function testMatch() + { + $client = $this->_getClient(); + $index = $client->getIndex('test'); + $index->create(array(), true); + $type = $index->getType('test'); + + $type->addDocuments(array( + new Document(1, array('name' => 'Basel-Stadt')), + new Document(2, array('name' => 'New York')), + new Document(3, array('name' => 'New Hampshire')), + new Document(4, array('name' => 'Basel Land')), + )); + + $index->refresh(); + + $field = 'name'; + $operator = 'or'; + + $query = new Match(); + $query->setFieldQuery($field, 'Basel New'); + $query->setFieldOperator($field, $operator); + + $resultSet = $index->search($query); + + $this->assertEquals(4, $resultSet->count()); + } + + /** + * @group functional + */ + public function testMatchSetFieldBoost() + { + $client = $this->_getClient(); + $index = $client->getIndex('test'); + $index->create(array(), true); + $type = $index->getType('test'); + + $type->addDocuments(array( + new Document(1, array('name' => 'Basel-Stadt')), + new Document(2, array('name' => 'New York')), + new Document(3, array('name' => 'New Hampshire')), + new Document(4, array('name' => 'Basel Land')), + )); + + $index->refresh(); + + $field = 'name'; + $operator = 'or'; + + $query = new Match(); + $query->setFieldQuery($field, 'Basel New'); + $query->setFieldOperator($field, $operator); + $query->setFieldBoost($field, 1.2); + + $resultSet = $index->search($query); + + $this->assertEquals(4, $resultSet->count()); + } + + /** + * @group functional + */ + public function testMatchSetFieldBoostWithString() + { + $client = $this->_getClient(); + $index = $client->getIndex('test'); + $index->create(array(), true); + $type = $index->getType('test'); + + $type->addDocuments(array( + new Document(1, array('name' => 'Basel-Stadt')), + new Document(2, array('name' => 'New York')), + new Document(3, array('name' => 'New Hampshire')), + new Document(4, array('name' => 'Basel Land')), + )); + + $index->refresh(); + + $field = 'name'; + $operator = 'or'; + + $query = new Match(); + $query->setFieldQuery($field, 'Basel New'); + $query->setFieldOperator($field, $operator); + $query->setFieldBoost($field, '1.2'); + + $resultSet = $index->search($query); + + $this->assertEquals(4, $resultSet->count()); + } + + /** + * @group functional + */ + public function testMatchZeroTerm() + { + $client = $this->_getClient(); + $index = $client->getIndex('test'); + $index->create(array(), true); + $type = $index->getType('test'); + + $type->addDocuments(array( + new Document(1, array('name' => 'Basel-Stadt')), + new Document(2, array('name' => 'New York')), + )); + + $index->refresh(); + + $query = new Match(); + $query->setFieldQuery('name', ''); + $query->setFieldZeroTermsQuery('name', Match::ZERO_TERM_ALL); + + $resultSet = $index->search($query); + + $this->assertEquals(2, $resultSet->count()); + } + + /** + * @group functional + */ + public function testMatchPhrase() + { + $client = $this->_getClient(); + $index = $client->getIndex('test'); + $index->create(array(), true); + $type = $index->getType('test'); + + $type->addDocuments(array( + new Document(1, array('name' => 'Basel-Stadt')), + new Document(2, array('name' => 'New York')), + new Document(3, array('name' => 'New Hampshire')), + new Document(4, array('name' => 'Basel Land')), + )); + + $index->refresh(); + + $field = 'name'; + $type = 'phrase'; + + $query = new Match(); + $query->setFieldQuery($field, 'New York'); + $query->setFieldType($field, $type); + + $resultSet = $index->search($query); + + $this->assertEquals(1, $resultSet->count()); + } + + /** + * @group functional + */ + public function testMatchPhraseAlias() + { + $client = $this->_getClient(); + $index = $client->getIndex('test'); + $index->create(array(), true); + $type = $index->getType('test'); + + $type->addDocuments(array( + new Document(1, array('name' => 'Basel-Stadt')), + new Document(2, array('name' => 'New York')), + new Document(3, array('name' => 'New Hampshire')), + new Document(4, array('name' => 'Basel Land')), + )); + + $index->refresh(); + + $field = 'name'; + + $query = new MatchPhrase(); + $query->setFieldQuery($field, 'New York'); + + $resultSet = $index->search($query); + + $this->assertEquals(1, $resultSet->count()); + } + + /** + * @group functional + */ + public function testMatchPhrasePrefix() + { + $client = $this->_getClient(); + $index = $client->getIndex('test'); + $index->create(array(), true); + $type = $index->getType('test'); + + $type->addDocuments(array( + new Document(1, array('name' => 'Basel-Stadt')), + new Document(2, array('name' => 'New York')), + new Document(3, array('name' => 'New Hampshire')), + new Document(4, array('name' => 'Basel Land')), + )); + + $index->refresh(); + + $field = 'name'; + $type = 'phrase_prefix'; + + $query = new Match(); + $query->setFieldQuery($field, 'New'); + $query->setFieldType($field, $type); + + $resultSet = $index->search($query); + + $this->assertEquals(2, $resultSet->count()); + } + + /** + * @group functional + */ + public function testMatchPhrasePrefixAlias() + { + $client = $this->_getClient(); + $index = $client->getIndex('test'); + $index->create(array(), true); + $type = $index->getType('test'); + + $type->addDocuments(array( + new Document(1, array('name' => 'Basel-Stadt')), + new Document(2, array('name' => 'New York')), + new Document(3, array('name' => 'New Hampshire')), + new Document(4, array('name' => 'Basel Land')), + )); + + $index->refresh(); + + $field = 'name'; + + $query = new MatchPhrasePrefix(); + $query->setFieldQuery($field, 'New'); + + $resultSet = $index->search($query); + + $this->assertEquals(2, $resultSet->count()); + } + + /** + * @group unit + */ + public function testMatchFuzzinessType() + { + $field = 'test'; + $query = new Match(); + + $fuzziness = 'AUTO'; + $query->setFieldFuzziness($field, $fuzziness); + + $parameters = $query->getParam($field); + $this->assertEquals($fuzziness, $parameters['fuzziness']); + + $fuzziness = 0.3; + $query->setFieldFuzziness($field, $fuzziness); + + $parameters = $query->getParam($field); + $this->assertEquals($fuzziness, $parameters['fuzziness']); + } + + /** + * @group unit + */ + public function testConstruct() + { + $match = new Match(null, 'values'); + $this->assertEquals(array('match' => array()), $match->toArray()); + + $match = new Match('field', null); + $this->assertEquals(array('match' => array()), $match->toArray()); + + $match1 = new Match('field', 'values'); + $match2 = new Match(); + $match2->setField('field', 'values'); + $this->assertEquals($match1->toArray(), $match2->toArray()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/MoreLikeThisTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/MoreLikeThisTest.php new file mode 100644 index 00000000..5dfe9379 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/MoreLikeThisTest.php @@ -0,0 +1,240 @@ +_getClient(); + $index = new Index($client, 'test'); + $index->create(array(), true); + $index->getSettings()->setNumberOfReplicas(0); + //$index->getSettings()->setNumberOfShards(1); + + $type = new Type($index, 'helloworldmlt'); + $mapping = new Mapping($type, array( + 'email' => array('store' => 'yes', 'type' => 'string', 'index' => 'analyzed'), + 'content' => array('store' => 'yes', 'type' => 'string', 'index' => 'analyzed'), + )); + + $mapping->setSource(array('enabled' => false)); + $type->setMapping($mapping); + + $doc = new Document(1000, array('email' => 'testemail@gmail.com', 'content' => 'This is a sample post. Hello World Fuzzy Like This!')); + $type->addDocument($doc); + + $doc = new Document(1001, array('email' => 'nospam@gmail.com', 'content' => 'This is a fake nospam email address for gmail')); + $type->addDocument($doc); + + // Refresh index + $index->refresh(); + + $mltQuery = new MoreLikeThis(); + $mltQuery->setLikeText('fake gmail sample'); + $mltQuery->setFields(array('email', 'content')); + $mltQuery->setMaxQueryTerms(1); + $mltQuery->setMinDocFrequency(1); + $mltQuery->setMinTermFrequency(1); + + $query = new Query(); + $query->setFields(array('email', 'content')); + $query->setQuery($mltQuery); + + $resultSet = $type->search($query); + $resultSet->getResponse()->getData(); + $this->assertEquals(2, $resultSet->count()); + } + + /** + * @group unit + */ + public function testSetFields() + { + $query = new MoreLikeThis(); + + $fields = array('firstname', 'lastname'); + $query->setFields($fields); + + $data = $query->toArray(); + $this->assertEquals($fields, $data['more_like_this']['fields']); + } + + /** + * @group unit + */ + public function testSetIds() + { + $query = new MoreLikeThis(); + $ids = array(1, 2, 3); + $query->setIds($ids); + + $data = $query->toArray(); + $this->assertEquals($ids, $data['more_like_this']['ids']); + } + + /** + * @group unit + */ + public function testSetLikeText() + { + $query = new MoreLikeThis(); + $query->setLikeText(' hello world'); + + $data = $query->toArray(); + $this->assertEquals('hello world', $data['more_like_this']['like_text']); + } + + /** + * @group unit + */ + public function testSetBoost() + { + $query = new MoreLikeThis(); + + $boost = 1.3; + $query->setBoost($boost); + + $this->assertEquals($boost, $query->getParam('boost')); + } + + /** + * @group unit + */ + public function testSetMaxQueryTerms() + { + $query = new MoreLikeThis(); + + $max = 3; + $query->setMaxQueryTerms($max); + + $this->assertEquals($max, $query->getParam('max_query_terms')); + } + + /** + * @group unit + */ + public function testSetPercentTermsToMatch() + { + $query = new MoreLikeThis(); + + $match = 0.8; + $query->setPercentTermsToMatch($match); + + $this->assertEquals($match, $query->getParam('percent_terms_to_match')); + } + + /** + * @group unit + */ + public function testSetMinimumShouldMatch() + { + $query = new MoreLikeThis(); + + $match = '80%'; + $query->setMinimumShouldMatch($match); + + $this->assertEquals($match, $query->getParam('minimum_should_match')); + } + + /** + * @group unit + */ + public function testSetMinDocFrequency() + { + $query = new MoreLikeThis(); + + $freq = 2; + $query->setMinDocFrequency($freq); + + $this->assertEquals($freq, $query->getParam('min_doc_freq')); + } + + /** + * @group unit + */ + public function testSetMaxDocFrequency() + { + $query = new MoreLikeThis(); + + $freq = 2; + $query->setMaxDocFrequency($freq); + + $this->assertEquals($freq, $query->getParam('max_doc_freq')); + } + + /** + * @group unit + */ + public function testSetMinWordLength() + { + $query = new MoreLikeThis(); + + $length = 4; + $query->setMinWordLength($length); + + $this->assertEquals($length, $query->getParam('min_word_length')); + } + + /** + * @group unit + */ + public function testSetMaxWordLength() + { + $query = new MoreLikeThis(); + + $length = 5; + $query->setMaxWordLength($length); + + $this->assertEquals($length, $query->getParam('max_word_length')); + } + + /** + * @group unit + */ + public function testSetBoostTerms() + { + $query = new MoreLikeThis(); + + $boost = false; + $query->setBoostTerms($boost); + + $this->assertEquals($boost, $query->getParam('boost_terms')); + } + + /** + * @group unit + */ + public function testSetAnalyzer() + { + $query = new MoreLikeThis(); + + $analyzer = 'UpperCase'; + $query->setAnalyzer($analyzer); + + $this->assertEquals($analyzer, $query->getParam('analyzer')); + } + + /** + * @group unit + */ + public function testSetStopWords() + { + $query = new MoreLikeThis(); + + $stopWords = array('no', 'yes', 'test'); + $query->setStopWords($stopWords); + + $this->assertEquals($stopWords, $query->getParam('stop_words')); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/MultiMatchTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/MultiMatchTest.php new file mode 100644 index 00000000..e32f91af --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/MultiMatchTest.php @@ -0,0 +1,214 @@ + 1, 'name' => 'Rodolfo', 'last_name' => 'Moraes', 'full_name' => 'Rodolfo Moraes'), + array('id' => 2, 'name' => 'Tristan', 'last_name' => 'Maindron', 'full_name' => 'Tristan Maindron'), + array('id' => 3, 'name' => 'Monique', 'last_name' => 'Maindron', 'full_name' => 'Monique Maindron'), + array('id' => 4, 'name' => 'John', 'last_name' => 'not Doe', 'full_name' => 'John not Doe'), + ); + + /** + * @group functional + */ + public function testMinimumShouldMatch() + { + $multiMatch = new MultiMatch(); + $multiMatch->setQuery('Tristan Maindron'); + $multiMatch->setFields(array('full_name', 'name')); + $multiMatch->setMinimumShouldMatch('2<100%'); + $resultSet = $this->_getResults($multiMatch); + + $this->assertEquals(1, $resultSet->count()); + } + + /** + * @group functional + */ + public function testAndOperator() + { + $multiMatch = new MultiMatch(); + $multiMatch->setQuery('Monique Maindron'); + $multiMatch->setFields(array('full_name', 'name')); + $multiMatch->setOperator(MultiMatch::OPERATOR_AND); + $resultSet = $this->_getResults($multiMatch); + + $this->assertEquals(1, $resultSet->count()); + } + + /** + * @group functional + */ + public function testType() + { + $multiMatch = new MultiMatch(); + $multiMatch->setQuery('Trist'); + $multiMatch->setFields(array('full_name', 'name')); + $multiMatch->setType(MultiMatch::TYPE_PHRASE_PREFIX); + $resultSet = $this->_getResults($multiMatch); + + $this->assertEquals(1, $resultSet->count()); + } + + /** + * @group functional + */ + public function testFuzzy() + { + $multiMatch = new MultiMatch(); + $multiMatch->setQuery('Tritsan'); // Mispell on purpose + $multiMatch->setFields(array('full_name', 'name')); + $multiMatch->setFuzziness(2); + $resultSet = $this->_getResults($multiMatch); + + $this->assertEquals(1, $resultSet->count()); + + $multiMatch = new MultiMatch(); + $multiMatch->setQuery('Tritsan'); // Mispell on purpose + $multiMatch->setFields(array('full_name', 'name')); + $multiMatch->setFuzziness(0); + $resultSet = $this->_getResults($multiMatch); + + $this->assertEquals(0, $resultSet->count()); + } + + /** + * @group functional + */ + public function testFuzzyWithOptions1() + { + // Here Elasticsearch will not accept mispells + // on the first 6 letters. + $multiMatch = new MultiMatch(); + $multiMatch->setQuery('Tritsan'); // Mispell on purpose + $multiMatch->setFields(array('full_name', 'name')); + $multiMatch->setFuzziness(2); + $multiMatch->setPrefixLength(6); + $resultSet = $this->_getResults($multiMatch); + + $this->assertEquals(0, $resultSet->count()); + } + + /** + * @group functional + */ + public function testFuzzyWithOptions2() + { + // Here with a 'M' search we should hit 'Moraes' first + // and then stop because MaxExpansion = 1. + // If MaxExpansion was set to 2, we could hit "Maindron" too. + $multiMatch = new MultiMatch(); + $multiMatch->setQuery('M'); + $multiMatch->setFields(array('name')); + $multiMatch->setType(MultiMatch::TYPE_PHRASE_PREFIX); + $multiMatch->setPrefixLength(0); + $multiMatch->setMaxExpansions(1); + $resultSet = $this->_getResults($multiMatch); + + $this->assertEquals(1, $resultSet->count()); + } + + /** + * @group functional + */ + public function testZeroTerm() + { + $multiMatch = new MultiMatch(); + $multiMatch->setQuery('not'); // This is a stopword. + $multiMatch->setFields(array('full_name', 'last_name')); + $multiMatch->setZeroTermsQuery(MultiMatch::ZERO_TERM_NONE); + $multiMatch->setAnalyzer('stops'); + $resultSet = $this->_getResults($multiMatch); + + $this->assertEquals(0, $resultSet->count()); + + $multiMatch->setZeroTermsQuery(MultiMatch::ZERO_TERM_ALL); + $resultSet = $this->_getResults($multiMatch); + + $this->assertEquals(4, $resultSet->count()); + } + + /** + * @group functional + */ + public function testBaseMultiMatch() + { + $multiMatch = new MultiMatch(); + $multiMatch->setQuery('Rodolfo'); + $multiMatch->setFields(array('name', 'last_name')); + $resultSet = $this->_getResults($multiMatch); + + $this->assertEquals(1, $resultSet->count()); + + $multiMatch = new MultiMatch(); + $multiMatch->setQuery('Moraes'); + $multiMatch->setFields(array('name', 'last_name')); + $resultSet = $this->_getResults($multiMatch); + + $this->assertEquals(1, $resultSet->count()); + } + + /** + * Executes the query with the current multimatch. + */ + private function _getResults(MultiMatch $multiMatch) + { + return $this->_generateIndex()->search(new Query($multiMatch)); + } + + /** + * Builds an index for testing. + */ + private function _generateIndex() + { + $client = $this->_getClient(); + $index = $client->getIndex('test'); + + $index->create(array( + 'analysis' => array( + 'analyzer' => array( + 'noStops' => array( + 'type' => 'standard', + 'stopwords' => '_none_', + ), + 'stops' => array( + 'type' => 'standard', + 'stopwords' => array('not'), + ), + ), + ), + ), true); + + $type = $index->getType('test'); + + $mapping = new Mapping($type, array( + 'name' => array('type' => 'string', 'store' => 'no', 'analyzer' => 'noStops'), + 'last_name' => array('type' => 'string', 'store' => 'no', 'analyzer' => 'noStops'), + 'full_name' => array('type' => 'string', 'store' => 'no', 'analyzer' => 'noStops'), + )); + + $type->setMapping($mapping); + + foreach (self::$data as $key => $docData) { + $type->addDocument(new Document($key, $docData)); + } + + // Refresh index + $index->refresh(); + + return $index; + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/NestedTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/NestedTest.php new file mode 100644 index 00000000..1b5fc368 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/NestedTest.php @@ -0,0 +1,30 @@ +assertInstanceOf('Elastica\Query\Nested', $nested->setQuery($queryString)); + $this->assertInstanceOf('Elastica\Query\Nested', $nested->setPath($path)); + $expected = array( + 'nested' => array( + 'query' => $queryString->toArray(), + 'path' => $path, + ), + ); + + $this->assertEquals($expected, $nested->toArray()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/PostFilterTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/PostFilterTest.php new file mode 100644 index 00000000..178c803e --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/PostFilterTest.php @@ -0,0 +1,64 @@ +_createIndex(); + $docs = array( + new Document(1, array('color' => 'green', 'make' => 'ford')), + new Document(2, array('color' => 'blue', 'make' => 'volvo')), + new Document(3, array('color' => 'red', 'make' => 'ford')), + new Document(4, array('color' => 'green', 'make' => 'renault')), + ); + $index->getType('test')->addDocuments($docs); + $index->refresh(); + + return $index; + } + + /** + * @group unit + */ + public function testToArray() + { + $query = new Query(); + + $post_filter = new Term(array('color' => 'green')); + $query->setPostFilter($post_filter); + + $data = $query->toArray(); + + $this->assertArrayHasKey('post_filter', $data); + $this->assertEquals(array('term' => array('color' => 'green')), $data['post_filter']); + } + + /** + * @group functional + */ + public function testQuery() + { + $query = new Query(); + + $match = new Match(); + $match->setField('make', 'ford'); + + $query->setQuery($match); + + $filter = new Term(); + $filter->setTerm('color', 'green'); + + $query->setPostFilter($filter); + + $results = $this->_getTestIndex()->search($query); + + $this->assertEquals(1, $results->getTotalHits()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/PrefixTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/PrefixTest.php new file mode 100644 index 00000000..e12ead05 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/PrefixTest.php @@ -0,0 +1,27 @@ +setPrefix($key, $value, $boost); + + $data = $query->toArray(); + + $this->assertInternalType('array', $data['prefix']); + $this->assertInternalType('array', $data['prefix'][$key]); + $this->assertEquals($data['prefix'][$key]['value'], $value); + $this->assertEquals($data['prefix'][$key]['boost'], $boost); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/QueryStringTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/QueryStringTest.php new file mode 100644 index 00000000..5b549a30 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/QueryStringTest.php @@ -0,0 +1,189 @@ + $str, + ); + + $this->assertEquals(array('query_string' => $expected), $query->toArray()); + + $fields = array(); + $max = rand() % 10 + 1; + for ($i = 0; $i < $max; $i++) { + $fields[] = md5(rand()); + } + + $query->setFields($fields); + $expected['fields'] = $fields; + $this->assertEquals(array('query_string' => $expected), $query->toArray()); + + foreach (array(false, true) as $val) { + $query->setUseDisMax($val); + $expected['use_dis_max'] = $val; + + $this->assertEquals(array('query_string' => $expected), $query->toArray()); + } + } + + /** + * @group functional + */ + public function testSearch() + { + $index = $this->_createIndex(); + $index->getSettings()->setNumberOfReplicas(0); + $type = $index->getType('helloworld'); + + $doc = new Document(1, array('email' => 'test@test.com', 'username' => 'hanswurst', 'test' => array('2', '3', '5'))); + $type->addDocument($doc); + $index->refresh(); + + $queryString = new QueryString('test*'); + $resultSet = $type->search($queryString); + + $this->assertEquals(1, $resultSet->count()); + } + + /** + * Tests if search in multiple fields is possible. + * + * @group functional + */ + public function testSearchFields() + { + $index = $this->_createIndex(); + $type = $index->getType('test'); + + $doc = new Document(1, array('title' => 'hello world', 'firstname' => 'nicolas', 'lastname' => 'ruflin', 'price' => '102', 'year' => '2012')); + $type->addDocument($doc); + $index->refresh(); + + $query = new QueryString(); + $query = $query->setQuery('ruf*'); + $query = $query->setDefaultField('title'); + $query = $query->setFields(array('title', 'firstname', 'lastname', 'price', 'year')); + + $resultSet = $type->search($query); + $this->assertEquals(1, $resultSet->count()); + } + + /** + * @group unit + */ + public function testSetDefaultOperator() + { + $operator = 'AND'; + $query = new QueryString('test'); + $query->setDefaultOperator($operator); + + $data = $query->toArray(); + + $this->assertEquals($data['query_string']['default_operator'], $operator); + } + + /** + * @group unit + */ + public function testSetDefaultField() + { + $default = 'field1'; + $query = new QueryString('test'); + $query->setDefaultField($default); + + $data = $query->toArray(); + + $this->assertEquals($data['query_string']['default_field'], $default); + } + + /** + * @group unit + */ + public function testSetRewrite() + { + $rewrite = 'scoring_boolean'; + $query = new QueryString('test'); + $query->setRewrite($rewrite); + + $data = $query->toArray(); + + $this->assertEquals($data['query_string']['rewrite'], $rewrite); + } + + /** + * @group unit + * @expectedException \Elastica\Exception\InvalidException + */ + public function testSetQueryInvalid() + { + $query = new QueryString(); + $query->setQuery(array()); + } + + /** + * @group unit + */ + public function testSetTimezone() + { + $timezone = 'Europe/Paris'; + $text = 'date:[2012 TO 2014]'; + + $query = new QueryString($text); + $query->setTimezone($timezone); + + $expected = array( + 'query_string' => array( + 'query' => $text, + 'time_zone' => $timezone, + ), + ); + + $this->assertEquals($expected, $query->toArray()); + $this->assertInstanceOf('Elastica\Query\QueryString', $query->setTimezone($timezone)); + } + + /** + * @group unit + */ + public function testSetPhraseSlop() + { + $phraseSlop = 9; + + $query = new QueryString('test'); + $query->setPhraseSlop($phraseSlop); + + $data = $query->toArray(); + $this->assertEquals($phraseSlop, $data['query_string']['phrase_slop']); + } + + /** + * @group functional + */ + public function testSetBoost() + { + $index = $this->_createIndex(); + $query = new QueryString('test'); + $query->setBoost(9.3); + + $doc = new Document('', array('name' => 'test')); + $index->getType('test')->addDocument($doc); + $index->refresh(); + + $resultSet = $index->search($query); + + $this->assertEquals(1, $resultSet->count()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/RangeTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/RangeTest.php new file mode 100644 index 00000000..108ef0c9 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/RangeTest.php @@ -0,0 +1,79 @@ +_getClient(); + $index = $client->getIndex('test'); + $index->create(array(), true); + $type = $index->getType('test'); + + $type->addDocuments(array( + new Document(1, array('age' => 16, 'height' => 140)), + new Document(2, array('age' => 21, 'height' => 155)), + new Document(3, array('age' => 33, 'height' => 160)), + new Document(4, array('age' => 68, 'height' => 160)), + )); + + $index->optimize(); + $index->refresh(); + + $query = new Range('age', array('from' => 10, 'to' => 20)); + $result = $type->search($query)->count(); + $this->assertEquals(1, $result); + + $query = new Range(); + $query->addField('height', array('gte' => 160)); + + $result = $type->search($query)->count(); + $this->assertEquals(2, $result); + } + + /** + * @group unit + */ + public function testToArray() + { + $range = new Range(); + + $field = array('from' => 20, 'to' => 40); + $range->addField('age', $field); + + $expectedArray = array( + 'range' => array( + 'age' => $field, + ), + ); + + $this->assertEquals($expectedArray, $range->toArray()); + } + + /** + * @group unit + */ + public function testConstruct() + { + $ranges = array('from' => 20, 'to' => 40); + $range = new Range( + 'age', + $ranges + ); + + $expectedArray = array( + 'range' => array( + 'age' => $ranges, + ), + ); + + $this->assertEquals($expectedArray, $range->toArray()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/RegexpTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/RegexpTest.php new file mode 100644 index 00000000..c67e6317 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/RegexpTest.php @@ -0,0 +1,31 @@ + array( + $field => array( + 'value' => $value, + 'boost' => $boost, + ), + ), + ); + + $this->assertequals($expectedArray, $query->toArray()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/RescoreTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/RescoreTest.php new file mode 100644 index 00000000..87842b0f --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/RescoreTest.php @@ -0,0 +1,236 @@ +setFieldQuery('test1', 'foo'); + $secQuery = new Term(); + $secQuery = $secQuery->setTerm('test2', 'bar', 2); + $queryRescore = new QueryRescore($secQuery); + $query->setQuery($mainQuery); + $query->setRescore($queryRescore); + $data = $query->toArray(); + + $expected = array( + 'query' => array( + 'match' => array( + 'test1' => array( + 'query' => 'foo', + ), + ), + ), + 'rescore' => array( + 'query' => array( + 'rescore_query' => array( + 'term' => array( + 'test2' => array( + 'value' => 'bar', + 'boost' => 2, + ), + ), + ), + ), + ), + ); + + $this->assertEquals($expected, $data); + } + + /** + * @group unit + */ + public function testSetSize() + { + $query = new Query(); + $mainQuery = new Match(); + $mainQuery = $mainQuery->setFieldQuery('test1', 'foo'); + $secQuery = new Term(); + $secQuery = $secQuery->setTerm('test2', 'bar', 2); + $queryRescore = new QueryRescore($secQuery); + $queryRescore->setWindowSize(50); + $query->setQuery($mainQuery); + $query->setRescore($queryRescore); + $data = $query->toArray(); + + $expected = array( + 'query' => array( + 'match' => array( + 'test1' => array( + 'query' => 'foo', + ), + ), + ), + 'rescore' => array( + 'window_size' => 50, + 'query' => array( + 'rescore_query' => array( + 'term' => array( + 'test2' => array( + 'value' => 'bar', + 'boost' => 2, + ), + ), + ), + ), + ), + ); + + $this->assertEquals($expected, $data); + } + + /** + * @group unit + */ + public function testSetWeights() + { + $query = new Query(); + $mainQuery = new Match(); + $mainQuery = $mainQuery->setFieldQuery('test1', 'foo'); + $secQuery = new Term(); + $secQuery = $secQuery->setTerm('test2', 'bar', 2); + $queryRescore = new QueryRescore($secQuery); + $queryRescore->setWindowSize(50); + $queryRescore->setQueryWeight(.7); + $queryRescore->setRescoreQueryWeight(1.2); + $query->setQuery($mainQuery); + $query->setRescore($queryRescore); + $data = $query->toArray(); + + $expected = array( + 'query' => array( + 'match' => array( + 'test1' => array( + 'query' => 'foo', + ), + ), + ), + 'rescore' => array( + 'window_size' => 50, + 'query' => array( + 'rescore_query' => array( + 'term' => array( + 'test2' => array( + 'value' => 'bar', + 'boost' => 2, + ), + ), + ), + 'query_weight' => 0.7, + 'rescore_query_weight' => 1.2, + ), + ), + ); + + $this->assertEquals($expected, $data); + } + + /** + * @group functional + */ + public function testMultipleQueries() + { + $query = new Query(); + $mainQuery = new Match(); + $mainQuery = $mainQuery->setFieldQuery('test1', 'foo'); + + $secQuery1 = new Term(); + $secQuery1 = $secQuery1->setTerm('test2', 'bar', 1); + $rescoreQuery1 = new QueryRescore(); + $rescoreQuery1->setRescoreQuery($secQuery1); + + $secQuery2 = new Term(); + $secQuery2 = $secQuery2->setTerm('test2', 'tom', 2); + $rescoreQuery2 = new QueryRescore(); + $rescoreQuery2->setRescoreQuery($secQuery2); + + $query->setQuery($mainQuery); + $query->setRescore(array($rescoreQuery1, $rescoreQuery2)); + $data = $query->toArray(); + + $expected = array( + 'query' => array( + 'match' => array( + 'test1' => array( + 'query' => 'foo', + ), + ), + ), + 'rescore' => array( + array( + 'query' => array( + 'rescore_query' => array( + 'term' => array( + 'test2' => array( + 'value' => 'bar', + 'boost' => 1, + ), + ), + ), + ), + ), + array( + 'query' => array( + 'rescore_query' => array( + 'term' => array( + 'test2' => array( + 'value' => 'tom', + 'boost' => 2, + ), + ), + ), + ), + ), + ), + ); + + $this->assertEquals($expected, $data); + + $index = $this->_createIndex(); + $index->refresh(); + $results = $index->search($query); + $response = $results->getResponse(); + + $this->assertEquals(true, $response->isOk()); + $this->assertEquals(0, $results->getTotalHits()); + } + + /** + * @group functional + */ + public function testQuery() + { + $query = new Query(); + $mainQuery = new Match(); + $mainQuery = $mainQuery->setFieldQuery('test1', 'foo'); + $secQuery = new Term(); + $secQuery = $secQuery->setTerm('test2', 'bar', 2); + $queryRescore = new QueryRescore($secQuery); + $queryRescore->setWindowSize(50); + $queryRescore->setQueryWeight(.7); + $queryRescore->setRescoreQueryWeight(1.2); + $query->setQuery($mainQuery); + $query->setRescore($queryRescore); + $data = $query->toArray(); + + $index = $this->_createIndex(); + $index->refresh(); + $results = $index->search($query); + $response = $results->getResponse(); + + $this->assertEquals(true, $response->isOk()); + $this->assertEquals(0, $results->getTotalHits()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/SimpleQueryStringTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/SimpleQueryStringTest.php new file mode 100644 index 00000000..80316547 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/SimpleQueryStringTest.php @@ -0,0 +1,103 @@ +setDefaultOperator(SimpleQueryString::OPERATOR_OR); + $query->setAnalyzer('whitespace'); + + $expected = array( + 'simple_query_string' => array( + 'query' => $string, + 'fields' => $fields, + 'analyzer' => 'whitespace', + 'default_operator' => SimpleQueryString::OPERATOR_OR, + ), + ); + + $this->assertEquals($expected, $query->toArray()); + } + + /** + * @group functional + */ + public function testQuery() + { + $index = $this->_createIndex(); + $docs = array( + new Document(1, array('make' => 'Gibson', 'model' => 'Les Paul')), + new Document(2, array('make' => 'Gibson', 'model' => 'SG Standard')), + new Document(3, array('make' => 'Gibson', 'model' => 'SG Supreme')), + new Document(4, array('make' => 'Gibson', 'model' => 'SG Faded')), + new Document(5, array('make' => 'Fender', 'model' => 'Stratocaster')), + ); + $index->getType('guitars')->addDocuments($docs); + $index->refresh(); + + $query = new SimpleQueryString('gibson +sg +-faded', array('make', 'model')); + $results = $index->search($query); + + $this->assertEquals(2, $results->getTotalHits()); + + $query->setFields(array('model')); + $results = $index->search($query); + + // We should not get any hits, since the "make" field was not included in the query. + $this->assertEquals(0, $results->getTotalHits()); + } + + /** + * @group unit + */ + public function testSetMinimumShouldMatch() + { + $expected = array( + 'simple_query_string' => array( + 'query' => 'DONT PANIC', + 'minimum_should_match' => '75%', + ), + ); + + $query = new SimpleQueryString($expected['simple_query_string']['query']); + $query->setMinimumShouldMatch($expected['simple_query_string']['minimum_should_match']); + + $this->assertEquals($expected, $query->toArray()); + $this->assertInstanceOf('Elastica\Query\SimpleQueryString', $query->setMinimumShouldMatch('75%')); + } + + /** + * @group functional + */ + public function testSetMinimumShouldMatchWorks() + { + $index = $this->_createIndex(); + $type = $index->getType('foobars'); + $type->addDocuments(array( + new Document(1, array('body' => 'foo')), + new Document(2, array('body' => 'bar')), + new Document(3, array('body' => 'foo bar')), + new Document(4, array('body' => 'foo baz bar')), + )); + $index->refresh(); + + $query = new SimpleQueryString('foo bar'); + $query->setMinimumShouldMatch(2); + $results = $type->search($query); + + $this->assertCount(2, $results); + $this->assertEquals(3, $results[0]->getId()); + $this->assertEquals(4, $results[1]->getId()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/SimpleTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/SimpleTest.php new file mode 100644 index 00000000..b4f1dc8d --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/SimpleTest.php @@ -0,0 +1,19 @@ + array('world'), 'name' => 'ruflin'); + $query = new Simple($testQuery); + + $this->assertEquals($testQuery, $query->toArray()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/TermTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/TermTest.php new file mode 100644 index 00000000..da15f0f2 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/TermTest.php @@ -0,0 +1,27 @@ +setTerm($key, $value, $boost); + + $data = $query->toArray(); + + $this->assertInternalType('array', $data['term']); + $this->assertInternalType('array', $data['term'][$key]); + $this->assertEquals($data['term'][$key]['value'], $value); + $this->assertEquals($data['term'][$key]['boost'], $boost); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/TermsTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/TermsTest.php new file mode 100644 index 00000000..f0c2b4b4 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/TermsTest.php @@ -0,0 +1,65 @@ +_createIndex(); + $type = $index->getType('helloworld'); + + $type->addDocuments(array( + new Document(1, array('name' => 'hello world')), + new Document(2, array('name' => 'nicolas ruflin')), + new Document(3, array('name' => 'ruflin')), + )); + + $query = new Terms(); + $query->setTerms('name', array('nicolas', 'hello')); + + $index->refresh(); + + $resultSet = $type->search($query); + + $this->assertEquals(2, $resultSet->count()); + + $query->addTerm('ruflin'); + $resultSet = $type->search($query); + + $this->assertEquals(3, $resultSet->count()); + } + + /** + * @group unit + */ + public function testSetMinimum() + { + $key = 'name'; + $terms = array('nicolas', 'ruflin'); + $minimum = 2; + + $query = new Terms($key, $terms); + $query->setMinimumMatch($minimum); + + $data = $query->toArray(); + $this->assertEquals($minimum, $data['terms']['minimum_match']); + } + + /** + * @group unit + * @expectedException \Elastica\Exception\InvalidException + */ + public function testInvalidParams() + { + $query = new Terms(); + + $query->toArray(); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/WildcardTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/WildcardTest.php new file mode 100644 index 00000000..7a0c379e --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/WildcardTest.php @@ -0,0 +1,106 @@ +assertEmpty($wildcard->getParams()); + } + + /** + * @group unit + */ + public function testToArray() + { + $key = 'name'; + $value = 'Ru*lin'; + $boost = 2.0; + + $wildcard = new Wildcard($key, $value, $boost); + + $expectedArray = array( + 'wildcard' => array( + $key => array( + 'value' => $value, + 'boost' => $boost, + ), + ), + ); + + $this->assertEquals($expectedArray, $wildcard->toArray()); + } + + /** + * @group functional + */ + public function testSearchWithAnalyzer() + { + $client = $this->_getClient(); + $index = $client->getIndex('test'); + + $indexParams = array( + 'analysis' => array( + 'analyzer' => array( + 'lw' => array( + 'type' => 'custom', + 'tokenizer' => 'keyword', + 'filter' => array('lowercase'), + ), + ), + ), + ); + + $index->create($indexParams, true); + $type = $index->getType('test'); + + $mapping = new Mapping($type, array( + 'name' => array('type' => 'string', 'store' => 'no', 'analyzer' => 'lw'), + ) + ); + $type->setMapping($mapping); + + $type->addDocuments(array( + new Document(1, array('name' => 'Basel-Stadt')), + new Document(2, array('name' => 'New York')), + new Document(3, array('name' => 'Baden')), + new Document(4, array('name' => 'Baden Baden')), + new Document(5, array('name' => 'New Orleans')), + )); + + $index->refresh(); + + $query = new Wildcard(); + $query->setValue('name', 'ba*'); + $resultSet = $index->search($query); + + $this->assertEquals(3, $resultSet->count()); + + $query = new Wildcard(); + $query->setValue('name', 'baden*'); + $resultSet = $index->search($query); + + $this->assertEquals(2, $resultSet->count()); + + $query = new Wildcard(); + $query->setValue('name', 'baden b*'); + $resultSet = $index->search($query); + + $this->assertEquals(1, $resultSet->count()); + + $query = new Wildcard(); + $query->setValue('name', 'baden bas*'); + $resultSet = $index->search($query); + + $this->assertEquals(0, $resultSet->count()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/QueryBuilder/DSL/AbstractDSLTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/QueryBuilder/DSL/AbstractDSLTest.php new file mode 100644 index 00000000..3e44f463 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/QueryBuilder/DSL/AbstractDSLTest.php @@ -0,0 +1,97 @@ +assertTrue(method_exists($dsl, $methodName)); + + // Check returned value + $return = call_user_func_array(array($dsl, $methodName), $arguments); + $this->assertTrue(class_exists($className), 'Class not exists but NotImplementedException is not thrown'); + $this->assertInstanceOf($className, $return); + + // Check method signature + $class = new \ReflectionClass($className); + $method = new \ReflectionMethod(get_class($dsl), $methodName); + if (!$class->hasMethod('__construct')) { + $this->assertEmpty($method->getParameters(), 'Constructor is not defined, but method has some parameters'); + } else { + $this->_assertParametersEquals($class->getMethod('__construct')->getParameters(), $method->getParameters()); + } + } + + /** + * @param DSL $dsl + * @param string $name + */ + protected function _assertNotImplemented(DSL $dsl, $methodName, $arguments) + { + try { + call_user_func(array($dsl, $methodName), $arguments); + $this->fail('NotImplementedException is not thrown'); + } catch (NotImplementedException $ex) { + // expected + } + } + + /** + * @param \ReflectionParameter[] $left + * @param \ReflectionParameter[] $right + */ + protected function _assertParametersEquals($left, $right) + { + $this->assertEquals(count($left), count($right), 'Parameters count mismatch'); + + for ($i = 0; $i < count($left); $i++) { + $this->assertEquals($left[$i]->getName(), $right[$i]->getName(), 'Parameters names mismatch'); + $this->assertEquals($left[$i]->isOptional(), $right[$i]->isOptional(), 'Parameters optionality mismatch'); + $this->assertEquals($this->_getHintName($left[$i]), $this->_getHintName($right[$i]), 'Parameters typehints mismatch'); + $this->assertEquals($this->_getDefaultValue($left[$i]), $this->_getDefaultValue($right[$i]), 'Default values mismatch'); + } + } + + /** + * @param \ReflectionParameter $param + * + * @return string|null + */ + protected function _getDefaultValue(\ReflectionParameter $param) + { + if ($param->isOptional()) { + return $param->getDefaultValue(); + } + } + + /** + * @param \ReflectionParameter $param + * + * @return string|null + */ + protected function _getHintName(\ReflectionParameter $param) + { + if (version_compare(phpversion(), '5.4', '>=') && $param->isCallable()) { + return 'callable'; + } + + if ($param->isArray()) { + return 'array'; + } + + if ($class = $param->getClass()) { + return $class->getName(); + } + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/QueryBuilder/DSL/AggregationTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/QueryBuilder/DSL/AggregationTest.php new file mode 100644 index 00000000..67c70862 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/QueryBuilder/DSL/AggregationTest.php @@ -0,0 +1,58 @@ +assertInstanceOf('Elastica\QueryBuilder\DSL', $aggregationDSL); + $this->assertEquals(DSL::TYPE_AGGREGATION, $aggregationDSL->getType()); + } + + /** + * @group unit + */ + public function testInterface() + { + $aggregationDSL = new DSL\Aggregation(); + + $this->_assertImplemented($aggregationDSL, 'avg', 'Elastica\Aggregation\Avg', array('name')); + $this->_assertImplemented($aggregationDSL, 'cardinality', 'Elastica\Aggregation\Cardinality', array('name')); + $this->_assertImplemented($aggregationDSL, 'date_histogram', 'Elastica\Aggregation\DateHistogram', array('name', 'field', 1)); + $this->_assertImplemented($aggregationDSL, 'date_range', 'Elastica\Aggregation\DateRange', array('name')); + $this->_assertImplemented($aggregationDSL, 'extended_stats', 'Elastica\Aggregation\ExtendedStats', array('name')); + $this->_assertImplemented($aggregationDSL, 'filter', 'Elastica\Aggregation\Filter', array('name', new Exists('field'))); + $this->_assertImplemented($aggregationDSL, 'filters', 'Elastica\Aggregation\Filters', array('name')); + $this->_assertImplemented($aggregationDSL, 'geo_distance', 'Elastica\Aggregation\GeoDistance', array('name', 'field', 'origin')); + $this->_assertImplemented($aggregationDSL, 'geohash_grid', 'Elastica\Aggregation\GeohashGrid', array('name', 'field')); + $this->_assertImplemented($aggregationDSL, 'global_agg', 'Elastica\Aggregation\GlobalAggregation', array('name')); + $this->_assertImplemented($aggregationDSL, 'histogram', 'Elastica\Aggregation\Histogram', array('name', 'field', 1)); + $this->_assertImplemented($aggregationDSL, 'ipv4_range', 'Elastica\Aggregation\IpRange', array('name', 'field')); + $this->_assertImplemented($aggregationDSL, 'max', 'Elastica\Aggregation\Max', array('name')); + $this->_assertImplemented($aggregationDSL, 'min', 'Elastica\Aggregation\Min', array('name')); + $this->_assertImplemented($aggregationDSL, 'missing', 'Elastica\Aggregation\Missing', array('name', 'field')); + $this->_assertImplemented($aggregationDSL, 'nested', 'Elastica\Aggregation\Nested', array('name', 'path')); + $this->_assertImplemented($aggregationDSL, 'percentiles', 'Elastica\Aggregation\Percentiles', array('name')); + $this->_assertImplemented($aggregationDSL, 'range', 'Elastica\Aggregation\Range', array('name')); + $this->_assertImplemented($aggregationDSL, 'reverse_nested', 'Elastica\Aggregation\ReverseNested', array('name')); + $this->_assertImplemented($aggregationDSL, 'scripted_metric', 'Elastica\Aggregation\ScriptedMetric', array('name')); + $this->_assertImplemented($aggregationDSL, 'significant_terms', 'Elastica\Aggregation\SignificantTerms', array('name')); + $this->_assertImplemented($aggregationDSL, 'stats', 'Elastica\Aggregation\Stats', array('name')); + $this->_assertImplemented($aggregationDSL, 'sum', 'Elastica\Aggregation\Sum', array('name')); + $this->_assertImplemented($aggregationDSL, 'terms', 'Elastica\Aggregation\Terms', array('name')); + $this->_assertImplemented($aggregationDSL, 'top_hits', 'Elastica\Aggregation\TopHits', array('name')); + $this->_assertImplemented($aggregationDSL, 'value_count', 'Elastica\Aggregation\ValueCount', array('name', 'field')); + + $this->_assertNotImplemented($aggregationDSL, 'children', array('name')); + $this->_assertNotImplemented($aggregationDSL, 'geo_bounds', array('name')); + $this->_assertNotImplemented($aggregationDSL, 'percentile_ranks', array('name')); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/QueryBuilder/DSL/FilterTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/QueryBuilder/DSL/FilterTest.php new file mode 100644 index 00000000..755bd18a --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/QueryBuilder/DSL/FilterTest.php @@ -0,0 +1,58 @@ +assertInstanceOf('Elastica\QueryBuilder\DSL', $filterDSL); + $this->assertEquals(DSL::TYPE_FILTER, $filterDSL->getType()); + } + + /** + * @group unit + */ + public function testInterface() + { + $filterDSL = new DSL\Filter(); + + $this->_assertImplemented($filterDSL, 'bool', 'Elastica\Filter\BoolFilter', array()); + $this->_assertImplemented($filterDSL, 'bool_and', 'Elastica\Filter\BoolAnd', array(array(new Exists('field')))); + $this->_assertImplemented($filterDSL, 'bool_not', 'Elastica\Filter\BoolNot', array(new Exists('field'))); + $this->_assertImplemented($filterDSL, 'bool_or', 'Elastica\Filter\BoolOr', array(array(new Exists('field')))); + $this->_assertImplemented($filterDSL, 'exists', 'Elastica\Filter\Exists', array('field')); + $this->_assertImplemented($filterDSL, 'geo_bounding_box', 'Elastica\Filter\GeoBoundingBox', array('field', array(1, 2))); + $this->_assertImplemented($filterDSL, 'geo_distance', 'Elastica\Filter\GeoDistance', array('key', 'location', 'distance')); + $this->_assertImplemented($filterDSL, 'geo_distance_range', 'Elastica\Filter\GeoDistanceRange', array('key', 'location')); + $this->_assertImplemented($filterDSL, 'geo_polygon', 'Elastica\Filter\GeoPolygon', array('key', array())); + $this->_assertImplemented($filterDSL, 'geo_shape_pre_indexed', 'Elastica\Filter\GeoShapePreIndexed', array('path', 'indexedId', 'indexedType', 'indexedIndex', 'indexedPath')); + $this->_assertImplemented($filterDSL, 'geo_shape_provided', 'Elastica\Filter\GeoShapeProvided', array('path', array())); + $this->_assertImplemented($filterDSL, 'geohash_cell', 'Elastica\Filter\GeohashCell', array('field', 'location')); + $this->_assertImplemented($filterDSL, 'has_child', 'Elastica\Filter\HasChild', array(new Match(), 'type')); + $this->_assertImplemented($filterDSL, 'has_parent', 'Elastica\Filter\HasParent', array(new Match(), 'type')); + $this->_assertImplemented($filterDSL, 'ids', 'Elastica\Filter\Ids', array('type', array())); + $this->_assertImplemented($filterDSL, 'indices', 'Elastica\Filter\Indices', array(new Exists('field'), array())); + $this->_assertImplemented($filterDSL, 'limit', 'Elastica\Filter\Limit', array(1)); + $this->_assertImplemented($filterDSL, 'match_all', 'Elastica\Filter\MatchAll', array()); + $this->_assertImplemented($filterDSL, 'missing', 'Elastica\Filter\Missing', array('field')); + $this->_assertImplemented($filterDSL, 'nested', 'Elastica\Filter\Nested', array()); + $this->_assertImplemented($filterDSL, 'numeric_range', 'Elastica\Filter\NumericRange', array()); + $this->_assertImplemented($filterDSL, 'prefix', 'Elastica\Filter\Prefix', array('field', 'prefix')); + $this->_assertImplemented($filterDSL, 'query', 'Elastica\Filter\Query', array(new Match())); + $this->_assertImplemented($filterDSL, 'range', 'Elastica\Filter\Range', array('field', array())); + $this->_assertImplemented($filterDSL, 'regexp', 'Elastica\Filter\Regexp', array('field', 'regex')); + $this->_assertImplemented($filterDSL, 'script', 'Elastica\Filter\Script', array('script')); + $this->_assertImplemented($filterDSL, 'term', 'Elastica\Filter\Term', array()); + $this->_assertImplemented($filterDSL, 'terms', 'Elastica\Filter\Terms', array('field', array())); + $this->_assertImplemented($filterDSL, 'type', 'Elastica\Filter\Type', array('type')); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/QueryBuilder/DSL/QueryTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/QueryBuilder/DSL/QueryTest.php new file mode 100644 index 00000000..d4669119 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/QueryBuilder/DSL/QueryTest.php @@ -0,0 +1,85 @@ +assertInstanceOf('Elastica\QueryBuilder\DSL', $queryDSL); + $this->assertEquals(DSL::TYPE_QUERY, $queryDSL->getType()); + } + + /** + * @group unit + */ + public function testMatch() + { + $queryDSL = new DSL\Query(); + + $match = $queryDSL->match('field', 'match'); + $this->assertEquals('match', $match->getParam('field')); + $this->assertInstanceOf('Elastica\Query\Match', $match); + } + + /** + * @group unit + */ + public function testInterface() + { + $queryDSL = new DSL\Query(); + + $this->_assertImplemented($queryDSL, 'bool', 'Elastica\Query\BoolQuery', array()); + $this->_assertImplemented($queryDSL, 'boosting', 'Elastica\Query\Boosting', array()); + $this->_assertImplemented($queryDSL, 'common_terms', 'Elastica\Query\Common', array('field', 'query', 0.001)); + $this->_assertImplemented($queryDSL, 'constant_score', 'Elastica\Query\ConstantScore', array(new Match())); + $this->_assertImplemented($queryDSL, 'dis_max', 'Elastica\Query\DisMax', array()); + $this->_assertImplemented($queryDSL, 'filtered', 'Elastica\Query\Filtered', array(new Match(), new Exists('field'))); + $this->_assertImplemented($queryDSL, 'function_score', 'Elastica\Query\FunctionScore', array()); + $this->_assertImplemented($queryDSL, 'fuzzy', 'Elastica\Query\Fuzzy', array('field', 'type')); + $this->_assertImplemented($queryDSL, 'fuzzy_like_this', 'Elastica\Query\FuzzyLikeThis', array()); + $this->_assertImplemented($queryDSL, 'has_child', 'Elastica\Query\HasChild', array(new Match())); + $this->_assertImplemented($queryDSL, 'has_parent', 'Elastica\Query\HasParent', array(new Match(), 'type')); + $this->_assertImplemented($queryDSL, 'ids', 'Elastica\Query\Ids', array('type', array())); + $this->_assertImplemented($queryDSL, 'match', 'Elastica\Query\Match', array('field', 'values')); + $this->_assertImplemented($queryDSL, 'match_all', 'Elastica\Query\MatchAll', array()); + $this->_assertImplemented($queryDSL, 'more_like_this', 'Elastica\Query\MoreLikeThis', array()); + $this->_assertImplemented($queryDSL, 'multi_match', 'Elastica\Query\MultiMatch', array()); + $this->_assertImplemented($queryDSL, 'nested', 'Elastica\Query\Nested', array()); + $this->_assertImplemented($queryDSL, 'prefix', 'Elastica\Query\Prefix', array()); + $this->_assertImplemented($queryDSL, 'query_string', 'Elastica\Query\QueryString', array()); + $this->_assertImplemented($queryDSL, 'range', 'Elastica\Query\Range', array('field', array())); + $this->_assertImplemented($queryDSL, 'regexp', 'Elastica\Query\Regexp', array('field', 'value', 1.0)); + $this->_assertImplemented($queryDSL, 'simple_query_string', 'Elastica\Query\SimpleQueryString', array('query')); + $this->_assertImplemented($queryDSL, 'term', 'Elastica\Query\Term', array()); + $this->_assertImplemented($queryDSL, 'terms', 'Elastica\Query\Terms', array('field', array())); + $this->_assertImplemented($queryDSL, 'top_children', 'Elastica\Query\TopChildren', array(new Match(), 'type')); + $this->_assertImplemented($queryDSL, 'wildcard', 'Elastica\Query\Wildcard', array()); + + $this->_assertNotImplemented($queryDSL, 'custom_boost_factor', array()); + $this->_assertNotImplemented($queryDSL, 'custom_filters_score', array()); + $this->_assertNotImplemented($queryDSL, 'custom_score', array()); + $this->_assertNotImplemented($queryDSL, 'field', array()); + $this->_assertNotImplemented($queryDSL, 'fuzzy_like_this_field', array()); + $this->_assertNotImplemented($queryDSL, 'geo_shape', array()); + $this->_assertNotImplemented($queryDSL, 'indices', array()); + $this->_assertNotImplemented($queryDSL, 'minimum_should_match', array()); + $this->_assertNotImplemented($queryDSL, 'more_like_this_field', array()); + $this->_assertNotImplemented($queryDSL, 'span_first', array()); + $this->_assertNotImplemented($queryDSL, 'span_multi_term', array()); + $this->_assertNotImplemented($queryDSL, 'span_near', array()); + $this->_assertNotImplemented($queryDSL, 'span_not', array()); + $this->_assertNotImplemented($queryDSL, 'span_or', array()); + $this->_assertNotImplemented($queryDSL, 'span_term', array()); + $this->_assertNotImplemented($queryDSL, 'template', array()); + $this->_assertNotImplemented($queryDSL, 'text', array()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/QueryBuilder/DSL/SuggestTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/QueryBuilder/DSL/SuggestTest.php new file mode 100644 index 00000000..b70e0ba7 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/QueryBuilder/DSL/SuggestTest.php @@ -0,0 +1,32 @@ +assertInstanceOf('Elastica\QueryBuilder\DSL', $suggestDSL); + $this->assertEquals(DSL::TYPE_SUGGEST, $suggestDSL->getType()); + } + + /** + * @group unit + */ + public function testInterface() + { + $suggestDSL = new DSL\Suggest(); + + $this->_assertImplemented($suggestDSL, 'completion', 'Elastica\Suggest\Completion', array('name', 'field')); + $this->_assertImplemented($suggestDSL, 'phrase', 'Elastica\Suggest\Phrase', array('name', 'field')); + $this->_assertImplemented($suggestDSL, 'term', 'Elastica\Suggest\Term', array('name', 'field')); + + $this->_assertNotImplemented($suggestDSL, 'context', array()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/QueryBuilder/VersionTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/QueryBuilder/VersionTest.php new file mode 100644 index 00000000..d92848a7 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/QueryBuilder/VersionTest.php @@ -0,0 +1,67 @@ +assertVersions($version, $dsl); + } + } + + private function assertVersions(Version $version, array $dsl) + { + foreach ($version->getQueries() as $query) { + $this->assertTrue( + method_exists($dsl[0], $query), + 'query "'.$query.'" in '.get_class($version).' must be defined in '.get_class($dsl[0]) + ); + } + + foreach ($version->getFilters() as $filter) { + $this->assertTrue( + method_exists($dsl[1], $filter), + 'filter "'.$filter.'" in '.get_class($version).' must be defined in '.get_class($dsl[1]) + ); + } + + foreach ($version->getAggregations() as $aggregation) { + $this->assertTrue( + method_exists($dsl[2], $aggregation), + 'aggregation "'.$aggregation.'" in '.get_class($version).' must be defined in '.get_class($dsl[2]) + ); + } + + foreach ($version->getSuggesters() as $suggester) { + $this->assertTrue( + method_exists($dsl[3], $suggester), + 'suggester "'.$suggester.'" in '.get_class($version).' must be defined in '.get_class($dsl[3]) + ); + } + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/QueryBuilderTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/QueryBuilderTest.php new file mode 100644 index 00000000..6cbfd37b --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/QueryBuilderTest.php @@ -0,0 +1,88 @@ +addDSL(new CustomDSL()); + + $this->assertTrue($qb->custom()->custom_method(), 'custom DSL execution failed'); + + // test custom DSL exception message + $exceptionMessage = ''; + try { + $qb->invalid(); + } catch (QueryBuilderException $exception) { + $exceptionMessage = $exception->getMessage(); + } + + $this->assertEquals('DSL "invalid" not supported', $exceptionMessage); + } + + /** + * @group unit + */ + public function testFacade() + { + $qb = new QueryBuilder(); + + // test one example QueryBuilder flow for each default DSL type + $this->assertInstanceOf('Elastica\Query\AbstractQuery', $qb->query()->match()); + $this->assertInstanceOf('Elastica\Filter\AbstractFilter', $qb->filter()->bool()); + $this->assertInstanceOf('Elastica\Aggregation\AbstractAggregation', $qb->aggregation()->avg('name')); + $this->assertInstanceOf('Elastica\Suggest\AbstractSuggest', $qb->suggest()->term('name', 'field')); + } + + /** + * @group unit + */ + public function testFacadeException() + { + $qb = new QueryBuilder(new QueryBuilder\Version\Version100()); + + // undefined + $exceptionMessage = ''; + try { + $qb->query()->invalid(); + } catch (QueryBuilderException $exception) { + $exceptionMessage = $exception->getMessage(); + } + + $this->assertEquals('undefined query "invalid"', $exceptionMessage); + + // unsupported + $exceptionMessage = ''; + try { + $qb->aggregation()->top_hits('top_hits'); + } catch (QueryBuilderException $exception) { + $exceptionMessage = $exception->getMessage(); + } + + $this->assertEquals('aggregation "top_hits" in Version100 not supported', $exceptionMessage); + } +} + +class CustomDSL implements QueryBuilder\DSL +{ + public function getType() + { + return 'custom'; + } + + public function custom_method() + { + return true; + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/QueryTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/QueryTest.php new file mode 100644 index 00000000..a39ab23a --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/QueryTest.php @@ -0,0 +1,458 @@ +toArray(); + + $this->assertInternalType('array', $queryArray); + + $this->assertEquals('2011-07-18 00:00:00', $queryArray['query']['filtered']['filter']['range']['due']['gte']); + } + + /** + * @group unit + */ + public function testRawQuery() + { + $textQuery = new Term(array('title' => 'test')); + + $query1 = Query::create($textQuery); + + $query2 = new Query(); + $query2->setRawQuery(array('query' => array('term' => array('title' => 'test')))); + + $this->assertEquals($query1->toArray(), $query2->toArray()); + } + + /** + * @group unit + */ + public function testSuggestShouldNotRemoveOtherParameters() + { + $query1 = new Query(); + $query2 = new Query(); + + $suggest = new Suggest(); + $suggest->setGlobalText('test'); + + $query1->setSize(40); + $query1->setSuggest($suggest); + + $query2->setSuggest($suggest); + $query2->setSize(40); + + $this->assertEquals($query1->toArray(), $query2->toArray()); + } + + /** + * @group unit + */ + public function testSetSuggestMustReturnQueryInstance() + { + $query = new Query(); + $suggest = new Suggest(); + $this->assertInstanceOf('Elastica\Query', $query->setSuggest($suggest)); + } + + /** + * @group unit + */ + public function testArrayQuery() + { + $query = array( + 'query' => array( + 'text' => array( + 'title' => 'test', + ), + ), + ); + + $query1 = Query::create($query); + + $query2 = new Query(); + $query2->setRawQuery(array('query' => array('text' => array('title' => 'test')))); + + $this->assertEquals($query1->toArray(), $query2->toArray()); + } + + /** + * @group functional + */ + public function testSetSort() + { + $index = $this->_createIndex(); + $type = $index->getType('test'); + + $type->addDocuments(array( + new Document(1, array('name' => 'hello world')), + new Document(2, array('firstname' => 'guschti', 'lastname' => 'ruflin')), + new Document(3, array('firstname' => 'nicolas', 'lastname' => 'ruflin')), + )); + + $queryTerm = new Term(); + $queryTerm->setTerm('lastname', 'ruflin'); + + $index->refresh(); + + $query = Query::create($queryTerm); + + // ASC order + $query->setSort(array(array('firstname' => array('order' => 'asc')))); + $resultSet = $type->search($query); + $this->assertEquals(2, $resultSet->count()); + + $first = $resultSet->current()->getData(); + $second = $resultSet->next()->getData(); + + $this->assertEquals('guschti', $first['firstname']); + $this->assertEquals('nicolas', $second['firstname']); + + // DESC order + $query->setSort(array('firstname' => array('order' => 'desc'))); + $resultSet = $type->search($query); + $this->assertEquals(2, $resultSet->count()); + + $first = $resultSet->current()->getData(); + $second = $resultSet->next()->getData(); + + $this->assertEquals('nicolas', $first['firstname']); + $this->assertEquals('guschti', $second['firstname']); + } + + /** + * @group unit + */ + public function testAddSort() + { + $query = new Query(); + $sortParam = array('firstname' => array('order' => 'asc')); + $query->addSort($sortParam); + + $this->assertEquals($query->getParam('sort'), array($sortParam)); + } + + /** + * @group unit + */ + public function testSetRawQuery() + { + $query = new Query(); + + $params = array('query' => 'test'); + $query->setRawQuery($params); + + $this->assertEquals($params, $query->toArray()); + } + + /** + * @group unit + */ + public function testSetFields() + { + $query = new Query(); + + $params = array('query' => 'test'); + + $query->setFields(array('firstname', 'lastname')); + + $data = $query->toArray(); + + $this->assertContains('firstname', $data['fields']); + $this->assertContains('lastname', $data['fields']); + $this->assertCount(2, $data['fields']); + } + + /** + * @group unit + */ + public function testGetQuery() + { + $query = new Query(); + + try { + $query->getQuery(); + $this->fail('should throw exception because query does not exist'); + } catch (InvalidException $e) { + $this->assertTrue(true); + } + + $termQuery = new Term(); + $termQuery->setTerm('text', 'value'); + $query->setQuery($termQuery); + + $this->assertEquals($termQuery->toArray(), $query->getQuery()); + } + + /** + * @group unit + */ + public function testSetFacets() + { + $query = new Query(); + + $facet = new Terms('text'); + $query->setFacets(array($facet)); + + $data = $query->toArray(); + + $this->assertArrayHasKey('facets', $data); + $this->assertEquals(array('text' => array('terms' => array())), $data['facets']); + + $query->setFacets(array()); + + $this->assertArrayNotHasKey('facets', $query->toArray()); + } + + /** + * @group unit + */ + public function testSetQueryToArrayCast() + { + $query = new Query(); + $termQuery = new Term(); + $termQuery->setTerm('text', 'value'); + $query->setQuery($termQuery); + + $termQuery->setTerm('text', 'another value'); + + $anotherQuery = new Query(); + $anotherQuery->setQuery($termQuery); + + $this->assertNotEquals($query->toArray(), $anotherQuery->toArray()); + } + + /** + * @group unit + */ + public function testSetQueryToArrayChangeQuery() + { + $query = new Query(); + $termQuery = new Term(); + $termQuery->setTerm('text', 'value'); + $query->setQuery($termQuery); + + $queryArray = $query->toArray(); + + $termQuery = $query->getQuery(); + $termQuery['term']['text']['value'] = 'another value'; + + $this->assertEquals($queryArray, $query->toArray()); + } + + /** + * @group unit + */ + public function testSetScriptFieldsToArrayCast() + { + $query = new Query(); + $scriptFields = new ScriptFields(); + $scriptFields->addScript('script', new Script('script')); + + $query->setScriptFields($scriptFields); + + $scriptFields->addScript('another script', new Script('another script')); + + $anotherQuery = new Query(); + $anotherQuery->setScriptFields($scriptFields); + + $this->assertNotEquals($query->toArray(), $anotherQuery->toArray()); + } + + /** + * @group unit + */ + public function testAddScriptFieldsToArrayCast() + { + $query = new Query(); + $scriptField = new Script('script'); + + $query->addScriptField('script', $scriptField); + + $scriptField->setScript('another script'); + + $anotherQuery = new Query(); + $anotherQuery->addScriptField('script', $scriptField); + + $this->assertNotEquals($query->toArray(), $anotherQuery->toArray()); + } + + /** + * @group unit + */ + public function testAddFacetToArrayCast() + { + $query = new Query(); + $facet = new Terms('text'); + + $query->addFacet($facet); + + $facet->setName('another text'); + + $anotherQuery = new Query(); + $anotherQuery->addFacet($facet); + + $this->assertNotEquals($query->toArray(), $anotherQuery->toArray()); + } + + /** + * @group unit + */ + public function testAddAggregationToArrayCast() + { + $query = new Query(); + $aggregation = new \Elastica\Aggregation\Terms('text'); + + $query->addAggregation($aggregation); + + $aggregation->setName('another text'); + + $anotherQuery = new Query(); + $anotherQuery->addAggregation($aggregation); + + $this->assertNotEquals($query->toArray(), $anotherQuery->toArray()); + } + + /** + * @group unit + */ + public function testSetSuggestToArrayCast() + { + $query = new Query(); + $suggest = new Suggest(); + $suggest->setGlobalText('text'); + + $query->setSuggest($suggest); + + $suggest->setGlobalText('another text'); + + $anotherQuery = new Query(); + $anotherQuery->setSuggest($suggest); + + $this->assertNotEquals($query->toArray(), $anotherQuery->toArray()); + } + + /** + * @group unit + */ + public function testSetRescoreToArrayCast() + { + $query = new Query(); + $rescore = new \Elastica\Rescore\Query(); + $rescore->setQueryWeight(1); + + $query->setRescore($rescore); + + $rescore->setQueryWeight(2); + + $anotherQuery = new Query(); + $anotherQuery->setRescore($rescore); + + $this->assertNotEquals($query->toArray(), $anotherQuery->toArray()); + } + + /** + * @group unit + */ + public function testSetPostFilterToArrayCast() + { + $query = new Query(); + $postFilter = new \Elastica\Filter\Terms(); + $postFilter->setTerms('key', array('term')); + $query->setPostFilter($postFilter); + + $postFilter->setTerms('another key', array('another term')); + + $anotherQuery = new Query(); + $anotherQuery->setPostFilter($postFilter); + + $this->assertNotEquals($query->toArray(), $anotherQuery->toArray()); + } + + /** + * @group functional + */ + public function testNoSource() + { + $index = $this->_createIndex(); + + $type = new Type($index, 'user'); + + // Adds 1 document to the index + $doc1 = new Document(1, + array('username' => 'ruflin', 'test' => array('2', '3', '5')) + ); + $type->addDocument($doc1); + + // To update index + $index->refresh(); + + $query = Query::create('ruflin'); + $resultSet = $type->search($query); + + // Disable source + $query->setSource(false); + + $resultSetNoSource = $type->search($query); + + $this->assertEquals(1, $resultSet->count()); + $this->assertEquals(1, $resultSetNoSource->count()); + + // Tests if no source is in response except id + $result = $resultSetNoSource->current(); + $this->assertEquals(1, $result->getId()); + $this->assertEmpty($result->getData()); + + // Tests if source is in response except id + $result = $resultSet->current(); + $this->assertEquals(1, $result->getId()); + $this->assertNotEmpty($result->getData()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/RequestTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/RequestTest.php new file mode 100644 index 00000000..987f2391 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/RequestTest.php @@ -0,0 +1,95 @@ + 'params'); + $data = array('key' => 'value'); + + $request = new Request($path, $method, $data, $query); + + $this->assertEquals($path, $request->getPath()); + $this->assertEquals($method, $request->getMethod()); + $this->assertEquals($query, $request->getQuery()); + $this->assertEquals($data, $request->getData()); + } + + /** + * @group unit + * @expectedException \Elastica\Exception\InvalidException + */ + public function testInvalidConnection() + { + $request = new Request('', Request::GET); + $request->send(); + } + + /** + * @group functional + */ + public function testSend() + { + $connection = new Connection(); + $connection->setHost($this->_getHost()); + $connection->setPort('9200'); + + $request = new Request('_status', Request::GET, array(), array(), $connection); + + $response = $request->send(); + + $this->assertInstanceOf('Elastica\Response', $response); + } + + /** + * @group unit + */ + public function testToString() + { + $path = 'test'; + $method = Request::POST; + $query = array('no' => 'params'); + $data = array('key' => 'value'); + + $connection = new Connection(); + $connection->setHost($this->_getHost()); + $connection->setPort('9200'); + + $request = new Request($path, $method, $data, $query, $connection); + + $data = $request->toArray(); + + $this->assertInternalType('array', $data); + $this->assertArrayHasKey('method', $data); + $this->assertArrayHasKey('path', $data); + $this->assertArrayHasKey('query', $data); + $this->assertArrayHasKey('data', $data); + $this->assertArrayHasKey('connection', $data); + $this->assertEquals($request->getMethod(), $data['method']); + $this->assertEquals($request->getPath(), $data['path']); + $this->assertEquals($request->getQuery(), $data['query']); + $this->assertEquals($request->getData(), $data['data']); + $this->assertInternalType('array', $data['connection']); + $this->assertArrayHasKey('host', $data['connection']); + $this->assertArrayHasKey('port', $data['connection']); + $this->assertEquals($request->getConnection()->getHost(), $data['connection']['host']); + $this->assertEquals($request->getConnection()->getPort(), $data['connection']['port']); + + $string = $request->toString(); + + $this->assertInternalType('string', $string); + + $string = (string) $request; + $this->assertInternalType('string', $string); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/ResponseTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/ResponseTest.php new file mode 100644 index 00000000..e7b83ade --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/ResponseTest.php @@ -0,0 +1,205 @@ +assertInstanceOf('Elastica\Facet\Histogram', $facet); + $this->assertInstanceOf('Elastica\Facet\AbstractFacet', $facet); + unset($facet); + } + + /** + * @group functional + */ + public function testResponse() + { + $index = $this->_createIndex(); + $type = $index->getType('helloworld'); + + $mapping = new Mapping($type, array( + 'name' => array('type' => 'string', 'store' => 'no'), + 'dtmPosted' => array('type' => 'date', 'store' => 'no', 'format' => 'yyyy-MM-dd HH:mm:ss'), + )); + $type->setMapping($mapping); + + $type->addDocuments(array( + new Document(1, array('name' => 'nicolas ruflin', 'dtmPosted' => '2011-06-23 21:53:00')), + new Document(2, array('name' => 'raul martinez jr', 'dtmPosted' => '2011-06-23 09:53:00')), + new Document(3, array('name' => 'rachelle clemente', 'dtmPosted' => '2011-07-08 08:53:00')), + new Document(4, array('name' => 'elastica search', 'dtmPosted' => '2011-07-08 01:53:00')), + )); + + $query = new Query(); + $query->setQuery(new MatchAll()); + $index->refresh(); + + $resultSet = $type->search($query); + + $engineTime = $resultSet->getResponse()->getEngineTime(); + $shardsStats = $resultSet->getResponse()->getShardsStatistics(); + + $this->assertInternalType('int', $engineTime); + $this->assertTrue(is_array($shardsStats)); + $this->assertArrayHasKey('total', $shardsStats); + $this->assertArrayHasKey('successful', $shardsStats); + } + + /** + * @group functional + */ + public function testIsOk() + { + $index = $this->_createIndex(); + $type = $index->getType('test'); + + $doc = new Document(1, array('name' => 'ruflin')); + $response = $type->addDocument($doc); + + $this->assertTrue($response->isOk()); + } + + /** + * @group functional + */ + public function testIsOkMultiple() + { + $index = $this->_createIndex(); + $type = $index->getType('test'); + + $docs = array( + new Document(1, array('name' => 'ruflin')), + new Document(2, array('name' => 'ruflin')), + ); + $response = $type->addDocuments($docs); + + $this->assertTrue($response->isOk()); + } + + /** + * @group unit + */ + public function testIsOkBulkWithErrorsField() + { + $response = new Response(json_encode(array( + 'took' => 213, + 'errors' => false, + 'items' => array( + array('index' => array('_index' => 'rohlik', '_type' => 'grocery', '_id' => '707891', '_version' => 4, 'status' => 200)), + array('index' => array('_index' => 'rohlik', '_type' => 'grocery', '_id' => '707893', '_version' => 4, 'status' => 200)), + ), + ))); + + $this->assertTrue($response->isOk()); + } + + /** + * @group unit + */ + public function testIsNotOkBulkWithErrorsField() + { + $response = new Response(json_encode(array( + 'took' => 213, + 'errors' => true, + 'items' => array( + array('index' => array('_index' => 'rohlik', '_type' => 'grocery', '_id' => '707891', '_version' => 4, 'status' => 200)), + array('index' => array('_index' => 'rohlik', '_type' => 'grocery', '_id' => '707893', '_version' => 4, 'status' => 200)), + ), + ))); + + $this->assertFalse($response->isOk()); + } + + /** + * @group unit + */ + public function testIsOkBulkItemsWithOkField() + { + $response = new Response(json_encode(array( + 'took' => 213, + 'items' => array( + array('index' => array('_index' => 'rohlik', '_type' => 'grocery', '_id' => '707891', '_version' => 4, 'ok' => true)), + array('index' => array('_index' => 'rohlik', '_type' => 'grocery', '_id' => '707893', '_version' => 4, 'ok' => true)), + ), + ))); + + $this->assertTrue($response->isOk()); + } + + /** + * @group unit + */ + public function testIsNotOkBulkItemsWithOkField() + { + $response = new Response(json_encode(array( + 'took' => 213, + 'items' => array( + array('index' => array('_index' => 'rohlik', '_type' => 'grocery', '_id' => '707891', '_version' => 4, 'ok' => true)), + array('index' => array('_index' => 'rohlik', '_type' => 'grocery', '_id' => '707893', '_version' => 4, 'ok' => false)), + ), + ))); + + $this->assertFalse($response->isOk()); + } + + /** + * @group unit + */ + public function testIsOkBulkItemsWithStatusField() + { + $response = new Response(json_encode(array( + 'took' => 213, + 'items' => array( + array('index' => array('_index' => 'rohlik', '_type' => 'grocery', '_id' => '707891', '_version' => 4, 'status' => 200)), + array('index' => array('_index' => 'rohlik', '_type' => 'grocery', '_id' => '707893', '_version' => 4, 'status' => 200)), + ), + ))); + + $this->assertTrue($response->isOk()); + } + + /** + * @group unit + */ + public function testIsNotOkBulkItemsWithStatusField() + { + $response = new Response(json_encode(array( + 'took' => 213, + 'items' => array( + array('index' => array('_index' => 'rohlik', '_type' => 'grocery', '_id' => '707891', '_version' => 4, 'status' => 200)), + array('index' => array('_index' => 'rohlik', '_type' => 'grocery', '_id' => '707893', '_version' => 4, 'status' => 301)), + ), + ))); + + $this->assertFalse($response->isOk()); + } + + /** + * @group functional + */ + public function testGetDataEmpty() + { + $index = $this->_createIndex(); + + $response = $index->request( + 'non-existant-type/_mapping', + Request::GET + )->getData(); + + $this->assertEquals(0, count($response)); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/ResultSetTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/ResultSetTest.php new file mode 100644 index 00000000..be76d4a2 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/ResultSetTest.php @@ -0,0 +1,95 @@ +_createIndex(); + $type = $index->getType('test'); + + $type->addDocuments(array( + new Document(1, array('name' => 'elastica search')), + new Document(2, array('name' => 'elastica library')), + new Document(3, array('name' => 'elastica test')), + )); + $index->refresh(); + + $resultSet = $type->search('elastica search'); + + $this->assertInstanceOf('Elastica\ResultSet', $resultSet); + $this->assertEquals(3, $resultSet->getTotalHits()); + $this->assertGreaterThan(0, $resultSet->getMaxScore()); + $this->assertInternalType('array', $resultSet->getResults()); + $this->assertEquals(3, count($resultSet)); + } + + /** + * @group functional + */ + public function testArrayAccess() + { + $index = $this->_createIndex(); + $type = $index->getType('test'); + + $type->addDocuments(array( + new Document(1, array('name' => 'elastica search')), + new Document(2, array('name' => 'elastica library')), + new Document(3, array('name' => 'elastica test')), + )); + $index->refresh(); + + $resultSet = $type->search('elastica search'); + + $this->assertInstanceOf('Elastica\ResultSet', $resultSet); + $this->assertInstanceOf('Elastica\Result', $resultSet[0]); + $this->assertInstanceOf('Elastica\Result', $resultSet[1]); + $this->assertInstanceOf('Elastica\Result', $resultSet[2]); + + $this->assertFalse(isset($resultSet[3])); + } + + /** + * @group functional + * @expectedException \Elastica\Exception\InvalidException + */ + public function testInvalidOffsetCreation() + { + $index = $this->_createIndex(); + $type = $index->getType('test'); + + $doc = new Document(1, array('name' => 'elastica search')); + $type->addDocument($doc); + $index->refresh(); + + $resultSet = $type->search('elastica search'); + + $result = new Result(array('_id' => 'fakeresult')); + $resultSet[1] = $result; + } + + /** + * @group functional + * @expectedException \Elastica\Exception\InvalidException + */ + public function testInvalidOffsetGet() + { + $index = $this->_createIndex(); + $type = $index->getType('test'); + + $doc = new Document(1, array('name' => 'elastica search')); + $type->addDocument($doc); + $index->refresh(); + + $resultSet = $type->search('elastica search'); + + return $resultSet[3]; + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/ResultTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/ResultTest.php new file mode 100644 index 00000000..a905fcc5 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/ResultTest.php @@ -0,0 +1,131 @@ +_createIndex(); + $type = $index->getType($typeName); + + // Adds 1 document to the index + $docId = 3; + $doc1 = new Document($docId, array('username' => 'hans')); + $type->addDocument($doc1); + + // Refreshes index + $index->refresh(); + + $resultSet = $type->search('hans'); + + $this->assertEquals(1, $resultSet->count()); + + $result = $resultSet->current(); + + $this->assertInstanceOf('Elastica\Result', $result); + $this->assertEquals($index->getName(), $result->getIndex()); + $this->assertEquals($typeName, $result->getType()); + $this->assertEquals($docId, $result->getId()); + $this->assertGreaterThan(0, $result->getScore()); + $this->assertInternalType('array', $result->getData()); + $this->assertTrue(isset($result->username)); + $this->assertEquals('hans', $result->username); + } + + /** + * @group functional + */ + public function testGetIdNoSource() + { + // Creates a new index 'xodoa' and a type 'user' inside this index + $indexName = 'xodoa'; + $typeName = 'user'; + + $client = $this->_getClient(); + $index = $client->getIndex($indexName); + $index->create(array(), true); + $type = $index->getType($typeName); + + $mapping = new Mapping($type); + $mapping->disableSource(); + $mapping->send(); + + // Adds 1 document to the index + $docId = 3; + $doc1 = new Document($docId, array('username' => 'hans')); + $type->addDocument($doc1); + + // Refreshes index + $index->refresh(); + + $resultSet = $type->search('hans'); + + $this->assertEquals(1, $resultSet->count()); + + $result = $resultSet->current(); + + $this->assertEquals(array(), $result->getSource()); + $this->assertInstanceOf('Elastica\Result', $result); + $this->assertEquals($indexName, $result->getIndex()); + $this->assertEquals($typeName, $result->getType()); + $this->assertEquals($docId, $result->getId()); + $this->assertGreaterThan(0, $result->getScore()); + $this->assertInternalType('array', $result->getData()); + } + + /** + * @group functional + */ + public function testGetTotalTimeReturnsExpectedResults() + { + $typeName = 'user'; + $index = $this->_createIndex(); + $type = $index->getType($typeName); + + // Adds 1 document to the index + $docId = 3; + $doc1 = new Document($docId, array('username' => 'hans')); + $type->addDocument($doc1); + + // Refreshes index + $index->refresh(); + + $resultSet = $type->search('hans'); + + $this->assertNotNull($resultSet->getTotalTime(), 'Get Total Time should never be a null value'); + $this->assertEquals( + 'integer', + getType($resultSet->getTotalTime()), + 'Total Time should be an integer' + ); + } + + /** + * @group unit + */ + public function testHasFields() + { + $data = array('value set'); + + $result = new Result(array()); + $this->assertFalse($result->hasFields()); + + $result = new Result(array('_source' => $data)); + $this->assertFalse($result->hasFields()); + + $result = new Result(array('fields' => $data)); + $this->assertTrue($result->hasFields()); + $this->assertEquals($data, $result->getFields()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/ScanAndScrollTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/ScanAndScrollTest.php new file mode 100644 index 00000000..9f06f9e6 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/ScanAndScrollTest.php @@ -0,0 +1,78 @@ +_prepareSearch(), '1m', 2); + $docCount = 0; + + /** @var ResultSet $resultSet */ + foreach ($scanAndScroll as $scrollId => $resultSet) { + $docCount += $resultSet->count(); + } + + /* + * number of loops and documents per iteration may fluctuate + * => only test end results + */ + $this->assertEquals(12, $docCount); + } + + /** + * query size revert options. + * + * @group functional + */ + public function testQuerySizeRevert() + { + $search = $this->_prepareSearch(); + $search->getQuery()->setSize(9); + + $scanAndScroll = new ScanAndScroll($search); + + $scanAndScroll->rewind(); + $this->assertEquals(9, $search->getQuery()->getParam('size')); + + $scanAndScroll->next(); + $this->assertEquals(9, $search->getQuery()->getParam('size')); + } + + /** + * index: 12 docs, 2 shards. + * + * @return Search + */ + private function _prepareSearch() + { + $index = $this->_createIndex('', true, 2); + $index->refresh(); + + $docs = array(); + for ($x = 1; $x <= 12; $x++) { + $docs[] = new Document($x, array('id' => $x, 'key' => 'value')); + } + + $type = $index->getType('scanAndScrollTest'); + $type->addDocuments($docs); + $index->refresh(); + + $search = new Search($this->_getClient()); + $search->addIndex($index)->addType($type); + + return $search; + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/ScriptFieldsTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/ScriptFieldsTest.php new file mode 100644 index 00000000..5448209d --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/ScriptFieldsTest.php @@ -0,0 +1,93 @@ +addScript('test', $script); + $this->assertEquals($scriptFields->getParam('test'), $script->toArray()); + + // setScripts + $scriptFields = new ScriptFields(); + $scriptFields->setScripts(array( + 'test' => $script, + )); + $this->assertEquals($scriptFields->getParam('test'), $script->toArray()); + + // Constructor + $scriptFields = new ScriptFields(array( + 'test' => $script, + )); + $this->assertEquals($scriptFields->getParam('test'), $script->toArray()); + } + + /** + * @group unit + */ + public function testSetScriptFields() + { + $query = new Query(); + $script = new Script('1 + 2'); + + $scriptFields = new ScriptFields(array( + 'test' => $script, + )); + $query->setScriptFields($scriptFields); + $this->assertEquals($query->getParam('script_fields'), $scriptFields->toArray()); + + $query->setScriptFields(array( + 'test' => $script, + )); + $this->assertEquals($query->getParam('script_fields'), $scriptFields->toArray()); + } + + /** + * @group unit + * @expectedException \Elastica\Exception\InvalidException + */ + public function testNameException() + { + $script = new Script('1 + 2'); + $scriptFields = new ScriptFields(array($script)); + } + + /** + * @group functional + */ + public function testQuery() + { + $index = $this->_createIndex(); + $type = $index->getType('test'); + + $doc = new Document(1, array('firstname' => 'guschti', 'lastname' => 'ruflin')); + $type->addDocument($doc); + $index->refresh(); + + $query = new Query(); + $script = new Script('1 + 2'); + $scriptFields = new ScriptFields(array( + 'test' => $script, + )); + $query->setScriptFields($scriptFields); + + $resultSet = $type->search($query); + $first = $resultSet->current()->getData(); + + // 1 + 2 + $this->assertEquals(3, $first['test'][0]); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/ScriptTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/ScriptTest.php new file mode 100644 index 00000000..b42d8646 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/ScriptTest.php @@ -0,0 +1,169 @@ + $value, + ); + $this->assertEquals($value, $script->getScript()); + $this->assertEquals($expected, $script->toArray()); + + $params = array( + 'param1' => 'one', + 'param2' => 10, + ); + + $script = new Script($value, $params); + + $expected = array( + 'script' => $value, + 'params' => $params, + ); + + $this->assertEquals($value, $script->getScript()); + $this->assertEquals($params, $script->getParams()); + $this->assertEquals($expected, $script->toArray()); + + $lang = 'mvel'; + + $script = new Script($value, $params, $lang); + + $expected = array( + 'script' => $value, + 'params' => $params, + 'lang' => $lang, + ); + + $this->assertEquals($value, $script->getScript()); + $this->assertEquals($params, $script->getParams()); + $this->assertEquals($lang, $script->getLang()); + $this->assertEquals($expected, $script->toArray()); + } + + /** + * @group unit + */ + public function testCreateString() + { + $string = '_score * 2.0'; + $script = Script::create($string); + + $this->assertInstanceOf('Elastica\Script', $script); + + $this->assertEquals($string, $script->getScript()); + + $expected = array( + 'script' => $string, + ); + $this->assertEquals($expected, $script->toArray()); + } + + /** + * @group unit + */ + public function testCreateScript() + { + $data = new Script('_score * 2.0'); + + $script = Script::create($data); + + $this->assertInstanceOf('Elastica\Script', $script); + $this->assertSame($data, $script); + } + + /** + * @group unit + */ + public function testCreateArray() + { + $string = '_score * 2.0'; + $lang = 'mvel'; + $params = array( + 'param1' => 'one', + 'param2' => 1, + ); + $array = array( + 'script' => $string, + 'lang' => $lang, + 'params' => $params, + ); + + $script = Script::create($array); + + $this->assertInstanceOf('Elastica\Script', $script); + + $this->assertEquals($string, $script->getScript()); + $this->assertEquals($params, $script->getParams()); + $this->assertEquals($lang, $script->getLang()); + + $this->assertEquals($array, $script->toArray()); + } + + /** + * @group unit + * @dataProvider dataProviderCreateInvalid + * @expectedException \Elastica\Exception\InvalidException + */ + public function testCreateInvalid($data) + { + Script::create($data); + } + + /** + * @return array + */ + public function dataProviderCreateInvalid() + { + return array( + array( + new \stdClass(), + ), + array( + array('params' => array('param1' => 'one')), + ), + array( + array('script' => '_score * 2.0', 'params' => 'param'), + ), + ); + } + + /** + * @group unit + */ + public function testSetLang() + { + $script = new Script('foo', array(), Script::LANG_GROOVY); + $this->assertEquals(Script::LANG_GROOVY, $script->getLang()); + + $script->setLang(Script::LANG_PYTHON); + $this->assertEquals(Script::LANG_PYTHON, $script->getLang()); + + $this->assertInstanceOf('Elastica\Script', $script->setLang(Script::LANG_PYTHON)); + } + + /** + * @group unit + */ + public function testSetScript() + { + $script = new Script('foo'); + $this->assertEquals('foo', $script->getScript()); + + $script->setScript('bar'); + $this->assertEquals('bar', $script->getScript()); + + $this->assertInstanceOf('Elastica\Script', $script->setScript('foo')); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/ScrollTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/ScrollTest.php new file mode 100644 index 00000000..df5b0317 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/ScrollTest.php @@ -0,0 +1,105 @@ +_prepareSearch()); + $count = 1; + + /** @var ResultSet $resultSet */ + foreach ($scroll as $scrollId => $resultSet) { + $this->assertNotEmpty($scrollId); + + $results = $resultSet->getResults(); + switch (true) { + case $count === 1: + // hits: 1 - 5 + $this->assertEquals(5, $resultSet->count()); + $this->assertEquals('1', $results[0]->getId()); + $this->assertEquals('5', $results[4]->getId()); + break; + case $count === 2: + // hits: 6 - 10 + $this->assertEquals(5, $resultSet->count()); + $this->assertEquals('6', $results[0]->getId()); + $this->assertEquals('10', $results[4]->getId()); + break; + case $count === 3: + // hit: 11 + $this->assertEquals(1, $resultSet->count()); + $this->assertEquals('11', $results[0]->getId()); + break; + case $count === 4: + $this->assertEquals(0, $resultSet->count()); + break; + default: + $this->fail('too many iterations'); + } + + $count++; + } + } + + /** + * Scroll must not overwrite options. + * + * @group functional + */ + public function testSearchRevert() + { + $search = $this->_prepareSearch(); + + $search->setOption(Search::OPTION_SCROLL, 'must'); + $search->setOption(Search::OPTION_SCROLL_ID, 'not'); + $search->setOption(Search::OPTION_SEARCH_TYPE, 'change'); + $old = $search->getOptions(); + + $scroll = new Scroll($search); + + $scroll->rewind(); + $this->assertEquals($old, $search->getOptions()); + + $scroll->next(); + $this->assertEquals($old, $search->getOptions()); + } + + /** + * index: 11 docs + * query size: 5. + * + * @return Search + */ + private function _prepareSearch() + { + $index = $this->_createIndex(); + $index->refresh(); + + $docs = array(); + for ($x = 1; $x <= 11; $x++) { + $docs[] = new Document($x, array('id' => $x, 'key' => 'value')); + } + + $type = $index->getType('scrollTest'); + $type->addDocuments($docs); + $index->refresh(); + + $search = new Search($this->_getClient()); + $search->addIndex($index)->addType($type); + $search->getQuery()->setSize(5); + + return $search; + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/SearchTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/SearchTest.php new file mode 100644 index 00000000..905f8462 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/SearchTest.php @@ -0,0 +1,647 @@ +_getClient(); + $search = new Search($client); + + $this->assertInstanceOf('Elastica\Search', $search); + $this->assertSame($client, $search->getClient()); + } + + /** + * @group functional + */ + public function testAddIndex() + { + $client = $this->_getClient(); + $search = new Search($client); + + $index1 = $this->_createIndex(); + $index2 = $this->_createIndex(); + + $search->addIndex($index1); + $indices = $search->getIndices(); + + $this->assertEquals(1, count($indices)); + + $search->addIndex($index2); + $indices = $search->getIndices(); + + $this->assertEquals(2, count($indices)); + + $this->assertTrue(in_array($index1->getName(), $indices)); + $this->assertTrue(in_array($index2->getName(), $indices)); + + // Add string + $search->addIndex('test3'); + $indices = $search->getIndices(); + + $this->assertEquals(3, count($indices)); + $this->assertTrue(in_array('test3', $indices)); + } + + /** + * @group unit + */ + public function testAddIndices() + { + $client = $this->_getClient(); + $search = new Search($client); + + $indices = array(); + $indices[] = $client->getIndex('elastica_test1'); + $indices[] = $client->getIndex('elastica_test2'); + + $search->addIndices($indices); + + $this->assertEquals(2, count($search->getIndices())); + } + + /** + * @group functional + */ + public function testAddType() + { + $client = $this->_getClient(); + $search = new Search($client); + + $index = $this->_createIndex(); + + $type1 = $index->getType('type1'); + $type2 = $index->getType('type2'); + + $this->assertEquals(array(), $search->getTypes()); + + $search->addType($type1); + $types = $search->getTypes(); + + $this->assertEquals(1, count($types)); + + $search->addType($type2); + $types = $search->getTypes(); + + $this->assertEquals(2, count($types)); + + $this->assertTrue(in_array($type1->getName(), $types)); + $this->assertTrue(in_array($type2->getName(), $types)); + + // Add string + $search->addType('test3'); + $types = $search->getTypes(); + + $this->assertEquals(3, count($types)); + $this->assertTrue(in_array('test3', $types)); + } + + /** + * @group unit + */ + public function testAddTypes() + { + $client = $this->_getClient(); + $search = new Search($client); + + $index = $client->getIndex('foo'); + + $types = array(); + $types[] = $index->getType('type1'); + $types[] = $index->getType('type2'); + + $search->addTypes($types); + + $this->assertEquals(2, count($search->getTypes())); + } + + /** + * @group unit + * @expectedException \Elastica\Exception\InvalidException + */ + public function testAddTypeInvalid() + { + $client = $this->_getClient(); + $search = new Search($client); + + $search->addType(new \stdClass()); + } + + /** + * @group unit + * @expectedException \Elastica\Exception\InvalidException + */ + public function testAddIndexInvalid() + { + $client = $this->_getClient(); + $search = new Search($client); + + $search->addIndex(new \stdClass()); + } + + /** + * @group unit + */ + public function testAddNumericIndex() + { + $client = $this->_getClient(); + $search = new Search($client); + + $search->addIndex(1); + + $this->assertContains('1', $search->getIndices(), 'Make sure it has been added and converted to string'); + } + + /** + * @group functional + */ + public function testGetPath() + { + $client = $this->_getClient(); + $search1 = new Search($client); + $search2 = new Search($client); + + $index1 = $this->_createIndex(); + $index2 = $this->_createIndex(); + + $type1 = $index1->getType('type1'); + $type2 = $index1->getType('type2'); + + // No index + $this->assertEquals('/_search', $search1->getPath()); + + // Only index + $search1->addIndex($index1); + $this->assertEquals($index1->getName().'/_search', $search1->getPath()); + + // MUltiple index, no types + $search1->addIndex($index2); + $this->assertEquals($index1->getName().','.$index2->getName().'/_search', $search1->getPath()); + + // Single type, no index + $search2->addType($type1); + $this->assertEquals('_all/'.$type1->getName().'/_search', $search2->getPath()); + + // Multiple types + $search2->addType($type2); + $this->assertEquals('_all/'.$type1->getName().','.$type2->getName().'/_search', $search2->getPath()); + + // Combine index and types + $search2->addIndex($index1); + $this->assertEquals($index1->getName().'/'.$type1->getName().','.$type2->getName().'/_search', $search2->getPath()); + } + + /** + * @group functional + */ + public function testSearchRequest() + { + $client = $this->_getClient(); + $search1 = new Search($client); + + $index1 = $this->_createIndex(); + $index2 = $this->_createIndex(); + + $type1 = $index1->getType('hello1'); + + $result = $search1->search(array()); + $this->assertFalse($result->getResponse()->hasError()); + + $search1->addIndex($index1); + + $result = $search1->search(array()); + $this->assertFalse($result->getResponse()->hasError()); + + $search1->addIndex($index2); + + $result = $search1->search(array()); + $this->assertFalse($result->getResponse()->hasError()); + + $search1->addType($type1); + + $result = $search1->search(array()); + $this->assertFalse($result->getResponse()->hasError()); + } + + /** + * @group functional + */ + public function testSearchScrollRequest() + { + $client = $this->_getClient(); + + $index = $this->_createIndex(); + $type = $index->getType('scrolltest'); + + $docs = array(); + for ($x = 1; $x <= 10; $x++) { + $docs[] = new Document($x, array('id' => $x, 'testscroll' => 'jbafford')); + } + + $type->addDocuments($docs); + $index->refresh(); + + $search = new Search($client); + $search->addIndex($index)->addType($type); + $result = $search->search(array(), array( + Search::OPTION_SEARCH_TYPE => Search::OPTION_SEARCH_TYPE_SCAN, + Search::OPTION_SCROLL => '5m', + Search::OPTION_SIZE => 5, + )); + $this->assertFalse($result->getResponse()->hasError()); + + $scrollId = $result->getResponse()->getScrollId(); + $this->assertNotEmpty($scrollId); + + //There are 10 items, and we're scrolling with a size of 5 + //So we should get two results of 5 items, and then no items + //We should also have sent the raw scroll_id as the HTTP request body + $search = new Search($client); + $result = $search->search(array(), array( + Search::OPTION_SCROLL => '5m', + Search::OPTION_SCROLL_ID => $scrollId, + )); + $this->assertFalse($result->getResponse()->hasError()); + $this->assertEquals(5, count($result->getResults())); + $this->assertArrayNotHasKey(Search::OPTION_SCROLL_ID, $search->getClient()->getLastRequest()->getQuery()); + $this->assertEquals($scrollId, $search->getClient()->getLastRequest()->getData()); + + $result = $search->search(array(), array( + Search::OPTION_SCROLL => '5m', + Search::OPTION_SCROLL_ID => $scrollId, + )); + $this->assertFalse($result->getResponse()->hasError()); + $this->assertEquals(5, count($result->getResults())); + $this->assertArrayNotHasKey(Search::OPTION_SCROLL_ID, $search->getClient()->getLastRequest()->getQuery()); + $this->assertEquals($scrollId, $search->getClient()->getLastRequest()->getData()); + + $result = $search->search(array(), array( + Search::OPTION_SCROLL => '5m', + Search::OPTION_SCROLL_ID => $scrollId, + )); + $this->assertFalse($result->getResponse()->hasError()); + $this->assertEquals(0, count($result->getResults())); + $this->assertArrayNotHasKey(Search::OPTION_SCROLL_ID, $search->getClient()->getLastRequest()->getQuery()); + $this->assertEquals($scrollId, $search->getClient()->getLastRequest()->getData()); + } + + /** + * Default Limit tests for \Elastica\Search. + * + * @group functional + */ + public function testLimitDefaultSearch() + { + $client = $this->_getClient(); + $search = new Search($client); + + $index = $client->getIndex('zero'); + $index->create(array('index' => array('number_of_shards' => 1, 'number_of_replicas' => 0)), true); + + $type = $index->getType('zeroType'); + $type->addDocuments(array( + new Document(1, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')), + new Document(2, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')), + new Document(3, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')), + new Document(4, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')), + new Document(5, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')), + new Document(6, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')), + new Document(7, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')), + new Document(8, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')), + new Document(9, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')), + new Document(10, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')), + new Document(11, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')), + )); + $index->refresh(); + + $search->addIndex($index)->addType($type); + + // default limit results (default limit is 10) + $resultSet = $search->search('farrelley'); + $this->assertEquals(10, $resultSet->count()); + + // limit = 1 + $resultSet = $search->search('farrelley', 1); + $this->assertEquals(1, $resultSet->count()); + } + + /** + * @group functional + * @expectedException \Elastica\Exception\InvalidException + */ + public function testArrayConfigSearch() + { + $client = $this->_getClient(); + $search = new Search($client); + + $index = $client->getIndex('zero'); + $index->create(array('index' => array('number_of_shards' => 1, 'number_of_replicas' => 0)), true); + + $docs = array(); + for ($i = 0; $i < 11; $i++) { + $docs[] = new Document($i, array('id' => 1, 'email' => 'test@test.com', 'username' => 'test')); + } + + $type = $index->getType('zeroType'); + $type->addDocuments($docs); + $index->refresh(); + + $search->addIndex($index)->addType($type); + //Backward compatibility, integer => limit + // default limit results (default limit is 10) + $resultSet = $search->search('test'); + $this->assertEquals(10, $resultSet->count()); + + // limit = 1 + $resultSet = $search->search('test', 1); + $this->assertEquals(1, $resultSet->count()); + + //Array with limit + $resultSet = $search->search('test', array('limit' => 2)); + $this->assertEquals(2, $resultSet->count()); + + //Array with size + $resultSet = $search->search('test', array('size' => 2)); + $this->assertEquals(2, $resultSet->count()); + + //Array with from + $resultSet = $search->search('test', array('from' => 10)); + $this->assertEquals(10, $resultSet->current()->getId()); + + //Array with routing + $resultSet = $search->search('test', array('routing' => 'r1,r2')); + $this->assertEquals(10, $resultSet->count()); + + //Array with limit and routing + $resultSet = $search->search('test', array('limit' => 5, 'routing' => 'r1,r2')); + $this->assertEquals(5, $resultSet->count()); + + //Search types + $resultSet = $search->search('test', array('limit' => 5, 'search_type' => 'count')); + $this->assertTrue(($resultSet->count() === 0) && $resultSet->getTotalHits() === 11); + + //Timeout - this one is a bit more tricky to test + $mockResponse = new \Elastica\Response(json_encode(array('timed_out' => true))); + $client = $this->getMockBuilder('Elastica\\Client') + ->disableOriginalConstructor() + ->getMock(); + $client->method('request') + ->will($this->returnValue($mockResponse)); + $search = new Search($client); + $script = new Script('Thread.sleep(100); return _score;'); + $query = new FunctionScore(); + $query->addScriptScoreFunction($script); + $resultSet = $search->search($query, array('timeout' => 50)); + $this->assertTrue($resultSet->hasTimedOut()); + + // Throws InvalidException + $resultSet = $search->search('test', array('invalid_option' => 'invalid_option_value')); + } + + /** + * @group functional + */ + public function testSearchWithVersionOption() + { + $index = $this->_createIndex(); + $doc = new Document(1, array('id' => 1, 'email' => 'test@test.com', 'username' => 'ruflin')); + $index->getType('test')->addDocument($doc); + $index->refresh(); + + $search = new Search($index->getClient()); + $search->addIndex($index); + + // Version param should not be inside by default + $results = $search->search(new MatchAll()); + $hit = $results->current(); + $this->assertEquals(array(), $hit->getParam('_version')); + + // Added version param to result + $results = $search->search(new MatchAll(), array('version' => true)); + $hit = $results->current(); + $this->assertEquals(1, $hit->getParam('_version')); + } + + /** + * @group functional + */ + public function testCountRequest() + { + $client = $this->_getClient(); + $search = new Search($client); + + $index = $client->getIndex('zero'); + $index->create(array('index' => array('number_of_shards' => 1, 'number_of_replicas' => 0)), true); + + $type = $index->getType('zeroType'); + $type->addDocuments(array( + new Document(1, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')), + new Document(2, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')), + new Document(3, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')), + new Document(4, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')), + new Document(5, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')), + new Document(6, array('id' => 1, 'email' => 'test@test.com', 'username' => 'marley')), + new Document(7, array('id' => 1, 'email' => 'test@test.com', 'username' => 'marley')), + new Document(8, array('id' => 1, 'email' => 'test@test.com', 'username' => 'marley')), + new Document(9, array('id' => 1, 'email' => 'test@test.com', 'username' => 'marley')), + new Document(10, array('id' => 1, 'email' => 'test@test.com', 'username' => 'marley')), + new Document(11, array('id' => 1, 'email' => 'test@test.com', 'username' => 'marley')), + )); + $index->refresh(); + + $search->addIndex($index)->addType($type); + + $count = $search->count('farrelley'); + $this->assertEquals(5, $count); + + $count = $search->count('marley'); + $this->assertEquals(6, $count); + + $count = $search->count(); + $this->assertEquals(6, $count, 'Uses previous query set'); + + $count = $search->count(new MatchAll()); + $this->assertEquals(11, $count); + + $count = $search->count('bunny'); + $this->assertEquals(0, $count); + } + + /** + * @group functional + */ + public function testEmptySearch() + { + $client = $this->_getClient(); + $search = new Search($client); + + $index = $client->getIndex('zero'); + $index->create(array('index' => array('number_of_shards' => 1, 'number_of_replicas' => 0)), true); + $type = $index->getType('zeroType'); + $type->addDocuments(array( + new Document(1, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')), + new Document(2, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')), + new Document(3, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')), + new Document(4, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')), + new Document(5, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')), + new Document(6, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')), + new Document(7, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')), + new Document(8, array('id' => 1, 'email' => 'test@test.com', 'username' => 'bunny')), + new Document(9, array('id' => 1, 'email' => 'test@test.com', 'username' => 'bunny')), + new Document(10, array('id' => 1, 'email' => 'test@test.com', 'username' => 'bunny')), + new Document(11, array('id' => 1, 'email' => 'test@test.com', 'username' => 'bunny')), + )); + $index->refresh(); + + $search->addIndex($index)->addType($type); + $resultSet = $search->search(); + $this->assertInstanceOf('Elastica\ResultSet', $resultSet); + $this->assertCount(10, $resultSet); + $this->assertEquals(11, $resultSet->getTotalHits()); + + $query = new QueryString('bunny'); + $search->setQuery($query); + + $resultSet = $search->search(); + + $this->assertCount(4, $resultSet); + $this->assertEquals(4, $resultSet->getTotalHits()); + $source = $resultSet->current()->getSource(); + $this->assertEquals('bunny', $source['username']); + } + + /** + * @group functional + */ + public function testCount() + { + $index = $this->_createIndex(); + $search = new Search($index->getClient()); + $type = $index->getType('test'); + + $doc = new Document(1, array('id' => 1, 'username' => 'ruflin')); + + $type->addDocument($doc); + $index->refresh(); + + $search->addIndex($index); + $search->addType($type); + + $result1 = $search->count(new \Elastica\Query\MatchAll()); + $this->assertEquals(1, $result1); + + $result2 = $search->count(new \Elastica\Query\MatchAll(), true); + $this->assertInstanceOf('\Elastica\ResultSet', $result2); + $this->assertEquals(1, $result2->getTotalHits()); + } + + /** + * @group functional + */ + public function testScanAndScroll() + { + $search = new Search($this->_getClient()); + $this->assertInstanceOf('Elastica\ScanAndScroll', $search->scanAndScroll()); + } + + /** + * @group functional + */ + public function testIgnoreUnavailableOption() + { + $client = $this->_getClient(); + $index = $client->getIndex('elastica_7086b4c2ee585bbb6740ece5ed7ece01'); + $query = new MatchAll(); + + $search = new Search($client); + $search->addIndex($index); + + $exception = null; + try { + $search->search($query); + } catch (ResponseException $e) { + $exception = $e; + } + $this->assertEquals('IndexMissingException', $exception->getElasticsearchException()->getExceptionName()); + + $results = $search->search($query, array(Search::OPTION_SEARCH_IGNORE_UNAVAILABLE => true)); + $this->assertInstanceOf('\Elastica\ResultSet', $results); + } + + /** + * @group functional + */ + public function testQueryCacheOption() + { + $client = $this->_getClient(); + + $index = $client->getIndex('zero'); + $index->create(array('index' => array('number_of_shards' => 1, 'number_of_replicas' => 0)), true); + $type = $index->getType('zeroType'); + $type->addDocuments(array( + new Document(1, array('id' => 1, 'username' => 'farrelley')), + new Document(2, array('id' => 2, 'username' => 'bunny')), + )); + $index->refresh(); + + $aggregation = new Aggregation\Terms('username'); + $aggregation->setField('username'); + + $query = new Query(); + $query->addAggregation($aggregation); + + $search = new Search($client); + $search->addIndex($index); + $search->setQuery($query); + $search->setOption(Search::OPTION_SEARCH_TYPE, Search::OPTION_SEARCH_TYPE_COUNT); + $search->setOption(Search::OPTION_QUERY_CACHE, true); + + // before search query cache should be empty + $statsData = $index->getStats()->getData(); + $queryCache = $statsData['_all']['primaries']['query_cache']; + + $this->assertEquals(0, $queryCache['memory_size_in_bytes']); + $this->assertEquals(0, $queryCache['evictions']); + $this->assertEquals(0, $queryCache['hit_count']); + $this->assertEquals(0, $queryCache['miss_count']); + + // first search should result in cache miss and save data to cache + $search->search(); + $index->getStats()->refresh(); + $statsData = $index->getStats()->getData(); + $queryCache = $statsData['_all']['primaries']['query_cache']; + + $this->assertNotEquals(0, $queryCache['memory_size_in_bytes']); + $this->assertEquals(0, $queryCache['evictions']); + $this->assertEquals(0, $queryCache['hit_count']); + $this->assertEquals(1, $queryCache['miss_count']); + + // next search should result in cache hit + $search->search(); + $index->getStats()->refresh(); + $statsData = $index->getStats()->getData(); + $queryCache = $statsData['_all']['primaries']['query_cache']; + + $this->assertNotEquals(0, $queryCache['memory_size_in_bytes']); + $this->assertEquals(0, $queryCache['evictions']); + $this->assertEquals(1, $queryCache['hit_count']); + $this->assertEquals(1, $queryCache['miss_count']); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/ShutdownTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/ShutdownTest.php new file mode 100644 index 00000000..93d6e32b --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/ShutdownTest.php @@ -0,0 +1,74 @@ +_getClient(); + $cluster = $client->getCluster(); + $nodes = $cluster->getNodes(); + + $nodesCount = count($nodes); + + if ($nodesCount < 2) { + $this->markTestIncomplete('At least two nodes have to be running, because 1 node is shutdown'); + } + + $portFound = false; + // sayonara, wolverine, we'd never love you + foreach ($nodes as $node) { + if ((int) $node->getInfo()->getPort() === 9201) { + $portFound = true; + $node->shutdown('1s'); + break; + } + } + + if (!$portFound) { + $this->markTestSkipped('This test was skipped as in the new docker environment all elasticsearch instances run on the same port'); + } + + // Wait until node is shutdown + sleep(5); + + // Get nodes again + $client = $this->_getClient(); + $cluster = $client->getCluster(); + $nodes = $cluster->getNodes(); + + // Only one left + $this->assertCount($nodesCount - 1, $nodes); + } + + /** + * @group shutdown + * @depends testNodeShutdown + * @expectedException \Elastica\Exception\Connection\HttpException + */ + public function testClusterShutdown() + { + // Get cluster nodes + $client = $this->_getClient(); + $cluster = $client->getCluster(); + $nodes = $cluster->getNodes(); + + // Shutdown cluster + $cluster->shutdown('1s'); + + // Wait... + sleep(5); + + // Now exception must be thrown + $client->getStatus(); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/SnapshotTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/SnapshotTest.php new file mode 100644 index 00000000..fa190e8c --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/SnapshotTest.php @@ -0,0 +1,109 @@ +_snapshot = new Snapshot($this->_getClient()); + + $this->_index = $this->_createIndex(); + $this->_docs = array( + new Document('1', array('city' => 'San Diego')), + new Document('2', array('city' => 'San Luis Obispo')), + new Document('3', array('city' => 'San Francisco')), + ); + $this->_index->getType('test')->addDocuments($this->_docs); + $this->_index->refresh(); + } + + /** + * @group functional + */ + public function testRegisterRepository() + { + $name = 'test_register'; + $location = '/tmp/test_register'; + + $response = $this->_snapshot->registerRepository($name, 'fs', array('location' => $location)); + $this->assertTrue($response->isOk()); + + $response = $this->_snapshot->getRepository($name); + $this->assertEquals($location, $response['settings']['location']); + + // attempt to retrieve a repository which does not exist + $this->setExpectedException('Elastica\Exception\NotFoundException'); + $this->_snapshot->getRepository('foobar'); + } + + /** + * @group functional + */ + public function testSnapshotAndRestore() + { + $repositoryName = 'test_repository'; + $location = "/tmp/{$repositoryName}"; + + // register the repository + $response = $this->_snapshot->registerRepository($repositoryName, 'fs', array('location' => $location)); + $this->assertTrue($response->isOk()); + + // create a snapshot of our test index + $snapshotName = 'test_snapshot_1'; + $response = $this->_snapshot->createSnapshot($repositoryName, $snapshotName, array('indices' => $this->_index->getName()), true); + + // ensure that the snapshot was created properly + $this->assertTrue($response->isOk()); + $this->assertArrayHasKey('snapshot', $response->getData()); + $data = $response->getData(); + $this->assertContains($this->_index->getName(), $data['snapshot']['indices']); + $this->assertEquals(1, sizeof($data['snapshot']['indices'])); // only the specified index should be present + $this->assertEquals($snapshotName, $data['snapshot']['snapshot']); + + // retrieve data regarding the snapshot + $response = $this->_snapshot->getSnapshot($repositoryName, $snapshotName); + $this->assertContains($this->_index->getName(), $response['indices']); + + // delete our test index + $this->_index->delete(); + + // restore the index from our snapshot + $response = $this->_snapshot->restoreSnapshot($repositoryName, $snapshotName, array(), true); + $this->assertTrue($response->isOk()); + + $this->_index->refresh(); + $this->_index->optimize(); + + // ensure that the index has been restored + $count = $this->_index->getType('test')->count(); + $this->assertEquals(sizeof($this->_docs), $count); + + // delete the snapshot + $response = $this->_snapshot->deleteSnapshot($repositoryName, $snapshotName); + $this->assertTrue($response->isOk()); + + // ensure that the snapshot has been deleted + $this->setExpectedException('Elastica\Exception\NotFoundException'); + $this->_snapshot->getSnapshot($repositoryName, $snapshotName); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/StatusTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/StatusTest.php new file mode 100644 index 00000000..fe6bb091 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/StatusTest.php @@ -0,0 +1,133 @@ +_createIndex(); + $status = new Status($index->getClient()); + $this->assertInstanceOf('Elastica\Response', $status->getResponse()); + } + + /** + * @group functional + */ + public function testGetIndexStatuses() + { + $index = $this->_createIndex(); + + $status = new Status($index->getClient()); + $statuses = $status->getIndexStatuses(); + + $this->assertInternalType('array', $statuses); + + foreach ($statuses as $indexStatus) { + $this->assertInstanceOf('Elastica\Index\Status', $indexStatus); + } + } + + /** + * @group functional + */ + public function testGetIndexNames() + { + $indexName = 'test'; + $client = $this->_getClient(); + $index = $client->getIndex($indexName); + $index->create(array(), true); + $index = $this->_createIndex(); + $index->refresh(); + $index->optimize(); + + $status = new Status($index->getClient()); + $names = $status->getIndexNames(); + + $this->assertInternalType('array', $names); + $this->assertContains($index->getName(), $names); + + foreach ($names as $name) { + $this->assertInternalType('string', $name); + } + } + + /** + * @group functional + */ + public function testIndexExists() + { + $indexName = 'elastica_test'; + $aliasName = 'elastica_test-alias'; + + $client = $this->_getClient(); + $index = $client->getIndex($indexName); + + try { + // Make sure index is deleted first + $index->delete(); + } catch (ResponseException $e) { + } + + $status = new Status($client); + $this->assertFalse($status->indexExists($indexName)); + $index->create(); + + $status->refresh(); + $this->assertTrue($status->indexExists($indexName)); + } + + /** + * @group functional + */ + public function testAliasExists() + { + $aliasName = 'elastica_test-alias'; + + $index1 = $this->_createIndex(); + $indexName = $index1->getName(); + + $status = new Status($index1->getClient()); + + foreach ($status->getIndicesWithAlias($aliasName) as $tmpIndex) { + $tmpIndex->removeAlias($aliasName); + } + + $this->assertFalse($status->aliasExists($aliasName)); + + $index1->addAlias($aliasName); + $status->refresh(); + $this->assertTrue($status->aliasExists($aliasName)); + + $indicesWithAlias = $status->getIndicesWithAlias($aliasName); + $this->assertEquals(array($indexName), array_map( + function ($index) { + return $index->getName(); + }, $indicesWithAlias)); + } + + /** + * @group functional + */ + public function testServerStatus() + { + $client = $this->_getClient(); + $status = $client->getStatus(); + $serverStatus = $status->getServerStatus(); + + $this->assertTrue(!empty($serverStatus)); + $this->assertTrue('array' == gettype($serverStatus)); + $this->assertArrayHasKey('status', $serverStatus); + $this->assertTrue($serverStatus['status'] == 200); + $this->assertArrayHasKey('version', $serverStatus); + + $versionInfo = $serverStatus['version']; + $this->assertArrayHasKey('number', $versionInfo); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Suggest/CompletionTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Suggest/CompletionTest.php new file mode 100644 index 00000000..6120743c --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Suggest/CompletionTest.php @@ -0,0 +1,140 @@ +_createIndex(); + $type = $index->getType('song'); + + $type->setMapping(array( + 'fieldName' => array( + 'type' => 'completion', + 'payloads' => true, + ), + )); + + $type->addDocuments(array( + new Document(1, array( + 'fieldName' => array( + 'input' => array('Nevermind', 'Nirvana'), + 'output' => 'Nevermind - Nirvana', + 'payload' => array( + 'year' => 1991, + ), + ), + )), + new Document(2, array( + 'fieldName' => array( + 'input' => array('Bleach', 'Nirvana'), + 'output' => 'Bleach - Nirvana', + 'payload' => array( + 'year' => 1989, + ), + ), + )), + new Document(3, array( + 'fieldName' => array( + 'input' => array('Incesticide', 'Nirvana'), + 'output' => 'Incesticide - Nirvana', + 'payload' => array( + 'year' => 1992, + ), + ), + )), + )); + + $index->refresh(); + + return $index; + } + + /** + * @group unit + */ + public function testToArray() + { + $suggest = new Completion('suggestName', 'fieldName'); + $suggest->setText('foo'); + $suggest->setSize(10); + $expected = array( + 'text' => 'foo', + 'completion' => array( + 'size' => 10, + 'field' => 'fieldName', + ), + ); + $this->assertEquals($expected, $suggest->toArray()); + } + + /** + * @group functional + */ + public function testSuggestWorks() + { + $suggest = new Completion('suggestName', 'fieldName'); + $suggest->setText('Never'); + + $index = $this->_getIndexForTest(); + $resultSet = $index->search(Query::create($suggest)); + + $this->assertTrue($resultSet->hasSuggests()); + + $suggests = $resultSet->getSuggests(); + $options = $suggests['suggestName'][0]['options']; + + $this->assertCount(1, $options); + $this->assertEquals('Nevermind - Nirvana', $options[0]['text']); + $this->assertEquals(1991, $options[0]['payload']['year']); + } + + /** + * @group functional + */ + public function testFuzzySuggestWorks() + { + $suggest = new Completion('suggestName', 'fieldName'); + $suggest->setFuzzy(array('fuzziness' => 2)); + $suggest->setText('Neavermint'); + + $index = $this->_getIndexForTest(); + $resultSet = $index->search(Query::create($suggest)); + + $this->assertTrue($resultSet->hasSuggests()); + + $suggests = $resultSet->getSuggests(); + $options = $suggests['suggestName'][0]['options']; + + $this->assertCount(1, $options); + $this->assertEquals('Nevermind - Nirvana', $options[0]['text']); + } + + /** + * @group unit + */ + public function testSetFuzzy() + { + $suggest = new Completion('suggestName', 'fieldName'); + + $fuzzy = array( + 'unicode_aware' => true, + 'fuzziness' => 3, + ); + + $suggest->setFuzzy($fuzzy); + + $this->assertEquals($fuzzy, $suggest->getParam('fuzzy')); + + $this->assertInstanceOf('Elastica\\Suggest\\Completion', $suggest->setFuzzy($fuzzy)); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Suggest/PhraseTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Suggest/PhraseTest.php new file mode 100644 index 00000000..9ce345d4 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Suggest/PhraseTest.php @@ -0,0 +1,82 @@ +_createIndex(); + $type = $index->getType('testSuggestType'); + $type->addDocuments(array( + new Document(1, array('text' => 'Github is pretty cool')), + new Document(2, array('text' => 'Elasticsearch is bonsai cool')), + new Document(3, array('text' => 'This is a test phrase')), + new Document(4, array('text' => 'Another sentence for testing')), + new Document(5, array('text' => 'Some more words here')), + )); + $index->refresh(); + + return $index; + } + + /** + * @group unit + */ + public function testToArray() + { + $suggest = new Suggest(); + $phraseSuggest = new Phrase('suggest1', 'text'); + $phraseSuggest->setText('elasticsearch is bansai coor'); + $phraseSuggest->setAnalyzer('simple'); + $suggest->addSuggestion($phraseSuggest); + $suggest->setGlobalText('global!'); + + $expected = array( + 'suggest' => array( + 'text' => 'global!', + 'suggest1' => array( + 'text' => 'elasticsearch is bansai coor', + 'phrase' => array( + 'field' => 'text', + 'analyzer' => 'simple', + ), + ), + ), + ); + + $this->assertEquals($expected, $suggest->toArray()); + } + + /** + * @group functional + */ + public function testPhraseSuggest() + { + $suggest = new Suggest(); + $phraseSuggest = new Phrase('suggest1', 'text'); + $phraseSuggest->setText('elasticsearch is bansai coor'); + $phraseSuggest->setAnalyzer('simple')->setHighlight('', '')->setStupidBackoffSmoothing(0.4); + $phraseSuggest->addCandidateGenerator(new DirectGenerator('text')); + $suggest->addSuggestion($phraseSuggest); + + $index = $this->_getIndexForTest(); + $result = $index->search($suggest); + $suggests = $result->getSuggests(); + + // 3 suggestions should be returned: One in which both misspellings are corrected, and two in which only one misspelling is corrected. + $this->assertEquals(3, sizeof($suggests['suggest1'][0]['options'])); + + $this->assertEquals('elasticsearch is bonsai cool', $suggests['suggest1'][0]['options'][0]['highlighted']); + $this->assertEquals('elasticsearch is bonsai cool', $suggests['suggest1'][0]['options'][0]['text']); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Suggest/TermTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Suggest/TermTest.php new file mode 100644 index 00000000..f1250e6f --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Suggest/TermTest.php @@ -0,0 +1,105 @@ +_createIndex(); + $type = $index->getType('testSuggestType'); + $type->addDocuments(array( + new Document(1, array('id' => 1, 'text' => 'GitHub')), + new Document(2, array('id' => 1, 'text' => 'Elastic')), + new Document(3, array('id' => 1, 'text' => 'Search')), + new Document(4, array('id' => 1, 'text' => 'Food')), + new Document(5, array('id' => 1, 'text' => 'Flood')), + new Document(6, array('id' => 1, 'text' => 'Folks')), + )); + $index->refresh(); + + return $index; + } + + /** + * @group unit + */ + public function testToArray() + { + $suggest = new Suggest(); + $suggest1 = new Term('suggest1', '_all'); + $suggest->addSuggestion($suggest1->setText('Foor')); + $suggest2 = new Term('suggest2', '_all'); + $suggest->addSuggestion($suggest2->setText('Girhub')); + + $expected = array( + 'suggest' => array( + 'suggest1' => array( + 'term' => array( + 'field' => '_all', + ), + 'text' => 'Foor', + ), + 'suggest2' => array( + 'term' => array( + 'field' => '_all', + ), + 'text' => 'Girhub', + ), + ), + ); + + $this->assertEquals($expected, $suggest->toArray()); + } + + /** + * @group functional + */ + public function testSuggestResults() + { + $suggest = new Suggest(); + $suggest1 = new Term('suggest1', '_all'); + $suggest->addSuggestion($suggest1->setText('Foor seach')); + $suggest2 = new Term('suggest2', '_all'); + $suggest->addSuggestion($suggest2->setText('Girhub')); + + $index = $this->_getIndexForTest(); + $result = $index->search($suggest); + + $this->assertEquals(2, $result->countSuggests()); + + $suggests = $result->getSuggests(); + + // Ensure that two suggestion results are returned for suggest1 + $this->assertEquals(2, sizeof($suggests['suggest1'])); + + $this->assertEquals('github', $suggests['suggest2'][0]['options'][0]['text']); + $this->assertEquals('food', $suggests['suggest1'][0]['options'][0]['text']); + } + + /** + * @group functional + */ + public function testSuggestNoResults() + { + $termSuggest = new Term('suggest1', '_all'); + $termSuggest->setText('Foobar')->setSize(4); + + $index = $this->_getIndexForTest(); + $result = $index->search($termSuggest); + + $this->assertEquals(1, $result->countSuggests()); + + // Assert that no suggestions were returned + $suggests = $result->getSuggests(); + $this->assertEquals(0, sizeof($suggests['suggest1'][0]['options'])); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Tool/CrossIndexTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Tool/CrossIndexTest.php new file mode 100644 index 00000000..f8fbf280 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Tool/CrossIndexTest.php @@ -0,0 +1,135 @@ +_createIndex(null, true, 2); + $this->_addDocs($oldIndex->getType('crossIndexTest'), 10); + + $newIndex = $this->_createIndex(null, true, 2); + + $this->assertInstanceOf( + 'Elastica\Index', + CrossIndex::reindex($oldIndex, $newIndex) + ); + + $this->assertEquals(10, $newIndex->count()); + } + + /** + * Test reindex type option. + */ + public function testReindexTypeOption() + { + $oldIndex = $this->_createIndex('', true, 2); + $type1 = $oldIndex->getType('crossIndexTest_1'); + $type2 = $oldIndex->getType('crossIndexTest_2'); + + $docs1 = $this->_addDocs($type1, 10); + $docs2 = $this->_addDocs($type2, 10); + + $newIndex = $this->_createIndex(null, true, 2); + + // \Elastica\Type + CrossIndex::reindex($oldIndex, $newIndex, array( + CrossIndex::OPTION_TYPE => $type1, + )); + $this->assertEquals(10, $newIndex->count()); + $newIndex->deleteDocuments($docs1); + + // string + CrossIndex::reindex($oldIndex, $newIndex, array( + CrossIndex::OPTION_TYPE => 'crossIndexTest_2', + )); + $this->assertEquals(10, $newIndex->count()); + $newIndex->deleteDocuments($docs2); + + // array + CrossIndex::reindex($oldIndex, $newIndex, array( + CrossIndex::OPTION_TYPE => array( + 'crossIndexTest_1', + $type2, + ), + )); + $this->assertEquals(20, $newIndex->count()); + } + + /** + * Test default copy. + */ + public function testCopy() + { + $oldIndex = $this->_createIndex(null, true, 2); + $newIndex = $this->_createIndex(null, true, 2); + + $oldType = $oldIndex->getType('copy_test'); + $oldMapping = array( + 'name' => array( + 'type' => 'string', + 'store' => true, + ), + ); + $oldType->setMapping($oldMapping); + $docs = $this->_addDocs($oldType, 10); + + // mapping + $this->assertInstanceOf( + 'Elastica\Index', + CrossIndex::copy($oldIndex, $newIndex) + ); + + $newMapping = $newIndex->getType('copy_test')->getMapping(); + if (!isset($newMapping['copy_test']['properties']['name'])) { + $this->fail('could not request new mapping'); + } + + $this->assertEquals( + $oldMapping['name'], + $newMapping['copy_test']['properties']['name'] + ); + + // document copy + $this->assertEquals(10, $newIndex->count()); + $newIndex->deleteDocuments($docs); + + // ignore mapping + $ignoredType = $oldIndex->getType('copy_test_1'); + $this->_addDocs($ignoredType, 10); + + CrossIndex::copy($oldIndex, $newIndex, array( + CrossIndex::OPTION_TYPE => $oldType, + )); + + $this->assertFalse($newIndex->getType($ignoredType->getName())->exists()); + $this->assertEquals(10, $newIndex->count()); + } + + /** + * @param Type $type + * @param int $docs + * + * @return array + */ + private function _addDocs(Type $type, $docs) + { + $insert = array(); + for ($i = 1; $i <= $docs; $i++) { + $insert[] = new Document($i, array('_id' => $i, 'key' => 'value')); + } + + $type->addDocuments($insert); + $type->getIndex()->refresh(); + + return $insert; + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/AbstractTransportTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/AbstractTransportTest.php new file mode 100644 index 00000000..20573cc7 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/AbstractTransportTest.php @@ -0,0 +1,80 @@ + 'Http')), + array(array('type' => new Http())), + array(new Http()), + ); + } + + /** + * @group unit + * @dataProvider getValidDefinitions + */ + public function testCanCreateTransportInstances($transport) + { + $connection = new Connection(); + $params = array(); + $transport = AbstractTransport::create($transport, $connection, $params); + $this->assertInstanceOf('Elastica\Transport\AbstractTransport', $transport); + $this->assertSame($connection, $transport->getConnection()); + } + + public function getInvalidDefinitions() + { + return array( + array(array('transport' => 'Http')), + array('InvalidTransport'), + ); + } + + /** + * @group unit + * @dataProvider getInvalidDefinitions + * @expectedException Elastica\Exception\InvalidException + * @expectedExceptionMessage Invalid transport + */ + public function testThrowsExecptionOnInvalidTransportDefinition($transport) + { + AbstractTransport::create($transport, new Connection()); + } + + /** + * @group unit + */ + public function testCanInjectParamsWhenUsingArray() + { + $connection = new Connection(); + $params = array( + 'param1' => 'some value', + 'param3' => 'value3', + ); + + $transport = AbstractTransport::create(array( + 'type' => 'Http', + 'param1' => 'value1', + 'param2' => 'value2', + ), $connection, $params); + + $this->assertSame('value1', $transport->getParam('param1')); + $this->assertSame('value2', $transport->getParam('param2')); + $this->assertSame('value3', $transport->getParam('param3')); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/GuzzleTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/GuzzleTest.php new file mode 100644 index 00000000..04e7ee2d --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/GuzzleTest.php @@ -0,0 +1,180 @@ + false, 'transport' => 'Guzzle'), + 'GET', + ), + array( + array('persistent' => false, 'transport' => array('type' => 'Guzzle', 'postWithRequestBody' => false)), + 'GET', + ), + array( + array('persistent' => false, 'transport' => array('type' => 'Guzzle', 'postWithRequestBody' => true)), + 'POST', + ), + ); + } + + /** + * @group functional + * @dataProvider getConfig + */ + public function testDynamicHttpMethodBasedOnConfigParameter(array $config, $httpMethod) + { + $client = $this->_getClient($config); + + $index = $client->getIndex('dynamic_http_method_test'); + $index->create(array(), true); + $type = $index->getType('test'); + $type->addDocument(new Document(1, array('test' => 'test'))); + $index->refresh(); + $resultSet = $index->search('test'); + $info = $resultSet->getResponse()->getTransferInfo(); + $this->assertStringStartsWith($httpMethod, $info['request_header']); + } + + /** + * @group functional + * @dataProvider getConfig + */ + public function testDynamicHttpMethodOnlyAffectsRequestsWithBody(array $config, $httpMethod) + { + $client = $this->_getClient($config); + + $status = $client->getStatus(); + $info = $status->getResponse()->getTransferInfo(); + $this->assertStringStartsWith('GET', $info['request_header']); + } + + /** + * @group functional + */ + public function testWithEnvironmentalProxy() + { + putenv('http_proxy='.$this->_getProxyUrl().'/'); + + $client = $this->_getClient(array('transport' => 'Guzzle', 'persistent' => false)); + $transferInfo = $client->request('/_nodes')->getTransferInfo(); + $this->assertEquals(200, $transferInfo['http_code']); + + $client->getConnection()->setProxy(null); // will not change anything + $transferInfo = $client->request('/_nodes')->getTransferInfo(); + $this->assertEquals(200, $transferInfo['http_code']); + + putenv('http_proxy='); + } + + /** + * @group functional + */ + public function testWithEnabledEnvironmentalProxy() + { + putenv('http_proxy='.$this->_getProxyUrl403().'/'); + + $client = $this->_getClient(array('transport' => 'Guzzle', 'persistent' => false)); + $transferInfo = $client->request('/_nodes')->getTransferInfo(); + $this->assertEquals(403, $transferInfo['http_code']); + + $client = $this->_getClient(array('transport' => 'Guzzle', 'persistent' => false)); + $client->getConnection()->setProxy(''); + $transferInfo = $client->request('/_nodes')->getTransferInfo(); + $this->assertEquals(200, $transferInfo['http_code']); + + putenv('http_proxy='); + } + + /** + * @group functional + */ + public function testWithProxy() + { + $client = $this->_getClient(array('transport' => 'Guzzle', 'persistent' => false)); + $client->getConnection()->setProxy($this->_getProxyUrl()); + + $transferInfo = $client->request('/_nodes')->getTransferInfo(); + $this->assertEquals(200, $transferInfo['http_code']); + } + + /** + * @group functional + */ + public function testWithoutProxy() + { + $client = $this->_getClient(array('transport' => 'Guzzle', 'persistent' => false)); + $client->getConnection()->setProxy(''); + + $transferInfo = $client->request('/_nodes')->getTransferInfo(); + $this->assertEquals(200, $transferInfo['http_code']); + } + + /** + * @group functional + */ + public function testBodyReuse() + { + $client = $this->_getClient(array('transport' => 'Guzzle', 'persistent' => false)); + + $index = $client->getIndex('elastica_body_reuse_test'); + $index->create(array(), true); + $this->_waitForAllocation($index); + + $type = $index->getType('test'); + $type->addDocument(new Document(1, array('test' => 'test'))); + + $index->refresh(); + + $resultSet = $index->search(array( + 'query' => array( + 'query_string' => array( + 'query' => 'pew pew pew', + ), + ), + )); + + $this->assertEquals(0, $resultSet->getTotalHits()); + + $response = $index->request('/_search', 'POST'); + $resultSet = new ResultSet($response, Query::create(array())); + + $this->assertEquals(1, $resultSet->getTotalHits()); + } + + /** + * @group unit + * @expectedException Elastica\Exception\Connection\GuzzleException + */ + public function testInvalidConnection() + { + $client = $this->_getClient(array('transport' => 'Guzzle', 'port' => 4500, 'persistent' => false)); + $response = $client->request('_status', 'GET'); + } + + protected function tearDown() + { + parent::tearDown(); + putenv('http_proxy='); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/HttpTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/HttpTest.php new file mode 100644 index 00000000..53ee105f --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/HttpTest.php @@ -0,0 +1,246 @@ + 'Http', 'curl' => array(CURLINFO_HEADER_OUT => true)), + 'GET', + ), + array( + array('transport' => array('type' => 'Http', 'postWithRequestBody' => false, 'curl' => array(CURLINFO_HEADER_OUT => true))), + 'GET', + ), + array( + array('transport' => array('type' => 'Http', 'postWithRequestBody' => true, 'curl' => array(CURLINFO_HEADER_OUT => true))), + 'POST', + ), + ); + } + + /** + * @group functional + * @dataProvider getConfig + */ + public function testDynamicHttpMethodBasedOnConfigParameter(array $config, $httpMethod) + { + $client = $this->_getClient($config); + + $index = $client->getIndex('dynamic_http_method_test'); + $index->create(array(), true); + $this->_waitForAllocation($index); + + $type = $index->getType('test'); + $type->addDocument(new Document(1, array('test' => 'test'))); + + $index->refresh(); + + $resultSet = $index->search('test'); + + $info = $resultSet->getResponse()->getTransferInfo(); + $this->assertStringStartsWith($httpMethod, $info['request_header']); + } + + /** + * @group functional + * @dataProvider getConfig + */ + public function testDynamicHttpMethodOnlyAffectsRequestsWithBody(array $config, $httpMethod) + { + $client = $this->_getClient($config); + + $status = $client->getStatus(); + $info = $status->getResponse()->getTransferInfo(); + $this->assertStringStartsWith('GET', $info['request_header']); + } + + /** + * @group functional + */ + public function testCurlNobodyOptionIsResetAfterHeadRequest() + { + $client = $this->_getClient(); + $index = $client->getIndex('curl_test'); + $index->create(array(), true); + $this->_waitForAllocation($index); + + $type = $index->getType('item'); + // Force HEAD request to set CURLOPT_NOBODY = true + $index->exists(); + + $id = 1; + $data = array('id' => $id, 'name' => 'Item 1'); + $doc = new \Elastica\Document($id, $data); + + $type->addDocument($doc); + + $index->refresh(); + + $doc = $type->getDocument($id); + + // Document should be retrieved correctly + $this->assertSame($data, $doc->getData()); + $this->assertEquals($id, $doc->getId()); + } + + /** + * @group functional + */ + public function testUnicodeData() + { + $client = $this->_getClient(); + $index = $client->getIndex('curl_test'); + $index->create(array(), true); + $this->_waitForAllocation($index); + + $type = $index->getType('item'); + + // Force HEAD request to set CURLOPT_NOBODY = true + $index->exists(); + + $id = 22; + $data = array('id' => $id, 'name' => ' + Сегодня, я вижу, особенно грустен твой взгляд, / + И руки особенно тонки, колени обняв. / + Послушай: далеко, далеко, на озере Чад / + Изысканный бродит жираф.'); + + $doc = new \Elastica\Document($id, $data); + + $type->addDocument($doc); + + $index->refresh(); + + $doc = $type->getDocument($id); + + // Document should be retrieved correctly + $this->assertSame($data, $doc->getData()); + $this->assertEquals($id, $doc->getId()); + } + + /** + * @group functional + */ + public function testWithEnvironmentalProxy() + { + putenv('http_proxy='.$this->_getProxyUrl().'/'); + + $client = $this->_getClient(); + $transferInfo = $client->request('/_nodes')->getTransferInfo(); + $this->assertEquals(200, $transferInfo['http_code']); + + $client->getConnection()->setProxy(null); // will not change anything + $transferInfo = $client->request('/_nodes')->getTransferInfo(); + $this->assertEquals(200, $transferInfo['http_code']); + + putenv('http_proxy='); + } + + /** + * @group functional + */ + public function testWithEnabledEnvironmentalProxy() + { + putenv('http_proxy='.$this->_getProxyUrl403().'/'); + $client = $this->_getClient(); + $transferInfo = $client->request('/_nodes')->getTransferInfo(); + $this->assertEquals(403, $transferInfo['http_code']); + $client = $this->_getClient(); + $client->getConnection()->setProxy(''); + $transferInfo = $client->request('/_nodes')->getTransferInfo(); + $this->assertEquals(200, $transferInfo['http_code']); + putenv('http_proxy='); + } + + /** + * @group functional + */ + public function testWithProxy() + { + $client = $this->_getClient(); + $client->getConnection()->setProxy($this->_getProxyUrl()); + + $transferInfo = $client->request('/_nodes')->getTransferInfo(); + $this->assertEquals(200, $transferInfo['http_code']); + } + + /** + * @group functional + */ + public function testWithoutProxy() + { + $client = $this->_getClient(); + $client->getConnection()->setProxy(''); + + $transferInfo = $client->request('/_nodes')->getTransferInfo(); + $this->assertEquals(200, $transferInfo['http_code']); + } + + /** + * @group functional + */ + public function testBodyReuse() + { + $client = $this->_getClient(); + + $index = $client->getIndex('elastica_body_reuse_test'); + $index->create(array(), true); + $this->_waitForAllocation($index); + + $type = $index->getType('test'); + $type->addDocument(new Document(1, array('test' => 'test'))); + + $index->refresh(); + + $resultSet = $index->search(array( + 'query' => array( + 'query_string' => array( + 'query' => 'pew pew pew', + ), + ), + )); + + $this->assertEquals(0, $resultSet->getTotalHits()); + + $response = $index->request('/_search', 'POST'); + $resultSet = new ResultSet($response, Query::create(array())); + + $this->assertEquals(1, $resultSet->getTotalHits()); + } + + /** + * @group functional + */ + public function testPostWith0Body() + { + $client = $this->_getClient(); + + $index = $client->getIndex('elastica_0_body'); + $index->create(array(), true); + $this->_waitForAllocation($index); + $index->refresh(); + + $tokens = $index->analyze('0'); + + $this->assertNotEmpty($tokens); + } + + protected function tearDown() + { + parent::tearDown(); + putenv('http_proxy='); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/MemcacheTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/MemcacheTest.php new file mode 100644 index 00000000..30897073 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/MemcacheTest.php @@ -0,0 +1,176 @@ +_getClient(array( + 'host' => $this->_getHost(), + 'port' => 11211, + 'transport' => 'Memcache', + )); + } + + /** + * @group functional + */ + public function testConstruct() + { + $client = $this->_getMemcacheClient(); + $this->assertEquals($this->_getHost(), $client->getConnection()->getHost()); + $this->assertEquals(11211, $client->getConnection()->getPort()); + } + + /** + * @group functional + */ + public function testCreateDocument() + { + $index = $this->_createIndex(); + $this->_waitForAllocation($index); + $type = $index->getType('foo'); + + // Create document + $document = new Document(1, array('username' => 'John Doe')); + $type->addDocument($document); + $index->refresh(); + + // Check it was saved + $document = $type->getDocument(1); + $this->assertEquals('John Doe', $document->get('username')); + } + + /** + * @group functional + * @expectedException Elastica\Exception\NotFoundException + */ + public function testDeleteDocument() + { + $index = $this->_createIndex(); + $this->_waitForAllocation($index); + $type = $index->getType('foo'); + + // Create document + $document = new Document(1, array('username' => 'John Doe')); + $type->addDocument($document); + $index->refresh(); + + // Delete document + $type->deleteById(1); + + // Check if document is not exists + $document = $type->getDocument(1); + } + + /** + * @group functional + */ + public function testUpdateDocument() + { + $index = $this->_createIndex(); + $this->_waitForAllocation($index); + $type = $index->getType('foo'); + + // Create document + $document = new Document(1, array('username' => 'John Doe')); + $type->addDocument($document); + $index->refresh(); + + // Check it was saved + $savedDocument = $type->getDocument(1); + $this->assertEquals('John Doe', $savedDocument->get('username')); + + // Update document + $newDocument = new Document(1, array('username' => 'Doe John')); + $type->updateDocument($newDocument); + $index->refresh(); + + // Check it was updated + $newSavedDocument = $type->getDocument(1); + $this->assertEquals('Doe John', $newSavedDocument->get('username')); + } + + /** + * @group functional + */ + public function testSearchDocument() + { + $index = $this->_createIndex(); + $this->_waitForAllocation($index); + $type = $index->getType('fruits'); + + // Create documents + $docs = array( + new Document(1, array('name' => 'banana')), + new Document(2, array('name' => 'apple')), + new Document(3, array('name' => 'orange')), + ); + $type->addDocuments($docs); + $index->refresh(); + + // Search documents + $queryString = new QueryString('orange'); + $query = new Query($queryString); + $resultSet = $type->search($query); + + // Check if correct document was found + $this->assertEquals(1, $resultSet->getTotalHits()); + $this->assertEquals(3, $resultSet[0]->getId()); + $data = $resultSet[0]->getData(); + $this->assertEquals('orange', $data['name']); + } + + /** + * @group functional + * @expectedException Elastica\Exception\InvalidException + * @expectedExceptionMessage is not supported in memcache transport + */ + public function testHeadRequest() + { + $client = $this->_getMemcacheClient(); + $client->request('foo', Request::HEAD); + } + + /** + * @group functional + * @expectedException Elastica\Exception\InvalidException + * @expectedExceptionMessage is not supported in memcache transport + */ + public function testInvalidRequest() + { + $client = $this->_getMemcacheClient(); + $client->request('foo', 'its_fail'); + } + + /** + * @group functional + * @expectedException Elastica\Exception\Connection\MemcacheException + * @expectedExceptionMessage is too long + */ + public function testRequestWithLongPath() + { + $client = $this->_getMemcacheClient(); + $index = $client->getIndex('memcache-test'); + $index->create(); + + $this->_waitForAllocation($index); + + $queryString = new QueryString(str_repeat('z', 300)); + $query = new Query($queryString); + $index->search($query); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/NullTransportTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/NullTransportTest.php new file mode 100644 index 00000000..cea3e3ba --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/NullTransportTest.php @@ -0,0 +1,96 @@ + + */ +class NullTransportTest extends BaseTest +{ + /** + * @group functional + */ + public function testEmptyResult() + { + // Creates a client with any destination, and verify it returns a response object when executed + $client = $this->_getClient(); + $connection = new Connection(array('transport' => 'NullTransport')); + $client->setConnections(array($connection)); + + $index = $client->getIndex('elasticaNullTransportTest1'); + + $resultSet = $index->search(new Query()); + $this->assertNotNull($resultSet); + + $response = $resultSet->getResponse(); + $this->assertNotNull($response); + + // Validate most of the expected fields in the response data. Consumers of the response + // object have a reasonable expectation of finding "hits", "took", etc + $responseData = $response->getData(); + $this->assertContains('took', $responseData); + $this->assertEquals(0, $responseData['took']); + $this->assertContains('_shards', $responseData); + $this->assertContains('hits', $responseData); + $this->assertContains('total', $responseData['hits']); + $this->assertEquals(0, $responseData['hits']['total']); + $this->assertContains('params', $responseData); + + $took = $response->getEngineTime(); + $this->assertEquals(0, $took); + + $errorString = $response->getError(); + $this->assertEmpty($errorString); + + $shards = $response->getShardsStatistics(); + $this->assertContains('total', $shards); + $this->assertEquals(0, $shards['total']); + $this->assertContains('successful', $shards); + $this->assertEquals(0, $shards['successful']); + $this->assertContains('failed', $shards); + $this->assertEquals(0, $shards['failed']); + } + + /** + * @group functional + */ + public function testExec() + { + $request = new Request('/test'); + $params = array('name' => 'ruflin'); + $transport = new NullTransport(); + $response = $transport->exec($request, $params); + + $this->assertInstanceOf('\Elastica\Response', $response); + + $data = $response->getData(); + $this->assertEquals($params, $data['params']); + } + + /** + * @group functional + */ + public function testOldObject() + { + if (version_compare(phpversion(), 7, '>=')) { + self::markTestSkipped('These objects are not supported in PHP 7'); + } + + $request = new Request('/test'); + $params = array('name' => 'ruflin'); + $transport = new \Elastica\Transport\Null(); + $response = $transport->exec($request, $params); + + $this->assertInstanceOf('\Elastica\Response', $response); + + $data = $response->getData(); + $this->assertEquals($params, $data['params']); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/ThriftTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/ThriftTest.php new file mode 100644 index 00000000..b73ef4f7 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/ThriftTest.php @@ -0,0 +1,135 @@ +_getHost(); + $port = 9500; + $client = $this->_getClient(array('host' => $host, 'port' => $port, 'transport' => 'Thrift')); + + $this->assertEquals($host, $client->getConnection()->getHost()); + $this->assertEquals($port, $client->getConnection()->getPort()); + } + + /** + * @group functional + * @dataProvider configProvider + */ + public function testSearchRequest($config) + { + $this->_checkPlugin(); + + // Creates a new index 'xodoa' and a type 'user' inside this index + $client = $this->_getClient($config); + + $index = $client->getIndex('elastica_test1'); + $index->create(array(), true); + + $type = $index->getType('user'); + + // Adds 1 document to the index + $doc1 = new Document(1, + array('username' => 'hans', 'test' => array('2', '3', '5')) + ); + $doc1->setVersion(0); + $type->addDocument($doc1); + + // Adds a list of documents with _bulk upload to the index + $docs = array(); + $docs[] = new Document(2, + array('username' => 'john', 'test' => array('1', '3', '6')) + ); + $docs[] = new Document(3, + array('username' => 'rolf', 'test' => array('2', '3', '7')) + ); + $type->addDocuments($docs); + + // Refresh index + $index->refresh(); + $resultSet = $type->search('rolf'); + + $this->assertEquals(1, $resultSet->getTotalHits()); + } + + /** + * @group unit + * @expectedException \Elastica\Exception\ConnectionException + */ + public function testInvalidHostRequest() + { + $this->_checkPlugin(); + + $client = $this->_getClient(array('host' => 'unknown', 'port' => 9555, 'transport' => 'Thrift')); + $client->getStatus(); + } + + /** + * @group functional + * @expectedException \Elastica\Exception\ResponseException + */ + public function testInvalidElasticRequest() + { + $this->_checkPlugin(); + + $connection = new Connection(); + $connection->setHost($this->_getHost()); + $connection->setPort(9500); + $connection->setTransport('Thrift'); + + $client = $this->_getClient(); + $client->addConnection($connection); + + $index = new Index($client, 'missing_index'); + $index->getStatus(); + } + + public function configProvider() + { + return array( + array( + array( + 'host' => $this->_getHost(), + 'port' => 9500, + 'transport' => 'Thrift', + ), + ), + array( + array( + 'host' => $this->_getHost(), + 'port' => 9500, + 'transport' => 'Thrift', + 'config' => array( + 'framedTransport' => false, + 'sendTimeout' => 10000, + 'recvTimeout' => 20000, + ), + ), + ), + ); + } + + protected function _checkPlugin() + { + $nodes = $this->_getClient()->getCluster()->getNodes(); + if (!$nodes[0]->getInfo()->hasPlugin('transport-thrift')) { + $this->markTestSkipped('transport-thrift plugin not installed.'); + } + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/TransportBenchmarkTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/TransportBenchmarkTest.php new file mode 100644 index 00000000..11a16a34 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/TransportBenchmarkTest.php @@ -0,0 +1,261 @@ +_getClient($config); + $index = $client->getIndex('benchmark'.uniqid()); + $index->create(array('index' => array('number_of_shards' => 1, 'number_of_replicas' => 0)), true); + + return $index->getType('benchmark'); + } + + /** + * @dataProvider providerTransport + * @group benchmark + */ + public function testAddDocument(array $config, $transport) + { + $this->_checkThrift($transport); + + $type = $this->getType($config); + $index = $type->getIndex(); + $index->create(array(), true); + + $times = array(); + for ($i = 0; $i < $this->_max; $i++) { + $data = $this->getData($i); + $doc = new Document($i, $data); + $result = $type->addDocument($doc); + $times[] = $result->getQueryTime(); + $this->assertTrue($result->isOk()); + } + + $index->refresh(); + + self::logResults('insert', $transport, $times); + } + + /** + * @depends testAddDocument + * @dataProvider providerTransport + * @group benchmark + */ + public function testRandomRead(array $config, $transport) + { + $this->_checkThrift($transport); + + $type = $this->getType($config); + + $type->search('test'); + + $times = array(); + for ($i = 0; $i < $this->_max; $i++) { + $test = rand(1, $this->_max); + $query = new Query(); + $query->setQuery(new \Elastica\Query\MatchAll()); + $query->setPostFilter(new \Elastica\Filter\Term(array('test' => $test))); + $result = $type->search($query); + $times[] = $result->getResponse()->getQueryTime(); + } + + self::logResults('random read', $transport, $times); + } + + /** + * @depends testAddDocument + * @dataProvider providerTransport + * @group benchmark + */ + public function testBulk(array $config, $transport) + { + $type = $this->getType($config); + + $times = array(); + for ($i = 0; $i < $this->_max; $i++) { + $docs = array(); + for ($j = 0; $j < 10; $j++) { + $data = $this->getData($i.$j); + $docs[] = new Document($i, $data); + } + + $result = $type->addDocuments($docs); + $times[] = $result->getQueryTime(); + } + + self::logResults('bulk', $transport, $times); + } + + /** + * @dataProvider providerTransport + * @group benchmark + */ + public function testGetMapping(array $config, $transport) + { + $client = $this->_getClient($config); + $index = $client->getIndex('benchmark'); + $index->create(array(), true); + $type = $index->getType('mappingTest'); + + // Define mapping + $mapping = new \Elastica\Type\Mapping(); + $mapping->setParam('_boost', array('name' => '_boost', 'null_value' => 1.0)); + $mapping->setProperties(array( + 'id' => array('type' => 'integer', 'include_in_all' => false), + 'user' => array( + 'type' => 'object', + 'properties' => array( + 'name' => array('type' => 'string', 'include_in_all' => true), + 'fullName' => array('type' => 'string', 'include_in_all' => true), + ), + ), + 'msg' => array('type' => 'string', 'include_in_all' => true), + 'tstamp' => array('type' => 'date', 'include_in_all' => false), + 'location' => array('type' => 'geo_point', 'include_in_all' => false), + '_boost' => array('type' => 'float', 'include_in_all' => false), + )); + + $type->setMapping($mapping); + $index->refresh(); + + $times = array(); + for ($i = 0; $i < $this->_max; $i++) { + $response = $type->request('_mapping', \Elastica\Request::GET); + $times[] = $response->getQueryTime(); + } + self::logResults('get mapping', $transport, $times); + } + + public function providerTransport() + { + return array( + array( + array( + 'transport' => 'Http', + 'host' => $this->_getHost(), + 'port' => $this->_getPort(), + 'persistent' => false, + ), + 'Http:NotPersistent', + ), + array( + array( + 'transport' => 'Http', + 'host' => $this->_getHost(), + 'port' => $this->_getPort(), + 'persistent' => true, + ), + 'Http:Persistent', + ), + array( + array( + 'transport' => 'Thrift', + 'host' => $this->_getHost(), + 'port' => 9500, + 'config' => array( + 'framedTransport' => false, + ), + ), + 'Thrift:Buffered', + ), + ); + } + + /** + * @param string $test + * + * @return array + */ + protected function getData($test) + { + $data = array( + 'test' => $test, + 'name' => array(), + ); + for ($i = 0; $i < $this->_maxData; $i++) { + $data['name'][] = uniqid(); + } + + return $data; + } + + /** + * @param $name + * @param $transport + * @param array $times + */ + protected static function logResults($name, $transport, array $times) + { + self::$_results[$name][$transport] = array( + 'count' => count($times), + 'max' => max($times) * 1000, + 'min' => min($times) * 1000, + 'mean' => (array_sum($times) / count($times)) * 1000, + ); + } + + protected static function printResults() + { + echo sprintf( + "\n%-12s | %-20s | %-12s | %-12s | %-12s | %-12s\n\n", + 'NAME', + 'TRANSPORT', + 'COUNT', + 'MAX', + 'MIN', + 'MEAN', + '%' + ); + foreach (self::$_results as $name => $values) { + $means = array(); + foreach ($values as $times) { + $means[] = $times['mean']; + } + $minMean = min($means); + foreach ($values as $transport => $times) { + $perc = (($times['mean'] - $minMean) / $minMean) * 100; + echo sprintf( + "%-12s | %-20s | %-12d | %-12.2f | %-12.2f | %-12.2f | %+03.2f\n", + $name, + $transport, + $times['count'], + $times['max'], + $times['min'], + $times['mean'], + $perc + ); + } + echo "\n"; + } + } + + protected function _checkThrift($transport) + { + if (strpos($transport, 'Thrift') !== false && !class_exists('Elasticsearch\\RestClient')) { + self::markTestSkipped('munkie/elasticsearch-thrift-php package should be installed to run thrift transport tests'); + } + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Type/MappingTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Type/MappingTest.php new file mode 100644 index 00000000..925c23bb --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Type/MappingTest.php @@ -0,0 +1,331 @@ +_getClient(); + $index = $client->getIndex('test'); + + $index->create(array(), true); + $type = $index->getType('test'); + + $mapping = new Mapping($type, + array( + 'firstname' => array('type' => 'string', 'store' => 'yes'), + // default is store => no expected + 'lastname' => array('type' => 'string'), + ) + ); + $mapping->disableSource(); + + $type->setMapping($mapping); + + $firstname = 'Nicolas'; + $doc = new Document(1, + array( + 'firstname' => $firstname, + 'lastname' => 'Ruflin', + ) + ); + + $type->addDocument($doc); + + $index->refresh(); + $queryString = new QueryString('ruflin'); + $query = Query::create($queryString); + $query->setFields(array('*')); + + $resultSet = $type->search($query); + $result = $resultSet->current(); + $fields = $result->getFields(); + + $this->assertEquals($firstname, $fields['firstname'][0]); + $this->assertArrayNotHasKey('lastname', $fields); + $this->assertEquals(1, count($fields)); + + $index->flush(); + $document = $type->getDocument(1); + + $this->assertEmpty($document->getData()); + + $index->delete(); + } + + /** + * @group functional + */ + public function testEnableAllField() + { + $index = $this->_createIndex(); + $type = $index->getType('test'); + + $mapping = new Mapping($type, array()); + + $mapping->enableAllField(); + + $data = $mapping->toArray(); + $this->assertTrue($data[$type->getName()]['_all']['enabled']); + + $response = $mapping->send(); + $this->assertTrue($response->isOk()); + + $index->delete(); + } + + /** + * @group functional + */ + public function testEnableTtl() + { + $client = $this->_getClient(); + $index = $client->getIndex('test'); + + $index->create(array(), true); + $type = $index->getType('test'); + + $mapping = new Mapping($type, array()); + + $mapping->enableTtl(); + + $data = $mapping->toArray(); + $this->assertTrue($data[$type->getName()]['_ttl']['enabled']); + + $index->delete(); + } + + /** + * @group functional + */ + public function testNestedMapping() + { + $client = $this->_getClient(); + $index = $client->getIndex('test'); + + $index->create(array(), true); + $type = $index->getType('test'); + + //$this->markTestIncomplete('nested mapping is not set right yet'); + $mapping = new Mapping($type, + array( + 'test' => array( + 'type' => 'object', 'store' => 'yes', 'properties' => array( + 'user' => array( + 'properties' => array( + 'firstname' => array('type' => 'string', 'store' => 'yes'), + 'lastname' => array('type' => 'string', 'store' => 'yes'), + 'age' => array('type' => 'integer', 'store' => 'yes'), + ), + ), + ), + ), + ) + ); + + $response = $type->setMapping($mapping); + $this->assertFalse($response->hasError()); + + $doc = new Document(1, array( + 'user' => array( + 'firstname' => 'Nicolas', + 'lastname' => 'Ruflin', + 'age' => 9, + ), + )); + + $type->addDocument($doc); + + $index->refresh(); + $resultSet = $type->search('ruflin'); + $this->assertEquals($resultSet->count(), 1); + + $index->delete(); + } + + /** + * @group functional + */ + public function testParentMapping() + { + $index = $this->_createIndex(); + $parenttype = new Type($index, 'parenttype'); + $parentmapping = new Mapping($parenttype, + array( + 'name' => array('type' => 'string', 'store' => 'yes'), + ) + ); + + $parenttype->setMapping($parentmapping); + + $childtype = new Type($index, 'childtype'); + $childmapping = new Mapping($childtype, + array( + 'name' => array('type' => 'string', 'store' => 'yes'), + ) + ); + $childmapping->setParent('parenttype'); + + $childtype->setMapping($childmapping); + + $data = $childmapping->toArray(); + $this->assertEquals('parenttype', $data[$childtype->getName()]['_parent']['type']); + + $index->delete(); + } + + /** + * @group functional + */ + public function testMappingExample() + { + $index = $this->_createIndex(); + $type = $index->getType('notes'); + + $mapping = new Mapping($type, + array( + 'note' => array( + 'store' => 'yes', 'properties' => array( + 'titulo' => array('type' => 'string', 'store' => 'no', 'include_in_all' => true, 'boost' => 1.0), + 'contenido' => array('type' => 'string', 'store' => 'no', 'include_in_all' => true, 'boost' => 1.0), + ), + ), + ) + ); + + $type->setMapping($mapping); + + $doc = new Document(1, array( + 'note' => array( + array( + 'titulo' => 'nota1', + 'contenido' => 'contenido1', + ), + array( + 'titulo' => 'nota2', + 'contenido' => 'contenido2', + ), + ), + ) + ); + + $type->addDocument($doc); + + $index->delete(); + } + + /** + * @group functional + * + * Test setting a dynamic template and validate whether the right mapping is applied after adding a document which + * should match the dynamic template. The example is the template_1 from the Elasticsearch documentation. + * + * @link http://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-root-object-type.html + */ + public function testDynamicTemplate() + { + $index = $this->_createIndex(); + $type = $index->getType('person'); + + // set a dynamic template "template_1" which creates a multi field for multi* matches. + $mapping = new Mapping($type); + $mapping->setParam('dynamic_templates', array( + array('template_1' => array( + 'match' => 'multi*', + 'mapping' => array( + 'type' => 'multi_field', + 'fields' => array( + '{name}' => array('type' => '{dynamic_type}', 'index' => 'analyzed'), + 'org' => array('type' => '{dynamic_type}', 'index' => 'not_analyzed'), + ), + ), + )), + )); + + $mapping->send(); + + // when running the tests, the mapping sometimes isn't available yet. Optimize index to enforce reload mapping. + $index->optimize(); + + // create a document which should create a mapping for the field: multiname. + $testDoc = new Document('person1', array('multiname' => 'Jasper van Wanrooy'), $type); + $index->addDocuments(array($testDoc)); + sleep(1); //sleep 1 to ensure that the test passes every time + + // read the mapping from Elasticsearch and assert that the multiname.org field is "not_analyzed" + $newMapping = $type->getMapping(); + $this->assertArrayHasKey('person', $newMapping, + 'Person type not available in mapping from ES. Mapping set at all?'); + $this->assertArrayHasKey('properties', $newMapping['person'], + 'Person type doesnt have any properties. Document properly added?'); + $this->assertArrayHasKey('multiname', $newMapping['person']['properties'], + 'The multiname property is not added to the mapping. Document properly added?'); + $this->assertArrayHasKey('fields', $newMapping['person']['properties']['multiname'], + 'The multiname field of the Person type is presumably not a multi_field type. Dynamic mapping not applied?'); + $this->assertArrayHasKey('org', $newMapping['person']['properties']['multiname']['fields'], + 'The multi* matcher did not create a mapping for the multiname.org property when indexing the document.'); + $this->assertArrayHasKey('index', $newMapping['person']['properties']['multiname']['fields']['org'], + 'Indexing status of the multiname.org not available. Dynamic mapping not fully applied!'); + $this->assertEquals('not_analyzed', $newMapping['person']['properties']['multiname']['fields']['org']['index']); + + $index->delete(); + } + + /** + * @group functional + */ + public function testSetMeta() + { + $index = $this->_createIndex(); + $type = $index->getType('test'); + $mapping = new Mapping($type, array( + 'firstname' => array('type' => 'string', 'store' => 'yes'), + 'lastname' => array('type' => 'string'), + )); + $mapping->setMeta(array('class' => 'test')); + $type->setMapping($mapping); + + $mappingData = $type->getMapping(); + $this->assertEquals('test', $mappingData['test']['_meta']['class']); + + $index->delete(); + } + + /** + * @group functional + */ + public function testGetters() + { + $index = $this->_createIndex(); + $type = $index->getType('test'); + $properties = array( + 'firstname' => array('type' => 'string', 'store' => 'yes'), + 'lastname' => array('type' => 'string'), + ); + $mapping = new Mapping($type, $properties); + $all = array( + 'enabled' => true, + 'store' => 'yes', + ); + $mapping->setParam('_all', $all); + $get_all = $mapping->getParam('_all'); + + $this->assertEquals($get_all, $all); + + $this->assertNull($mapping->getParam('_boost', $all)); + + $this->assertEquals($properties, $mapping->getProperties()); + + $index->delete(); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/TypeTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/TypeTest.php new file mode 100644 index 00000000..dcb74ac5 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/TypeTest.php @@ -0,0 +1,968 @@ +_createIndex(); + + $type = new Type($index, 'user'); + + // Adds 1 document to the index + $doc1 = new Document(1, + array('username' => 'hans', 'test' => array('2', '3', '5')) + ); + $type->addDocument($doc1); + + // Adds a list of documents with _bulk upload to the index + $docs = array(); + $docs[] = new Document(2, + array('username' => 'john', 'test' => array('1', '3', '6')) + ); + $docs[] = new Document(3, + array('username' => 'rolf', 'test' => array('2', '3', '7')) + ); + $type->addDocuments($docs); + $index->refresh(); + + $resultSet = $type->search('rolf'); + $this->assertEquals(1, $resultSet->count()); + + $count = $type->count('rolf'); + $this->assertEquals(1, $count); + + // Test if source is returned + $result = $resultSet->current(); + $this->assertEquals(3, $result->getId()); + $data = $result->getData(); + $this->assertEquals('rolf', $data['username']); + } + + /** + * @group functional + */ + public function testCreateSearch() + { + $client = $this->_getClient(); + $index = new Index($client, 'test_index'); + $type = new Type($index, 'test_type'); + + $query = new Query\QueryString('test'); + $options = array( + 'limit' => 5, + 'explain' => true, + ); + + $search = $type->createSearch($query, $options); + + $expected = array( + 'query' => array( + 'query_string' => array( + 'query' => 'test', + ), + ), + 'size' => 5, + 'explain' => true, + ); + $this->assertEquals($expected, $search->getQuery()->toArray()); + $this->assertEquals(array('test_index'), $search->getIndices()); + $this->assertTrue($search->hasIndices()); + $this->assertTrue($search->hasIndex($index)); + $this->assertTrue($search->hasIndex('test_index')); + $this->assertFalse($search->hasIndex('test')); + $this->assertEquals(array('test_type'), $search->getTypes()); + $this->assertTrue($search->hasTypes()); + $this->assertTrue($search->hasType($type)); + $this->assertTrue($search->hasType('test_type')); + $this->assertFalse($search->hasType('test_type2')); + } + + /** + * @group functional + */ + public function testCreateSearchWithArray() + { + $client = $this->_getClient(); + $index = new Index($client, 'test_index'); + $type = new Type($index, 'test_type'); + + $query = array( + 'query' => array( + 'query_string' => array( + 'query' => 'test', + ), + ), + ); + + $options = array( + 'limit' => 5, + 'explain' => true, + ); + + $search = $type->createSearch($query, $options); + + $expected = array( + 'query' => array( + 'query_string' => array( + 'query' => 'test', + ), + ), + 'size' => 5, + 'explain' => true, + ); + $this->assertEquals($expected, $search->getQuery()->toArray()); + $this->assertEquals(array('test_index'), $search->getIndices()); + $this->assertTrue($search->hasIndices()); + $this->assertTrue($search->hasIndex($index)); + $this->assertTrue($search->hasIndex('test_index')); + $this->assertFalse($search->hasIndex('test')); + $this->assertEquals(array('test_type'), $search->getTypes()); + $this->assertTrue($search->hasTypes()); + $this->assertTrue($search->hasType($type)); + $this->assertTrue($search->hasType('test_type')); + $this->assertFalse($search->hasType('test_type2')); + } + + /** + * @group functional + */ + public function testNoSource() + { + $index = $this->_createIndex(); + + $type = new Type($index, 'user'); + $mapping = new Mapping($type, array( + 'id' => array('type' => 'integer', 'store' => 'yes'), + 'username' => array('type' => 'string', 'store' => 'no'), + )); + $mapping->setSource(array('enabled' => false)); + $type->setMapping($mapping); + + $mapping = $type->getMapping(); + + $this->assertArrayHasKey('user', $mapping); + $this->assertArrayHasKey('properties', $mapping['user']); + $this->assertArrayHasKey('id', $mapping['user']['properties']); + $this->assertArrayHasKey('type', $mapping['user']['properties']['id']); + $this->assertEquals('integer', $mapping['user']['properties']['id']['type']); + + // Adds 1 document to the index + $doc1 = new Document(1, + array('username' => 'hans', 'test' => array('2', '3', '5')) + ); + $type->addDocument($doc1); + + // Adds a list of documents with _bulk upload to the index + $docs = array(); + $docs[] = new Document(2, + array('username' => 'john', 'test' => array('1', '3', '6')) + ); + $docs[] = new Document(3, + array('username' => 'rolf', 'test' => array('2', '3', '7')) + ); + $type->addDocuments($docs); + + // To update index + $index->refresh(); + + $resultSet = $type->search('rolf'); + + $this->assertEquals(1, $resultSet->count()); + + // Tests if no source is in response except id + $result = $resultSet->current(); + $this->assertEquals(3, $result->getId()); + $this->assertEmpty($result->getData()); + } + + /** + * @group functional + */ + public function testDeleteById() + { + $index = $this->_createIndex(); + $type = new Type($index, 'user'); + + // Adds hans, john and rolf to the index + $docs = array( + new Document(1, array('username' => 'hans', 'test' => array('2', '3', '5'))), + new Document(2, array('username' => 'john', 'test' => array('1', '3', '6'))), + new Document(3, array('username' => 'rolf', 'test' => array('2', '3', '7'))), + new Document('foo/bar', array('username' => 'georg', 'test' => array('4', '2', '5'))), + ); + $type->addDocuments($docs); + $index->refresh(); + + // sanity check for rolf + $resultSet = $type->search('rolf'); + $this->assertEquals(1, $resultSet->count()); + $data = $resultSet->current()->getData(); + $this->assertEquals('rolf', $data['username']); + + // delete rolf + $type->deleteById(3); + $index->refresh(); + + // rolf should no longer be there + $resultSet = $type->search('rolf'); + $this->assertEquals(0, $resultSet->count()); + + // sanity check for id with slash + $resultSet = $type->search('georg'); + $this->assertEquals(1, $resultSet->count()); + + // delete georg + $type->deleteById('foo/bar'); + $index->refresh(); + + // georg should no longer be there + $resultSet = $type->search('georg'); + $this->assertEquals(0, $resultSet->count()); + + // it should not be possible to delete the entire type with this method + try { + $type->deleteById(''); + $this->fail('Delete with empty string id should fail'); + } catch (\InvalidArgumentException $e) { + $this->assertTrue(true); + } + + try { + $type->deleteById(' '); + $this->fail('Delete with one space string id should fail'); + } catch (\InvalidArgumentException $e) { + $this->assertTrue(true); + } + + try { + $type->deleteById(null); + $this->fail('Delete with null id should fail'); + } catch (\InvalidArgumentException $e) { + $this->assertTrue(true); + } + + try { + $type->deleteById(array()); + $this->fail('Delete with empty array id should fail'); + } catch (\InvalidArgumentException $e) { + $this->assertTrue(true); + } + + try { + $type->deleteById('*'); + $this->fail('Delete request should fail because of invalid id: *'); + } catch (NotFoundException $e) { + $this->assertTrue(true); + } + + try { + $type->deleteById('*:*'); + $this->fail('Delete request should fail because document with id *.* does not exist'); + } catch (NotFoundException $e) { + $this->assertTrue(true); + } + + try { + $type->deleteById('!'); + $this->fail('Delete request should fail because document with id ! does not exist'); + } catch (NotFoundException $e) { + $this->assertTrue(true); + } + + $index->refresh(); + + // rolf should no longer be there + $resultSet = $type->search('john'); + $this->assertEquals(1, $resultSet->count()); + } + + /** + * @group functional + */ + public function testDeleteDocument() + { + $index = $this->_createIndex(); + $type = new Type($index, 'user'); + + // Adds hans, john and rolf to the index + $docs = array( + new Document(1, array('username' => 'hans', 'test' => array('2', '3', '5'))), + new Document(2, array('username' => 'john', 'test' => array('1', '3', '6'))), + new Document(3, array('username' => 'rolf', 'test' => array('2', '3', '7'))), + ); + $type->addDocuments($docs); + $index->refresh(); + + $document = $type->getDocument(1); + $this->assertEquals(1, $document->getId()); + $this->assertEquals('hans', $document->get('username')); + + $this->assertEquals(3, $type->count()); + + $type->deleteDocument($document); + $index->refresh(); + + try { + $type->getDocument(1); + $this->fail('Document was not deleted'); + } catch (NotFoundException $e) { + $this->assertTrue(true); + $this->assertEquals(2, $type->count(), 'Documents count in type should be 2'); + } + } + + /** + * @group functional + * @expectedException \Elastica\Exception\NotFoundException + */ + public function testGetDocumentNotExist() + { + $index = $this->_createIndex(); + $type = new Type($index, 'test'); + $type->addDocument(new Document(1, array('name' => 'ruflin'))); + $index->refresh(); + + $type->getDocument(1); + + $type->getDocument(2); + } + + /** + * @group functional + * @expectedException \Elastica\Exception\ResponseException + */ + public function testGetDocumentNotExistingIndex() + { + $client = $this->_getClient(); + $index = new Index($client, 'index'); + $type = new Type($index, 'type'); + + $type->getDocument(1); + } + + /** + * @group functional + */ + public function testDeleteByQueryWithQueryString() + { + $index = $this->_createIndex(); + $type = new Type($index, 'test'); + $type->addDocument(new Document(1, array('name' => 'ruflin nicolas'))); + $type->addDocument(new Document(2, array('name' => 'ruflin'))); + $index->refresh(); + + $response = $index->search('ruflin*'); + $this->assertEquals(2, $response->count()); + + $response = $index->search('nicolas'); + $this->assertEquals(1, $response->count()); + + // Delete first document + $response = $type->deleteByQuery('nicolas'); + $this->assertTrue($response->isOk()); + + $index->refresh(); + + // Makes sure, document is deleted + $response = $index->search('ruflin*'); + $this->assertEquals(1, $response->count()); + + $response = $index->search('nicolas'); + $this->assertEquals(0, $response->count()); + } + + /** + * @group functional + */ + public function testDeleteByQueryWithQuery() + { + $index = $this->_createIndex(); + $type = new Type($index, 'test'); + $type->addDocument(new Document(1, array('name' => 'ruflin nicolas'))); + $type->addDocument(new Document(2, array('name' => 'ruflin'))); + $index->refresh(); + + $response = $index->search('ruflin*'); + $this->assertEquals(2, $response->count()); + + $response = $index->search('nicolas'); + $this->assertEquals(1, $response->count()); + + // Delete first document + $response = $type->deleteByQuery(new SimpleQueryString('nicolas')); + $this->assertTrue($response->isOk()); + + $index->refresh(); + + // Makes sure, document is deleted + $response = $index->search('ruflin*'); + $this->assertEquals(1, $response->count()); + + $response = $index->search('nicolas'); + $this->assertEquals(0, $response->count()); + } + + /** + * @group functional + */ + public function testDeleteByQueryWithQueryAndOptions() + { + $index = $this->_createIndex(null, true, 2); + $type = new Type($index, 'test'); + $type->addDocument(new Document(1, array('name' => 'ruflin nicolas'))); + $type->addDocument(new Document(2, array('name' => 'ruflin'))); + $index->refresh(); + + $response = $index->search('ruflin*'); + $this->assertEquals(2, $response->count()); + + $response = $index->search('nicolas'); + $this->assertEquals(1, $response->count()); + + // Route to the wrong document id; should not delete + $response = $type->deleteByQuery(new SimpleQueryString('nicolas'), array('routing' => '2')); + $this->assertTrue($response->isOk()); + + $index->refresh(); + + $response = $index->search('ruflin*'); + $this->assertEquals(2, $response->count()); + + $response = $index->search('nicolas'); + $this->assertEquals(1, $response->count()); + + // Delete first document + $response = $type->deleteByQuery(new SimpleQueryString('nicolas'), array('routing' => '1')); + $this->assertTrue($response->isOk()); + + $index->refresh(); + + // Makes sure, document is deleted + $response = $index->search('ruflin*'); + $this->assertEquals(1, $response->count()); + + $response = $index->search('nicolas'); + $this->assertEquals(0, $response->count()); + } + + /** + * Test to see if Elastica_Type::getDocument() is properly using + * the fields array when available instead of _source. + * + * @group functional + */ + public function testGetDocumentWithFieldsSelection() + { + $index = $this->_createIndex(); + $type = new Type($index, 'test'); + $type->addDocument(new Document(1, array('name' => 'loris', 'country' => 'FR', 'email' => 'test@test.com'))); + $index->refresh(); + + $document = $type->getDocument(1, array('fields' => 'name,email')); + $data = $document->getData(); + + $this->assertArrayHasKey('name', $data); + $this->assertArrayHasKey('email', $data); + $this->assertArrayNotHasKey('country', $data); + } + + /** + * Test to see if search Default Limit works. + * + * @group functional + */ + public function testLimitDefaultType() + { + $client = $this->_getClient(); + $index = $client->getIndex('zero'); + $index->create(array('index' => array('number_of_shards' => 1, 'number_of_replicas' => 0)), true); + + $docs = array(); + $docs[] = new Document(1, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')); + $docs[] = new Document(2, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')); + $docs[] = new Document(3, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')); + $docs[] = new Document(4, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')); + $docs[] = new Document(5, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')); + $docs[] = new Document(6, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')); + $docs[] = new Document(7, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')); + $docs[] = new Document(8, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')); + $docs[] = new Document(9, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')); + $docs[] = new Document(10, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')); + $docs[] = new Document(11, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')); + + $type = $index->getType('zeroType'); + $type->addDocuments($docs); + $index->refresh(); + + // default results (limit default is 10) + $resultSet = $type->search('farrelley'); + $this->assertEquals(10, $resultSet->count()); + + // limit = 1 + $resultSet = $type->search('farrelley', 1); + $this->assertEquals(1, $resultSet->count()); + } + + /** + * Test Delete of index type. After delete will check for type mapping. + * + * @group functional + */ + public function testDeleteType() + { + $index = $this->_createIndex(); + $type = new Type($index, 'test'); + $type->addDocuments(array( + new Document(1, array('name' => 'ruflin nicolas')), + new Document(2, array('name' => 'ruflin')), + )); + $index->refresh(); + + // sleep a moment to be sure that all nodes in cluster has new type + sleep(5); + + $type->delete(); + $index->optimize(); + + $this->assertFalse($type->exists()); + } + + /** + * @group functional + */ + public function testMoreLikeThisApi() + { + $client = $this->_getClient(array('persistent' => false)); + $index = $client->getIndex('elastica_test'); + $index->create(array('index' => array('number_of_shards' => 1, 'number_of_replicas' => 0)), true); + + $type = new Type($index, 'mlt_test'); + $type->addDocuments(array( + new Document(1, array('visible' => true, 'name' => 'bruce wayne batman')), + new Document(2, array('visible' => true, 'name' => 'bruce wayne')), + new Document(3, array('visible' => false, 'name' => 'bruce wayne')), + new Document(4, array('visible' => true, 'name' => 'batman')), + new Document(5, array('visible' => false, 'name' => 'batman')), + new Document(6, array('visible' => true, 'name' => 'superman')), + new Document(7, array('visible' => true, 'name' => 'spiderman')), + )); + $index->refresh(); + + $document = $type->getDocument(1); + + // Return all similar + $resultSet = $type->moreLikeThis($document, array('min_term_freq' => '1', 'min_doc_freq' => '1')); + $this->assertEquals(4, $resultSet->count()); + + // Return just the visible similar + $query = new Query(); + $filterTerm = new Term(); + $filterTerm->setTerm('visible', true); + $query->setPostFilter($filterTerm); + + $resultSet = $type->moreLikeThis($document, array('min_term_freq' => '1', 'min_doc_freq' => '1'), $query); + $this->assertEquals(2, $resultSet->count()); + } + + /** + * @group functional + */ + public function testUpdateDocument() + { + $client = $this->_getClient(); + $index = $client->getIndex('elastica_test'); + $type = $index->getType('update_type'); + $id = 1; + $type->addDocument(new Document($id, array('name' => 'bruce wayne batman', 'counter' => 1))); + $newName = 'batman'; + + $document = new Document(); + $script = new Script( + 'ctx._source.name = name; ctx._source.counter += count', + array( + 'name' => $newName, + 'count' => 2, + ), + null, + $id + ); + $script->setUpsert($document); + + $type->updateDocument($script, array('refresh' => true)); + $updatedDoc = $type->getDocument($id)->getData(); + $this->assertEquals($newName, $updatedDoc['name'], 'Name was not updated'); + $this->assertEquals(3, $updatedDoc['counter'], 'Counter was not incremented'); + } + + /** + * @group functional + */ + public function testUpdateDocumentWithIdForwardSlashes() + { + $client = $this->_getClient(); + $index = $client->getIndex('elastica_test'); + $type = $index->getType('update_type'); + $id = '/id/with/forward/slashes'; + $type->addDocument(new Document($id, array('name' => 'bruce wayne batman', 'counter' => 1))); + $newName = 'batman'; + + $document = new Document(); + $script = new Script( + 'ctx._source.name = name; ctx._source.counter += count', + array( + 'name' => $newName, + 'count' => 2, + ), + null, + $id + ); + $script->setUpsert($document); + + $type->updateDocument($script, array('refresh' => true)); + $updatedDoc = $type->getDocument($id)->getData(); + $this->assertEquals($newName, $updatedDoc['name'], 'Name was not updated'); + $this->assertEquals(3, $updatedDoc['counter'], 'Counter was not incremented'); + } + + /** + * @group functional + */ + public function testUpdateDocumentWithParameter() + { + $client = $this->_getClient(); + $index = $client->getIndex('elastica_test'); + $type = $index->getType('update_type'); + $id = 1; + $type->addDocument(new Document($id, array('name' => 'bruce wayne batman', 'counter' => 1))); + $newName = 'batman'; + + $document = new Document(); + $script = new Script( + 'ctx._source.name = name; ctx._source.counter += count', + array( + 'name' => $newName, + 'count' => 2, + ), + null, + $id + ); + $script->setUpsert($document); + + try { + $type->updateDocument($script, array('version' => 999)); // Wrong version number to make the update fail + } catch (ResponseException $e) { + $this->assertContains('VersionConflictEngineException', $e->getMessage()); + } + $updatedDoc = $type->getDocument($id)->getData(); + $this->assertNotEquals($newName, $updatedDoc['name'], 'Name was updated'); + $this->assertNotEquals(3, $updatedDoc['counter'], 'Counter was incremented'); + } + + /** + * @group functional + */ + public function testUpdateDocumentWithFieldsSource() + { + $client = $this->_getClient(); + $index = $client->getIndex('elastica_test'); + $type = $index->getType('update_type'); + + $client->setConfigValue('document', array('autoPopulate' => true)); + + $newDocument = new Document(null, array('counter' => 5, 'name' => 'Batman')); + + $this->assertFalse($newDocument->hasVersion()); + + $response = $type->addDocument($newDocument); + $responseData = $response->getData(); + + $this->assertTrue($newDocument->hasVersion()); + $this->assertArrayHasKey('_version', $responseData, '_version is missing in response data it is weird'); + $this->assertEquals(1, $responseData['_version']); + $this->assertEquals($responseData['_version'], $newDocument->getVersion()); + + $this->assertTrue($newDocument->hasId()); + + $script = new Script('ctx._source.counter += count; ctx._source.realName = realName'); + $script->setId($newDocument->getId()); + $script->setParam('count', 7); + $script->setParam('realName', 'Bruce Wayne'); + $script->setUpsert($newDocument); + + $newDocument->setFieldsSource(); + + $response = $type->updateDocument($script); + $responseData = $response->getData(); + + $data = $type->getDocument($newDocument->getId())->getData(); + + $this->assertEquals(12, $data['counter']); + $this->assertEquals('Batman', $data['name']); + $this->assertEquals('Bruce Wayne', $data['realName']); + + $this->assertTrue($newDocument->hasVersion()); + $this->assertArrayHasKey('_version', $responseData, '_version is missing in response data it is weird'); + $this->assertEquals(2, $responseData['_version']); + + $document = $type->getDocument($newDocument->getId()); + } + + /** + * @group functional + * @expectedException \Elastica\Exception\InvalidException + */ + public function testUpdateDocumentWithoutId() + { + $index = $this->_createIndex(); + $this->_waitForAllocation($index); + $type = $index->getType('elastica_type'); + + $document = new Document(); + + $type->updateDocument($document); + } + + /** + * @group functional + */ + public function testUpdateDocumentWithoutSource() + { + $index = $this->_createIndex(); + $type = $index->getType('elastica_type'); + + $mapping = new Mapping(); + $mapping->setProperties(array( + 'name' => array( + 'type' => 'string', + 'store' => 'yes', ), + 'counter' => array( + 'type' => 'integer', + 'store' => 'no', + ), + )); + $mapping->disableSource(); + $type->setMapping($mapping); + + $newDocument = new Document(); + $newDocument->setAutoPopulate(); + $newDocument->set('name', 'Batman'); + $newDocument->set('counter', 1); + + $type->addDocument($newDocument); + + $script = new Script('ctx._source.counter += count; ctx._source.name = name'); + $script->setId($newDocument->getId()); + $script->setParam('count', 2); + $script->setParam('name', 'robin'); + + $script->setUpsert($newDocument); + + try { + $type->updateDocument($script); + $this->fail('Update request should fail because source is disabled. Fields param is not set'); + } catch (ResponseException $e) { + $this->assertContains('DocumentSourceMissingException', $e->getMessage()); + } + + $newDocument->setFieldsSource(); + + try { + $type->updateDocument($newDocument); + $this->fail('Update request should fail because source is disabled. Fields param is set to _source'); + } catch (ResponseException $e) { + $this->assertContains('DocumentSourceMissingException', $e->getMessage()); + } + } + + /** + * @group functional + */ + public function testAddDocumentHashId() + { + $index = $this->_createIndex(); + $type = $index->getType('test2'); + + $hashId = '#1'; + + $doc = new Document($hashId, array('name' => 'ruflin')); + $type->addDocument($doc); + + $index->refresh(); + + $search = new Search($index->getClient()); + $search->addIndex($index); + $resultSet = $search->search(new MatchAll()); + $this->assertEquals($hashId, $resultSet->current()->getId()); + + $doc = $type->getDocument($hashId); + $this->assertEquals($hashId, $doc->getId()); + } + + /** + * @group functional + */ + public function testAddDocumentAutoGeneratedId() + { + $index = $this->_createIndex(); + $type = $index->getType('elastica_type'); + + $document = new Document(); + $document->setAutoPopulate(); + $document->set('name', 'ruflin'); + $this->assertEquals('', $document->getId()); + $this->assertFalse($document->hasId()); + + $type->addDocument($document); + + $this->assertNotEquals('', $document->getId()); + $this->assertTrue($document->hasId()); + + $foundDoc = $type->getDocument($document->getId()); + $this->assertInstanceOf('Elastica\Document', $foundDoc); + $this->assertEquals($document->getId(), $foundDoc->getId()); + $data = $foundDoc->getData(); + $this->assertArrayHasKey('name', $data); + $this->assertEquals('ruflin', $data['name']); + } + + /** + * @group functional + * @expectedException \Elastica\Exception\RuntimeException + */ + public function testAddDocumentWithoutSerializer() + { + $index = $this->_createIndex(); + $this->_waitForAllocation($index); + + $type = new Type($index, 'user'); + + $type->addObject(new \stdClass()); + } + + /** + * @group functional + */ + public function testAddObject() + { + $index = $this->_createIndex(); + + $type = new Type($index, 'user'); + $type->setSerializer('get_object_vars'); + + $userObject = new \stdClass(); + $userObject->username = 'hans'; + $userObject->test = array('2', '3', '5'); + + $type->addObject($userObject); + + $index->refresh(); + + $resultSet = $type->search('hans'); + $this->assertEquals(1, $resultSet->count()); + + // Test if source is returned + $result = $resultSet->current(); + $data = $result->getData(); + $this->assertEquals('hans', $data['username']); + } + + /** + * @group unit + */ + public function testSetSerializer() + { + $index = $this->_getClient()->getIndex('foo'); + $type = $index->getType('user'); + $ret = $type->setSerializer('get_object_vars'); + $this->assertInstanceOf('Elastica\Type', $ret); + } + + /** + * @group functional + */ + public function testExists() + { + $index = $this->_createIndex(); + $this->assertTrue($index->exists()); + + $type = new Type($index, 'user'); + $this->assertFalse($type->exists()); + + $type->addDocument(new Document(1, array('name' => 'test name'))); + $index->optimize(); + + // sleep a moment to be sure that all nodes in cluster has new type + sleep(5); + + //Test if type exists + $this->assertTrue($type->exists()); + + $index->delete(); + $this->assertFalse($index->exists()); + } + + /** + * @group functional + */ + public function testGetMapping() + { + $typeName = 'test-type'; + + $index = $this->_createIndex(); + $indexName = $index->getName(); + $type = new Type($index, $typeName); + $mapping = new Mapping($type, $expect = array( + 'id' => array('type' => 'integer', 'store' => true), + )); + $type->setMapping($mapping); + + $client = $index->getClient(); + + $this->assertEquals( + array('test-type' => array('properties' => $expect)), + $client->getIndex($indexName)->getType($typeName)->getMapping() + ); + } + + /** + * @group functional + */ + public function testGetMappingAlias() + { + $aliasName = 'test-alias'; + $typeName = 'test-alias-type'; + + $index = $this->_createIndex(); + $index->addAlias($aliasName); + $type = new Type($index, $typeName); + $mapping = new Mapping($type, $expect = array( + 'id' => array('type' => 'integer', 'store' => true), + )); + $type->setMapping($mapping); + + $client = $index->getClient(); + + $this->assertEquals( + array('test-alias-type' => array('properties' => $expect)), + $client->getIndex($aliasName)->getType($typeName)->getMapping() + ); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/UtilTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/UtilTest.php new file mode 100644 index 00000000..a5b0f42e --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/UtilTest.php @@ -0,0 +1,139 @@ +assertEquals($escaped, Util::escapeTerm($unescaped)); + } + + public function getEscapeTermPairs() + { + return array( + array('', ''), + array('pragmatic banana', 'pragmatic banana'), + array('oh yeah!', 'oh yeah\\!'), + // Seperate test below because phpunit seems to have some problems + //array('\\+-&&||!(){}[]^"~*?:', '\\\\\\+\\-\\&&\\||\\!\\(\\)\\{\\}\\[\\]\\^\\"\\~\\*\\?\\:'), + array('some signs, can stay.', 'some signs, can stay.'), + ); + } + + /** + * @group unit + * @dataProvider getReplaceBooleanWordsPairs + */ + public function testReplaceBooleanWords($before, $after) + { + $this->assertEquals($after, Util::replaceBooleanWords($before)); + } + + public function getReplaceBooleanWordsPairs() + { + return array( + array('to be OR not to be', 'to be || not to be'), + array('ORIGINAL GIFTS', 'ORIGINAL GIFTS'), + array('Black AND White', 'Black && White'), + array('TIMBERLAND Men`s', 'TIMBERLAND Men`s'), + array('hello NOT kitty', 'hello !kitty'), + array('SEND NOTIFICATION', 'SEND NOTIFICATION'), + ); + } + + /** + * @group unit + */ + public function testEscapeTermSpecialCharacters() + { + $before = '\\+-&&||!(){}[]^"~*?:/<>'; + $after = '\\\\\\+\\-\\&&\\||\\!\\(\\)\\{\\}\\[\\]\\^\\"\\~\\*\\?\\:\\/\<\>'; + + $this->assertEquals(Util::escapeTerm($before), $after); + } + + /** + * @group unit + */ + public function testToCamelCase() + { + $string = 'hello_world'; + $this->assertEquals('HelloWorld', Util::toCamelCase($string)); + + $string = 'how_are_you_today'; + $this->assertEquals('HowAreYouToday', Util::toCamelCase($string)); + } + + /** + * @group unit + */ + public function testToSnakeCase() + { + $string = 'HelloWorld'; + $this->assertEquals('hello_world', Util::toSnakeCase($string)); + + $string = 'HowAreYouToday'; + $this->assertEquals('how_are_you_today', Util::toSnakeCase($string)); + } + + /** + * @group unit + */ + public function testConvertRequestToCurlCommand() + { + $path = 'test'; + $method = Request::POST; + $query = array('no' => 'params'); + $data = array('key' => 'value'); + + $connection = new Connection(); + $connection->setHost($this->_getHost()); + $connection->setPort('9200'); + + $request = new Request($path, $method, $data, $query, $connection); + + $curlCommand = Util::convertRequestToCurlCommand($request); + + $expected = 'curl -XPOST \'http://'.$this->_getHost().':9200/test?no=params\' -d \'{"key":"value"}\''; + $this->assertEquals($expected, $curlCommand); + } + + /** + * @group unit + */ + public function testConvertDateTimeObjectWithTimezone() + { + $dateTimeObject = new \DateTime(); + $timestamp = $dateTimeObject->getTimestamp(); + + $convertedString = Util::convertDateTimeObject($dateTimeObject); + + $date = date('Y-m-d\TH:i:sP', $timestamp); + + $this->assertEquals($convertedString, $date); + } + + /** + * @group unit + */ + public function testConvertDateTimeObjectWithoutTimezone() + { + $dateTimeObject = new \DateTime(); + $timestamp = $dateTimeObject->getTimestamp(); + + $convertedString = Util::convertDateTimeObject($dateTimeObject, false); + + $date = date('Y-m-d\TH:i:s\Z', $timestamp); + + $this->assertEquals($convertedString, $date); + } +} -- cgit v1.2.3-54-g00ecf