Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dolibarr#14295 use AbstractRestAPITest in all RestAPITest class #31330

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 4 additions & 71 deletions test/phpunit/RestAPIContactTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,7 @@
* \remarks To run this script as CLI: phpunit filename.php
*/

global $conf,$user,$langs,$db;
//define('TEST_DB_FORCE_TYPE','mysql'); // This is to force using mysql driver
//require_once 'PHPUnit/Autoload.php';
require_once dirname(__FILE__).'/../../htdocs/master.inc.php';
require_once dirname(__FILE__).'/../../htdocs/core/lib/date.lib.php';
require_once dirname(__FILE__).'/../../htdocs/core/lib/geturl.lib.php';
require_once dirname(__FILE__).'/CommonClassTest.class.php';

if (empty($user->id)) {
print "Load permissions for admin user nb 1\n";
$user->fetch(1);
$user->getrights();
}
$conf->global->MAIN_DISABLE_ALL_MAILS = 1;
$conf->global->MAIN_UMASK = '0666';

require_once __DIR__."/AbstractRestAPITest.php";

/**
* Class for PHPUnit tests
Expand All @@ -49,65 +34,11 @@
* @backupStaticAttributes enabled
* @remarks backupGlobals must be disabled to have db,conf,user and lang not erased.
*/
class RestAPIContactTest extends CommonClassTest
class RestAPIContactTest extends AbstractRestAPITest
{
protected $api_url;
protected $api_key;

/**
* setUpBeforeClass
*
* @return void
*/
public static function setUpBeforeClass(): void
{
global $conf,$user,$langs,$db;
$db->begin(); // This is to have all actions inside a transaction even if test launched without suite.

if (!isModEnabled('api')) {
print __METHOD__." module api must be enabled.\n";
die(1);
}

print __METHOD__."\n";
}

/**
* Init phpunit tests
*
* @return void
*/
protected function setUp(): void
{
global $conf,$user,$langs,$db;
$conf = $this->savconf;
$user = $this->savuser;
$langs = $this->savlangs;
$db = $this->savdb;

$this->api_url = DOL_MAIN_URL_ROOT.'/api/index.php';

$login = 'admin';
$password = 'admin';
$url = $this->api_url.'/login?login='.$login.'&password='.$password;
// Call the API login method to save api_key for this test class.
// At first call, if token is not defined a random value is generated and returned.
$result = getURLContent($url, 'GET', '', 1, array(), array('http', 'https'), 2);
print __METHOD__." result = ".var_export($result, true)."\n";
print __METHOD__." curl_error_no: ".$result['curl_error_no']."\n";
$this->assertEquals($result['curl_error_no'], '');
$object = json_decode($result['content'], true); // If success content is just an id, if not an array

$this->assertNotNull($object, "Parsing of json result must not be null");
$this->assertNotEquals(500, (empty($object['error']['code']) ? 0 : $object['error']['code']), 'Error'.(empty($object['error']['message']) ? '' : ' '.$object['error']['message']));
$this->assertEquals('200', $object['success']['code']);

$this->api_key = $object['success']['token'];

print __METHOD__." api_key: $this->api_key \n";
}


/**
* testRestGetContact
*
Expand Down Expand Up @@ -184,6 +115,8 @@ public function testRestCreateContact()

$body = json_encode($bodyobj);

var_export($body);

$result = getURLContent($url, 'POST', $body, 1, $addheaders, array('http', 'https'), 2);

$this->assertEquals($result['curl_error_no'], '');
Expand Down
72 changes: 3 additions & 69 deletions test/phpunit/RestAPIDocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,9 @@
* \brief PHPUnit test
* \remarks To run this script as CLI: phpunit filename.php.
*/
global $conf,$user,$langs,$db;
//define('TEST_DB_FORCE_TYPE','mysql'); // This is to force using mysql driver
//require_once 'PHPUnit/Autoload.php';
require_once dirname(__FILE__).'/../../htdocs/master.inc.php';
require_once dirname(__FILE__).'/../../htdocs/core/lib/date.lib.php';
require_once dirname(__FILE__).'/../../htdocs/core/lib/geturl.lib.php';
require_once dirname(__FILE__).'/../../htdocs/core/lib/files.lib.php';
require_once dirname(__FILE__).'/CommonClassTest.class.php';

if (empty($user->id)) {
echo "Load permissions for admin user nb 1\n";
$user->fetch(1);
$user->getrights();
}
$conf->global->MAIN_DISABLE_ALL_MAILS = 1;
$conf->global->MAIN_UMASK = '0666';
require_once __DIR__."/AbstractRestAPITest.php";
require_once dirname(__FILE__).'/../../htdocs/core/lib/files.lib.php';

/**
* Class for PHPUnit tests.
Expand All @@ -48,64 +35,11 @@
* @backupStaticAttributes enabled
* @remarks backupGlobals must be disabled to have db,conf,user and lang not erased.
*/
class RestAPIDocumentTest extends CommonClassTest
class RestAPIDocumentTest extends AbstractRestAPITest
{
protected $api_url;
protected $api_key;

/**
* setUpBeforeClass
*
* @return void
*/
public static function setUpBeforeClass(): void
{
global $conf,$user,$langs,$db;
$db->begin(); // This is to have all actions inside a transaction even if test launched without suite.

if (!isModEnabled('api')) {
print __METHOD__." module api must be enabled.\n";
die(1);
}

echo __METHOD__."\n";
}

/**
* Init phpunit tests.
*
* @return void
*/
protected function setUp(): void
{
global $conf,$user,$langs,$db;
$conf = $this->savconf;
$user = $this->savuser;
$langs = $this->savlangs;
$db = $this->savdb;

$this->api_url = DOL_MAIN_URL_ROOT.'/api/index.php';

$login = 'admin';
$password = 'admin';
$url = $this->api_url.'/login?login='.$login.'&password='.$password;
// Call the API login method to save api_key for this test class.
// At first call, if token is not defined a random value is generated and returned.
$result = getURLContent($url, 'GET', '', 1, array(), array('http', 'https'), 2);
print __METHOD__." result = ".var_export($result, true)."\n";
print __METHOD__." curl_error_no: ".$result['curl_error_no']."\n";
$this->assertEquals($result['curl_error_no'], '');
$object = json_decode($result['content'], true); // If success content is just an id, if not an array

$this->assertNotNull($object, "Parsing of json result must not be null");
$this->assertNotEquals(500, (empty($object['error']['code']) ? 0 : $object['error']['code']), 'Error'.(empty($object['error']['message']) ? '' : ' '.$object['error']['message']));
$this->assertEquals('200', $object['success']['code']);

$this->api_key = $object['success']['token'];

echo __METHOD__." api_key: $this->api_key \n";
}

/**
* testPushDocument.
*
Expand Down
Loading