2
0

OptionsResolverTest.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace Test\Michel\Resolver;
  3. use Michel\Resolver\Option;
  4. use Michel\Resolver\OptionsResolver;
  5. use Michel\UniTester\TestCase;
  6. class OptionsResolverTest 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->testResolveOptionsSuccessfully();
  19. $this->testMissingRequiredOptions();
  20. $this->testInvalidOptions();
  21. }
  22. public function testResolveOptionsSuccessfully()
  23. {
  24. $resolver = new OptionsResolver([
  25. Option::mixed('option1'),
  26. Option::mixed('option2')->setOptional('default'),
  27. ]);
  28. $options = $resolver->resolve([
  29. 'option1' => 'value1',
  30. ]);
  31. $this->assertStrictEquals($options, ['option1' => 'value1', 'option2' => 'default']);
  32. }
  33. public function testMissingRequiredOptions()
  34. {
  35. $resolver = new OptionsResolver([
  36. Option::mixed('requiredOption'),
  37. ]);
  38. $this->expectException(\InvalidArgumentException::class, function () use ($resolver) {
  39. $resolver->resolve([]);
  40. });
  41. }
  42. public function testInvalidOptions()
  43. {
  44. $resolver = new OptionsResolver([
  45. Option::mixed('validOption')->validator(static function ($value) {
  46. return $value > 0;
  47. }),
  48. ]);
  49. $this->expectException(\InvalidArgumentException::class, function () use ($resolver) {
  50. $resolver->resolve(['validOption' => 0]);
  51. });
  52. }
  53. }