2
0

MinMaxOptionsTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace Test\Michel\Resolver;
  3. use Michel\Resolver\Option;
  4. use Michel\Resolver\OptionsResolver;
  5. use Michel\UniTester\TestCase;
  6. class MinMaxOptionsTest extends TestCase
  7. {
  8. protected function setUp(): void
  9. {
  10. // TODO: Implement setUp() method.
  11. }
  12. protected function tearDown(): void
  13. {
  14. // TODO: Implement tearDown() method.
  15. }
  16. protected function execute(): void
  17. {
  18. $this->testStringLength();
  19. $this->testIntValue();
  20. $this->testArrayCount();
  21. }
  22. public function testStringLength(): void
  23. {
  24. $resolver = new OptionsResolver([
  25. Option::string('username')->min(3)->max(10),
  26. ]);
  27. // Valid
  28. $options = $resolver->resolve(['username' => 'johndoe']);
  29. $this->assertStrictEquals('johndoe', $options['username']);
  30. // Invalid (too short)
  31. $this->expectException(\InvalidArgumentException::class, function () use ($resolver) {
  32. $resolver->resolve(['username' => 'jo']);
  33. });
  34. // Invalid (too long)
  35. $this->expectException(\InvalidArgumentException::class, function () use ($resolver) {
  36. $resolver->resolve(['username' => 'johndoelongname']);
  37. });
  38. }
  39. public function testIntValue(): void
  40. {
  41. $resolver = new OptionsResolver([
  42. Option::int('port')->min(1024)->max(65535),
  43. ]);
  44. // Valid
  45. $options = $resolver->resolve(['port' => 8080]);
  46. $this->assertStrictEquals(8080, $options['port']);
  47. // Invalid (too small)
  48. $this->expectException(\InvalidArgumentException::class, function () use ($resolver) {
  49. $resolver->resolve(['port' => 80]);
  50. });
  51. // Invalid (too large)
  52. $this->expectException(\InvalidArgumentException::class, function () use ($resolver) {
  53. $resolver->resolve(['port' => 70000]);
  54. });
  55. }
  56. public function testArrayCount(): void
  57. {
  58. $resolver = new OptionsResolver([
  59. Option::array('items')->min(1)->max(3),
  60. ]);
  61. // Valid
  62. $options = $resolver->resolve(['items' => ['a', 'b']]);
  63. $this->assertCount(2, $options['items']);
  64. // Invalid (too few)
  65. $this->expectException(\InvalidArgumentException::class, function () use ($resolver) {
  66. $resolver->resolve(['items' => []]);
  67. });
  68. // Invalid (too many)
  69. $this->expectException(\InvalidArgumentException::class, function () use ($resolver) {
  70. $resolver->resolve(['items' => ['a', 'b', 'c', 'd']]);
  71. });
  72. }
  73. }