public function testVariableStringHandling() { // variables handling by constructor $string = new Str('this is a string', ['those', 'are', 'variables']); $reflectedProperty = new \ReflectionProperty(Str::class, 'variables'); $reflectedProperty->setAccessible(true); $stringVariables = $reflectedProperty->getValue($string); $this->assertEquals(['those', 'are', 'variables'], $stringVariables); // anonymous placeholders $string = new Str('This string contains a %s'); $string->addVariable('placeholder'); $this->assertEquals('This string contains a placeholder', $string->build()); $string->clear(); $this->assertEquals('This string contains a %s', $string->build()); $string->setVariable(0, 'placeholder (again!)'); $this->assertEquals('This string contains a placeholder (again!)', $string->build()); // named placeholders $string = new Str('This string contains a :named-placeholder'); $string->setVariable('named-placeholder', 'named placeholder (I tell you!)'); $this->assertEquals('This string contains a named placeholder (I tell you!)', $string->build()); // mixed // // named placeholders are handled apart from anonymous ones, so // they aren't taken in account for anonymous variables position $string = new Str('This string contains both :named and %s placeholders!'); $string->setVariable('named', 'a named'); $string->addVariable('an anonymous'); $this->assertEquals('This string contains both a named and an anonymous placeholders!', $string->build()); $string->clear(); $string->setVariables(['an anonymous', 'named' => 'a named']); $this->assertEquals('This string contains both a named and an anonymous placeholders!', $string->build()); // finally check that __toString() calls build() $this->assertEquals('This string contains both a named and an anonymous placeholders!', (string) $string); }