1
+ <?php
2
+ /**
3
+ * Elasticsearch PHP Client
4
+ *
5
+ * @link https://github.com/elastic/elasticsearch-php
6
+ * @copyright Copyright (c) Elasticsearch B.V (https://www.elastic.co)
7
+ * @license https://opensource.org/licenses/MIT MIT License
8
+ *
9
+ * Licensed to Elasticsearch B.V under one or more agreements.
10
+ * Elasticsearch B.V licenses this file to you under the MIT License.
11
+ * See the LICENSE file in the project root for more information.
12
+ */
13
+ declare (strict_types = 1 );
14
+
15
+ namespace Elastic \Elasticsearch \Tests \Traits ;
16
+
17
+ use Elastic \Elasticsearch \Exception \ContentTypeException ;
18
+ use Elastic \Elasticsearch \Exception \MissingParameterException ;
19
+ use Elastic \Elasticsearch \Traits \EndpointTrait ;
20
+ use PHPUnit \Framework \TestCase ;
21
+
22
+ class EndpointTraitTest extends TestCase
23
+ {
24
+ use EndpointTrait;
25
+
26
+ public function getQueryStrings (): array
27
+ {
28
+ return [
29
+ ['http://localhost:9200 ' , ['refresh ' => true ], ['refresh ' ], 'http://localhost:9200?refresh=true ' ],
30
+ ['http://localhost:9200 ' , ['refresh ' => 'true ' ], ['refresh ' ], 'http://localhost:9200?refresh=true ' ],
31
+ ['http://localhost ' , ['refresh ' => 'true ' ], ['refresh ' , 'pretty ' ], 'http://localhost?refresh=true ' ],
32
+ ['http://localhost ' , ['body ' => [], 'foo ' => 'bar ' ], ['refresh ' ], 'http://localhost ' ]
33
+ ];
34
+ }
35
+
36
+ /**
37
+ * @dataProvider getQueryStrings
38
+ */
39
+ public function testAddQueryString (string $ url , array $ params , array $ keys , string $ expected )
40
+ {
41
+ $ result = $ this ->addQueryString ($ url , $ params , $ keys );
42
+ $ this ->assertEquals ($ expected , $ result );
43
+ }
44
+
45
+ public function getBodySerialized (): array
46
+ {
47
+ return [
48
+ [[ 'foo ' => 'bar ' ], 'application/json ' , '{"foo":"bar"} ' ],
49
+ [[[ 'foo ' => 'bar ' ], ['bar ' => 'baz ' ]], 'application/x-ndjson ' , "{ \"foo \": \"bar \"} \n{ \"bar \": \"baz \"} \n" ]
50
+ ];
51
+ }
52
+
53
+ /**
54
+ * @dataProvider getBodySerialized
55
+ */
56
+ public function testBodySerialize (array $ body , string $ contentType , string $ expected )
57
+ {
58
+ $ result = $ this ->bodySerialize ($ body , $ contentType );
59
+ $ this ->assertEquals ($ expected , $ result );
60
+ }
61
+
62
+ public function testBodySerializeUnknownContentTypeThrowsException ()
63
+ {
64
+ $ this ->expectException (ContentTypeException::class);
65
+ $ this ->bodySerialize (['foo ' => 'bar ' ], 'Unknown-content-type ' );
66
+ }
67
+
68
+ public function getRequestParts (): array
69
+ {
70
+ return [
71
+ [ 'GET ' , 'http://localhost:9200 ' , ['Foo ' => 'bar ' , 'Content-Type ' => 'application/json ' ], []],
72
+ [ 'GET ' , 'http://localhost ' , ['Foo ' => 'bar ' , 'Content-Type ' => 'application/json ' ], []],
73
+ [ 'POST ' , 'http://localhost:9200 ' , ['Content-Type ' => 'application/json ' ], ['foo ' => 'bar ' ]],
74
+ [ 'POST ' , 'http://localhost:9200 ' , ['Content-Type ' => 'application/x-ndjson ' ], [[ 'foo ' => 'bar ' ], ['bar ' => 'baz ' ]]]
75
+ ];
76
+ }
77
+
78
+ /**
79
+ * @dataProvider getRequestParts
80
+ */
81
+ public function testCreateRequest (string $ method , string $ url , array $ headers , array $ body )
82
+ {
83
+ $ request = $ this ->createRequest ($ method , $ url , $ headers , $ body );
84
+ $ this ->assertEquals ($ method , $ request ->getMethod ());
85
+ $ this ->assertEquals ($ url , (string ) $ request ->getUri ());
86
+ $ host = parse_url ($ url , PHP_URL_HOST );
87
+ $ port = parse_url ($ url , PHP_URL_PORT );
88
+ $ this ->assertEquals (empty ($ port ) ? $ host : $ host . ': ' . $ port , $ request ->getHeader ('Host ' )[0 ]);
89
+ foreach ($ headers as $ name => $ value ) {
90
+ $ header = $ request ->getHeader ($ name );
91
+ $ this ->assertEquals ($ value , implode (', ' , $ header ));
92
+ }
93
+ if (!empty ($ body )) {
94
+ $ this ->assertEquals ($ this ->bodySerialize ($ body , $ headers ['Content-Type ' ]), (string ) $ request ->getBody ());
95
+ }
96
+ }
97
+
98
+ public function getRequiredParams (): array
99
+ {
100
+ return [
101
+ [['index ' ], ['index ' => '1 ' ], false ],
102
+ [['index ' ], ['foo ' => 'bar ' ], true ]
103
+ ];
104
+ }
105
+
106
+ /**
107
+ * @dataProvider getRequiredParams
108
+ */
109
+ public function testCheckRequiredParameters (array $ required , array $ params , bool $ exception )
110
+ {
111
+ if (!$ exception ) {
112
+ $ this ->assertNull ($ this ->checkRequiredParameters ($ required , $ params ));
113
+ } else {
114
+ $ this ->expectException (MissingParameterException::class);
115
+ $ this ->checkRequiredParameters ($ required , $ params );
116
+ }
117
+ }
118
+ }
0 commit comments