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

N°5139 - Let data that is null also be null at the other side-fix #52

Open
wants to merge 1 commit into
base: master
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
2 changes: 1 addition & 1 deletion core/collector.class.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ protected function AddRow($aRow)
{
$aData = array();
foreach ($this->aCSVHeaders as $sHeader) {
if (is_null($aRow[$sHeader]) && $this->AttributeIsNullified($sHeader)) {
if (strlen($aRow[$sHeader])==0 && $this->AttributeIsNullified($sHeader)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (strlen($aRow[$sHeader])==0 && $this->AttributeIsNullified($sHeader)) {
if (strlen($aRow[$sHeader] ?? '') === 0 && $this->AttributeIsNullified($sHeader)) {

Otherwise in case a null value (as it seem it can be possible) it will trigger a warning with PHP 8.

$aData[] = NULL_VALUE;
} else {
$aData[] = $aRow[$sHeader];
Expand Down
18 changes: 11 additions & 7 deletions test/CollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,19 @@ public function testAttributeIsNullified($sCollectorXmlSubSection, $bExpectedIsN

$this->assertEquals($bExpectedIsNullified, $oCollector->AttributeIsNullified('phone'));

$oCollector->Collect(1);
$oCollector->Collect();
$sExpectedCsv = <<<CSV
primary_key;first_name;name;org_id;phone;mobile_phone;employee_number;email;function
1;isaac;asimov;Demo;;123456;9998877665544;[email protected];writer
1;isaac;asimov_null;Demo;;123456;9998877665544;[email protected];writer
2;isaac;asimov_empty;Demo;;123456;9998877665544;[email protected];writer
3;isaac;asimov_notempty;Demo;"not empty";123456;9998877665544;[email protected];writer

CSV;
$sExpectedNullifiedCsv = <<<CSV
primary_key;first_name;name;org_id;phone;mobile_phone;employee_number;email;function
1;isaac;asimov;Demo;<NULL>;123456;9998877665544;[email protected];writer
1;isaac;asimov_null;Demo;<NULL>;123456;9998877665544;[email protected];writer
2;isaac;asimov_empty;Demo;<NULL>;123456;9998877665544;[email protected];writer
3;isaac;asimov_notempty;Demo;"not empty";123456;9998877665544;[email protected];writer

CSV;

Expand Down Expand Up @@ -158,12 +162,12 @@ public function testUpdateSDSAttributes($aExpectedAttrDef, $aSynchroAttrDef, boo
$oCollector = new iTopPersonCollector();
$oMockClient = $this->CreateMock('RestClient');
$oMockClient->expects($this->exactly($bWillUpdate ? 1 : 0))->method("Update")->willReturn(['code' => 0]);

$bRet = $this->InvokeNonPublicMethod(get_class($oCollector), 'UpdateSDSAttributes', $oCollector, [$aExpectedAttrDef, $aSynchroAttrDef, '', $oMockClient]);

$this->assertTrue($bRet);
}

public function providerUpdateSDSAttributes()
{
return [
Expand Down Expand Up @@ -271,7 +275,7 @@ public function InvokeNonPublicMethod($sObjectClass, $sMethodName, $oObject, $aA
$class = new \ReflectionClass($sObjectClass);
$method = $class->getMethod($sMethodName);
$method->setAccessible(true);

return $method->invokeArgs($oObject, $aArgs);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,62 @@

class iTopPersonCollector extends Collector
{
private $bFetched;
private $i;
private $aCurrentData;

protected function Fetch()
{
if (! $this->bFetched) {
$this->bFetched = true;
return [
'primary_key' => 1,
'first_name' => "isaac",
'name' => "asimov",
'org_id' => "Demo",
'phone' => null,
'mobile_phone' => "123456",
'employee_number' => "9998877665544",
'email' => "[email protected]",
'function' => "writer",
'Status' => "Active",
if (is_null($this->aCurrentData)){
$this->i = 0;
$this->aCurrentData = [
[
'primary_key' => 1,
'first_name' => "isaac",
'name' => "asimov_null",
'org_id' => "Demo",
'phone' => null,
'mobile_phone' => "123456",
'employee_number' => "9998877665544",
'email' => "[email protected]",
'function' => "writer",
'Status' => "Active",
],
[
'primary_key' => 2,
'first_name' => "isaac",
'name' => "asimov_empty",
'org_id' => "Demo",
'phone' => "",
'mobile_phone' => "123456",
'employee_number' => "9998877665544",
'email' => "[email protected]",
'function' => "writer",
'Status' => "Active",
],
[
'primary_key' => 3,
'first_name' => "isaac",
'name' => "asimov_notempty",
'org_id' => "Demo",
'phone' => "not empty",
'mobile_phone' => "123456",
'employee_number' => "9998877665544",
'email' => "[email protected]",
'function' => "writer",
'Status' => "Active",
],
];
}

return null;
$res = null;
if ($this->i < count($this->aCurrentData)){
$res=$this->aCurrentData[$this->i];
$this->i++;
}

return $res;
}

/**
* {@inheritDoc}
* @see Collector::AttributeIsOptional()
Expand Down
4 changes: 2 additions & 2 deletions test/single_json/nullified_json_1/expected_generated.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
primary_key;name;status;first_name;email;phone;org_id;function
1;"My last name";active;"My first name";[email protected];"+00 000 000 000";1;<NULL>
2;Picasso;active;Pablo;[email protected];;3;
3;Dali;active;Salvador;[email protected];;3;
2;Picasso;active;Pablo;[email protected];;3;<NULL>
3;Dali;active;Salvador;[email protected];;3;<NULL>