Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/Domain/Entities/Check.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace RPGPlayground\Domain\Entities;

final class Check
{
private function __construct(
public readonly string $title,
public readonly int $threshold,
) {}

/**
* @param string $title
* @param int $threshold
* @return self
* @throws \InvalidArgumentException
*/
public static function create(string $title, int $threshold): self
{
if (empty($title)) {
throw new \InvalidArgumentException('Title cannot be empty.');
}

if ($threshold < 0) {
throw new \InvalidArgumentException('Threshold must be greater than or equal to 0.');
}

return new self($title, $threshold);
}

/**
* @param int $roll
* @return bool
*/
public function isSuccess(int $roll): bool
{
return $roll >= $this->threshold;
}

/**
* @param int $roll
* @return bool
*/
public function isFailure(int $roll): bool
{
return $roll < $this->threshold;
}
}
2 changes: 1 addition & 1 deletion src/Domain/ValueObjects/Dice.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ final class Dice
* @throws \InvalidArgumentException
*/
public function __construct(
public int $sides,
public readonly int $sides,
) {
if ($sides < self::MINIMUM_VALUE) {
throw new \InvalidArgumentException('Invalid number of sides for a dice');
Expand Down
59 changes: 59 additions & 0 deletions tests/Domain/Entities/CheckTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace Tests\Domain\Entities;

use PHPUnit\Framework\TestCase;
use RPGPlayground\Domain\Entities\Check;

final class CheckTest extends TestCase
{
// -------------------------------------------------------------------------
// Happy path
// -------------------------------------------------------------------------

public function test_check_is_created_with_valid_data(): void
{
$check = Check::create('My Check', 10);

$this->assertSame('My Check', $check->title);
$this->assertSame(10, $check->threshold);
}

public function test_check_is_successful_when_roll_is_greater_than_or_equal_to_threshold(): void
{
$check = Check::create('My Check', 10);

$this->assertTrue($check->isSuccess(10));
$this->assertFalse($check->isSuccess(9));
}

public function test_check_is_failure_when_roll_is_less_than_threshold(): void
{
$check = Check::create('My Check', 10);

$this->assertFalse($check->isFailure(10));
$this->assertTrue($check->isFailure(9));
}

// -------------------------------------------------------------------------
// Exceptions
// -------------------------------------------------------------------------

public function test_check_throws_exception_when_threshold_is_negative(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Threshold must be greater than or equal to 0.');

Check::create('My Check', -1);
}

public function test_check_throws_exception_when_title_is_empty(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Title cannot be empty.');

Check::create('', 10);
}
}
Loading