Skip to content
This repository has been archived by the owner on May 2, 2023. It is now read-only.

Commit

Permalink
chore (php7): Update all phpunit tests, refactor psr-0 for test
Browse files Browse the repository at this point in the history
Update all phpunit test according to phpunit update.
Add phpunit config file allowing to overload default config.
Update autoload
  • Loading branch information
Wilfried committed Oct 29, 2019
1 parent 544cd6c commit 41dfc94
Show file tree
Hide file tree
Showing 21 changed files with 182 additions and 143 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
nbproject/
sftp-*
/vendor
phpunit.xml

# many file generated by IDE
.buildpath
Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,8 @@
},
"autoload": {
"psr-0": {"Simples": "lib/"}
}
},
"autoload-dev": {
"psr-0": { "Tests_Simples": "tests/lib" }
}
}
24 changes: 24 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
bootstrap="./tests/lib/Autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
>
<php>
<ini name="memory_limit" value="-1"/>
<env name="host" value="127.0.0.1" force="true" />
<env name="port" value="9200" force="true" />
<env name="protocol" value="http" force="true" />
</php>
<testsuites>
<testsuite name="AllTests">
<directory>./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
20 changes: 20 additions & 0 deletions tests/lib/Autoload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

if (!defined('SIMPLES_TESTS_ROOT')) {
define('SIMPLES_TESTS_ROOT', dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR);
}

/**
* Simples autoload method for tests.
*
* @param string $class Class name to load
*/
function autoload_simples_tests ($class) {
$path = str_replace('_', DIRECTORY_SEPARATOR , $class);
if (file_exists(SIMPLES_TESTS_ROOT . $path . '.php')) {
require_once(SIMPLES_TESTS_ROOT . $path . '.php');
}
}

// Register our custom autoload method
spl_autoload_register('autoload_simples_tests');
7 changes: 3 additions & 4 deletions tests/lib/Simples/DocumentTest.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
<?php
require_once(dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'bootstrap.php') ;

use PHPUnit\Framework\TestCase;

class Simples_DocumentTest extends TestCase {
class Simples_DocumentTest extends Simples_HttpTestCase {

/**
* Set up some fixtures.
*/
protected function setUp() : void {
parent::setUp();

$this->data['standard'] = array(
'firstname' => 'Jim',
'lastname' => 'Morrison',
Expand Down
46 changes: 46 additions & 0 deletions tests/lib/Simples/HttpTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

use PHPUnit\Framework\TestCase;

class Simples_HttpTestCase extends TestCase {

/**
* @var Simples_Transport_Http|null
*/
protected $client;

/**
* @var array
*/
protected $defaultHttpConfig = [
'host' => '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;
}
}
10 changes: 1 addition & 9 deletions tests/lib/Simples/Request/CreateIndexTest.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
<?php

require_once(dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'bootstrap.php');
class Simples_Request_CreateIndexTest extends Simples_HttpTestCase {

use PHPUnit\Framework\TestCase;

class Simples_Request_CreateIndexTest extends TestCase {

protected function setUp() : void {
$this->client = new Simples_Transport_Http() ;
}

public function testCreate() {
$this->client->config('index','test_index') ;
$request = $this->client->createIndex() ;
Expand Down
9 changes: 2 additions & 7 deletions tests/lib/Simples/Request/DeleteIndexTest.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
<?php

require_once(dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'bootstrap.php');

use PHPUnit\Framework\TestCase;

class Simples_Request_DeleteIndexTest extends TestCase {
class Simples_Request_DeleteIndexTest extends Simples_HttpTestCase {

public function testDelete() {
$client = new Simples_Transport_Http();
$request = $client->deleteIndex('twitter') ;
$request = $this->client->deleteIndex('twitter') ;
$this->assertEquals(Simples_Request::DELETE, $request->method()) ;
$this->assertEquals('/twitter/', (string) $request->path()) ;
}
Expand Down
8 changes: 2 additions & 6 deletions tests/lib/Simples/Request/DeleteTest.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
<?php

require_once(dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'bootstrap.php');

use PHPUnit\Framework\TestCase;

class Simples_Request_DeleteTest extends TestCase {
class Simples_Request_DeleteTest extends Simples_HttpTestCase {

public function testDelete() {
$client = new Simples_Transport_Http();
$client = $this->client;
$this->assertTrue($client->index(
array(
'content' => 'Pliz, pliz, delete me !'
Expand Down
11 changes: 4 additions & 7 deletions tests/lib/Simples/Request/DeleteTypeTest.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
<?php

require_once(dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'bootstrap.php');

use PHPUnit\Framework\TestCase;

class Simples_Request_DeleteTypeTest extends TestCase {
class Simples_Request_DeleteTypeTest extends Simples_HttpTestCase {

public $client ;

protected function setUp() : void {
$this->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() ;
}

Expand All @@ -31,5 +29,4 @@ protected function tearDown() : void {
$this->client->deleteIndex()->execute() ;
}
}

}
32 changes: 14 additions & 18 deletions tests/lib/Simples/Request/GetTest.php
Original file line number Diff line number Diff line change
@@ -1,33 +1,29 @@
<?php

require_once(dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'bootstrap.php');

use PHPUnit\Framework\TestCase;

class Simples_Request_GetTest extends TestCase {
class Simples_Request_GetTest extends Simples_HttpTestCase {

protected function setUp() : void
{
parent::setUp();
$this->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']) ;
Expand Down
16 changes: 5 additions & 11 deletions tests/lib/Simples/Request/IndexTest.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
<?php

require_once(dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'bootstrap.php');

use PHPUnit\Framework\TestCase;

class Simples_Request_IndexTest extends TestCase {
class Simples_Request_IndexTest extends Simples_HttpTestCase {

public $client ;

protected function setUp() : void {
$this->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'
Expand Down
15 changes: 5 additions & 10 deletions tests/lib/Simples/Request/MappingTest.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
<?php

require_once(dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'bootstrap.php');

use PHPUnit\Framework\TestCase;

class Simples_Request_MappingTest extends TestCase {
class Simples_Request_MappingTest extends Simples_HttpTestCase {

protected function setUp() : void {
$this->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() ;
}

Expand Down
3 changes: 3 additions & 0 deletions tests/lib/Simples/Request/Search/FacetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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') ;
Expand All @@ -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') ;
Expand All @@ -42,6 +44,7 @@ public function testIn() {
)
)
) ;
$this->assertEquals($expected, $res);
}

public function testMultipleFields() {
Expand Down
13 changes: 4 additions & 9 deletions tests/lib/Simples/Request/SearchTest.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
<?php

require_once(dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'bootstrap.php');

use PHPUnit\Framework\TestCase;

class Simples_Request_SearchTest extends TestCase {
class Simples_Request_SearchTest extends Simples_HttpTestCase {

public $client ;

protected function setUp() : void {
$this->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() ;

Expand Down
8 changes: 2 additions & 6 deletions tests/lib/Simples/Request/StatusTest.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
<?php
require_once(dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'bootstrap.php') ;

use PHPUnit\Framework\TestCase;

class Simples_Request_StatusTest extends TestCase {
class Simples_Request_StatusTest extends Simples_HttpTestCase {

public function testStatus() {
$client = new Simples_Transport_Http() ;
$results = $client->status()->execute() ;
$results = $this->client->status()->execute() ;
$this->assertEquals(true, $results->ok) ;
$this->assertTrue(isset($results->_shards->total)) ;
}
Expand Down
15 changes: 5 additions & 10 deletions tests/lib/Simples/Request/UpdateTest.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
<?php

require_once(dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'bootstrap.php');

use PHPUnit\Framework\TestCase;

class Simples_Request_UpdateTest extends TestCase {
class Simples_Request_UpdateTest extends Simples_HttpTestCase {

protected function setUp() : void {
$this->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() {
Expand Down
Loading

0 comments on commit 41dfc94

Please sign in to comment.