Skip to content

Commit

Permalink
Validate for unique weekdays
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Kolb committed Jun 9, 2024
1 parent 2e8af17 commit 7969ee8
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Weekdays.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ public function __construct(
/** @var array<int, Weekday> $weekdays */
public array $weekdays,
) {
// TODO: Validate for unique
$enumValues = [];
foreach ($this->weekdays as $weekday) {
$enumValues[] = $weekday->value;
}
if (count($enumValues) !== count(array_unique($enumValues))) {
throw new \InvalidArgumentException('Weekdays must be unique.');
}
}

// -- Array normalizable
Expand Down
47 changes: 47 additions & 0 deletions tests/Weekdays/ConstructionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace DigitalCraftsman\DateTimePrecision\Weekdays;

use DigitalCraftsman\DateTimePrecision\Weekday;
use DigitalCraftsman\DateTimePrecision\Weekdays;
use PHPUnit\Framework\TestCase;

/** @coversDefaultClass \DigitalCraftsman\DateTimePrecision\Weekdays */
final class ConstructionTest extends TestCase
{
/**
* @test
*
* @covers ::__construct
*
* @doesNotPerformAssertions
*/
public function construction_works(): void
{
// -- Act & Assert
new Weekdays([
Weekday::MONDAY,
Weekday::TUESDAY,
]);
}

/**
* @test
*
* @covers ::__construct
*/
public function construction_fails(): void
{
// -- Assert
$this->expectException(\InvalidArgumentException::class);

// -- Act
new Weekdays([
Weekday::MONDAY,
Weekday::MONDAY,
Weekday::TUESDAY,
]);
}
}

0 comments on commit 7969ee8

Please sign in to comment.