lithium\tests\cases\util\StringTest::testInsert PHP Method

testInsert() public method

public testInsert ( )
    public function testInsert()
    {
        $string = '2 + 2 = {:sum}. Lithium is {:adjective}.';
        $expected = '2 + 2 = 4. Lithium is yummy.';
        $result = String::insert($string, array('sum' => '4', 'adjective' => 'yummy'));
        $this->assertEqual($expected, $result);
        $string = '2 + 2 = %sum. Lithium is %adjective.';
        $result = String::insert($string, array('sum' => '4', 'adjective' => 'yummy'), array('before' => '%', 'after' => ''));
        $this->assertEqual($expected, $result);
        $string = '2 + 2 = 2sum2. Lithium is 9adjective9.';
        $result = String::insert($string, array('sum' => '4', 'adjective' => 'yummy'), array('format' => '/([\\d])%s\\1/'));
        $this->assertEqual($expected, $result);
        $string = '2 + 2 = 12sum21. Lithium is 23adjective45.';
        $expected = '2 + 2 = 4. Lithium is 23adjective45.';
        $result = String::insert($string, array('sum' => '4', 'adjective' => 'yummy'), array('format' => '/([\\d])([\\d])%s\\2\\1/'));
        $this->assertEqual($expected, $result);
        $string = '{:web} {:web_site}';
        $expected = 'www http';
        $result = String::insert($string, array('web' => 'www', 'web_site' => 'http'));
        $this->assertEqual($expected, $result);
        $string = '2 + 2 = <sum. Lithium is <adjective>.';
        $expected = '2 + 2 = <sum. Lithium is yummy.';
        $result = String::insert($string, array('sum' => '4', 'adjective' => 'yummy'), array('before' => '<', 'after' => '>'));
        $this->assertEqual($expected, $result);
        $string = '2 + 2 = \\:sum. Lithium is :adjective.';
        $expected = '2 + 2 = :sum. Lithium is yummy.';
        $result = String::insert($string, array('sum' => '4', 'adjective' => 'yummy'), array('before' => ':', 'after' => null, 'escape' => '\\'));
        $this->assertEqual($expected, $result);
        $string = '2 + 2 = !:sum. Lithium is :adjective.';
        $result = String::insert($string, array('sum' => '4', 'adjective' => 'yummy'), array('escape' => '!', 'before' => ':', 'after' => ''));
        $this->assertEqual($expected, $result);
        $string = '2 + 2 = \\%sum. Lithium is %adjective.';
        $expected = '2 + 2 = %sum. Lithium is yummy.';
        $result = String::insert($string, array('sum' => '4', 'adjective' => 'yummy'), array('before' => '%', 'after' => '', 'escape' => '\\'));
        $this->assertEqual($expected, $result);
        $string = ':a :b \\:a :a';
        $expected = '1 2 :a 1';
        $result = String::insert($string, array('a' => 1, 'b' => 2), array('before' => ':', 'after' => '', 'escape' => '\\'));
        $this->assertEqual($expected, $result);
        $string = '{:a} {:b} {:c}';
        $expected = '2 3';
        $result = String::insert($string, array('b' => 2, 'c' => 3), array('clean' => true));
        $this->assertEqual($expected, $result);
        $string = '{:a} {:b} {:c}';
        $expected = '1 3';
        $result = String::insert($string, array('a' => 1, 'c' => 3), array('clean' => true));
        $this->assertEqual($expected, $result);
        $string = '{:a} {:b} {:c}';
        $expected = '2 3';
        $result = String::insert($string, array('b' => 2, 'c' => 3), array('clean' => true));
        $this->assertEqual($expected, $result);
        $string = ':a, :b and :c';
        $expected = '2 and 3';
        $result = String::insert($string, array('b' => 2, 'c' => 3), array('clean' => true, 'before' => ':', 'after' => ''));
        $this->assertEqual($expected, $result);
        $string = '{:a}, {:b} and {:c}';
        $expected = '2 and 3';
        $result = String::insert($string, array('b' => 2, 'c' => 3), array('clean' => true));
        $this->assertEqual($expected, $result);
        $string = '"{:a}, {:b} and {:c}"';
        $expected = '"1, 2"';
        $result = String::insert($string, array('a' => 1, 'b' => 2), array('clean' => true));
        $this->assertEqual($expected, $result);
        $string = '"${a}, ${b} and ${c}"';
        $expected = '"1, 2"';
        $result = String::insert($string, array('a' => 1, 'b' => 2), array('before' => '${', 'after' => '}', 'clean' => true));
        $this->assertEqual($expected, $result);
        $string = '<img src="{:src}" alt="{:alt}" class="foo {:extra} bar"/>';
        $expected = '<img src="foo" class="foo bar"/>';
        $result = String::insert($string, array('src' => 'foo'), array('clean' => 'html'));
        $this->assertEqual($expected, $result);
        $string = '<img src=":src" class=":no :extra"/>';
        $expected = '<img src="foo"/>';
        $result = String::insert($string, array('src' => 'foo'), array('clean' => 'html', 'before' => ':', 'after' => ''));
        $this->assertEqual($expected, $result);
        $string = '<img src="{:src}" class="{:no} {:extra}"/>';
        $expected = '<img src="foo" class="bar"/>';
        $result = String::insert($string, array('src' => 'foo', 'extra' => 'bar'), array('clean' => 'html'));
        $this->assertEqual($expected, $result);
        $result = String::insert("this is a ? string", array("test"));
        $expected = "this is a test string";
        $this->assertEqual($expected, $result);
        $result = String::insert("this is a ? string with a ? ? ?", array('long', 'few?', 'params', 'you know'));
        $expected = "this is a long string with a few? params you know";
        $this->assertEqual($expected, $result);
        $result = String::insert('update saved_urls set url = :url where id = :id', array('url' => 'http://testurl.com/param1:url/param2:id', 'id' => 1), array('before' => ':', 'after' => ''));
        $expected = "update saved_urls set url = http://testurl.com/param1:url/param2:id ";
        $expected .= "where id = 1";
        $this->assertEqual($expected, $result);
        $result = String::insert('update saved_urls set url = :url where id = :id', array('id' => 1, 'url' => 'http://www.testurl.com/param1:url/param2:id'), array('before' => ':', 'after' => ''));
        $expected = "update saved_urls set url = http://www.testurl.com/param1:url/param2:id ";
        $expected .= "where id = 1";
        $this->assertEqual($expected, $result);
        $result = String::insert('{:me} lithium. {:subject} {:verb} fantastic.', array('me' => 'I :verb', 'subject' => 'lithium', 'verb' => 'is'));
        $expected = "I :verb lithium. lithium is fantastic.";
        $this->assertEqual($expected, $result);
        $result = String::insert(':I.am: :not.yet: passing.', array('I.am' => 'We are'), array('before' => ':', 'after' => ':', 'clean' => array('replacement' => ' of course', 'method' => 'text')));
        $expected = "We are of course passing.";
        $this->assertEqual($expected, $result);
        $result = String::insert(':I.am: :not.yet: passing.', array('I.am' => 'We are'), array('before' => ':', 'after' => ':', 'clean' => true));
        $expected = "We are passing.";
        $this->assertEqual($expected, $result);
        $result = String::insert('?-pended result', array('Pre'));
        $expected = "Pre-pended result";
        $this->assertEqual($expected, $result);
    }