-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(tests): Add unit tests for Builder classes (#16)
Signed-off-by: Oğuzhan Durgun <oguzhandurgun95@gmail.com>
- Loading branch information
1 parent
436eb1b
commit a46a3d9
Showing
7 changed files
with
222 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Cerbos\Test\Sdk\Builder; | ||
|
||
use Cerbos\Sdk\Builder\AuxData; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
// Copyright 2021-2022 Zenauth Ltd. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
class AuxDataTest extends TestCase | ||
{ | ||
private string $token = "token"; | ||
private string $keySetId = "keySetId"; | ||
|
||
public function testAuxData(): void { | ||
$auxData = AuxData::WithJwt($this->token, $this->keySetId) | ||
->toAuxData(); | ||
|
||
$this->assertEquals($this->token, $auxData->jwt->token, "value of the auxData token is invalid"); | ||
$this->assertEquals($this->keySetId, $auxData->jwt->keySetId, "value of the auxData keySetId is invalid"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
namespace Cerbos\Test\Sdk\Builder; | ||
|
||
use Cerbos\Sdk\Builder\Principal; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
// Copyright 2021-2022 Zenauth Ltd. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
class PrincipalTest extends TestCase | ||
{ | ||
private string $id = "john"; | ||
private string $policyVersion = "20210210"; | ||
private string $scope = "a_scope"; | ||
private array $roles = array("employee", "manager", "admin"); | ||
|
||
private string $lonelyAttr = "lonelyAttr"; | ||
private string $boolAttr = "boolAttr"; | ||
private string $intAttr = "intAttr"; | ||
private string $stringAttr = "stringAttr"; | ||
private string $floatAttr = "floatAttr"; | ||
|
||
|
||
public function testPrincipal(): void { | ||
$principal = Principal::newInstance($this->id) | ||
->withPolicyVersion($this->policyVersion) | ||
->withScope($this->scope) | ||
->withAttribute($this->lonelyAttr, $this->lonelyAttr) | ||
->withAttributes(array( | ||
$this->boolAttr => true, | ||
$this->intAttr => 10, | ||
$this->stringAttr => $this->stringAttr, | ||
$this->floatAttr => 1.2, | ||
)) | ||
->withRole($this->roles[0]) | ||
->withRoles(array($this->roles[1], $this->roles[2]))->toPrincipal(); | ||
|
||
$this->assertEquals($this->id, $principal->id, "value of the principal id is invalid"); | ||
$this->assertEquals($this->policyVersion, $principal->policyVersion, "value of the principal policyVersion is invalid"); | ||
$this->assertEquals($this->scope, $principal->scope, "value of the principal scope is invalid"); | ||
|
||
$this->assertArrayHasKey($this->lonelyAttr, $principal->attributes, "the principal does not have '".$this->lonelyAttr."' attribute"); | ||
$this->assertArrayHasKey($this->boolAttr, $principal->attributes, "the principal does not have '".$this->boolAttr."' attribute"); | ||
$this->assertArrayHasKey($this->intAttr, $principal->attributes, "the principal does not have '".$this->intAttr."' attribute"); | ||
$this->assertArrayHasKey($this->stringAttr, $principal->attributes, "the principal does not have '".$this->stringAttr."' attribute"); | ||
$this->assertArrayHasKey($this->floatAttr, $principal->attributes, "the principal does not have '".$this->floatAttr."' attribute"); | ||
|
||
$this->assertIsString($principal->attributes[$this->lonelyAttr], "'".$this->lonelyAttr."' of the principal is not of type string"); | ||
$this->assertIsBool($principal->attributes[$this->boolAttr], "'".$this->boolAttr."' of the principal is not of type bool"); | ||
$this->assertIsInt($principal->attributes[$this->intAttr], "'".$this->intAttr."' of the principal is not of type int"); | ||
$this->assertIsString($principal->attributes[$this->stringAttr], "'".$this->stringAttr."' of the principal is not of type string"); | ||
$this->assertIsFloat($principal->attributes[$this->floatAttr], "'".$this->floatAttr."' of the principal is not of type float"); | ||
|
||
$this->assertEquals($this->lonelyAttr, $principal->attributes[$this->lonelyAttr], "'".$this->lonelyAttr."' of the principal is not equal to the expected value"); | ||
$this->assertEquals(true, $principal->attributes[$this->boolAttr], "'".$this->boolAttr."' of the principal is not equal to the expected value"); | ||
$this->assertEquals(10, $principal->attributes[$this->intAttr], "'".$this->intAttr."' of the principal is not equal to the expected value"); | ||
$this->assertEquals($this->stringAttr, $principal->attributes[$this->stringAttr], "'".$this->stringAttr."' of the principal is not equal to the expected value"); | ||
$this->assertEquals(1.2, $principal->attributes[$this->floatAttr], "'".$this->floatAttr."' of the principal is not equal to the expected value"); | ||
|
||
$this->assertEquals($this->roles[0], $principal->roles[0], "first role of the principal is not equal to the expected value"); | ||
$this->assertEquals($this->roles[1], $principal->roles[1], "second role of the principal is not equal to the expected value"); | ||
$this->assertEquals($this->roles[2], $principal->roles[2], "third role of the principal is not equal to the expected value"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
namespace Cerbos\Test\Sdk\Builder; | ||
|
||
use Cerbos\Sdk\Builder\ResourceAction; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
// Copyright 2021-2022 Zenauth Ltd. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
class ResourceActionTest extends TestCase | ||
{ | ||
private string $kind = "leave_request"; | ||
private string $id = "XX125"; | ||
private string $policyVersion = "20210210"; | ||
private string $scope = "a_scope"; | ||
private array $actions = array("create", "view", "defer"); | ||
|
||
private string $lonelyAttr = "lonelyAttr"; | ||
private string $boolAttr = "boolAttr"; | ||
private string $intAttr = "intAttr"; | ||
private string $stringAttr = "stringAttr"; | ||
private string $floatAttr = "floatAttr"; | ||
|
||
public function testResourceAction(): void { | ||
$resourceAction = ResourceAction::newInstance($this->kind, $this->id) | ||
->withPolicyVersion($this->policyVersion) | ||
->withScope($this->scope) | ||
->withAttribute($this->lonelyAttr, $this->lonelyAttr) | ||
->withAttributes(array( | ||
$this->boolAttr => true, | ||
$this->intAttr => 10, | ||
$this->stringAttr => $this->stringAttr, | ||
$this->floatAttr => 1.2, | ||
)) | ||
->withAction($this->actions[0]) | ||
->withActions(array($this->actions[1], $this->actions[2])) | ||
->toResourceAction(); | ||
|
||
$this->assertEquals($this->kind, $resourceAction->resource->kind, "value of the resource kind is invalid"); | ||
$this->assertEquals($this->id, $resourceAction->resource->id, "value of the resource id is invalid"); | ||
$this->assertEquals($this->policyVersion, $resourceAction->resource->policyVersion, "value of the resource policyVersion is invalid"); | ||
$this->assertEquals($this->scope, $resourceAction->resource->scope, "value of the resource scope is invalid"); | ||
|
||
$this->assertArrayHasKey($this->lonelyAttr, $resourceAction->resource->attributes, "the resource does not have '".$this->lonelyAttr."' attribute"); | ||
$this->assertArrayHasKey($this->boolAttr, $resourceAction->resource->attributes, "the resource does not have '".$this->boolAttr."' attribute"); | ||
$this->assertArrayHasKey($this->intAttr, $resourceAction->resource->attributes, "the resource does not have '".$this->intAttr."' attribute"); | ||
$this->assertArrayHasKey($this->stringAttr, $resourceAction->resource->attributes, "the resource does not have '".$this->stringAttr."' attribute"); | ||
$this->assertArrayHasKey($this->floatAttr, $resourceAction->resource->attributes, "the resource does not have '".$this->floatAttr."' attribute"); | ||
|
||
$this->assertIsString($resourceAction->resource->attributes[$this->lonelyAttr], "'".$this->lonelyAttr."' of the resource is not of type string"); | ||
$this->assertIsBool($resourceAction->resource->attributes[$this->boolAttr], "'".$this->boolAttr."' of the resource is not of type bool"); | ||
$this->assertIsInt($resourceAction->resource->attributes[$this->intAttr], "'".$this->intAttr."' of the resource is not of type int"); | ||
$this->assertIsString($resourceAction->resource->attributes[$this->stringAttr], "'".$this->stringAttr."' of the resource is not of type string"); | ||
$this->assertIsFloat($resourceAction->resource->attributes[$this->floatAttr], "'".$this->floatAttr."' of the resource is not of type float"); | ||
|
||
$this->assertEquals($this->lonelyAttr, $resourceAction->resource->attributes[$this->lonelyAttr], "'".$this->lonelyAttr."' of the resource is not equal to the expected value"); | ||
$this->assertEquals(true, $resourceAction->resource->attributes[$this->boolAttr], "'".$this->boolAttr."' of the resource is not equal to the expected value"); | ||
$this->assertEquals(10, $resourceAction->resource->attributes[$this->intAttr], "'".$this->intAttr."' of the resource is not equal to the expected value"); | ||
$this->assertEquals($this->stringAttr, $resourceAction->resource->attributes[$this->stringAttr], "'".$this->stringAttr."' of the resource is not equal to the expected value"); | ||
$this->assertEquals(1.2, $resourceAction->resource->attributes[$this->floatAttr], "'".$this->floatAttr."' of the resource is not equal to the expected value"); | ||
|
||
$this->assertEquals($this->actions[0], $resourceAction->actions[0], "first action of the resource is not equal to the expected value"); | ||
$this->assertEquals($this->actions[1], $resourceAction->actions[1], "second action of the resource is not equal to the expected value"); | ||
$this->assertEquals($this->actions[2], $resourceAction->actions[2], "third action of the resource is not equal to the expected value"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
namespace Cerbos\Test\Sdk\Builder; | ||
|
||
use Cerbos\Sdk\Builder\Resource; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
// Copyright 2021-2022 Zenauth Ltd. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
class ResourceTest extends TestCase | ||
{ | ||
private string $kind = "leave_request"; | ||
private string $id = "XX125"; | ||
private string $policyVersion = "20210210"; | ||
private string $scope = "a_scope"; | ||
|
||
private string $lonelyAttr = "lonelyAttr"; | ||
private string $boolAttr = "boolAttr"; | ||
private string $intAttr = "intAttr"; | ||
private string $stringAttr = "stringAttr"; | ||
private string $floatAttr = "floatAttr"; | ||
|
||
|
||
public function testResource(): void { | ||
$resource = Resource::newInstance($this->kind, $this->id) | ||
->withPolicyVersion($this->policyVersion) | ||
->withScope($this->scope) | ||
->withAttribute($this->lonelyAttr, $this->lonelyAttr) | ||
->withAttributes(array( | ||
$this->boolAttr => true, | ||
$this->intAttr => 10, | ||
$this->stringAttr => $this->stringAttr, | ||
$this->floatAttr => 1.2, | ||
)) | ||
->toResource(); | ||
|
||
$this->assertEquals($this->kind, $resource->kind, "value of the resource kind is invalid"); | ||
$this->assertEquals($this->id, $resource->id, "value of the resource id is invalid"); | ||
$this->assertEquals($this->policyVersion, $resource->policyVersion, "value of the resource policyVersion is invalid"); | ||
$this->assertEquals($this->scope, $resource->scope, "value of the resource scope is invalid"); | ||
|
||
$this->assertArrayHasKey($this->lonelyAttr, $resource->attributes, "the resource does not have '".$this->lonelyAttr."' attribute"); | ||
$this->assertArrayHasKey($this->boolAttr, $resource->attributes, "the resource does not have '".$this->boolAttr."' attribute"); | ||
$this->assertArrayHasKey($this->intAttr, $resource->attributes, "the resource does not have '".$this->intAttr."' attribute"); | ||
$this->assertArrayHasKey($this->stringAttr, $resource->attributes, "the resource does not have '".$this->stringAttr."' attribute"); | ||
$this->assertArrayHasKey($this->floatAttr, $resource->attributes, "the resource does not have '".$this->floatAttr."' attribute"); | ||
|
||
$this->assertIsString($resource->attributes[$this->lonelyAttr], "'".$this->lonelyAttr."' of the resource is not of type string"); | ||
$this->assertIsBool($resource->attributes[$this->boolAttr], "'".$this->boolAttr."' of the resource is not of type bool"); | ||
$this->assertIsInt($resource->attributes[$this->intAttr], "'".$this->intAttr."' of the resource is not of type int"); | ||
$this->assertIsString($resource->attributes[$this->stringAttr], "'".$this->stringAttr."' of the resource is not of type string"); | ||
$this->assertIsFloat($resource->attributes[$this->floatAttr], "'".$this->floatAttr."' of the resource is not of type float"); | ||
|
||
$this->assertEquals($this->lonelyAttr, $resource->attributes[$this->lonelyAttr], "'".$this->lonelyAttr."' of the resource is not equal to the expected value"); | ||
$this->assertEquals(true, $resource->attributes[$this->boolAttr], "'".$this->boolAttr."' of the resource is not equal to the expected value"); | ||
$this->assertEquals(10, $resource->attributes[$this->intAttr], "'".$this->intAttr."' of the resource is not equal to the expected value"); | ||
$this->assertEquals($this->stringAttr, $resource->attributes[$this->stringAttr], "'".$this->stringAttr."' of the resource is not equal to the expected value"); | ||
$this->assertEquals(1.2, $resource->attributes[$this->floatAttr], "'".$this->floatAttr."' of the resource is not equal to the expected value"); | ||
} | ||
} |