Skip to content

Commit

Permalink
Public properties (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
rogervila authored Nov 18, 2022
1 parent 0211a35 commit ebcfb1d
Show file tree
Hide file tree
Showing 13 changed files with 165 additions and 13 deletions.
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
</include>
<report>
<clover outputFile="clover.xml"/>
<text outputFile="php://stdout"/>
</report>
</coverage>
<logging>
Expand Down
4 changes: 0 additions & 4 deletions src/Contracts/AlgorithmInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,4 @@
*/
interface AlgorithmInterface
{
public function __construct(
string $value,
?array $available_algorithms = null,
);
}
22 changes: 22 additions & 0 deletions src/Contracts/ProvablyFairInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace ProvablyFair\Contracts;

use ProvablyFair\Result;

/**
* @property SeedInterface $clientSeed
* @property SeedInterface $serverSeed
* @property AlgorithmInterface $algorithm
*/
interface ProvablyFairInterface
{
public function setSystem(SystemInterface $system): self;

public function getSystem(): SystemInterface;

/**
* @return Result[]
*/
public function generate(int $amount, bool $include_original = false): array;
}
1 change: 0 additions & 1 deletion src/Contracts/SeedInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@
*/
interface SeedInterface
{
public function __construct(string $value);
}
3 changes: 3 additions & 0 deletions src/Contracts/SystemInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace ProvablyFair\Contracts;

/**
* @property string $algorithm
*/
interface SystemInterface
{
public function generateServerSeed(SeedInterface $seed): SeedInterface;
Expand Down
14 changes: 10 additions & 4 deletions src/ProvablyFair.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@
namespace ProvablyFair;

use ProvablyFair\Contracts\AlgorithmInterface;
use ProvablyFair\Contracts\ProvablyFairInterface;
use ProvablyFair\Contracts\SeedInterface;
use ProvablyFair\Contracts\SystemInterface;

class ProvablyFair
class ProvablyFair implements ProvablyFairInterface
{
protected SystemInterface $system;

public function __construct(
protected readonly SeedInterface $clientSeed,
protected readonly SeedInterface $serverSeed,
protected readonly AlgorithmInterface $algorithm,
public readonly SeedInterface $clientSeed,
public readonly SeedInterface $serverSeed,
public readonly AlgorithmInterface $algorithm,
) {
$this->setSystem(new System($this->algorithm));
}
Expand All @@ -24,6 +25,11 @@ public function setSystem(SystemInterface $system): self
return $this;
}

public function getSystem(): SystemInterface
{
return $this->system;
}

/**
* @return Result[]
*/
Expand Down
4 changes: 3 additions & 1 deletion src/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace ProvablyFair;

use ProvablyFair\Contracts\ProvablyFairInterface;

class Result
{
public function __construct(
public readonly ProvablyFair $provablyFair,
public readonly ProvablyFairInterface $provablyFair,
public readonly int $index,
public readonly string $hash,
public readonly string $value,
Expand Down
2 changes: 1 addition & 1 deletion src/System.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class System implements SystemInterface
{
public function __construct(
protected readonly AlgorithmInterface $algorithm
public readonly AlgorithmInterface $algorithm
) {
}

Expand Down
10 changes: 8 additions & 2 deletions tests/AlgorithmTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@

final class AlgorithmTest extends TestCase
{
public function test_valid(): void
/**
* @throws InvalidAlgorithmException
*/
public function test_property(): void
{
$this->assertInstanceOf(Algorithm::class, new Algorithm('sha512'));
$algorithms = hash_hmac_algos();

$algorithm = new Algorithm($value = $algorithms[array_rand($algorithms)]);
$this->assertEquals($algorithm->value, $value);
}

/**
Expand Down
72 changes: 72 additions & 0 deletions tests/ProvablyFairTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,39 @@

namespace ProvablyFairTests;

use Exception;
use PHPUnit\Framework\TestCase;
use ProvablyFair\Algorithm;
use ProvablyFair\Contracts\AlgorithmInterface;
use ProvablyFair\Contracts\ProvablyFairInterface;
use ProvablyFair\Contracts\SeedInterface;
use ProvablyFair\Contracts\SystemInterface;
use ProvablyFair\Exceptions\InvalidAlgorithmException;
use ProvablyFair\Exceptions\InvalidSeedException;
use ProvablyFair\ProvablyFair;
use ProvablyFair\Seed;

final class ProvablyFairTest extends TestCase
{
public function test_properties(): void
{
$provablyFair = new ProvablyFair(
$clientSeed = $this->createMock(SeedInterface::class),
$serverSeed = $this->createMock(SeedInterface::class),
$algorithm = $this->createMock(AlgorithmInterface::class),
);

$this->assertInstanceOf(ProvablyFairInterface::class, $provablyFair);

$this->assertEquals($provablyFair->clientSeed, $clientSeed);
$this->assertEquals($provablyFair->serverSeed, $serverSeed);
$this->assertEquals($provablyFair->algorithm, $algorithm);

$this->assertInstanceOf(SystemInterface::class, $provablyFair->getSystem());
$provablyFair->setSystem($system = $this->createMock(SystemInterface::class));
$this->assertEquals($system, $provablyFair->getSystem());
}

public function test_expected_results(): void
{
$provablyFair = new ProvablyFair(
Expand Down Expand Up @@ -60,4 +86,50 @@ public function test_expected_results_including_original(): void

$this->assertArrayNotHasKey(3, $results);
}

/**
* @throws InvalidAlgorithmException
* @throws InvalidSeedException
* @throws Exception
*/
public function test_random_calculations(): void
{
$available_algorithms = hash_hmac_algos();

foreach ($available_algorithms as $algorithm) {
$amount = random_int(10, 100);
$provablyFair = new ProvablyFair(
new Seed(uniqid()),
new Seed(uniqid()),
new Algorithm($algorithm),
);

$results = $provablyFair->generate($amount, boolval(random_int(0, 1)));
$this->assertCount($amount, $results);

for ($i = 0; $i < $amount; $i++) {
$this->assertEquals($i, $results[$i]->index);

if ($i === ($amount - 1)) {
continue;
}

$currentResults = (new ProvablyFair(
$results[$i]->provablyFair->clientSeed,
new Seed($results[$i]->hash),
$results[$i]->provablyFair->algorithm,
))->generate(1);

$this->assertEquals(
$currentResults[0]->hash,
$results[$i + 1]->hash
);

$this->assertEquals(
$currentResults[0]->value,
$results[$i + 1]->value
);
}
}
}
}
29 changes: 29 additions & 0 deletions tests/ResultTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace ProvablyFairTests;

use Exception;
use PHPUnit\Framework\TestCase;
use ProvablyFair\Contracts\ProvablyFairInterface;
use ProvablyFair\Result;

final class ResultTest extends TestCase
{
/**
* @throws Exception
*/
public function test_readonly_properties(): void
{
$result = new Result(
$provablyFair = $this->createMock(ProvablyFairInterface::class),
$index = random_int(0, 10),
$hash = uniqid(),
$value = uniqid(),
);

$this->assertEquals($result->provablyFair, $provablyFair);
$this->assertEquals($result->index, $index);
$this->assertEquals($result->hash, $hash);
$this->assertEquals($result->value, $value);
}
}
9 changes: 9 additions & 0 deletions tests/SeedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@

final class SeedTest extends TestCase
{
/**
* @throws InvalidSeedException
*/
public function test_value(): void
{
$seed = new Seed($value = uniqid());
$this->assertEquals($seed->value, $value);
}

public function test_fails_if_seed_is_not_valid(): void
{
$this->expectException(InvalidSeedException::class);
Expand Down
7 changes: 7 additions & 0 deletions tests/SystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHPUnit\Framework\TestCase;
use ProvablyFair\Algorithm;
use ProvablyFair\Contracts\AlgorithmInterface;
use ProvablyFair\Contracts\SeedInterface;
use ProvablyFair\Exceptions\InvalidAlgorithmException;
use ProvablyFair\Exceptions\InvalidSeedException;
Expand All @@ -23,6 +24,12 @@ private function createSystem(): System
}
}

public function test_readonly_properties(): void
{
$system = new System($algorithm = $this->createMock(AlgorithmInterface::class));
$this->assertEquals($system->algorithm, $algorithm);
}

public function test_calculate_expected_result(): void
{
$system = $this->createSystem();
Expand Down

1 comment on commit ebcfb1d

@vercel
Copy link

@vercel vercel bot commented on ebcfb1d Nov 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

provably-fair – ./

provably-fair.vercel.app
provably-fair-git-master-rogervila.vercel.app
provably-fair-rogervila.vercel.app

Please sign in to comment.