Fix phpunit

This commit is contained in:
Laurent Destailleur
2023-08-25 13:58:27 +02:00
parent 519318953d
commit bcd3401e47
4 changed files with 48 additions and 62 deletions

View File

@@ -49,24 +49,24 @@ class Cpaiement
public $code; public $code;
/** /**
* @var int ID * @var int ID
*/ */
public $id; public $id;
/** /**
* @var string[] array of errors messages * @var string[] array of errors messages
*/ */
public $errors; public $errors;
/** /**
* @var string * @var string
* @deprecated * @deprecated
* @see $label * @see $label
*/ */
public $libelle; public $libelle;
/** /**
* @var string * @var string
*/ */
public $label; public $label;

View File

@@ -125,22 +125,9 @@ class RestAPIContactTest extends PHPUnit\Framework\TestCase
$this->api_url=DOL_MAIN_URL_ROOT.'/api/index.php'; $this->api_url=DOL_MAIN_URL_ROOT.'/api/index.php';
$login='admin'; $this->api_key = 'admin'; // Test on API to get this token is inside RestAPIUserTest.php
$password='adminadmin';
$url=$this->api_url.'/login?login='.$login.'&password='.$password;
// Call the API login method to save api_key for this test class
$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);
$this->assertNotNull($object, "Parsing of json result must not be null");
$this->assertEquals('200', $object['success']['code']);
$this->api_key = $object['success']['token'];
print __METHOD__." api_key: $this->api_key \n"; print __METHOD__." api_key: $this->api_key \n";
print __METHOD__."\n";
} }
/** /**
@@ -155,32 +142,32 @@ class RestAPIContactTest extends PHPUnit\Framework\TestCase
/** /**
* testRestGetUser * testRestGetContact
* *
* @return int * @return int
*/ */
public function testRestGetContact() public function testRestGetContact()
{ {
global $conf,$user,$langs,$db; global $conf,$user,$langs,$db;
//fetch Non-Existent contact //fetch Non-Existent contact
$url = $this->api_url.'/contacts/123456789?api_key='.$this->api_key; $url = $this->api_url.'/contacts/123456789?api_key='.$this->api_key;
//$addheaders=array('Content-Type: application/json'); //$addheaders=array('Content-Type: application/json');
print __METHOD__." Request GET url=".$url."\n"; print __METHOD__." Request GET url=".$url."\n";
$result=getURLContent($url, 'GET', '', 1, array(), array('http', 'https'), 2); $result = getURLContent($url, 'GET', '', 1, array(), array('http', 'https'), 2);
//print __METHOD__." result for get on unexisting user: ".var_export($result, true)."\n"; print __METHOD__." result for get on unexisting contact: ".var_export($result, true)."\n";
print __METHOD__." curl_error_no: ".$result['curl_error_no']."\n"; print __METHOD__." curl_error_no: ".$result['curl_error_no']."\n";
$this->assertEquals($result['curl_error_no'], ''); $this->assertEquals($result['curl_error_no'], '');
$object=json_decode($result['content'], true); $object=json_decode($result['content'], true);
$this->assertNotNull($object, "Parsing of json result must not be null"); $this->assertNotNull($object, "Parsing of json result must not be null");
$this->assertEquals(404, $object['error']['code'], 'Error code is not 404'); $this->assertEquals(404, $object['error']['code'], 'Error code is not 404');
//fetch an existent contact //fetch an existent contact
$url = $this->api_url.'/contacts/1?api_key='.$this->api_key; $url = $this->api_url.'/contacts/1?api_key='.$this->api_key;
print __METHOD__." Request GET url=".$url."\n"; print __METHOD__." Request GET url=".$url."\n";
$result=getURLContent($url, 'GET', '', 1, array(), array('http', 'https'), 2); $result=getURLContent($url, 'GET', '', 1, array(), array('http', 'https'), 2);
print __METHOD__." result for get on an existing user: ".var_export($result, true)."\n"; print __METHOD__." result for get on an existing contact: ".var_export($result, true)."\n";
print __METHOD__." curl_error_no: ".$result['curl_error_no']."\n"; print __METHOD__." curl_error_no: ".$result['curl_error_no']."\n";
$this->assertEquals($result['curl_error_no'], ''); $this->assertEquals($result['curl_error_no'], '');
$object=json_decode($result['content'], true); $object=json_decode($result['content'], true);
@@ -188,13 +175,21 @@ class RestAPIContactTest extends PHPUnit\Framework\TestCase
$this->assertEquals(1, $object['statut']); $this->assertEquals(1, $object['statut']);
} }
/**
* testRestCreateContact
*
* @return int
*
* @depends testRestGetContact
* The depends says test is run only if previous is ok
*/
public function testRestCreateContact() public function testRestCreateContact()
{ {
global $conf,$user,$langs,$db; global $conf,$user,$langs,$db;
// attempt to create without mandatory fields // attempt to create without mandatory fields
$url = $this->api_url.'/contacts?api_key='.$this->api_key; $url = $this->api_url.'/contacts?api_key='.$this->api_key;
$addheaders=array('Content-Type: application/json'); $addheaders=array('Content-Type: application/json');
$bodyobj= array( $bodyobj= array(
"firstname" => "firstname" "firstname" => "firstname"
); );
@@ -217,9 +212,9 @@ class RestAPIContactTest extends PHPUnit\Framework\TestCase
"firstname" => "testRestContact" . mt_rand(), "firstname" => "testRestContact" . mt_rand(),
"lastname" => "testRestContact", "lastname" => "testRestContact",
); );
$body = json_encode($bodyobj); $body = json_encode($bodyobj);
$result = getURLContent($url, 'POST', $body, 1, $addheaders, array('http', 'https'), 2); $result = getURLContent($url, 'POST', $body, 1, $addheaders, array('http', 'https'), 2);
$this->assertEquals($result['curl_error_no'], ''); $this->assertEquals($result['curl_error_no'], '');
@@ -231,36 +226,36 @@ class RestAPIContactTest extends PHPUnit\Framework\TestCase
//update the contact //update the contact
// Update the firstname of the contact // Update the firstname of the contact
/* /*
$updateBody = array( $updateBody = array(
"firstname" => "UpdatedFirstName", "firstname" => "UpdatedFirstName",
); );
$updateRequestBody = json_encode($updateBody); $updateRequestBody = json_encode($updateBody);
$updateUrl = $this->api_url . '/contacts/' . $resid. '?api_key=' . $this->api_key; $updateUrl = $this->api_url . '/contacts/' . $resid. '?api_key=' . $this->api_key;
$updateResult = getURLContent($updateUrl, 'PUT', $updateRequestBody, 1, $addheaders, array('http', 'https'), 2); $updateResult = getURLContent($updateUrl, 'PUT', $updateRequestBody, 1, $addheaders, array('http', 'https'), 2);
$this->assertEquals($updateResult['curl_error_no'], ''); $this->assertEquals($updateResult['curl_error_no'], '');
$updateResponse = json_decode($updateResult['content'], true); $updateResponse = json_decode($updateResult['content'], true);
$this->assertNotNull($updateResponse, "Parsing of JSON result must not be null"); $this->assertNotNull($updateResponse, "Parsing of JSON result must not be null");
print_r($updateResponse); print_r($updateResponse);
// Check if the updated fields match the changes you made // Check if the updated fields match the changes you made
if ($updateResponse['firstname'] === $updateBody['firstname']) { if ($updateResponse['firstname'] === $updateBody['firstname']) {
// Update was successful // Update was successful
$this->assertTrue(true); $this->assertTrue(true);
} else { } else {
// Update might have failed // Update might have failed
$this->assertTrue(false, "Update might have failed"); $this->assertTrue(false, "Update might have failed");
} }
// Deleting the Contact // Deleting the Contact
$deleteUrl = $this->api_url . '/contacts/' . $resid . '?api_key=' . $this->api_key; $deleteUrl = $this->api_url . '/contacts/' . $resid . '?api_key=' . $this->api_key;
$deleteResult = getURLContent($deleteUrl, 'DELETE', '', 1, $addheaders, array('http', 'https'), 2); $deleteResult = getURLContent($deleteUrl, 'DELETE', '', 1, $addheaders, array('http', 'https'), 2);
$this->assertEquals($deleteResult['curl_error_no'], ''); $this->assertEquals($deleteResult['curl_error_no'], '');
$deleteResponse = json_decode($deleteResult['content'], true); $deleteResponse = json_decode($deleteResult['content'], true);
@@ -269,14 +264,15 @@ class RestAPIContactTest extends PHPUnit\Framework\TestCase
// Update Non-Existent Contact // Update Non-Existent Contact
// Delete Non-Existent Contact
// Delete Non-Existent Contact
*/ */
return 0;
} }
}
}

View File

@@ -118,22 +118,9 @@ class RestAPIDocumentTest extends PHPUnit\Framework\TestCase
$this->api_url = DOL_MAIN_URL_ROOT.'/api/index.php'; $this->api_url = DOL_MAIN_URL_ROOT.'/api/index.php';
$login = 'admin'; $this->api_key = 'admin'; // Test on API to get this token is inside RestAPIUserTest.php
$password = 'admin';
$url = $this->api_url.'/login?login='.$login.'&password='.$password;
// Call the API login method to save api_key for this test class
$result = getURLContent($url, 'GET', '', 1, array(), array('http', 'https'), 2);
echo __METHOD__.' result = '.var_export($result, true)."\n";
echo __METHOD__.' curl_error_no: '.$result['curl_error_no']."\n";
$this->assertEquals($result['curl_error_no'], '');
$object = json_decode($result['content'], true);
$this->assertNotNull($object, 'Parsing of json result must no be null');
$this->assertEquals('200', $object['success']['code']);
$this->api_key = $object['success']['token'];
echo __METHOD__." api_key: $this->api_key \n"; echo __METHOD__." api_key: $this->api_key \n";
echo __METHOD__."\n";
} }
/** /**

View File

@@ -139,8 +139,6 @@ class RestAPIUserTest extends PHPUnit\Framework\TestCase
$this->api_key = $object['success']['token']; $this->api_key = $object['success']['token'];
print __METHOD__." api_key: $this->api_key \n"; print __METHOD__." api_key: $this->api_key \n";
print __METHOD__."\n";
} }
/** /**
@@ -185,12 +183,17 @@ class RestAPIUserTest extends PHPUnit\Framework\TestCase
$object=json_decode($result['content'], true); $object=json_decode($result['content'], true);
$this->assertNotNull($object, "Parsing of json result must not be null"); $this->assertNotNull($object, "Parsing of json result must not be null");
$this->assertEquals(1, $object['statut']); $this->assertEquals(1, $object['statut']);
return $object['id'];
} }
/** /**
* testRestCreateUser * testRestCreateUser
* *
* @return void * @return void
*
* @depends testRestGetUser
* The depends says test is run only if previous is ok
*/ */
public function testRestCreateUser() public function testRestCreateUser()
{ {