From a46a3d914d6951693fec62439bd7293022c8a021 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?O=C4=9Fuzhan=20Durgun?= Date: Mon, 4 Jul 2022 14:52:58 +0300 Subject: [PATCH] chore(tests): Add unit tests for Builder classes (#16) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Oğuzhan Durgun --- src/Api/V1/Engine/ResourceAction.php | 4 +- src/Sdk/Builder/Principal.php | 4 +- src/Sdk/Builder/Resource.php | 4 +- tests/Sdk/Builder/AuxDataTest.php | 23 ++++++++ tests/Sdk/Builder/PrincipalTest.php | 65 +++++++++++++++++++++++ tests/Sdk/Builder/ResourceActionTest.php | 67 ++++++++++++++++++++++++ tests/Sdk/Builder/ResourceTest.php | 61 +++++++++++++++++++++ 7 files changed, 222 insertions(+), 6 deletions(-) create mode 100644 tests/Sdk/Builder/AuxDataTest.php create mode 100644 tests/Sdk/Builder/PrincipalTest.php create mode 100644 tests/Sdk/Builder/ResourceActionTest.php create mode 100644 tests/Sdk/Builder/ResourceTest.php diff --git a/src/Api/V1/Engine/ResourceAction.php b/src/Api/V1/Engine/ResourceAction.php index d7383a9..4bb430f 100644 --- a/src/Api/V1/Engine/ResourceAction.php +++ b/src/Api/V1/Engine/ResourceAction.php @@ -9,8 +9,8 @@ class ResourceAction implements \JsonSerializable { - private Resource $resource; - private array $actions; + public Resource $resource; + public array $actions; /** * @param Resource $resource diff --git a/src/Sdk/Builder/Principal.php b/src/Sdk/Builder/Principal.php index 17e052f..75594c4 100644 --- a/src/Sdk/Builder/Principal.php +++ b/src/Sdk/Builder/Principal.php @@ -86,8 +86,8 @@ public function withAttribute(?string $key, $value): Principal { */ public function withAttributes(?array $attributes): Principal { if ($attributes == null) return $this; - foreach ($attributes as $attribute) { - $this->principal->attributes[] = $attribute; + foreach ($attributes as $key => $value) { + $this->principal->attributes[$key] = $value; } return $this; } diff --git a/src/Sdk/Builder/Resource.php b/src/Sdk/Builder/Resource.php index 309fd78..093324c 100644 --- a/src/Sdk/Builder/Resource.php +++ b/src/Sdk/Builder/Resource.php @@ -76,8 +76,8 @@ public function withAttribute(?string $key, $value): Resource { */ public function withAttributes(?array $attributes): Resource { if ($attributes == null) return $this; - foreach ($attributes as $attribute) { - $this->resource->attributes[] = $attribute; + foreach ($attributes as $key => $value) { + $this->resource->attributes[$key] = $value; } return $this; } diff --git a/tests/Sdk/Builder/AuxDataTest.php b/tests/Sdk/Builder/AuxDataTest.php new file mode 100644 index 0000000..e03d1c1 --- /dev/null +++ b/tests/Sdk/Builder/AuxDataTest.php @@ -0,0 +1,23 @@ +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"); + } +} \ No newline at end of file diff --git a/tests/Sdk/Builder/PrincipalTest.php b/tests/Sdk/Builder/PrincipalTest.php new file mode 100644 index 0000000..aed237a --- /dev/null +++ b/tests/Sdk/Builder/PrincipalTest.php @@ -0,0 +1,65 @@ +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"); + } +} \ No newline at end of file diff --git a/tests/Sdk/Builder/ResourceActionTest.php b/tests/Sdk/Builder/ResourceActionTest.php new file mode 100644 index 0000000..ee3f973 --- /dev/null +++ b/tests/Sdk/Builder/ResourceActionTest.php @@ -0,0 +1,67 @@ +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"); + } +} \ No newline at end of file diff --git a/tests/Sdk/Builder/ResourceTest.php b/tests/Sdk/Builder/ResourceTest.php new file mode 100644 index 0000000..f0fe87b --- /dev/null +++ b/tests/Sdk/Builder/ResourceTest.php @@ -0,0 +1,61 @@ +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"); + } +} \ No newline at end of file