File modules/util/text/variables/VariablesTest.class.php
Last commit: Sat Mar 13 22:38:30 2021 +0100 Jan Dankert New filter for using links in text nodes (useful in CSS or script files)
1 <?php 2 3 namespace util\text\variables; 4 5 use util\test\TestCase; 6 7 class VariablesTest extends TestCase { 8 9 public function testResolver() { 10 $res = new \util\text\variables\VariableResolver(); 11 12 $example = <<<SRC 13 Hello \${planet:unknown planet}! 14 15 Are you ok? My name is \${me.name:unnamed} and robots name is \${me.\${nix.nada:name}}, i was born \${me.date:before some years}. 16 Message: \${message.somemessage:defaultMessage} 17 SRC; 18 19 $res->addDefaultResolver( function($x) {return 'world';} ); 20 $res->addResolver('me', function($t) {if ($t == 'name') return 'alice';return '';}); 21 $res->addResolver('message', function($t) {return 'this is a message';}); 22 23 $this->assertNotEmpty($res->resolveVariables( $example ) ); 24 } 25 26 27 /** 28 * Test variables with a prefix. 29 */ 30 public function testNamespaced() { 31 32 $resolver = new VariableResolver(); 33 $resolver->addDefaultResolver( function($key) { return 'default'; } ); 34 $resolver->addResolver( 'name', function($key) { return 'me'; } ); 35 $resolver->addResolver( 'cms', function($key) { return 'orcms'; } ); 36 37 $this->assertEquals( 'default',$resolver->resolveVariables('${x}') ); 38 $this->assertEquals( 'me',$resolver->resolveVariables('${name.x}') ); 39 $this->assertEquals( 'orcms',$resolver->resolveVariables('${cms.x}') ); 40 } 41 42 public function testSpecials() { 43 44 $resolver = new VariableResolver(); 45 $resolver->addDefaultResolver( ['0','1','2',''=>'space','name'=>'name'] ); 46 47 $resolver->marker = ''; 48 49 $resolver->parseString('a{0}b'); 50 51 $this->assertEquals( 'name',$resolver->resolveVariables('{name}') ); 52 $this->assertEquals( 'space',$resolver->resolveVariables('{}') ); 53 $this->assertEquals( '2',$resolver->resolveVariables('{2}') ); 54 $this->assertEquals( '1',$resolver->resolveVariables('{1}') ); 55 $this->assertEquals( '0',$resolver->resolveVariables('{0}') ); 56 } 57 } 58
Downloadmodules/util/text/variables/VariablesTest.class.php
History Sat, 13 Mar 2021 22:38:30 +0100 Jan Dankert New filter for using links in text nodes (useful in CSS or script files) Thu, 19 Nov 2020 21:42:39 +0100 Jan Dankert Fix: Variables with key '0' are now resolved. Thu, 10 Sep 2020 23:46:04 +0200 Jan Dankert New: Simple Unit-Testing. Wed, 13 May 2020 23:29:44 +0200 Jan Dankert Refactoring: New Variable Resolver with support for namespaces, default values and nested value expressions.