Skip to content

Commit

Permalink
[TEST] add PHP_CodeSniffer to build (#638)
Browse files Browse the repository at this point in the history
* [TEST] PSR-2 fixes

* [TEST] PHP_CodeSniffer check PSR-2

* sort dependencies in composer.json

It will prevent some unnecessary future merge conflicts

* fix incorrectly merged code
  • Loading branch information
mhujer authored and polyfractal committed Nov 14, 2017
1 parent 75c6680 commit 088a509
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 67 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ before_script:
- php util/EnsureClusterAlive.php

script:
- composer run-script phpcs
- composer run-script phpstan
- vendor/bin/phpunit $PHPUNIT_FLAGS
- vendor/bin/phpunit -c phpunit-integration.xml --group sync $PHPUNIT_FLAGS

Expand Down
25 changes: 19 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@
"require": {
"php": "^7.0",
"ext-json": ">=1.3.7",
"psr/log": "~1.0",
"guzzlehttp/ringphp" : "~1.0"
"guzzlehttp/ringphp": "~1.0",
"psr/log": "~1.0"
},
"require-dev": {
"phpunit/phpunit": "^4.7|^5.4",
"cpliakas/git-wrapper": "~1.0",
"doctrine/inflector": "^1.1",
"mockery/mockery": "0.9.4",
"symfony/yaml": "^2.8",
"phpstan/phpstan-shim": "0.8.3",
"phpunit/phpunit": "6.3.0",
"squizlabs/php_codesniffer": "3.0.2",
"symfony/finder": "^2.8",
"cpliakas/git-wrapper": "~1.0",
"doctrine/inflector": "^1.1"
"symfony/yaml": "^2.8"
},
"suggest": {
"ext-curl": "*",
Expand All @@ -36,5 +38,16 @@
"psr-4": {
"Elasticsearch\\Tests\\": "tests/Elasticsearch/Tests/"
}
},
"config": {
"sort-packages": true
},
"scripts": {
"phpcs": [
"phpcs --standard=ruleset.xml --extensions=php --encoding=utf-8 --tab-width=4 -sp tests"
],
"phpstan": [
"@php vendor/phpstan/phpstan-shim/phpstan.phar analyse -c phpstan.neon tests --level 7 --no-progress"
]
}
}
6 changes: 6 additions & 0 deletions ruleset.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0"?>
<ruleset name="Elasticsearch-PHP">
<rule ref="PSR2">
<exclude name="Generic.Files.LineLength.TooLong"/>
</rule>
</ruleset>
19 changes: 11 additions & 8 deletions tests/Elasticsearch/Tests/ClientIntegrationTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

declare(strict_types = 1);

namespace Elasticsearch\Tests;

use Elasticsearch;

/**
* Class ClientTest
*
Expand All @@ -12,23 +16,22 @@
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache2
* @link http://elasticsearch.org
*/
class ClientIntegrationTests extends \PHPUnit_Framework_TestCase
class ClientIntegrationTests extends \PHPUnit\Framework\TestCase
{
public function testCustomQueryParams()
{
$params = array();

$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);
}

}
$this->assertFalse($exists);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public function testTenConnections()
$this->assertSame($mockConnections[2], $roundRobin->select($mockConnections));
$this->assertSame($mockConnections[3], $roundRobin->select($mockConnections));
$this->assertSame($mockConnections[4], $roundRobin->select($mockConnections));

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache2
* @link http://elasticsearch.org
*/
class SniffingConnectionPoolTest extends \PHPUnit_Framework_TestCase
class SniffingConnectionPoolTest extends \PHPUnit\Framework\TestCase
{
protected function setUp()
{
Expand Down Expand Up @@ -197,9 +197,6 @@ public function testAddTenNodesTimeoutAllButLast()
$this->assertSame($connections[9], $retConnection);
}

/**
* @expectedException \Elasticsearch\Common\Exceptions\NoNodesAvailableException
*/
public function testAddTenNodesAllTimeout()
{
$connections = [];
Expand All @@ -226,6 +223,9 @@ public function testAddTenNodesAllTimeout()
$connectionPoolParams = ['randomizeHosts' => false];
$connectionPool = new SniffingConnectionPool($connections, $selector, $connectionFactory, $connectionPoolParams);

$this->expectException(\Elasticsearch\Common\Exceptions\NoNodesAvailableException::class);
$this->expectExceptionMessage('No alive nodes found in your cluster');

$retConnection = $connectionPool->nextConnection();
}

Expand Down Expand Up @@ -274,10 +274,7 @@ public function testAddOneHostSniffTwo()
$this->assertSame($newConnections[1], $retConnection);
}

/**
* @expectedException \Elasticsearch\Common\Exceptions\NoNodesAvailableException
*/
public function testAddSeed_SniffTwo_TimeoutTwo()
public function testAddSeedSniffTwoTimeoutTwo()
{
$clusterState = json_decode('{"ok":true,"cluster_name":"elasticsearch_zach","nodes":{"node1":{"name":"Vesta","transport_address":"inet[/192.168.1.119:9300]","hostname":"zach-ThinkPad-W530","version":"0.90.5","http_address":"inet[/192.168.1.119:9200]"}, "node2":{"name":"Vesta","transport_address":"inet[/192.168.1.119:9301]","hostname":"zach-ThinkPad-W530","version":"0.90.5","http_address":"inet[/192.168.1.119:9201]"}}}', true);

Expand Down Expand Up @@ -317,11 +314,13 @@ public function testAddSeed_SniffTwo_TimeoutTwo()
];
$connectionPool = new SniffingConnectionPool($connections, $selector, $connectionFactory, $connectionPoolParams);

$this->expectException(\Elasticsearch\Common\Exceptions\NoNodesAvailableException::class);
$this->expectExceptionMessage('No alive nodes found in your cluster');

$retConnection = $connectionPool->nextConnection();
$this->assertSame($mockConnection, $retConnection);
}

public function testTen_TimeoutNine_SniffTenth_AddTwoAlive()
public function testTenTimeoutNineSniffTenthAddTwoAlive()
{
$clusterState = json_decode('{"ok":true,"cluster_name":"elasticsearch_zach","nodes":{"node1":{"name":"Vesta","transport_address":"inet[/192.168.1.119:9300]","hostname":"zach-ThinkPad-W530","version":"0.90.5","http_address":"inet[/192.168.1.119:9200]"}, "node2":{"name":"Vesta","transport_address":"inet[/192.168.1.119:9301]","hostname":"zach-ThinkPad-W530","version":"0.90.5","http_address":"inet[/192.168.1.119:9201]"}}}', true);

Expand Down Expand Up @@ -375,10 +374,7 @@ public function testTen_TimeoutNine_SniffTenth_AddTwoAlive()
$this->assertSame($newConnections[12], $retConnection);
}

/**
* @expectedException \Elasticsearch\Common\Exceptions\NoNodesAvailableException
*/
public function testTen_TimeoutNine_SniffTenth_AddTwoDead_TimeoutEveryone()
public function testTenTimeoutNineSniffTenthAddTwoDeadTimeoutEveryone()
{
$clusterState = json_decode('{"ok":true,"cluster_name":"elasticsearch_zach","nodes":{"node1":{"name":"Vesta","transport_address":"inet[/192.168.1.119:9300]","hostname":"zach-ThinkPad-W530","version":"0.90.5","http_address":"inet[/192.168.1.119:9200]"}, "node2":{"name":"Vesta","transport_address":"inet[/192.168.1.119:9301]","hostname":"zach-ThinkPad-W530","version":"0.90.5","http_address":"inet[/192.168.1.119:9201]"}}}', true);

Expand Down Expand Up @@ -418,8 +414,6 @@ public function testTen_TimeoutNine_SniffTenth_AddTwoDead_TimeoutEveryone()
->andReturnValues($newConnections)
->getMock();

$RRConnections = $newConnections;
//array_push($connections);
$connectionFactory = m::mock(ConnectionFactory::class)
->shouldReceive('create')->with(['host' => '192.168.1.119', 'port' => 9200])->andReturn($newConnections[10])->getMock()
->shouldReceive('create')->with(['host' => '192.168.1.119', 'port' => 9201])->andReturn($newConnections[11])->getMock();
Expand All @@ -430,10 +424,9 @@ public function testTen_TimeoutNine_SniffTenth_AddTwoDead_TimeoutEveryone()
];
$connectionPool = new SniffingConnectionPool($connections, $selector, $connectionFactory, $connectionPoolParams);

$retConnection = $connectionPool->nextConnection();
$this->assertSame($newConnections[11], $retConnection);
$this->expectException(\Elasticsearch\Common\Exceptions\NoNodesAvailableException::class);
$this->expectExceptionMessage('No alive nodes found in your cluster');

$retConnection = $connectionPool->nextConnection();
$this->assertSame($newConnections[12], $retConnection);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
class StaticConnectionPoolIntegrationTest extends \PHPUnit_Framework_TestCase
{
// Issue #636
public function test404Liveness() {
public function test404Liveness()
{
$client = \Elasticsearch\ClientBuilder::create()
->setHosts([$_SERVER['ES_TEST_HOST']])
->setConnectionPool(\Elasticsearch\ConnectionPool\StaticConnectionPool::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache2
* @link http://elasticsearch.org
*/
class StaticConnectionPoolTest extends \PHPUnit_Framework_TestCase
class StaticConnectionPoolTest extends \PHPUnit\Framework\TestCase
{
public function tearDown()
{
Expand All @@ -48,8 +48,10 @@ public function testAddOneHostThenGetConnection()

$connectionFactory = m::mock(ConnectionFactory::class);

$randomizeHosts = false;
$connectionPool = new StaticConnectionPool($connections, $selector, $connectionFactory, $randomizeHosts);
$connectionPoolParams = [
'randomizeHosts' => false,
];
$connectionPool = new StaticConnectionPool($connections, $selector, $connectionFactory, $connectionPoolParams);

$retConnection = $connectionPool->nextConnection();

Expand Down Expand Up @@ -80,17 +82,16 @@ public function testAddMultipleHostsThenGetFirst()

$connectionFactory = m::mock(ConnectionFactory::class);

$randomizeHosts = false;
$connectionPool = new StaticConnectionPool($connections, $selector, $connectionFactory, $randomizeHosts);
$connectionPoolParams = [
'randomizeHosts' => false,
];
$connectionPool = new StaticConnectionPool($connections, $selector, $connectionFactory, $connectionPoolParams);

$retConnection = $connectionPool->nextConnection();

$this->assertSame($connections[0], $retConnection);
}

/**
* @expectedException \Elasticsearch\Common\Exceptions\NoNodesAvailableException
*/
public function testAllHostsFailPing()
{
$connections = [];
Expand All @@ -117,8 +118,13 @@ public function testAllHostsFailPing()

$connectionFactory = m::mock(ConnectionFactory::class);

$randomizeHosts = false;
$connectionPool = new StaticConnectionPool($connections, $selector, $connectionFactory, $randomizeHosts);
$connectionPoolParams = [
'randomizeHosts' => false,
];
$connectionPool = new StaticConnectionPool($connections, $selector, $connectionFactory, $connectionPoolParams);

$this->expectException(\Elasticsearch\Common\Exceptions\NoNodesAvailableException::class);
$this->expectExceptionMessage('No alive nodes found in your cluster');

$connectionPool->nextConnection();
}
Expand Down Expand Up @@ -162,8 +168,10 @@ public function testAllExceptLastHostFailPingRevivesInSkip()

$connectionFactory = m::mock(ConnectionFactory::class);

$randomizeHosts = false;
$connectionPool = new StaticConnectionPool($connections, $selector, $connectionFactory, $randomizeHosts);
$connectionPoolParams = [
'randomizeHosts' => false,
];
$connectionPool = new StaticConnectionPool($connections, $selector, $connectionFactory, $connectionPoolParams);

$ret = $connectionPool->nextConnection();
$this->assertSame($goodConnection, $ret);
Expand Down Expand Up @@ -208,8 +216,10 @@ public function testAllExceptLastHostFailPingRevivesPreSkip()

$connectionFactory = m::mock(ConnectionFactory::class);

$randomizeHosts = false;
$connectionPool = new StaticConnectionPool($connections, $selector, $connectionFactory, $randomizeHosts);
$connectionPoolParams = [
'randomizeHosts' => false,
];
$connectionPool = new StaticConnectionPool($connections, $selector, $connectionFactory, $connectionPoolParams);

$ret = $connectionPool->nextConnection();
$this->assertSame($goodConnection, $ret);
Expand All @@ -224,13 +234,9 @@ public function testCustomConnectionPoolIT()
->setConnectionPool(StaticConnectionPool::class, [])
->build();

try {
$client->search([]);
$this->fail("Should have thrown NoNodesAvailableException");
} catch (Elasticsearch\Common\Exceptions\NoNodesAvailableException $e) {
// All good
} catch (\Exception $e) {
throw $e;
}
$this->expectException(Elasticsearch\Common\Exceptions\NoNodesAvailableException::class);
$this->expectExceptionMessage('No alive nodes found in your cluster');

$client->search([]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ public function testWithHits()
]
]
]
]);
]
);

$mock_client->shouldReceive('scroll')
->once()
Expand Down
2 changes: 2 additions & 0 deletions tests/Elasticsearch/Tests/RegisteredNamespaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function testNonExistingNamespace()
}
}

// @codingStandardsIgnoreStart "Each class must be in a file by itself" - not worth the extra work here
class FooNamespaceBuilder implements Elasticsearch\Namespaces\NamespaceBuilderInterface
{
public function getName()
Expand All @@ -65,3 +66,4 @@ public function fooMethod()
return "123";
}
}
// @codingStandardsIgnoreEnd
Loading

0 comments on commit 088a509

Please sign in to comment.