Skip to content

Commit

Permalink
[TEST] tests code cleanup (#618)
Browse files Browse the repository at this point in the history
* [TEST] use ::class instead of classname string

Reason - when the class is renamed or removed, it will be detected much faster.
It helps with the static analysis of the code (also used by IDEs)

* [TEST] use assertSame() instead of assertEquals()

assertEquals() means ==
assertSame() is ===

Can cause tests not detecting issues, because different instances of
same class with same data are equal, but not the same.

* [TEST] convert array() to [] syntax

* [TEST] $this->assert* should be used

See
sebastianbergmann/phpunit#1914 (comment)
  • Loading branch information
mhujer authored and polyfractal committed Aug 21, 2017
1 parent fcd5ff1 commit dc5d18c
Show file tree
Hide file tree
Showing 9 changed files with 235 additions and 224 deletions.
73 changes: 37 additions & 36 deletions tests/Elasticsearch/Tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Elasticsearch;
use Elasticsearch\ClientBuilder;
use Elasticsearch\Common\Exceptions\MaxRetriesException;
use Mockery as m;

/**
Expand Down Expand Up @@ -35,18 +36,18 @@ public function testConstructorIllegalPort()

public function testCustomQueryParams()
{
$params = array();
$params = [];

$client = Elasticsearch\ClientBuilder::create()->setHosts([$_SERVER['ES_TEST_HOST']])->build();

$getParams = array(
$getParams = [
'index' => 'test',
'type' => 'test',
'id' => 1,
'parent' => 'abc',
'custom' => array('customToken' => 'abc', 'otherToken' => 123),
'custom' => ['customToken' => 'abc', 'otherToken' => 123],
'client' => ['ignore' => 400]
);
];
$exists = $client->exists($getParams);
}

Expand Down Expand Up @@ -226,15 +227,15 @@ public function testMaxRetriesException()
->setRetries(0)
->build();

$searchParams = array(
$searchParams = [
'index' => 'test',
'type' => 'test',
'body' => [
'query' => [
'match_all' => []
]
]
);
];

$client = Elasticsearch\ClientBuilder::create()
->setHosts(["localhost:1"])
Expand All @@ -247,7 +248,7 @@ public function testMaxRetriesException()
} catch (Elasticsearch\Common\Exceptions\Curl\CouldNotConnectToHost $e) {
// All good
$previous = $e->getPrevious();
$this->assertInstanceOf('Elasticsearch\Common\Exceptions\MaxRetriesException', $previous);
$this->assertInstanceOf(MaxRetriesException::class, $previous);
} catch (\Exception $e) {
throw $e;
}
Expand All @@ -264,7 +265,7 @@ public function testMaxRetriesException()
} catch (Elasticsearch\Common\Exceptions\TransportException $e) {
// All good
$previous = $e->getPrevious();
$this->assertInstanceOf('Elasticsearch\Common\Exceptions\MaxRetriesException', $previous);
$this->assertInstanceOf(MaxRetriesException::class, $previous);
} catch (\Exception $e) {
throw $e;
}
Expand All @@ -276,39 +277,39 @@ public function testInlineHosts()
'localhost:9200'
])->build();
$host = $client->transport->getConnection();
$this->assertEquals("localhost:9200", $host->getHost());
$this->assertEquals("http", $host->getTransportSchema());
$this->assertSame("localhost:9200", $host->getHost());
$this->assertSame("http", $host->getTransportSchema());


$client = Elasticsearch\ClientBuilder::create()->setHosts([
'http://localhost:9200'
])->build();
$host = $client->transport->getConnection();
$this->assertEquals("localhost:9200", $host->getHost());
$this->assertEquals("http", $host->getTransportSchema());
$this->assertSame("localhost:9200", $host->getHost());
$this->assertSame("http", $host->getTransportSchema());

$client = Elasticsearch\ClientBuilder::create()->setHosts([
'http://foo.com:9200'
])->build();
$host = $client->transport->getConnection();
$this->assertEquals("foo.com:9200", $host->getHost());
$this->assertEquals("http", $host->getTransportSchema());
$this->assertSame("foo.com:9200", $host->getHost());
$this->assertSame("http", $host->getTransportSchema());

$client = Elasticsearch\ClientBuilder::create()->setHosts([
'https://foo.com:9200'
])->build();
$host = $client->transport->getConnection();
$this->assertEquals("foo.com:9200", $host->getHost());
$this->assertEquals("https", $host->getTransportSchema());
$this->assertSame("foo.com:9200", $host->getHost());
$this->assertSame("https", $host->getTransportSchema());


$client = Elasticsearch\ClientBuilder::create()->setHosts([
'https://user:pass@foo.com:9200'
])->build();
$host = $client->transport->getConnection();
$this->assertEquals("foo.com:9200", $host->getHost());
$this->assertEquals("https", $host->getTransportSchema());
$this->assertEquals("user:pass", $host->getUserPass());
$this->assertSame("foo.com:9200", $host->getHost());
$this->assertSame("https", $host->getTransportSchema());
$this->assertSame("user:pass", $host->getUserPass());
}

public function testExtendedHosts()
Expand All @@ -321,8 +322,8 @@ public function testExtendedHosts()
]
])->build();
$host = $client->transport->getConnection();
$this->assertEquals("localhost:9200", $host->getHost());
$this->assertEquals("http", $host->getTransportSchema());
$this->assertSame("localhost:9200", $host->getHost());
$this->assertSame("http", $host->getTransportSchema());


$client = Elasticsearch\ClientBuilder::create()->setHosts([
Expand All @@ -333,8 +334,8 @@ public function testExtendedHosts()
]
])->build();
$host = $client->transport->getConnection();
$this->assertEquals("foo.com:9200", $host->getHost());
$this->assertEquals("http", $host->getTransportSchema());
$this->assertSame("foo.com:9200", $host->getHost());
$this->assertSame("http", $host->getTransportSchema());


$client = Elasticsearch\ClientBuilder::create()->setHosts([
Expand All @@ -345,8 +346,8 @@ public function testExtendedHosts()
]
])->build();
$host = $client->transport->getConnection();
$this->assertEquals("foo.com:9200", $host->getHost());
$this->assertEquals("https", $host->getTransportSchema());
$this->assertSame("foo.com:9200", $host->getHost());
$this->assertSame("https", $host->getTransportSchema());


$client = Elasticsearch\ClientBuilder::create()->setHosts([
Expand All @@ -356,8 +357,8 @@ public function testExtendedHosts()
]
])->build();
$host = $client->transport->getConnection();
$this->assertEquals("foo.com:9200", $host->getHost());
$this->assertEquals("http", $host->getTransportSchema());
$this->assertSame("foo.com:9200", $host->getHost());
$this->assertSame("http", $host->getTransportSchema());


$client = Elasticsearch\ClientBuilder::create()->setHosts([
Expand All @@ -366,8 +367,8 @@ public function testExtendedHosts()
]
])->build();
$host = $client->transport->getConnection();
$this->assertEquals("foo.com:9200", $host->getHost());
$this->assertEquals("http", $host->getTransportSchema());
$this->assertSame("foo.com:9200", $host->getHost());
$this->assertSame("http", $host->getTransportSchema());


$client = Elasticsearch\ClientBuilder::create()->setHosts([
Expand All @@ -378,8 +379,8 @@ public function testExtendedHosts()
]
])->build();
$host = $client->transport->getConnection();
$this->assertEquals("foo.com:9500", $host->getHost());
$this->assertEquals("https", $host->getTransportSchema());
$this->assertSame("foo.com:9500", $host->getHost());
$this->assertSame("https", $host->getTransportSchema());


try {
Expand All @@ -401,8 +402,8 @@ public function testExtendedHosts()
]
])->build();
$host = $client->transport->getConnection();
$this->assertEquals("the_foo.com:9200", $host->getHost());
$this->assertEquals("http", $host->getTransportSchema());
$this->assertSame("the_foo.com:9200", $host->getHost());
$this->assertSame("http", $host->getTransportSchema());


// Special characters in user/pass, would break inline
Expand All @@ -414,8 +415,8 @@ public function testExtendedHosts()
]
])->build();
$host = $client->transport->getConnection();
$this->assertEquals("foo.com:9200", $host->getHost());
$this->assertEquals("http", $host->getTransportSchema());
$this->assertEquals("user:abc#$@?%!abc", $host->getUserPass());
$this->assertSame("foo.com:9200", $host->getHost());
$this->assertSame("http", $host->getTransportSchema());
$this->assertSame("user:abc#$@?%!abc", $host->getUserPass());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Elasticsearch\Tests\ConnectionPool;

use Elasticsearch\ClientBuilder;
use Elasticsearch\ConnectionPool\SniffingConnectionPool;

/**
* Class SniffingConnectionPoolIntegrationTest
Expand All @@ -22,7 +23,7 @@ public function testSniff()
{
$client = ClientBuilder::create()
->setHosts([$_SERVER['ES_TEST_HOST']])
->setConnectionPool('\Elasticsearch\ConnectionPool\SniffingConnectionPool', ['sniffingInterval' => -10])
->setConnectionPool(SniffingConnectionPool::class, ['sniffingInterval' => -10])
->build();

$client->ping();
Expand Down
Loading

0 comments on commit dc5d18c

Please sign in to comment.