Skip to content

Commit

Permalink
chore(tests): Add unit tests for Builder classes (#16)
Browse files Browse the repository at this point in the history
Signed-off-by: Oğuzhan Durgun <oguzhandurgun95@gmail.com>
  • Loading branch information
oguzhand95 authored Jul 4, 2022
1 parent 436eb1b commit a46a3d9
Show file tree
Hide file tree
Showing 7 changed files with 222 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/Api/V1/Engine/ResourceAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

class ResourceAction implements \JsonSerializable
{
private Resource $resource;
private array $actions;
public Resource $resource;
public array $actions;

/**
* @param Resource $resource
Expand Down
4 changes: 2 additions & 2 deletions src/Sdk/Builder/Principal.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Sdk/Builder/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
23 changes: 23 additions & 0 deletions tests/Sdk/Builder/AuxDataTest.php
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");
}
}
65 changes: 65 additions & 0 deletions tests/Sdk/Builder/PrincipalTest.php
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");
}
}
67 changes: 67 additions & 0 deletions tests/Sdk/Builder/ResourceActionTest.php
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");
}
}
61 changes: 61 additions & 0 deletions tests/Sdk/Builder/ResourceTest.php
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");
}
}

0 comments on commit a46a3d9

Please sign in to comment.