diff --git a/src/Config/JSONCoercion.php b/src/Config/JSONCoercion.php new file mode 100644 index 00000000..472bf363 --- /dev/null +++ b/src/Config/JSONCoercion.php @@ -0,0 +1,16 @@ +assertEquals(true, $c->coerce('t')); + $this->assertEquals(true, $c->coerce('true')); + $this->assertEquals(true, $c->coerce('1')); + $this->assertEquals(true, $c->coerce('yes')); + $this->assertEquals(true, $c->coerce('YES')); + $this->assertEquals(true, $c->coerce('T')); + $this->assertEquals(true, $c->coerce('TRUE')); + + // Falses + $this->assertEquals(false, $c->coerce('f')); + $this->assertEquals(false, $c->coerce('false')); + $this->assertEquals(false, $c->coerce('no')); + $this->assertEquals(false, $c->coerce('0')); + } + + public function testIgnoresBooleans() + { + $c = new BoolCoercion(); + + $this->assertEquals(true, $c->coerce(true)); + $this->assertEquals(false, $c->coerce(false)); + } + + public function testNullIsFalse() + { + $c = new BoolCoercion(); + + $this->assertEquals(false, $c->coerce(null)); + } + + public function testAnythingElseIsFalse() + { + $c = new BoolCoercion(); + + // $c is "any object" + $this->assertEquals(false, $c->coerce($c)); + } +} diff --git a/tests/Config/JSONCoercionTest.php b/tests/Config/JSONCoercionTest.php new file mode 100644 index 00000000..446ce215 --- /dev/null +++ b/tests/Config/JSONCoercionTest.php @@ -0,0 +1,32 @@ +assertEquals([ + "foo" => 1 + ], $c->coerce('{"foo": 1}')); + } + + // Return null for any invalid JSON + public function testInvalidJSON() + { + $c = new JSONCoercion(); + $this->assertEquals(null, $c->coerce('foo: 1}')); + } + + public function testIgnoresNonString() + { + $c = new JSONCoercion(); + $this->assertEquals(10, $c->coerce(10)); + } +}