Skip to content

Commit 8eb89f5

Browse files
committed
Improved tests with body as string
1 parent 80f873b commit 8eb89f5

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

tests/Integration/BasicTest.php

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
*/
2525
class BasicTest extends TestCase
2626
{
27-
protected static array $ids;
28-
2927
public function setUp(): void
3028
{
3129
$this->client = Utility::getClient();
@@ -73,7 +71,33 @@ public function testIndexDocuments(string $date, string $open, string $high, str
7371
$this->assertEquals(201, $response->getStatusCode());
7472
$this->assertEquals('created', $response['result']);
7573
$this->assertEquals('stocks', $response['_index']);
76-
self::$ids[] = $response['_id'];
74+
return $response['_id'];
75+
}
76+
77+
public function testIndexDocumentWithBodyAsString()
78+
{
79+
$response = $this->client->index([
80+
'index' => 'stocks',
81+
'refresh' => true,
82+
'body' => '{"date":"2020-09-26","open":47.32,"high":47.32,"low":47.32,"close":47.32,"volume":968728,"name":"XXX"}'
83+
]);
84+
$this->assertEquals(201, $response->getStatusCode());
85+
$this->assertEquals('created', $response['result']);
86+
$this->assertEquals('stocks', $response['_index']);
87+
88+
return $response['_id'];
89+
}
90+
91+
/**
92+
* @depends testIndexDocumentWithBodyAsString
93+
*/
94+
public function testDeleteADocument(string $id)
95+
{
96+
$response = $this->client->delete([
97+
'index' => 'stocks',
98+
'id' => $id
99+
]);
100+
$this->assertEquals(200, $response->getStatusCode());
77101
}
78102

79103
/**

tests/Traits/EndpointTraitTest.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,16 @@ public function getRequestParts(): array
7171
[ 'GET', 'http://localhost:9200', ['Foo' => 'bar', 'Content-Type' => 'application/json'], []],
7272
[ 'GET', 'http://localhost', ['Foo' => 'bar', 'Content-Type' => 'application/json'], []],
7373
[ 'POST', 'http://localhost:9200', ['Content-Type' => 'application/json'], ['foo' => 'bar']],
74-
[ 'POST', 'http://localhost:9200', ['Content-Type' => 'application/x-ndjson'], [[ 'foo' => 'bar'], ['bar' => 'baz']]]
74+
[ 'POST', 'http://localhost:9200', ['Content-Type' => 'application/x-ndjson'], [[ 'foo' => 'bar'], ['bar' => 'baz']]],
75+
// test body as string
76+
[ 'POST', 'http://localhost', ['Content-Type' => 'application/x-ndjson'], '{"foo":"bar"}']
7577
];
7678
}
7779

7880
/**
7981
* @dataProvider getRequestParts
8082
*/
81-
public function testCreateRequest(string $method, string $url, array $headers, array $body)
83+
public function testCreateRequest(string $method, string $url, array $headers, $body)
8284
{
8385
$request = $this->createRequest($method, $url, $headers, $body);
8486
$this->assertEquals($method, $request->getMethod());
@@ -91,7 +93,11 @@ public function testCreateRequest(string $method, string $url, array $headers, a
9193
$this->assertEquals($value, implode(',', $header));
9294
}
9395
if (!empty($body)) {
94-
$this->assertEquals($this->bodySerialize($body, $headers['Content-Type']), (string) $request->getBody());
96+
if (is_array($body)) {
97+
$this->assertEquals($this->bodySerialize($body, $headers['Content-Type']), (string) $request->getBody());
98+
} else {
99+
$this->assertEquals($body, (string) $request->getBody());
100+
}
95101
}
96102
}
97103

0 commit comments

Comments
 (0)