Skip to content

Commit

Permalink
make rules, features, and feature sets more easily serializable
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Thomas committed Nov 8, 2017
1 parent 30fd73e commit a9959f5
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 5 deletions.
14 changes: 13 additions & 1 deletion src/Features/Feature.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use RemotelyLiving\Doorkeeper\Rules;

class Feature
class Feature implements \JsonSerializable
{
/**
* @var string
Expand Down Expand Up @@ -68,4 +68,16 @@ public function getRules(): array
{
return $this->rule_set;
}

/**
* @return array
*/
public function jsonSerialize(): array
{
return [
'name' => $this->name,
'enabled' => $this->enabled,
'rules' => $this->rule_set,
];
}
}
10 changes: 9 additions & 1 deletion src/Features/Set.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
namespace RemotelyLiving\Doorkeeper\Features;

class Set
class Set implements \JsonSerializable
{
/**
* @var \RemotelyLiving\Doorkeeper\Features\Feature[]
Expand Down Expand Up @@ -57,4 +57,12 @@ public function getFeatureByName(string $feature_name): Feature

return $this->features[$feature_name];
}

/**
* @return array
*/
public function jsonSerialize(): array
{
return ['features' => $this->features];
}
}
12 changes: 12 additions & 0 deletions src/Rules/RuleAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,18 @@ final public function canBeSatisfied(Requestor $requestor = null): bool
return $this->childCanBeSatisfied($requestor);
}

/**
* @return array
*/
public function jsonSerialize(): array
{
return [
'type' => static::class,
'value' => $this->getValue(),
'prerequisites' => $this->prerequisites,
];
}

/**
* @inheritdoc
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/RuleInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use RemotelyLiving\Doorkeeper\Requestor;

interface RuleInterface
interface RuleInterface extends \JsonSerializable
{
/**
* @return RuleInterface[]
Expand Down
13 changes: 11 additions & 2 deletions tests/Features/FeatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function isEnabled()
/**
* @test
*/
public function getId()
public function getsId()
{
$feature = new Feature('someId', false);

Expand All @@ -32,7 +32,7 @@ public function getId()
/**
* @test
*/
public function getRules()
public function getsRules()
{
$feature = new Feature('someId', true);
$rule = new Random();
Expand All @@ -45,4 +45,13 @@ public function getRules()

$this->assertSame($rule, array_shift($rules));
}

/**
* @test
*/
public function jsonSerializes()
{
$feature = new Feature('someId', true);
$this->assertEquals('{"name":"someId","enabled":true,"rules":[]}', json_encode($feature));
}
}
16 changes: 16 additions & 0 deletions tests/Features/SetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ public function pushesQueriesAndGetsFeatures()
$this->assertTrue($set->offsetExists($feature_2->getName()));
}

/**
* @test
*/
public function jsonSerializesFeatures()
{
$feature_1 = new Feature('feature1Id', false);
$feature_2 = new Feature('feature2Id', true);

$set = new Set([$feature_1, $feature_2]);
//@codingStandardsIgnoreStart
$expected_json = '{"features":{"feature1Id":{"name":"feature1Id","enabled":false,"rules":[]},"feature2Id":{"name":"feature2Id","enabled":true,"rules":[]}}}';
//@codingStandardsIgnoreStop

$this->assertEquals($expected_json, json_encode($set));
}

/**
* @test
* @expectedException \OutOfBoundsException
Expand Down
33 changes: 33 additions & 0 deletions tests/Rules/AbstractTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
namespace RemotelyLiving\Doorkeeper\Tests\Rules;

use PHPUnit\Framework\TestCase;
use RemotelyLiving\Doorkeeper\Rules\Random;
use RemotelyLiving\Doorkeeper\Rules\RuleAbstract;
use RemotelyLiving\Doorkeeper\Tests\Utility\RuleAbstractMock;

class AbstractTest extends TestCase
{
/**
* @var RuleAbstract
*/
private $rule_abstract;

protected function setUp()
{
$this->rule_abstract = new RuleAbstractMock();
}

/**
* @test
*/
public function jsonSerializes()
{
$this->rule_abstract->addPrerequisite(new Random());
//@codingStandardsIgnoreStart
$expected = '{"type":"RemotelyLiving\\\Doorkeeper\\\Tests\\\Utility\\\RuleAbstractMock","value":"mockValue","prerequisites":[{"type":"RemotelyLiving\\\Doorkeeper\\\Rules\\\Random","value":null,"prerequisites":[]}]}';
//@codingStandardsIgnoreStop

$this->assertEquals($expected, json_encode($this->rule_abstract));
}
}
25 changes: 25 additions & 0 deletions tests/Utility/RuleAbstractMock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
namespace RemotelyLiving\Doorkeeper\Tests\Utility;

use RemotelyLiving\Doorkeeper\Requestor;
use RemotelyLiving\Doorkeeper\Rules\RuleAbstract;

class RuleAbstractMock extends RuleAbstract
{
/**
* @inheritdoc
*/
public function getValue()
{
return 'mockValue';
}

/**
* @inheritdoc
*/
public function childCanBeSatisfied(Requestor $requestor = null): bool
{
$requestor;
return true;
}
}

0 comments on commit a9959f5

Please sign in to comment.