From 8cc66f7c75b46a9f3fdfc2849acf3cd4f42b6723 Mon Sep 17 00:00:00 2001 From: odain Date: Wed, 15 May 2024 15:25:56 +0200 Subject: [PATCH] =?UTF-8?q?N=C2=B05139=20-=20Let=20data=20that=20is=20null?= =?UTF-8?q?=20also=20be=20null=20at=20the=20other=20side-fix?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/collector.class.inc.php | 2 +- test/CollectorTest.php | 18 +++-- .../iTopPersonCollector.class.inc.php | 65 ++++++++++++++----- .../nullified_json_1/expected_generated.csv | 4 +- 4 files changed, 63 insertions(+), 26 deletions(-) diff --git a/core/collector.class.inc.php b/core/collector.class.inc.php index 3b4d624..8d4acab 100644 --- a/core/collector.class.inc.php +++ b/core/collector.class.inc.php @@ -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)) { $aData[] = NULL_VALUE; } else { $aData[] = $aRow[$sHeader]; diff --git a/test/CollectorTest.php b/test/CollectorTest.php index 67004b5..49a72c2 100644 --- a/test/CollectorTest.php +++ b/test/CollectorTest.php @@ -109,15 +109,19 @@ public function testAttributeIsNullified($sCollectorXmlSubSection, $bExpectedIsN $this->assertEquals($bExpectedIsNullified, $oCollector->AttributeIsNullified('phone')); - $oCollector->Collect(1); + $oCollector->Collect(); $sExpectedCsv = <<;123456;9998877665544;issac.asimov@function.io;writer +1;isaac;asimov_null;Demo;;123456;9998877665544;issac.asimov@function.io;writer +2;isaac;asimov_empty;Demo;;123456;9998877665544;issac.asimov@function.io;writer +3;isaac;asimov_notempty;Demo;"not empty";123456;9998877665544;issac.asimov@function.io;writer CSV; @@ -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 [ @@ -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); } } diff --git a/test/collector/attribute_isnullified/iTopPersonCollector.class.inc.php b/test/collector/attribute_isnullified/iTopPersonCollector.class.inc.php index eb87c65..481ed39 100644 --- a/test/collector/attribute_isnullified/iTopPersonCollector.class.inc.php +++ b/test/collector/attribute_isnullified/iTopPersonCollector.class.inc.php @@ -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' => "issac.asimov@function.io", - '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' => "issac.asimov@function.io", + 'function' => "writer", + 'Status' => "Active", + ], + [ + 'primary_key' => 2, + 'first_name' => "isaac", + 'name' => "asimov_empty", + 'org_id' => "Demo", + 'phone' => "", + 'mobile_phone' => "123456", + 'employee_number' => "9998877665544", + 'email' => "issac.asimov@function.io", + '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' => "issac.asimov@function.io", + '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() diff --git a/test/single_json/nullified_json_1/expected_generated.csv b/test/single_json/nullified_json_1/expected_generated.csv index 3bab6b3..468643d 100644 --- a/test/single_json/nullified_json_1/expected_generated.csv +++ b/test/single_json/nullified_json_1/expected_generated.csv @@ -1,4 +1,4 @@ primary_key;name;status;first_name;email;phone;org_id;function 1;"My last name";active;"My first name";my.email@foo.org;"+00 000 000 000";1; -2;Picasso;active;Pablo;pablo@demo.com;;3; -3;Dali;active;Salvador;dali@demo.com;;3; +2;Picasso;active;Pablo;pablo@demo.com;;3; +3;Dali;active;Salvador;dali@demo.com;;3;