diff --git a/tests/Model/Integer/IntegerTest.php b/tests/Model/Integer/IntegerTest.php new file mode 100644 index 0000000..948bb87 --- /dev/null +++ b/tests/Model/Integer/IntegerTest.php @@ -0,0 +1,112 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace OpenSolid\Domain\Tests\Model\Integer; + +use OpenSolid\Domain\Model\Integer\Integer; +use PHPUnit\Framework\TestCase; + +class IntegerTest extends TestCase +{ + public function testCreate(): void + { + $i = Integer::from(1); + + $this->assertSame(1, $i->value()); + } + + public function testEquals(): void + { + $i = Integer::from(1); + $j = Integer::from(1); + $k = Integer::from(2); + + $this->assertTrue($i->equals($j)); + $this->assertFalse($i->equals($k)); + } + + public function testGreaterThan(): void + { + $i = Integer::from(2); + $j = Integer::from(1); + $k = Integer::from(2); + + $this->assertTrue($i->greaterThan($j)); + $this->assertFalse($i->greaterThan($k)); + } + + public function testGreaterThanOrEqual(): void + { + $i = Integer::from(2); + $j = Integer::from(1); + $k = Integer::from(2); + + $this->assertTrue($i->greaterThanOrEqual($j)); + $this->assertTrue($i->greaterThanOrEqual($k)); + } + + public function testLessThan(): void + { + $i = Integer::from(2); + $j = Integer::from(1); + $k = Integer::from(2); + + $this->assertTrue($j->lessThan($i)); + $this->assertFalse($k->lessThan($i)); + } + + public function testLessThanOrEqual(): void + { + $i = Integer::from(2); + $j = Integer::from(1); + $k = Integer::from(2); + + $this->assertTrue($j->lessThanOrEqual($i)); + $this->assertTrue($k->lessThanOrEqual($i)); + } + + public function testMultiply(): void + { + $i = Integer::from(2); + $j = Integer::from(3); + $k = Integer::from(6); + + $this->assertTrue($k->equals($i->multiply($j))); + } + + public function testDivide(): void + { + $i = Integer::from(6); + $j = Integer::from(3); + $k = Integer::from(2); + + $this->assertTrue($k->equals($i->divide($j))); + } + + public function testModule(): void + { + $i = Integer::from(6); + $j = Integer::from(3); + $k = Integer::from(0); + + $this->assertTrue($k->equals($i->module($j))); + } + + public function testToString(): void + { + $i = Integer::from(1); + + $this->assertSame('1', $i->toString()); + $this->assertSame('1', (string) $i); + } +} diff --git a/tests/Model/Integer/PositiveIntegerTest.php b/tests/Model/Integer/PositiveIntegerTest.php new file mode 100644 index 0000000..36cdd04 --- /dev/null +++ b/tests/Model/Integer/PositiveIntegerTest.php @@ -0,0 +1,36 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace OpenSolid\Domain\Tests\Model\Integer; + +use OpenSolid\Domain\Error\InvariantViolation; +use OpenSolid\Domain\Model\Integer\PositiveInteger; +use PHPUnit\Framework\TestCase; + +class PositiveIntegerTest extends TestCase +{ + public function testPositiveInteger(): void + { + $i = PositiveInteger::from(1); + + $this->assertSame(1, $i->value()); + } + + public function testNegativeInteger(): void + { + $this->expectException(InvariantViolation::class); + $this->expectExceptionMessage('Value must be positive.'); + + PositiveInteger::from(-1); + } +} diff --git a/tests/Model/String/NonEmptyStringTest.php b/tests/Model/String/NonEmptyStringTest.php new file mode 100644 index 0000000..e980770 --- /dev/null +++ b/tests/Model/String/NonEmptyStringTest.php @@ -0,0 +1,42 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace OpenSolid\Domain\Tests\Model\String; + +use OpenSolid\Domain\Error\InvariantViolation; +use OpenSolid\Domain\Model\String\NonEmptyString; +use PHPUnit\Framework\TestCase; + +class NonEmptyStringTest extends TestCase +{ + public function testEmptyError(): void + { + $this->expectException(InvariantViolation::class); + $this->expectExceptionMessage('String cannot be empty.'); + + NonEmptyString::from(''); + } + + public function testEmptyWithBlankSpacesError(): void + { + $this->expectException(InvariantViolation::class); + $this->expectExceptionMessage('String cannot be empty.'); + + NonEmptyString::from(' '); + } + + public function testTrimString(): void + { + $this->assertSame('A', NonEmptyString::from(' A ')->value()); + } +} diff --git a/tests/Model/String/StrTest.php b/tests/Model/String/StrTest.php new file mode 100644 index 0000000..d95cc29 --- /dev/null +++ b/tests/Model/String/StrTest.php @@ -0,0 +1,89 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace OpenSolid\Domain\Tests\Model\String; + +use OpenSolid\Domain\Model\String\Str; +use PHPUnit\Framework\TestCase; + +class StrTest extends TestCase +{ + public function testCreate(): void + { + $s = Str::from('foo'); + + $this->assertSame('foo', $s->value()); + } + + public function testEquals(): void + { + $s1 = Str::from('foo'); + $s2 = Str::from('foo'); + $s3 = Str::from('bar'); + + $this->assertTrue($s1->equalsTo($s2)); + $this->assertFalse($s1->equalsTo($s3)); + } + + public function testToString(): void + { + $s = Str::from('foo'); + + $this->assertSame('foo', (string) $s); + $this->assertSame('foo', $s->toString()); + } + + public function testLength(): void + { + $s = Str::from('foo'); + + $this->assertSame(3, $s->length()); + } + + public function testIsEmpty(): void + { + $s1 = Str::from('foo'); + $s2 = Str::from(''); + + $this->assertFalse($s1->isEmpty()); + $this->assertTrue($s2->isEmpty()); + } + + public function testTrim(): void + { + $s = Str::from(' foo '); + + $this->assertSame('foo', $s->trim()->value()); + } + + public function testToLower(): void + { + $s = Str::from('FOO'); + + $this->assertSame('foo', $s->lower()->value()); + } + + public function testToUpper(): void + { + $s = Str::from('foo'); + + $this->assertSame('FOO', $s->upper()->value()); + } + + public function testToTitle(): void + { + $s = Str::from('foo bar'); + + $this->assertSame('Foo bar', $s->title()->value()); + } +} diff --git a/tests/Model/Uid/UuidV7Base58Test.php b/tests/Model/Uid/UuidV7Base58Test.php new file mode 100644 index 0000000..be5232f --- /dev/null +++ b/tests/Model/Uid/UuidV7Base58Test.php @@ -0,0 +1,61 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace OpenSolid\Domain\Tests\Model\Uid; + +use OpenSolid\Domain\Error\InvariantViolation; +use OpenSolid\Domain\Model\Uid\UuidV7Base58; +use PHPUnit\Framework\TestCase; + +class UuidV7Base58Test extends TestCase +{ + public function testCreate(): void + { + $id = UuidV7Base58::create(); + + $this->assertSame(22, strlen($id->value())); + } + + public function testCreateFromString(): void + { + $id = UuidV7Base58::from('1C4Gx3HwRKMqqm8pYTjiXg'); + + $this->assertSame(22, strlen($id->value())); + $this->assertSame('1C4Gx3HwRKMqqm8pYTjiXg', $id->value()); + } + + public function testCreateFromStringInvalid(): void + { + $this->expectException(InvariantViolation::class); + $this->expectExceptionMessage('Invalid UUID: "1C4Gx"'); + + UuidV7Base58::from('1C4Gx'); + } + + public function testEquals(): void + { + $id1 = UuidV7Base58::from('1C4Gx3HwRKMqqm8pYTjiXg'); + $id2 = UuidV7Base58::from('1C4Gx3HwRKMqqm8pYTjiXg'); + $id3 = UuidV7Base58::from('1C4Gx3HwRKMqqm8pYTjiXh'); + + $this->assertTrue($id1->equals($id2)); + $this->assertFalse($id1->equals($id3)); + } + + public function testToString(): void + { + $id = UuidV7Base58::from('1C4Gx3HwRKMqqm8pYTjiXg'); + + $this->assertSame('1C4Gx3HwRKMqqm8pYTjiXg', (string) $id); + } +} diff --git a/tests/Model/Uid/UuidV7Rfc4122Test.php b/tests/Model/Uid/UuidV7Rfc4122Test.php new file mode 100644 index 0000000..520361f --- /dev/null +++ b/tests/Model/Uid/UuidV7Rfc4122Test.php @@ -0,0 +1,61 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace OpenSolid\Domain\Tests\Model\Uid; + +use OpenSolid\Domain\Error\InvariantViolation; +use OpenSolid\Domain\Model\Uid\UuidV7Rfc4122; +use PHPUnit\Framework\TestCase; + +class UuidV7Rfc4122Test extends TestCase +{ + public function testCreate(): void + { + $id = UuidV7Rfc4122::create(); + + $this->assertSame(36, strlen($id->value())); + } + + public function testCreateFromString(): void + { + $id = UuidV7Rfc4122::from('f81d4fae-7dec-11d0-a765-00a0c91e6bf6'); + + $this->assertSame(36, strlen($id->value())); + $this->assertSame('f81d4fae-7dec-11d0-a765-00a0c91e6bf6', $id->value()); + } + + public function testCreateFromStringInvalid(): void + { + $this->expectException(InvariantViolation::class); + $this->expectExceptionMessage('Invalid UUID: "f81d4fae".'); + + UuidV7Rfc4122::from('f81d4fae'); + } + + public function testEquals(): void + { + $id1 = UuidV7Rfc4122::from('f81d4fae-7dec-11d0-a765-00a0c91e6bf6'); + $id2 = UuidV7Rfc4122::from('f81d4fae-7dec-11d0-a765-00a0c91e6bf6'); + $id3 = UuidV7Rfc4122::from('f81d4fae-7dec-11d0-a765-00a0c91e6bf7'); + + $this->assertTrue($id1->equals($id2)); + $this->assertFalse($id1->equals($id3)); + } + + public function testToString(): void + { + $id = UuidV7Rfc4122::from('f81d4fae-7dec-11d0-a765-00a0c91e6bf6'); + + $this->assertSame('f81d4fae-7dec-11d0-a765-00a0c91e6bf6', (string) $id); + } +}