File modules/phariable/VariablesTest.class.php
Last commit: Thu Dec 26 19:49:19 2024 +0100 Jan Dankert New: Import the module 'phariable' via a new dependency script. The Variable Resolver has now it's own repository.
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/phariable/VariablesTest.class.php
History Thu, 26 Dec 2024 19:49:19 +0100 Jan Dankert New: Import the module 'phariable' via a new dependency script. The Variable Resolver has now it's own repository.