From 41dfc9490800909b2a9dbb6eda6b5d5560f3e562 Mon Sep 17 00:00:00 2001 From: Wilfried Date: Fri, 25 Oct 2019 17:19:26 +0200 Subject: [PATCH] chore (php7): Update all phpunit tests, refactor psr-0 for test Update all phpunit test according to phpunit update. Add phpunit config file allowing to overload default config. Update autoload --- .gitignore | 1 + composer.json | 5 +- phpunit.xml.dist | 24 +++++++ tests/lib/Autoload.php | 20 ++++++ tests/lib/Simples/DocumentTest.php | 7 +- tests/lib/Simples/HttpTestCase.php | 46 +++++++++++++ tests/lib/Simples/Request/CreateIndexTest.php | 10 +-- tests/lib/Simples/Request/DeleteIndexTest.php | 9 +-- tests/lib/Simples/Request/DeleteTest.php | 8 +-- tests/lib/Simples/Request/DeleteTypeTest.php | 11 ++- tests/lib/Simples/Request/GetTest.php | 32 ++++----- tests/lib/Simples/Request/IndexTest.php | 16 ++--- tests/lib/Simples/Request/MappingTest.php | 15 ++-- .../lib/Simples/Request/Search/FacetTest.php | 3 + tests/lib/Simples/Request/SearchTest.php | 13 ++-- tests/lib/Simples/Request/StatusTest.php | 8 +-- tests/lib/Simples/Request/UpdateTest.php | 15 ++-- tests/lib/Simples/RequestTest.php | 7 +- tests/lib/Simples/SimplesTest.php | 5 +- tests/lib/Simples/Transport/HttpTest.php | 68 +++++++++---------- tests/lib/bootstrap.php | 2 +- 21 files changed, 182 insertions(+), 143 deletions(-) create mode 100644 phpunit.xml.dist create mode 100644 tests/lib/Autoload.php create mode 100755 tests/lib/Simples/HttpTestCase.php diff --git a/.gitignore b/.gitignore index cb3a429..f055a98 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ nbproject/ sftp-* /vendor +phpunit.xml # many file generated by IDE .buildpath diff --git a/composer.json b/composer.json index 97a4e17..0ec64f1 100644 --- a/composer.json +++ b/composer.json @@ -19,5 +19,8 @@ }, "autoload": { "psr-0": {"Simples": "lib/"} - } + }, + "autoload-dev": { + "psr-0": { "Tests_Simples": "tests/lib" } + } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..fe6e152 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,24 @@ + + + + + + + + + + + ./tests/ + + + diff --git a/tests/lib/Autoload.php b/tests/lib/Autoload.php new file mode 100644 index 0000000..11762d4 --- /dev/null +++ b/tests/lib/Autoload.php @@ -0,0 +1,20 @@ +data['standard'] = array( 'firstname' => 'Jim', 'lastname' => 'Morrison', diff --git a/tests/lib/Simples/HttpTestCase.php b/tests/lib/Simples/HttpTestCase.php new file mode 100755 index 0000000..99234ec --- /dev/null +++ b/tests/lib/Simples/HttpTestCase.php @@ -0,0 +1,46 @@ + '127.0.0.1', + 'port' => 9200, + 'protocol' => 'http', + 'timeout' => 1000, + 'check' => true, + 'index' => null, + 'type' => null + ]; + + /** + * Redefine http config from env var + * given by phpunit.xml + */ + protected function setUp() : void { + foreach ($this->defaultHttpConfig as $key => $value) { + if (getenv($key)) { + $this->defaultHttpConfig[$key] = getenv($key); + } + } + parent::setUp(); + $this->client = new Simples_Transport_Http($this->getTransportHttpConfig()); + } + + /** + * @return [] + */ + protected function getTransportHttpConfig() + { + return $this->defaultHttpConfig; + } +} \ No newline at end of file diff --git a/tests/lib/Simples/Request/CreateIndexTest.php b/tests/lib/Simples/Request/CreateIndexTest.php index 55957de..6257638 100644 --- a/tests/lib/Simples/Request/CreateIndexTest.php +++ b/tests/lib/Simples/Request/CreateIndexTest.php @@ -1,15 +1,7 @@ client = new Simples_Transport_Http() ; - } - public function testCreate() { $this->client->config('index','test_index') ; $request = $this->client->createIndex() ; diff --git a/tests/lib/Simples/Request/DeleteIndexTest.php b/tests/lib/Simples/Request/DeleteIndexTest.php index 0c0926f..a79614b 100644 --- a/tests/lib/Simples/Request/DeleteIndexTest.php +++ b/tests/lib/Simples/Request/DeleteIndexTest.php @@ -1,14 +1,9 @@ deleteIndex('twitter') ; + $request = $this->client->deleteIndex('twitter') ; $this->assertEquals(Simples_Request::DELETE, $request->method()) ; $this->assertEquals('/twitter/', (string) $request->path()) ; } diff --git a/tests/lib/Simples/Request/DeleteTest.php b/tests/lib/Simples/Request/DeleteTest.php index 51e4059..70608c2 100644 --- a/tests/lib/Simples/Request/DeleteTest.php +++ b/tests/lib/Simples/Request/DeleteTest.php @@ -1,13 +1,9 @@ client; $this->assertTrue($client->index( array( 'content' => 'Pliz, pliz, delete me !' diff --git a/tests/lib/Simples/Request/DeleteTypeTest.php b/tests/lib/Simples/Request/DeleteTypeTest.php index ba71c96..0b16d70 100644 --- a/tests/lib/Simples/Request/DeleteTypeTest.php +++ b/tests/lib/Simples/Request/DeleteTypeTest.php @@ -1,15 +1,13 @@ client = new Simples_Transport_Http(array('index' => 'test_delete', 'type' => 'test_delete_type')); + parent::setUp(); + $this->client->config('index', 'test_delete'); + $this->client->config('type', 'test_delete_type'); $this->client->createIndex()->execute() ; } @@ -31,5 +29,4 @@ protected function tearDown() : void { $this->client->deleteIndex()->execute() ; } } - } \ No newline at end of file diff --git a/tests/lib/Simples/Request/GetTest.php b/tests/lib/Simples/Request/GetTest.php index 4f49284..2f01445 100644 --- a/tests/lib/Simples/Request/GetTest.php +++ b/tests/lib/Simples/Request/GetTest.php @@ -1,33 +1,29 @@ client->config('index', 'twitter'); + $this->client->config('type', 'tweet'); + } + public function testGet() { - $client = new Simples_Transport_Http(array( - 'index' => 'twitter', - 'type' => 'tweet' - )); + $this->client->config('index', 'twitter'); + $this->client->config('type', 'tweet'); - $this->assertTrue($client->index(array('content' => 'I\'m there.'), array('id' => 'test_get'))->ok) ; - $this->assertEquals('I\'m there.', $client->get('test_get')->_source->content); + $this->assertTrue($this->client->index(array('content' => 'I\'m there.'), array('id' => 'test_get'))->ok) ; + $this->assertEquals('I\'m there.', $this->client->get('test_get')->_source->content); } public function testMultiple() { - $client = new Simples_Transport_Http(array( - 'index' => 'twitter', - 'type' => 'tweet' - )); - - $client->index(array( + $this->client->index(array( array('id' => '1', 'value' => 'first'), array('id' => '2', 'value' => 'second') ), array('refresh' => true)) ; - $request = $client->get(array(1,2)) ; + $request = $this->client->get(array(1,2)) ; $this->assertEquals('/_mget/', (string) $request->path()) ; $body = $request->body() ; $this->assertEquals('1', $body['docs'][0]['_id']) ; diff --git a/tests/lib/Simples/Request/IndexTest.php b/tests/lib/Simples/Request/IndexTest.php index 9f4f4e6..98ea1b5 100644 --- a/tests/lib/Simples/Request/IndexTest.php +++ b/tests/lib/Simples/Request/IndexTest.php @@ -1,23 +1,17 @@ client = new Simples_Transport_Http(array( - 'index' => 'twitter', - 'type' => 'tweet' - )); + parent::setUp(); + + $this->client->config('index', 'twitter'); + $this->client->config('type', 'tweet'); } public function testIndex() { - $request = new Simples_Request_Index(null, null, new Simples_Transport_Http()); - $request = $this->client->index(array( 'user' => 'scharrier', 'fullname' => 'Sébastien Charrier' diff --git a/tests/lib/Simples/Request/MappingTest.php b/tests/lib/Simples/Request/MappingTest.php index 19e1a9b..e146cf9 100644 --- a/tests/lib/Simples/Request/MappingTest.php +++ b/tests/lib/Simples/Request/MappingTest.php @@ -1,17 +1,12 @@ client = new Simples_Transport_Http(array( - 'index' => 'music', - 'type' => 'composers', - 'log' => true - )); + parent::setUp(); + $this->client->config('index', 'music'); + $this->client->config('type', 'composers'); + $this->client->config('log', true); $this->client->createIndex()->execute() ; } diff --git a/tests/lib/Simples/Request/Search/FacetTest.php b/tests/lib/Simples/Request/Search/FacetTest.php index 851a9fd..1193310 100644 --- a/tests/lib/Simples/Request/Search/FacetTest.php +++ b/tests/lib/Simples/Request/Search/FacetTest.php @@ -22,6 +22,7 @@ public function testIn() { ) ) ) ; + $this->assertEquals($expected, $res); $facet = new Simples_Request_Search_Facet(array('in' => 'category_id')) ; $res = $facet->to('array') ; @@ -32,6 +33,7 @@ public function testIn() { ) ) ) ; + $this->assertEquals($expected, $res); $facet = new Simples_Request_Search_Facet(array('field' => 'category_id')) ; $res = $facet->to('array') ; @@ -42,6 +44,7 @@ public function testIn() { ) ) ) ; + $this->assertEquals($expected, $res); } public function testMultipleFields() { diff --git a/tests/lib/Simples/Request/SearchTest.php b/tests/lib/Simples/Request/SearchTest.php index e2d5d75..8cff9f0 100644 --- a/tests/lib/Simples/Request/SearchTest.php +++ b/tests/lib/Simples/Request/SearchTest.php @@ -1,18 +1,13 @@ client = new Simples_Transport_Http(array( - 'index' => 'twitter', - 'type' => 'tweet' - )); + parent::setUp(); + $this->client->config('index', 'twitter'); + $this->client->config('type', 'tweet'); $this->client->deleteIndex()->execute() ; diff --git a/tests/lib/Simples/Request/StatusTest.php b/tests/lib/Simples/Request/StatusTest.php index 4c22314..a6716e8 100644 --- a/tests/lib/Simples/Request/StatusTest.php +++ b/tests/lib/Simples/Request/StatusTest.php @@ -1,13 +1,9 @@ status()->execute() ; + $results = $this->client->status()->execute() ; $this->assertEquals(true, $results->ok) ; $this->assertTrue(isset($results->_shards->total)) ; } diff --git a/tests/lib/Simples/Request/UpdateTest.php b/tests/lib/Simples/Request/UpdateTest.php index 89d8670..03c9e8a 100644 --- a/tests/lib/Simples/Request/UpdateTest.php +++ b/tests/lib/Simples/Request/UpdateTest.php @@ -1,17 +1,12 @@ client = new Simples_Transport_Http(array( - 'index' => 'twitter', - 'type' => 'tweet', - 'log' => true - )); + parent::setUp(); + $this->client->config('index', 'twitter'); + $this->client->config('type', 'tweet'); + $this->client->config('log', true); } public function testUpdate() { diff --git a/tests/lib/Simples/RequestTest.php b/tests/lib/Simples/RequestTest.php index dddc979..c4101c9 100644 --- a/tests/lib/Simples/RequestTest.php +++ b/tests/lib/Simples/RequestTest.php @@ -1,9 +1,6 @@ execute() ; $this->assertTrue($request->execute() instanceof Simples_Response) ; - $res = $request->client(new Simples_Transport_Http())->execute() ; + $res = $request->client($this->client)->execute() ; $this->assertTrue($res->get('ok') === true) ; } diff --git a/tests/lib/Simples/SimplesTest.php b/tests/lib/Simples/SimplesTest.php index 07ad4b1..dde6408 100644 --- a/tests/lib/Simples/SimplesTest.php +++ b/tests/lib/Simples/SimplesTest.php @@ -6,8 +6,9 @@ class SimplesTest extends TestCase { public function testStaticUsage() { + $host = getenv('host') ?: '127.0.0.1'; $client = Simples::connect(array( - 'host' => '127.0.0.1', + 'host' => $host, )) ; $this->assertTrue(Simples::connected()) ; $this->assertTrue($client->connected()) ; @@ -16,7 +17,7 @@ public function testStaticUsage() { $this->assertFalse(Simples::connected()) ; $client = Simples::connect() ; - $this->assertEquals('127.0.0.1', $client->config('host')) ; + $this->assertEquals($host, $client->config('host')) ; $this->assertEquals(true, Simples::current()->status()->ok) ; Simples::current()->config(array( diff --git a/tests/lib/Simples/Transport/HttpTest.php b/tests/lib/Simples/Transport/HttpTest.php index 425af4f..95ee245 100644 --- a/tests/lib/Simples/Transport/HttpTest.php +++ b/tests/lib/Simples/Transport/HttpTest.php @@ -1,18 +1,15 @@ connect() ; - $this->assertTrue($transport->connected()) ; - $this->assertTrue($transport instanceof Simples_Transport_Http) ; + $this->client->connect() ; + $this->assertTrue($this->client->connected()) ; + $this->assertTrue($this->client instanceof Simples_Transport_Http) ; - $transport->disconnect() ; - $this->assertFalse($transport->connected()) ; + $this->client->disconnect() ; + $this->assertFalse($this->client->connected()) ; } catch (Exception $e) { $this->markTestSkipped($e->getMessage()) ; } @@ -22,38 +19,37 @@ public function testConnection() { */ public function testConnectionException() { $this->expectException(\Exception::class); - $transport = new Simples_Transport_Http(array('host' => 'www.google.com', 'port' => '80')) ; - $transport->connect() ; + $this->client->config('host', 'www.google.com'); + $this->client->config('port', '80'); + $this->client->connect() ; } /** */ public function testCheck() { $this->expectException(\Exception::class); - $transport = new Simples_Transport_Http() ; - - $transport->config(array( + + $this->client->config(array( 'host' => 'www.google.com', 'port' => 80 )) ; - $transport->connect() ; + $this->client->connect() ; } public function testUrl() { - $transport = new Simples_Transport_Http() ; - $this->assertEquals('http://127.0.0.1/', $transport->url()) ; + $this->client->config('host', '127.0.0.1'); + $this->assertEquals('http://127.0.0.1/', $this->client->url()) ; - $transport->config('host', 'farhost') ; - $this->assertEquals('http://farhost/', $transport->url()) ; + $this->client->config('host', 'farhost'); + $this->assertEquals('http://farhost/', $this->client->url()) ; - $this->assertEquals('http://farhost/_status', $transport->url('_status')) ; - $this->assertEquals('http://farhost/_status', $transport->url('/_status')) ; + $this->assertEquals('http://farhost/_status', $this->client->url('_status')) ; + $this->assertEquals('http://farhost/_status', $this->client->url('/_status')) ; } public function testCall() { - $transport = new Simples_Transport_Http() ; - $res = $transport->call() ; + $res = $this->client->call() ; $this->assertTrue($res['ok']); $this->assertTrue(isset($res['version']['number'])) ; } @@ -63,8 +59,8 @@ public function testCall() { public function testCallCannotJsonDecodeException() { $this->expectException(\Exception::class); $this->expectExceptionMessage('Cannot JSON decode the response : No handler found for uri [/test] and method [GET]'); - $transport = new Simples_Transport_Http(); - $transport->call('/test'); + + $this->client->call('/test'); } /** @@ -72,8 +68,8 @@ public function testCallCannotJsonDecodeException() { public function testCallCurlReturnFalse() { $this->expectException(\Exception::class); $this->expectExceptionMessage('Error during the request (6)'); - $transport = new Simples_Transport_Http(array( 'host' => 'nowhere' )); - $transport->call('/test'); + $this->client->config(array( 'host' => 'nowhere' )); + $this->client->call('/test'); } /** @@ -81,8 +77,8 @@ public function testCallCurlReturnFalse() { public function testCallEmptyResponse() { $this->expectException(\Exception::class); $this->expectExceptionMessage('The ES server returned an empty response.'); - $transport = new Simples_Transport_Http(); - $transport->call('/test', 'HEAD'); + + $this->client->call('/test', 'HEAD'); } /** @@ -90,21 +86,19 @@ public function testCallEmptyResponse() { public function testCallCurlHttpCode() { $this->expectException(\Exception::class); $this->expectExceptionMessage('Error during the request (HTTP CODE: 400)'); - $transport = new Simples_Transport_Http(); try { - $transport->call('/test', 'DELETE'); - $transport->call('/test', 'PUT'); + $this->client->call('/test', 'DELETE'); + $this->client->call('/test', 'PUT'); } catch(Exception $e) {} - $transport->call('/test', 'POST'); + $this->client->call('/test', 'POST'); } public function testMagicCall() { - $transport = new Simples_Transport_Http() ; - $status = $transport->status() ; + $status = $this->client->status() ; $this->assertTrue($status instanceof Simples_Request_Status) ; - $response = $transport->status()->execute() ; + $response = $this->client->status()->execute() ; $this->assertTrue($response instanceof Simples_Response) ; } } diff --git a/tests/lib/bootstrap.php b/tests/lib/bootstrap.php index 350de8a..9b31b22 100644 --- a/tests/lib/bootstrap.php +++ b/tests/lib/bootstrap.php @@ -1,3 +1,3 @@