|
4 | 4 |
|
5 | 5 | namespace Elasticsearch\Tests; |
6 | 6 |
|
7 | | -use Elasticsearch; |
| 7 | +use Elasticsearch\ClientBuilder; |
| 8 | +use Elasticsearch\Common\Exceptions\Missing404Exception; |
| 9 | +use Elasticsearch\Tests\ClientBuilder\ArrayLogger; |
| 10 | +use Psr\Log\LogLevel; |
8 | 11 |
|
9 | 12 | /** |
10 | 13 | * Class ClientTest |
|
18 | 21 | */ |
19 | 22 | class ClientIntegrationTests extends \PHPUnit\Framework\TestCase |
20 | 23 | { |
21 | | - public function testCustomQueryParams() |
| 24 | + public function setUp() |
22 | 25 | { |
23 | | - $client = Elasticsearch\ClientBuilder::create() |
| 26 | + if (empty(getenv('ES_TEST_HOST'))) { |
| 27 | + $this->markTestSkipped('I cannot execute integration test without ES_TEST_HOST env'); |
| 28 | + } |
| 29 | + $this->logger = new ArrayLogger(); |
| 30 | + } |
| 31 | + |
| 32 | + public function testLogRequestSuccessHasInfoNotEmpty() |
| 33 | + { |
| 34 | + $client = ClientBuilder::create() |
| 35 | + ->setHosts([getenv('ES_TEST_HOST')]) |
| 36 | + ->setLogger($this->logger) |
| 37 | + ->build(); |
| 38 | + |
| 39 | + $result = $client->info(); |
| 40 | + |
| 41 | + $this->assertNotEmpty($this->getLevelOutput(LogLevel::INFO, $this->logger->output)); |
| 42 | + } |
| 43 | + |
| 44 | + public function testLogRequestSuccessHasPortInInfo() |
| 45 | + { |
| 46 | + $client = ClientBuilder::create() |
24 | 47 | ->setHosts([getenv('ES_TEST_HOST')]) |
| 48 | + ->setLogger($this->logger) |
25 | 49 | ->build(); |
26 | 50 |
|
27 | | - $getParams = [ |
28 | | - 'index' => 'test', |
29 | | - 'type' => 'test', |
30 | | - 'id' => 1, |
31 | | - 'parent' => 'abc', |
32 | | - 'custom' => ['customToken' => 'abc', 'otherToken' => 123], |
33 | | - 'client' => ['ignore' => 400] |
34 | | - ]; |
35 | | - $exists = $client->exists($getParams); |
36 | | - |
37 | | - $this->assertFalse((bool) $exists); |
| 51 | + $result = $client->info(); |
| 52 | + |
| 53 | + $this->assertContains('"port"', $this->getLevelOutput(LogLevel::INFO, $this->logger->output)); |
| 54 | + } |
| 55 | + |
| 56 | + public function testLogRequestFailHasWarning() |
| 57 | + { |
| 58 | + $client = ClientBuilder::create() |
| 59 | + ->setHosts([getenv('ES_TEST_HOST')]) |
| 60 | + ->setLogger($this->logger) |
| 61 | + ->build(); |
| 62 | + |
| 63 | + try { |
| 64 | + $result = $client->get([ |
| 65 | + 'index' => 'foo', |
| 66 | + 'id' => 'bar' |
| 67 | + ]); |
| 68 | + } catch (Missing404Exception $e) { |
| 69 | + $this->assertNotEmpty($this->getLevelOutput(LogLevel::WARNING, $this->logger->output)); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private function getLevelOutput(string $level, array $output): string |
| 74 | + { |
| 75 | + foreach ($output as $out) { |
| 76 | + if (false !== strpos($out, $level)) { |
| 77 | + return $out; |
| 78 | + } |
| 79 | + } |
| 80 | + return ''; |
38 | 81 | } |
39 | 82 | } |
0 commit comments