Skip to content

Commit

Permalink
[PHP] ObjectSerializerTest: add tests for ObjectSerializer::deseriali…
Browse files Browse the repository at this point in the history
…ze() associative arrays bugfix (#12989)

* [PHP] ObjectSerializerTest.php: code cleanup

* [PHP] ObjectSerializerTest.php: added tests for ::deserialize() associative arrays bugfix - related to PR #12849
  • Loading branch information
thomasphansen authored Jul 23, 2022
1 parent 6c9cd65 commit c0ad635
Showing 1 changed file with 90 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,53 @@

namespace OpenAPI\Client;

use DateTime;
use GuzzleHttp\Psr7\Utils;
use OpenAPI\Client\Model\Pet;
use OpenAPI\Client\Model\Tag;
use PHPUnit\Framework\TestCase;
use SplFileObject;

// test object serializer
/**
* class ObjectSerializerTest
*
* @package OpenAPI\Client
*/
class ObjectSerializerTest extends TestCase
{
// test sanitizeFilename
public function testSanitizeFilename()
/**
* Test sanitizeFilename
*
* @covers ObjectSerializer::sanitizeFilename
*/
public function testSanitizeFilename(): void
{
// initialize the API client
$s = new ObjectSerializer();

$this->assertSame("sun.gif", $s->sanitizeFilename("sun.gif"));
$this->assertSame("sun.gif", $s->sanitizeFilename("../sun.gif"));
$this->assertSame("sun.gif", $s->sanitizeFilename("/var/tmp/sun.gif"));
$this->assertSame("sun.gif", $s->sanitizeFilename("./sun.gif"));
$this->assertSame("sun.gif", ObjectSerializer::sanitizeFilename("sun.gif"));
$this->assertSame("sun.gif", ObjectSerializer::sanitizeFilename("../sun.gif"));
$this->assertSame("sun.gif", ObjectSerializer::sanitizeFilename("/var/tmp/sun.gif"));
$this->assertSame("sun.gif", ObjectSerializer::sanitizeFilename("./sun.gif"));

$this->assertSame("sun", $s->sanitizeFilename("sun"));
$this->assertSame("sun.gif", $s->sanitizeFilename("..\sun.gif"));
$this->assertSame("sun.gif", $s->sanitizeFilename("\var\tmp\sun.gif"));
$this->assertSame("sun.gif", $s->sanitizeFilename("c:\var\tmp\sun.gif"));
$this->assertSame("sun.gif", $s->sanitizeFilename(".\sun.gif"));
$this->assertSame("sun", ObjectSerializer::sanitizeFilename("sun"));
$this->assertSame("sun.gif", ObjectSerializer::sanitizeFilename("..\sun.gif"));
$this->assertSame("sun.gif", ObjectSerializer::sanitizeFilename("\var\tmp\sun.gif"));
$this->assertSame("sun.gif", ObjectSerializer::sanitizeFilename("c:\var\tmp\sun.gif"));
$this->assertSame("sun.gif", ObjectSerializer::sanitizeFilename(".\sun.gif"));
}

/**
* Test SplFileObject class deserialization.
*
* @see https://github.com/OpenAPITools/openapi-generator/pull/11184
* @covers ObjectSerializer::serialize
* @covers ObjectSerializer::deserialize
* @dataProvider provideFileStreams
*/
public function testDeserializeFile($stream, ?array $httpHeaders = null, ?string $expectedFilename = null): void
{
$s = new ObjectSerializer();

/** @var \SplFileObject */
$file = $s->deserialize($stream, '\SplFileObject', $httpHeaders);
$this->assertInstanceOf(\SplFileObject::class, $file);
/** @var SplFileObject $file */
$file = ObjectSerializer::deserialize($stream, '\SplFileObject', $httpHeaders);
$this->assertInstanceOf(SplFileObject::class, $file);

if (is_string($expectedFilename)) {
$this->assertEquals($expectedFilename, $file->getFilename());
Expand All @@ -48,7 +57,11 @@ public function testDeserializeFile($stream, ?array $httpHeaders = null, ?string
}
}

public function provideFileStreams()
/**
* File Streams Provider
* @return array[]
*/
public function provideFileStreams(): array
{
return [
'File stream without headers' => [
Expand Down Expand Up @@ -90,9 +103,14 @@ public function testSanitizeTimestamp(string $timestamp, string $expected): void
public function testDateTimeParseSecondAccuracy(string $timestamp, string $expected): void
{
$dateTime = ObjectSerializer::deserialize($timestamp, '\DateTime');
$this->assertEquals(new \DateTime($expected), $dateTime);
$this->assertEquals(new DateTime($expected), $dateTime);
}

/**
* Timestamps provider
*
* @return string[][]
*/
public function provideTimestamps(): array
{
return [
Expand Down Expand Up @@ -149,6 +167,11 @@ public function testToQueryValue(
$this->assertEquals($expected, $query);
}

/**
* Query params provider
*
* @return array[]
*/
public function provideQueryParams(): array
{
$array = ['blue', 'black', 'brown'];
Expand Down Expand Up @@ -321,6 +344,11 @@ public function testDeepObjectStyleQueryParam(
$this->assertEquals($expected, $result);
}

/**
* Deep Objects provider
*
* @return array
*/
public function provideDeepObjects(): array
{
return [
Expand Down Expand Up @@ -360,6 +388,11 @@ public function testToString($input, string $expected): void
$this->assertSame($expected, $result);
}

/**
* toString() input provider
*
* @return array
*/
public function provideToStringInput(): array
{
return [
Expand All @@ -384,11 +417,12 @@ public function provideToStringInput(): array
'expected' => 'some string',
],
'a date' => [
'input' => new \DateTime('14-04-2022'),
'input' => new DateTime('14-04-2022'),
'expected' => '2022-04-14T00:00:00+00:00',
],
];
}

/**
* @covers ObjectSerializer::toQueryValue
* @dataProvider provideQueryParamsWithStringBooleanFormatForQueryString
Expand All @@ -412,6 +446,11 @@ public function testToQueryValueWithStringBooleanFormatForQueryString(
$this->assertEquals($expected, $query);
}

/**
* Query Params with string boolean format provider
*
* @return array[]
*/
public function provideQueryParamsWithStringBooleanFormatForQueryString(): array
{
return [
Expand All @@ -431,4 +470,33 @@ public function provideQueryParamsWithStringBooleanFormatForQueryString(): array
],
];
}

/**
* Test array to class deserialization.
*
* @covers ObjectSerializer::deserialize
*
* @see https://github.com/OpenAPITools/openapi-generator/pull/12849#issuecomment-1186130098
*/
public function testArrayGivenAsObjectForDeserialize(): void
{
$data = [
'name' => 'Pet Name',
'status' => Pet::STATUS_AVAILABLE,
'tags' => [
['name' => 'Tag Name'],
]
];

/** @var Pet $pet */
$pet = ObjectSerializer::deserialize($data, Pet::class);
$this->assertEquals('Pet Name', $pet->getName());
$this->assertEquals(Pet::STATUS_AVAILABLE, $pet->getStatus());

$tags = $pet->getTags();
$this->assertIsArray($tags);

$tag = $tags[0];
$this->assertInstanceOf(Tag::class, $tag);
}
}

0 comments on commit c0ad635

Please sign in to comment.