| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- <?php
- namespace Test\Michel\Resolver;
- use Michel\Resolver\Option;
- use Michel\Resolver\OptionsResolver;
- use Michel\UniTester\TestCase;
- class MinMaxOptionsTest extends TestCase
- {
- protected function setUp(): void
- {
- // TODO: Implement setUp() method.
- }
- protected function tearDown(): void
- {
- // TODO: Implement tearDown() method.
- }
- protected function execute(): void
- {
- $this->testStringLength();
- $this->testIntValue();
- $this->testArrayCount();
- }
- public function testStringLength(): void
- {
- $resolver = new OptionsResolver([
- Option::string('username')->min(3)->max(10),
- ]);
- // Valid
- $options = $resolver->resolve(['username' => 'johndoe']);
- $this->assertStrictEquals('johndoe', $options['username']);
- // Invalid (too short)
- $this->expectException(\InvalidArgumentException::class, function () use ($resolver) {
- $resolver->resolve(['username' => 'jo']);
- });
- // Invalid (too long)
- $this->expectException(\InvalidArgumentException::class, function () use ($resolver) {
- $resolver->resolve(['username' => 'johndoelongname']);
- });
- }
- public function testIntValue(): void
- {
- $resolver = new OptionsResolver([
- Option::int('port')->min(1024)->max(65535),
- ]);
- // Valid
- $options = $resolver->resolve(['port' => 8080]);
- $this->assertStrictEquals(8080, $options['port']);
- // Invalid (too small)
- $this->expectException(\InvalidArgumentException::class, function () use ($resolver) {
- $resolver->resolve(['port' => 80]);
- });
- // Invalid (too large)
- $this->expectException(\InvalidArgumentException::class, function () use ($resolver) {
- $resolver->resolve(['port' => 70000]);
- });
- }
- public function testArrayCount(): void
- {
- $resolver = new OptionsResolver([
- Option::array('items')->min(1)->max(3),
- ]);
- // Valid
- $options = $resolver->resolve(['items' => ['a', 'b']]);
- $this->assertCount(2, $options['items']);
- // Invalid (too few)
- $this->expectException(\InvalidArgumentException::class, function () use ($resolver) {
- $resolver->resolve(['items' => []]);
- });
- // Invalid (too many)
- $this->expectException(\InvalidArgumentException::class, function () use ($resolver) {
- $resolver->resolve(['items' => ['a', 'b', 'c', 'd']]);
- });
- }
- }
|