2
0

DependentOptionsTest.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace Test\Michel\Resolver;
  3. use Michel\Resolver\Option;
  4. use Michel\Resolver\OptionsResolver;
  5. use Michel\UniTester\TestCase;
  6. class DependentOptionsTest 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->testDependentOptionIsRequired();
  19. $this->testDependentOptionIsNotRequired();
  20. $this->testDependentOptionIsProvided();
  21. }
  22. public function testDependentOptionIsRequired(): void
  23. {
  24. $resolver = new OptionsResolver([
  25. Option::string('auth_method'),
  26. Option::string('password'),
  27. ]);
  28. $resolver->addRequiredIf('password', 'auth_method', 'credentials');
  29. $this->expectException(\InvalidArgumentException::class, function () use ($resolver) {
  30. $resolver->resolve(['auth_method' => 'credentials']);
  31. });
  32. }
  33. public function testDependentOptionIsNotRequired(): void
  34. {
  35. $resolver = new OptionsResolver([
  36. Option::string('auth_method'),
  37. Option::string('password')->setOptional(),
  38. ]);
  39. $resolver->addRequiredIf('password', 'auth_method', 'credentials');
  40. $options = $resolver->resolve(['auth_method' => 'token']);
  41. $this->assertNull($options['password']);
  42. }
  43. public function testDependentOptionIsProvided(): void
  44. {
  45. $resolver = new OptionsResolver([
  46. Option::string('auth_method'),
  47. Option::string('password'),
  48. ]);
  49. $resolver->addRequiredIf('password', 'auth_method', 'credentials');
  50. $options = $resolver->resolve(['auth_method' => 'credentials', 'password' => 'secret']);
  51. $this->assertArrayHasKey('password', $options);
  52. $this->assertStrictEquals('secret', $options['password']);
  53. }
  54. }