From 75404a033833ef2af7cffc5147a3ae84341d3114 Mon Sep 17 00:00:00 2001 From: gotterdemarung Date: Tue, 10 Dec 2013 22:15:28 +0200 Subject: [PATCH] Stringy must correctly handle non-string arguments and implicitly cast to string to avoid problems in __toString() method --- tests/Stringy/StringyTest.php | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tests/Stringy/StringyTest.php b/tests/Stringy/StringyTest.php index c1e57d2..51c7e65 100644 --- a/tests/Stringy/StringyTest.php +++ b/tests/Stringy/StringyTest.php @@ -15,6 +15,35 @@ class StringyTestCase extends CommonTest $this->assertEquals('UTF-8', $stringy->encoding); } + public function testToString() + { + // Correct work for primitives + $this->assertSame('', (string) new S(null)); + $this->assertSame('', (string) new S(false)); + $this->assertSame('1', (string) new S(true)); + $this->assertSame('-9', (string) new S(-9)); + $this->assertSame('1.18', (string) new S(1.18)); + } + + /** + * @expectedException InvalidArgumentException + */ + public function testToStringArray() + { + (string) new S(array()); + $this->fail('Expecting exception on receiving array as constructor argument'); + } + + /** + * @expectedException InvalidArgumentException + */ + public function testToStringResource() + { + (string) new S(fopen('php://stdout', 'w')); + $this->fail('Expecting exception on receiving array as constructor argument'); + } + + public function testCreate() { $stringy = S::create('foo bar', 'UTF-8');