Skip to content

Commit

Permalink
Merge pull request #133 from microsoft/dev
Browse files Browse the repository at this point in the history
Release 1.2.0
  • Loading branch information
SilasKenneth authored Mar 12, 2024
2 parents a25bf28 + 6a7e3f6 commit fd6b016
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 13 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/conflicting-pr-label.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ on:
types: [synchronize]
branches: [ main ]

permissions:
pull-requests: write
contents: read

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
Expand Down
29 changes: 25 additions & 4 deletions .github/workflows/pr-validation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,51 @@ on:
pull_request:
branches: [ main, dev ]

permissions:
contents: read
pull-requests: read

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
php-versions: ['7.4', '8.0', '8.1', '8.2']
php-versions: ['7.4', '8.0', '8.1', '8.2', '8.3']
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup PHP and Xdebug for Code Coverage report
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
coverage: none
- name: Install dependencies
run: composer install
- name: Run static analysis
run: ./vendor/bin/phpstan
- name: Run tests without coverage
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
- 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 }}
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"doctrine/annotations": "^1.13 || ^2.0",
"open-telemetry/sdk": "^1.0.0",
"ramsey/uuid": "^3 || ^4",
"stduritemplate/stduritemplate": "^0.0.48 || ^0.0.49",
"stduritemplate/stduritemplate": "^0.0.53 || ^0.0.54",
"psr/http-message": "^1.1 || ^2.0"
},
"require-dev": {
Expand Down
7 changes: 6 additions & 1 deletion src/Authentication/AllowedHostsValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

namespace Microsoft\Kiota\Abstractions\Authentication;

use InvalidArgumentException;

/**
* Class AllowedHostsValidator
*
Expand Down Expand Up @@ -40,6 +42,9 @@ public function setAllowedHosts(array $hosts): void
{
foreach ($hosts as $host) {
$host = strtolower(trim($host));
if (str_starts_with($host, "https://") || str_starts_with($host, 'http://')) {
throw new InvalidArgumentException("The host $host is not valid as it contains the scheme.");
}
if (!array_key_exists($host, $this->allowedHosts)) {
$this->allowedHosts[$host] = true;
}
Expand Down Expand Up @@ -81,4 +86,4 @@ private function extractHost(string $url): string
throw new \InvalidArgumentException("$url must contain host");
}
}
}
}
6 changes: 5 additions & 1 deletion src/RequestInformation.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@ private function sanitizeValue($value) {
if (is_object($value) && is_a($value, DateTime::class)) {
return $value->format(DateTimeInterface::ATOM);
}
return $value;
if (is_object($value) && is_subclass_of($value, Enum::class)) {
return $value->value();
}
return is_array($value) ?
array_map(fn ($x) => $this->sanitizeValue($x), $value) : $value;
}

/**
Expand Down
10 changes: 10 additions & 0 deletions tests/Authentication/AllowedHostsValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ public function testSetAllowedHostsSetLowercaseTrimmedDeduplicatedHosts(): void
$this->assertEquals($expected, $validator->getAllowedHosts());
}

public function testShouldThrowException(): void
{
$hosts = ["https://abc.com "];
$this->expectException(\InvalidArgumentException::class);
$validator = new AllowedHostsValidator();
$validator->setAllowedHosts($hosts);
$expected = ["abc.com"]; //duplicates should not be added to allowed hosts

}

public function testIsUrlHostValidWithValidHost(): void
{
$this->assertTrue($this->defaultValidator->isUrlHostValid("https://abc.com"));
Expand Down
21 changes: 15 additions & 6 deletions tests/RequestInformationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use DateTime;
use DateTimeZone;
use InvalidArgumentException;
use Microsoft\Kiota\Abstractions\Enum;
use Microsoft\Kiota\Abstractions\HttpMethod;
use Microsoft\Kiota\Abstractions\RequestInformation;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -35,19 +36,20 @@ public function testSetUri(): void{
* @throws InvalidArgumentException
*/
public function testSetQueryParameters(): void {
$this->requestInformation->urlTemplate = '{?%24select,top,%24count}';
$this->requestInformation->urlTemplate = '{?%24select,top,%24count,enum}';

$queryParam = new TestQueryParameter();
$queryParam->enum = [new TestEnum('a'), new TestEnum('b')];
$this->requestInformation->setQueryParameters($queryParam);
$this->assertEquals('?top=10', $this->requestInformation->getUri());
$this->assertTrue(sizeof($this->requestInformation->queryParameters) == 1);
$this->assertEquals('?top=10&enum=a,b', $this->requestInformation->getUri());
$this->assertEquals(2, sizeof($this->requestInformation->queryParameters));
$queryParam->select = ['displayName', 'age'];
$this->requestInformation->setQueryParameters($queryParam);
$this->assertTrue(sizeof($this->requestInformation->queryParameters) == 2);
$this->assertEquals(3, sizeof($this->requestInformation->queryParameters));
$this->assertArrayHasKey('%24select', $this->requestInformation->queryParameters);
$this->assertEquals(['displayName', 'age'], $this->requestInformation->queryParameters['%24select']);
$this->assertArrayHasKey('top', $this->requestInformation->queryParameters);
$this->assertEquals('?%24select=displayName,age&top=10', $this->requestInformation->getUri());
$this->assertEquals('?%24select=displayName,age&top=10&enum=a,b', $this->requestInformation->getUri());
}

/**
Expand Down Expand Up @@ -78,7 +80,7 @@ public function testPathParametersOfDateTimeOffsetType(): void

// Act
$fromDateTime =new DateTime("2022-08-01T2:33", new DateTimeZone('+02:00'));
$toDateTime =new DateTime('2022-08-02T10:00', new DateTimeZone('-1:00'));
$toDateTime = new DateTime('2022-08-02T10:00', new DateTimeZone('-1:00'));
$requestInfo->pathParameters["fromDateTime"] = $fromDateTime;
$requestInfo->pathParameters["toDateTime"] = $toDateTime;

Expand Down Expand Up @@ -132,4 +134,11 @@ class TestQueryParameter {
public ?array $select = null;
public bool $count = false;
public int $top = 10; // no annotation
/** @var array<TestEnum>|null */
public ?array $enum = null;
}

class TestEnum extends Enum {
public const A = "a";
public const B = "b";
}

0 comments on commit fd6b016

Please sign in to comment.