Skip to content

Commit

Permalink
Merge pull request #18 from veewee/object-null-values
Browse files Browse the repository at this point in the history
Accept nillable values in object encoder
  • Loading branch information
veewee authored Jul 24, 2024
2 parents 39e438c + a737a26 commit a9610b6
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/Encoder/ObjectEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ static function (string $normalizePropertyName, Property $property) use ($object
$type,
$iso->to($value)
))(...) : $defaultAction,
$property->getName() === '_' => $value
$property->getName() === '_' => $value !== null
? buildValue($iso->to($value))
: (new NilAttributeBuilder())(...),
default => $value ? raw($iso->to($value)) : $defaultAction
default => raw($iso->to($value))
};
}
)
Expand Down
54 changes: 54 additions & 0 deletions tests/PhpCompatibility/Implied/ImpliedSchema001Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
declare(strict_types=1);

namespace Soap\Encoding\Test\PhpCompatibility\Implied;

use PHPUnit\Framework\Attributes\CoversClass;
use Soap\Encoding\Decoder;
use Soap\Encoding\Driver;
use Soap\Encoding\Encoder;
use Soap\Encoding\Test\PhpCompatibility\AbstractCompatibilityTests;

#[CoversClass(Driver::class)]
#[CoversClass(Encoder::class)]
#[CoversClass(Decoder::class)]
final class ImpliedSchema001Test extends AbstractCompatibilityTests
{
protected string $schema = <<<EOXML
<element name="testType">
<complexType>
<sequence>
<element name="customerId" type="xsd:string" />
<element name="countryCode" type="xsd:string" nillable="true" />
</sequence>
</complexType>
</element>
EOXML;
protected string $type = 'type="tns:testType"';

protected function calculateParam(): mixed
{
return (object)[
'customerId' => '123',
'countryCode' => null,
];
}

protected function expectXml(): string
{
return <<<XML
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="http://test-uri/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<tns:test>
<testParam xsi:type="tns:testType">
<customerId xsi:type="xsd:string">123</customerId>
<countryCode xsi:nil="true" />
</testParam>
</tns:test>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
XML;
}
}
54 changes: 54 additions & 0 deletions tests/PhpCompatibility/Implied/ImpliedSchema002Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
declare(strict_types=1);

namespace Soap\Encoding\Test\PhpCompatibility\Implied;

use PHPUnit\Framework\Attributes\CoversClass;
use Soap\Encoding\Decoder;
use Soap\Encoding\Driver;
use Soap\Encoding\Encoder;
use Soap\Encoding\Test\PhpCompatibility\AbstractCompatibilityTests;

#[CoversClass(Driver::class)]
#[CoversClass(Encoder::class)]
#[CoversClass(Decoder::class)]
final class ImpliedSchema002Test extends AbstractCompatibilityTests
{
protected string $schema = <<<EOXML
<element name="testType">
<complexType>
<sequence>
<element name="customerId" type="xsd:string" />
<element name="countryCode" type="xsd:string" nillable="true" />
</sequence>
</complexType>
</element>
EOXML;
protected string $type = 'type="tns:testType"';

protected function calculateParam(): mixed
{
return (object)[
'customerId' => '123',
'countryCode' => 'BE',
];
}

protected function expectXml(): string
{
return <<<XML
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="http://test-uri/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<tns:test>
<testParam xsi:type="tns:testType">
<customerId xsi:type="xsd:string">123</customerId>
<countryCode xsi:type="xsd:string">BE</countryCode>
</testParam>
</tns:test>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
XML;
}
}
53 changes: 53 additions & 0 deletions tests/PhpCompatibility/Implied/ImpliedSchema003Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
declare(strict_types=1);

namespace Soap\Encoding\Test\PhpCompatibility\Implied;

use PHPUnit\Framework\Attributes\CoversClass;
use Soap\Encoding\Decoder;
use Soap\Encoding\Driver;
use Soap\Encoding\Encoder;
use Soap\Encoding\Test\PhpCompatibility\AbstractCompatibilityTests;

#[CoversClass(Driver::class)]
#[CoversClass(Encoder::class)]
#[CoversClass(Decoder::class)]
final class ImpliedSchema003Test extends AbstractCompatibilityTests
{
protected string $schema = <<<EOXML
<element name="testType">
<complexType>
<sequence>
<element name="customerId" type="xsd:string" />
<element name="countryCode" type="xsd:string" minOccurs="0" maxOccurs="1" />
</sequence>
</complexType>
</element>
EOXML;
protected string $type = 'type="tns:testType"';

protected function calculateParam(): mixed
{
return (object)[
'customerId' => '123',
'countryCode' => null,
];
}

protected function expectXml(): string
{
return <<<XML
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="http://test-uri/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<tns:test>
<testParam xsi:type="tns:testType">
<customerId xsi:type="xsd:string">123</customerId>
</testParam>
</tns:test>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
XML;
}
}
10 changes: 2 additions & 8 deletions tests/PhpCompatibility/Schema031Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ protected function calculateParam(): mixed
{
return (object)[
"int" => 123,
"str" => "str",
];
}

Expand All @@ -40,18 +41,11 @@ protected function expectXml(): string
<tns:test>
<testParam xsi:type="tns:testType">
<int xsi:type="xsd:int">123</int>
<str xsi:type="xsd:string">str</str>
</testParam>
</tns:test>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
XML;
}

protected function expectDecoded(): mixed
{
return (object)[
"int" => 123,
"str" => null,
];
}
}
15 changes: 5 additions & 10 deletions tests/PhpCompatibility/Schema049Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ final class Schema049Test extends AbstractCompatibilityTests
<complexContent>
<restriction base="tns:testType2">
<sequence>
<element name="int" type="int"/>
<element name="int2" type="int"/>
</sequence>
</restriction>
Expand All @@ -35,7 +36,8 @@ final class Schema049Test extends AbstractCompatibilityTests
protected function calculateParam(): mixed
{
return (object)[
"int2" => 123,
"int" => 123,
"int2" => 456,
];
}

Expand All @@ -48,19 +50,12 @@ protected function expectXml(): string
<SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<tns:test>
<testParam xsi:type="tns:testType">
<int2 xsi:type="xsd:int">123</int2>
<int xsi:type="xsd:int">123</int>
<int2 xsi:type="xsd:int">456</int2>
</testParam>
</tns:test>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
XML;
}

protected function expectDecoded(): mixed
{
return (object)[
"int2" => 123,
"int" => null,
];
}
}

0 comments on commit a9610b6

Please sign in to comment.