From 387f99b82f3ae31b77bf32351ed2423d43fafaf8 Mon Sep 17 00:00:00 2001 From: silaskenneth Date: Mon, 18 Mar 2024 16:25:04 +0300 Subject: [PATCH 01/11] Fix coverage reporting workflow to only run in php8.0 --- .github/workflows/pr-validation.yml | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/.github/workflows/pr-validation.yml b/.github/workflows/pr-validation.yml index 329192d..b63ed09 100644 --- a/.github/workflows/pr-validation.yml +++ b/.github/workflows/pr-validation.yml @@ -16,25 +16,42 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - php-versions: ['7.4', '8.0', '8.1', '8.2'] + php-versions: ['7.4', '8.1', '8.2', '8.3'] steps: - name: Checkout - uses: actions/checkout@v4.1.2 - - name: Setup PHP and Xdebug for Code Coverage report + uses: actions/checkout@v4.1.1 + - name: Setup PHP uses: shivammathur/setup-php@v2 with: php-version: ${{ matrix.php-versions }} - coverage: xdebug + coverage: none - name: Install dependencies run: composer install - name: Run static analysis run: ./vendor/bin/phpstan - name: Run tests + run: ./vendor/bin/phpunit + + code-coverage: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + - name: Setup PHP and Xdebug for Code Coverage report + uses: shivammathur/setup-php@v2 + with: + php-version: '8.0' + coverage: xdebug + - name: Install dependencies + run: composer install + - name: Run static analysis + run: ./vendor/bin/phpstan + - name: Run tests with coverage run: ./vendor/bin/phpunit --coverage-clover=coverage.xml - name: Fix code coverage paths run: sed -i 's@'$GITHUB_WORKSPACE'@/github/workspace/@g' coverage.xml - name: SonarCloud Scan - if: ${{ matrix.php-versions == '8.0' && !github.event.pull_request.head.repo.fork }} + if: ${{ !github.event.pull_request.head.repo.fork }} uses: SonarSource/sonarcloud-github-action@master env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} From 2597fc9803d449636212231b8e4b099b5de34ea2 Mon Sep 17 00:00:00 2001 From: silaskenneth Date: Tue, 19 Mar 2024 12:10:36 +0300 Subject: [PATCH 02/11] Add PHP 8.0 to pipeline. --- .github/workflows/pr-validation.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pr-validation.yml b/.github/workflows/pr-validation.yml index b63ed09..82cef73 100644 --- a/.github/workflows/pr-validation.yml +++ b/.github/workflows/pr-validation.yml @@ -16,7 +16,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - php-versions: ['7.4', '8.1', '8.2', '8.3'] + php-versions: ['7.4', '8.0', '8.1', '8.2', '8.3'] steps: - name: Checkout uses: actions/checkout@v4.1.1 From 988e4b98261dad004995d1e537737e59a213a6c9 Mon Sep 17 00:00:00 2001 From: silaskenneth Date: Tue, 19 Mar 2024 12:12:52 +0300 Subject: [PATCH 03/11] Remove PHP 8.0 from matrix since it is already covered in coverage --- .github/workflows/pr-validation.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pr-validation.yml b/.github/workflows/pr-validation.yml index 82cef73..b63ed09 100644 --- a/.github/workflows/pr-validation.yml +++ b/.github/workflows/pr-validation.yml @@ -16,7 +16,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - php-versions: ['7.4', '8.0', '8.1', '8.2', '8.3'] + php-versions: ['7.4', '8.1', '8.2', '8.3'] steps: - name: Checkout uses: actions/checkout@v4.1.1 From f17bd6cb69e5b4a47e9fa348605a3a5d221cc030 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Fri, 22 Mar 2024 01:14:49 +0000 Subject: [PATCH 04/11] Bump dependabot/fetch-metadata from 1.6.0 to 2.0.0 Bumps [dependabot/fetch-metadata](https://github.com/dependabot/fetch-metadata) from 1.6.0 to 2.0.0. - [Release notes](https://github.com/dependabot/fetch-metadata/releases) - [Commits](https://github.com/dependabot/fetch-metadata/compare/v1.6.0...v2.0.0) --- updated-dependencies: - dependency-name: dependabot/fetch-metadata dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] --- .github/workflows/auto-merge-dependabot.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/auto-merge-dependabot.yml b/.github/workflows/auto-merge-dependabot.yml index 6e5953f..145487d 100644 --- a/.github/workflows/auto-merge-dependabot.yml +++ b/.github/workflows/auto-merge-dependabot.yml @@ -19,7 +19,7 @@ jobs: steps: - name: Dependabot metadata id: metadata - uses: dependabot/fetch-metadata@v1.6.0 + uses: dependabot/fetch-metadata@v2.0.0 with: github-token: "${{ secrets.GITHUB_TOKEN }}" From 750c452218d2401179d8f0fb7a30d326fb3dbb1e Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 25 Apr 2024 01:46:36 +0000 Subject: [PATCH 05/11] Bump dependabot/fetch-metadata from 2.0.0 to 2.1.0 Bumps [dependabot/fetch-metadata](https://github.com/dependabot/fetch-metadata) from 2.0.0 to 2.1.0. - [Release notes](https://github.com/dependabot/fetch-metadata/releases) - [Commits](https://github.com/dependabot/fetch-metadata/compare/v2.0.0...v2.1.0) --- updated-dependencies: - dependency-name: dependabot/fetch-metadata dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] --- .github/workflows/auto-merge-dependabot.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/auto-merge-dependabot.yml b/.github/workflows/auto-merge-dependabot.yml index 145487d..72d96dd 100644 --- a/.github/workflows/auto-merge-dependabot.yml +++ b/.github/workflows/auto-merge-dependabot.yml @@ -19,7 +19,7 @@ jobs: steps: - name: Dependabot metadata id: metadata - uses: dependabot/fetch-metadata@v2.0.0 + uses: dependabot/fetch-metadata@v2.1.0 with: github-token: "${{ secrets.GITHUB_TOKEN }}" From 62b694f2e8b4329b8f2927b8551167efe97a5749 Mon Sep 17 00:00:00 2001 From: silaskenneth Date: Mon, 29 Apr 2024 16:06:11 +0300 Subject: [PATCH 06/11] Make sure negative date periods are correctly handled. --- src/JsonParseNode.php | 16 +++++++++++++++- src/JsonSerializationWriter.php | 8 +++++++- tests/JsonParseNodeTest.php | 14 +++++++++++++- tests/JsonSerializationWriterTest.php | 12 +++++++++++- 4 files changed, 46 insertions(+), 4 deletions(-) diff --git a/src/JsonParseNode.php b/src/JsonParseNode.php index 87cbcfe..b98b489 100644 --- a/src/JsonParseNode.php +++ b/src/JsonParseNode.php @@ -282,7 +282,21 @@ public function getDateTimeValue(): ?DateTime { * @throws Exception */ public function getDateIntervalValue(): ?DateInterval{ - return ($this->jsonNode !== null) ? new DateInterval(strval($this->jsonNode)) : null; + if ($this->jsonNode === null){ + return null; + } + $originalValue = strval($this->jsonNode); + $negativeValPosition = strpos($originalValue, '-'); + $invert = 0; + $str = $originalValue; + if ($negativeValPosition !== false && $negativeValPosition === 0) { + // Invert the interval + $invert = 1; + $str = substr($originalValue, 1); + } + $dateInterval = new DateInterval($str); + $dateInterval->invert = $invert; + return $dateInterval; } public function getBinaryContent(): ?StreamInterface { diff --git a/src/JsonSerializationWriter.php b/src/JsonSerializationWriter.php index aa845b1..10d3923 100644 --- a/src/JsonSerializationWriter.php +++ b/src/JsonSerializationWriter.php @@ -452,7 +452,13 @@ public function writeDateIntervalValue(?string $key, ?DateInterval $value): void if (!empty($key)) { $this->writePropertyName($key); } - $res = "P{$value->y}Y{$value->y}M{$value->d}DT{$value->h}H{$value->i}M{$value->s}S"; + $year = $value->y > 0 ? "%yY" : ""; + $month = $value->m > 0 ? "%mM" : ""; + $day = $value->d > 0 ? '%dD' : ""; + $hour = $value->h > 0 ? '%hH' : ""; + $minute = $value->i > 0 ? '%iM' : ""; + $second = $value->s > 0 ? '%sS' : ""; + $res = $value->format("%rP$year$month{$day}T{$hour}{$minute}{$second}"); $val = "\"$res\"" ; $this->writePropertyValue($key, $val); } diff --git a/tests/JsonParseNodeTest.php b/tests/JsonParseNodeTest.php index 6049823..aed80e4 100644 --- a/tests/JsonParseNodeTest.php +++ b/tests/JsonParseNodeTest.php @@ -2,6 +2,7 @@ namespace Microsoft\Kiota\Serialization\Tests; +use DateInterval; use DateTime; use DateTimeInterface; use Exception; @@ -178,10 +179,21 @@ public function testGetBinaryContent(): void { $this->parseNode = new JsonParseNode(100); $this->assertEquals("100", $this->parseNode->getBinaryContent()->getContents()); } - + public function testGetBinaryContentFromArray(): void { $this->parseNode = new JsonParseNode(json_decode($this->stream->getContents(), true)); $this->stream->rewind(); $this->assertEquals($this->stream->getContents(), $this->parseNode->getBinaryContent()->getContents()); } + + /** + * @throws Exception + */ + public function testGetNegativeDateInterval(): void + { + $this->parseNode = new JsonParseNode('-P1D'); + $expected = new DateInterval('P1D'); + $expected->invert = 1; + $this->assertEquals($this->parseNode->getDateIntervalValue(), $expected); + } } diff --git a/tests/JsonSerializationWriterTest.php b/tests/JsonSerializationWriterTest.php index 3d1824f..57ffaff 100644 --- a/tests/JsonSerializationWriterTest.php +++ b/tests/JsonSerializationWriterTest.php @@ -240,7 +240,17 @@ public function testWriteDateIntervalValue(): void $this->jsonSerializationWriter->writeAnyValue('timeTaken', $interval); $content = $this->jsonSerializationWriter->getSerializedContent(); - $this->assertEquals('"timeTaken":"P0Y0M300DT0H0M100S"', $content->getContents()); + $this->assertEquals('"timeTaken":"P300DT100S"', $content->getContents()); + } + + public function testWriteNegativeDateIntervalValue(): void + { + $this->jsonSerializationWriter = new JsonSerializationWriter(); + $interval = new DateInterval('P1DT6H7M7S'); + $interval->invert = 1; + $this->jsonSerializationWriter->writeAnyValue('timeTaken', $interval); + $content = $this->jsonSerializationWriter->getSerializedContent(); + $this->assertEquals('"timeTaken":"-P1DT6H7M7S"', $content->getContents()); } public function testWriteBinaryContentValue(): void From 864b9b114dd752057415162a406ade7c1697201f Mon Sep 17 00:00:00 2001 From: silaskenneth Date: Thu, 2 May 2024 15:21:38 +0300 Subject: [PATCH 07/11] Do major refactor to use abstraction implementations. --- composer.json | 2 +- src/JsonParseNode.php | 17 +++++------------ src/JsonSerializationWriter.php | 22 +++++++--------------- 3 files changed, 13 insertions(+), 28 deletions(-) diff --git a/composer.json b/composer.json index 7865bf1..216dfbf 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,7 @@ } }, "require": { - "microsoft/kiota-abstractions": "^1.0.2", + "microsoft/kiota-abstractions": "^1.3.0", "guzzlehttp/psr7": "^1.6 || ^2", "php": "^7.4 || ^8", "ext-json": "*" diff --git a/src/JsonParseNode.php b/src/JsonParseNode.php index b98b489..4ee13f3 100644 --- a/src/JsonParseNode.php +++ b/src/JsonParseNode.php @@ -11,6 +11,8 @@ use Microsoft\Kiota\Abstractions\Serialization\AdditionalDataHolder; use Microsoft\Kiota\Abstractions\Serialization\Parsable; use Microsoft\Kiota\Abstractions\Serialization\ParseNode; +use Microsoft\Kiota\Abstractions\Serialization\ParseNodeFromStringTrait; +use Microsoft\Kiota\Abstractions\Serialization\SerializationWriterToStringTrait; use Microsoft\Kiota\Abstractions\Types\Date; use Microsoft\Kiota\Abstractions\Types\Time; use Psr\Http\Message\StreamInterface; @@ -22,6 +24,8 @@ */ class JsonParseNode implements ParseNode { + use ParseNodeFromStringTrait; + /** @var mixed|null $jsonNode*/ private $jsonNode; @@ -285,18 +289,7 @@ public function getDateIntervalValue(): ?DateInterval{ if ($this->jsonNode === null){ return null; } - $originalValue = strval($this->jsonNode); - $negativeValPosition = strpos($originalValue, '-'); - $invert = 0; - $str = $originalValue; - if ($negativeValPosition !== false && $negativeValPosition === 0) { - // Invert the interval - $invert = 1; - $str = substr($originalValue, 1); - } - $dateInterval = new DateInterval($str); - $dateInterval->invert = $invert; - return $dateInterval; + return $this->parseDateIntervalFromString(strval($this->jsonNode)); } public function getBinaryContent(): ?StreamInterface { diff --git a/src/JsonSerializationWriter.php b/src/JsonSerializationWriter.php index 10d3923..5c01eff 100644 --- a/src/JsonSerializationWriter.php +++ b/src/JsonSerializationWriter.php @@ -10,6 +10,7 @@ use Microsoft\Kiota\Abstractions\Enum; use Microsoft\Kiota\Abstractions\Serialization\Parsable; use Microsoft\Kiota\Abstractions\Serialization\SerializationWriter; +use Microsoft\Kiota\Abstractions\Serialization\SerializationWriterToStringTrait; use Microsoft\Kiota\Abstractions\Types\Date; use Microsoft\Kiota\Abstractions\Types\Time; use Psr\Http\Message\StreamInterface; @@ -22,6 +23,8 @@ */ class JsonSerializationWriter implements SerializationWriter { + use SerializationWriterToStringTrait; + /** @var array $writer */ private array $writer = []; @@ -45,13 +48,11 @@ private function writePropertyName(string $propertyName): void { * @inheritDoc */ public function writeStringValue(?string $key, ?string $value): void { - - $propertyValue = $value !== null ? '"'.addcslashes($value, "\\\r\n\"\t").'"' : ''; if ($value !== null) { if (!empty($key)) { $this->writePropertyName($key); } - $this->writePropertyValue($key, $propertyValue); + $this->writePropertyValue($key, "\"{$this->getStringValueAsEscapedString($value)}\""); } } @@ -63,8 +64,7 @@ public function writeBooleanValue(?string $key, ?bool $value): void { if (!empty($key)) { $this->writePropertyName($key); } - $options = ['false', 'true']; - $this->writePropertyValue($key, $options[$value]); + $this->writePropertyValue($key, $this->getBooleanValueAsString($value)); } } @@ -100,7 +100,7 @@ public function writeDateTimeValue(?string $key, ?DateTime $value): void { if (!empty($key)) { $this->writePropertyName($key); } - $this->writePropertyValue($key, "\"{$value->format(DateTimeInterface::RFC3339)}\""); + $this->writePropertyValue($key, "\"{$this->getDateTimeValueAsString($value)}\""); } } @@ -452,15 +452,7 @@ public function writeDateIntervalValue(?string $key, ?DateInterval $value): void if (!empty($key)) { $this->writePropertyName($key); } - $year = $value->y > 0 ? "%yY" : ""; - $month = $value->m > 0 ? "%mM" : ""; - $day = $value->d > 0 ? '%dD' : ""; - $hour = $value->h > 0 ? '%hH' : ""; - $minute = $value->i > 0 ? '%iM' : ""; - $second = $value->s > 0 ? '%sS' : ""; - $res = $value->format("%rP$year$month{$day}T{$hour}{$minute}{$second}"); - $val = "\"$res\"" ; - $this->writePropertyValue($key, $val); + $this->writePropertyValue($key, "\"{$this->getDateIntervalValueAsString($value)}\""); } } } From 644064a5dc297ed458d82b2fbd0979a7fcef2662 Mon Sep 17 00:00:00 2001 From: silaskenneth Date: Thu, 2 May 2024 15:24:35 +0300 Subject: [PATCH 08/11] Remove unused imports. --- src/JsonParseNode.php | 2 -- src/JsonSerializationWriter.php | 1 - 2 files changed, 3 deletions(-) diff --git a/src/JsonParseNode.php b/src/JsonParseNode.php index 4ee13f3..b045744 100644 --- a/src/JsonParseNode.php +++ b/src/JsonParseNode.php @@ -12,11 +12,9 @@ use Microsoft\Kiota\Abstractions\Serialization\Parsable; use Microsoft\Kiota\Abstractions\Serialization\ParseNode; use Microsoft\Kiota\Abstractions\Serialization\ParseNodeFromStringTrait; -use Microsoft\Kiota\Abstractions\Serialization\SerializationWriterToStringTrait; use Microsoft\Kiota\Abstractions\Types\Date; use Microsoft\Kiota\Abstractions\Types\Time; use Psr\Http\Message\StreamInterface; -use RuntimeException; /** * @method onBeforeAssignFieldValues(Parsable $result) diff --git a/src/JsonSerializationWriter.php b/src/JsonSerializationWriter.php index 5c01eff..c798eb0 100644 --- a/src/JsonSerializationWriter.php +++ b/src/JsonSerializationWriter.php @@ -4,7 +4,6 @@ use DateInterval; use DateTime; -use DateTimeInterface; use GuzzleHttp\Psr7\Utils; use InvalidArgumentException; use Microsoft\Kiota\Abstractions\Enum; From 772a8c13b19258b460fc70cc0920648a0bd87215 Mon Sep 17 00:00:00 2001 From: silaskenneth Date: Thu, 2 May 2024 15:43:21 +0300 Subject: [PATCH 09/11] Change method visibility. --- src/JsonParseNode.php | 2 +- src/JsonSerializationWriter.php | 7 ++++++- tests/JsonParseNodeTest.php | 2 +- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/JsonParseNode.php b/src/JsonParseNode.php index b045744..66252c6 100644 --- a/src/JsonParseNode.php +++ b/src/JsonParseNode.php @@ -22,7 +22,7 @@ */ class JsonParseNode implements ParseNode { - use ParseNodeFromStringTrait; + use ParseNodeFromStringTrait {parseDateIntervalFromString as public;} /** @var mixed|null $jsonNode*/ private $jsonNode; diff --git a/src/JsonSerializationWriter.php b/src/JsonSerializationWriter.php index c798eb0..e575c3f 100644 --- a/src/JsonSerializationWriter.php +++ b/src/JsonSerializationWriter.php @@ -22,7 +22,12 @@ */ class JsonSerializationWriter implements SerializationWriter { - use SerializationWriterToStringTrait; + use SerializationWriterToStringTrait { + getStringValueAsEscapedString as private; + getBooleanValueAsString as private; + getDateIntervalValueAsString as private; + getDateTimeValueAsString as private; + } /** @var array $writer */ private array $writer = []; diff --git a/tests/JsonParseNodeTest.php b/tests/JsonParseNodeTest.php index aed80e4..ce63400 100644 --- a/tests/JsonParseNodeTest.php +++ b/tests/JsonParseNodeTest.php @@ -179,7 +179,7 @@ public function testGetBinaryContent(): void { $this->parseNode = new JsonParseNode(100); $this->assertEquals("100", $this->parseNode->getBinaryContent()->getContents()); } - + public function testGetBinaryContentFromArray(): void { $this->parseNode = new JsonParseNode(json_decode($this->stream->getContents(), true)); $this->stream->rewind(); From 98d021b573f7dd13fe7463046d91511fe701430f Mon Sep 17 00:00:00 2001 From: silaskenneth Date: Thu, 2 May 2024 16:12:11 +0300 Subject: [PATCH 10/11] Remove visibility configuration --- src/JsonParseNode.php | 2 +- src/JsonSerializationWriter.php | 7 +------ 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/src/JsonParseNode.php b/src/JsonParseNode.php index 66252c6..b045744 100644 --- a/src/JsonParseNode.php +++ b/src/JsonParseNode.php @@ -22,7 +22,7 @@ */ class JsonParseNode implements ParseNode { - use ParseNodeFromStringTrait {parseDateIntervalFromString as public;} + use ParseNodeFromStringTrait; /** @var mixed|null $jsonNode*/ private $jsonNode; diff --git a/src/JsonSerializationWriter.php b/src/JsonSerializationWriter.php index e575c3f..c798eb0 100644 --- a/src/JsonSerializationWriter.php +++ b/src/JsonSerializationWriter.php @@ -22,12 +22,7 @@ */ class JsonSerializationWriter implements SerializationWriter { - use SerializationWriterToStringTrait { - getStringValueAsEscapedString as private; - getBooleanValueAsString as private; - getDateIntervalValueAsString as private; - getDateTimeValueAsString as private; - } + use SerializationWriterToStringTrait; /** @var array $writer */ private array $writer = []; From 96898b8a8b8ff8f7275cc2c3404062e34b90a1a5 Mon Sep 17 00:00:00 2001 From: silaskenneth Date: Thu, 2 May 2024 17:12:00 +0300 Subject: [PATCH 11/11] Update abstractions package version. --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 216dfbf..c7646df 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,7 @@ } }, "require": { - "microsoft/kiota-abstractions": "^1.3.0", + "microsoft/kiota-abstractions": "^1.3.1", "guzzlehttp/psr7": "^1.6 || ^2", "php": "^7.4 || ^8", "ext-json": "*"