/**
  * @expectedException \ConfigToken\TokenResolver\Exception\ScopeTokenValueSerializationException
  */
 public function testUnserializable()
 {
     $scope = array('a' => fopen('php://stdout', 'w'));
     $lines = array('Value of a is [[json:a]]');
     $string = implode("\n", $lines);
     $tokenParser = new TokenParser();
     $tokens = $tokenParser->parseString($string);
     $tokenResolver = new JsonScopeTokenResolver('json', $scope);
     $tokens->resolve($tokenResolver);
 }
 /**
  * @expectedException \ConfigToken\TokenResolver\Exception\TokenFormatException
  */
 public function testTokenFormatException()
 {
     $scope = array('a' => 'value of a');
     $lines = array('Value of a is [[json:]]');
     $string = implode("\n", $lines);
     $tokenParser = new TokenParser();
     $tokens = $tokenParser->parseString($string);
     $tokenResolver = new ScopeTokenResolver('json', $scope);
     $tokens->resolve($tokenResolver, false);
 }
Beispiel #3
0
 public function testCustomFilter()
 {
     $tokenFilter = new CustomTokenFilter();
     TokenFilterFactory::register($tokenFilter);
     $this->assertTrue(TokenFilterFactory::isRegisteredByName($tokenFilter::getName()));
     $data = '[[token|custom_test_filter|lower]]';
     $tokenResolver = new RegisteredTokenResolver(array('token' => 'Class.Method'));
     $tokenParser = new TokenParser();
     $tokens = $tokenParser->parseString($data);
     $tokens->resolve($tokenResolver);
     $token = $tokens->get($data);
     $this->assertEquals('class->method', $token->getTokenValue());
 }
 protected function getTokensFromArray(&$array, TokenParser $parser, &$result, $level = 0)
 {
     foreach ($array as $key => &$value) {
         $t = array();
         if (gettype($key) == 'string') {
             $tokens = $parser->parseString($key);
             if (!$tokens->isEmpty()) {
                 $t[self::_KEY] = $key;
                 $t[self::_KEY_TOKENS] = $tokens;
                 $t[self::_KEY_REF] =& $array;
             }
         }
         $valueType = gettype($value);
         if ($valueType == 'string') {
             $tokens = $parser->parseString($value);
             if (!$tokens->isEmpty()) {
                 $t[self::_VALUE] = $value;
                 $t[self::_VALUE_TOKENS] = $tokens;
                 $t[self::_VALUE_REF] =& $value;
             }
         }
         if (count($t) > 0) {
             $t[self::_LEVEL] = $level;
             $result[] = $t;
             unset($t);
         }
         if ($valueType == 'array') {
             $this->getTokensFromArray($value, $parser, $result, $level + 1);
         }
     }
     unset($value);
     if ($level == 0) {
         usort($result, function ($a, $b) {
             return $b[XrefTokenResolverCollection::_LEVEL] - $a[XrefTokenResolverCollection::_LEVEL];
         });
     }
 }
 public function getTokenParser()
 {
     if (!isset($this->tokenParser)) {
         $this->tokenParser = new TokenParser();
         if ($this->hasTokenFilterDelimiter()) {
             $this->tokenParser->setFilterDelimiter($this->getTokenFilterDelimiter());
         }
         if ($this->hasTokenRegex()) {
             $this->tokenParser->setTokenRegex($this->getTokenRegex());
         } else {
             if ($this->hasTokenPrefix() && $this->hasTokenSuffix()) {
                 $this->tokenParser->setTokenRegexByDelimiters($this->getTokenPrefix(), $this->getTokenSuffix());
             }
         }
     }
     return $this->tokenParser;
 }
Beispiel #6
0
 public function testSimpleTokenParser()
 {
     $data = array('a' => '[[token1]]', 'b' => '[[token5]]', 'c_[[token1~underscore~lower]]' => 'prefix-[[token1]]-suffix-[[token5]]', 'j' => '[[token6~dash~lower]]', 'd' => '[[token2~dash]]', 'e' => '[[token3~dot]]', 'f' => '[[token4~lower~unknown_filter]]', 'g' => '[[token5~upper]]', 'h' => '[[token5~underscore]]', 'i' => '[[token5~underscore~upper]]');
     $expected = array('a' => 'The Value-of.Token1', 'b' => 'The Value-of.Token5', 'c_the_value_of_token1' => 'prefix-The Value-of.Token1-suffix-The Value-of.Token5', 'j' => '[[token6~dash~lower]]', 'd' => 'The-Value-of-Token2', 'e' => 'The.Value.of.Token3', 'f' => 'the value-of.token4', 'g' => 'THE VALUE-OF.TOKEN5', 'h' => 'The_Value_of_Token5', 'i' => 'THE_VALUE_OF_TOKEN5');
     $jsonData = json_encode($data);
     $jsonExpectedData = json_encode($expected);
     $tokenParser = new TokenParser('~');
     $tokens = $tokenParser->parseString($jsonData);
     $this->assertEquals(md5($jsonData), $tokens->getSourceHash());
     $result = $tokens->findByName('token1');
     $this->assertEquals(2, count($result));
     $result = $tokens->findByName('token2');
     $this->assertEquals(1, count($tokens->findByName('token2')));
     $this->assertEquals(0, count($tokens->findByName('token7')));
     $this->assertTrue(isset($result['[[token2~dash]]']));
     $result = current($result);
     /** @var Token $result */
     $filters = $result->getFilters();
     $this->assertEquals(1, count($filters));
     $this->assertEquals('dash', current($filters));
     $this->assertEquals(4, count($tokens->findByName('token5')));
     $this->assertEquals(10, $tokens->getCount());
     $tokenValues = array('token1' => 'The Value-of.Token1', 'token2' => 'The_Value of.Token2', 'token3' => 'The Value-of_Token3', 'token4' => 'The Value-of.Token4', 'token5' => 'The Value-of.Token5', 'token7' => 'The Value-of.Token7');
     $tokenResolver = new RegisteredTokenResolver($tokenValues);
     $this->assertEquals($tokenValues, $tokenResolver->getRegisteredTokenValues());
     $this->assertEquals($tokenValues['token5'], $tokenResolver->getRegisteredTokenValue('token5'));
     $this->assertNull($tokenResolver->getRegisteredTokenValue('token6'));
     $tokens->resolve($tokenResolver);
     $resolvedTokens = $tokens->findResolved();
     $expectedResolved = array('[[token1]]', '[[token5]]', '[[token1~underscore~lower]]', '[[token2~dash]]', '[[token3~dot]]', '[[token4~lower~unknown_filter]]', '[[token5~upper]]', '[[token5~underscore]]', '[[token5~underscore~upper]]');
     $this->assertEquals($expectedResolved, array_keys($resolvedTokens));
     $resolvedAndNotInjectedTokens = $tokens->findResolvedAndNotInjected();
     $this->assertEquals($expectedResolved, array_keys($resolvedAndNotInjectedTokens));
     $this->assertTrue($tokens->hasUnresolved());
     $unresolvedTokens = $tokens->findUnresolved();
     $this->assertEquals(array('[[token6~dash~lower]]'), array_keys($unresolvedTokens));
     /** @var Token $unresolvedToken */
     $unresolvedToken = current($unresolvedTokens);
     $this->assertNull($unresolvedToken->getTokenValue());
     $this->assertEquals(array('dash', 'lower'), array_values($unresolvedToken->getUnresolvedFilters()));
     $withUnresolvedFilters = $tokens->findWithUnresolvedFilters();
     $this->assertEquals(2, count($withUnresolvedFilters));
     /** @var Token $token6 */
     $token6 = current($unresolvedTokens);
     $tokenResolver->registerTokenValue('token6', 'The Value of Token6');
     $token6->resolve($tokenResolver);
     $this->assertTrue($token6->getIsResolved());
     $tokenResolver->unRegisterTokenValue($token6->getTokenName());
     $this->assertFalse($tokenResolver->isTokenValueRegistered($token6->getTokenName()));
     try {
         $tokenResolver->unRegisterTokenValue($token6->getTokenName());
         $this->assertTrue(false);
     } catch (UnknownTokenException $e) {
         $this->assertTrue(true);
     }
     try {
         $tokenResolver->getTokenValue($token6->getTokenName(), false);
         $this->assertTrue(false);
     } catch (UnknownTokenException $e) {
         $this->assertTrue(true);
     }
     $token6->resolve($tokenResolver);
     $this->assertTrue($token6->getIsResolved());
     $this->assertTrue($token6->getIsFiltered());
     $this->assertFalse($token6->hasUnresolvedFilters());
     $unresolvedTokens = $tokens->findUnresolved();
     $this->assertEmpty($unresolvedTokens);
     $tokens->remove($token6);
     $this->assertFalse($tokens->has($token6->getTokenString()));
     unset($result);
     $result = $tokens->findByName('token4');
     $token4 = current($result);
     $this->assertTrue($token4->getIsResolved());
     $this->assertTrue($token4->getIsFiltered());
     $this->assertTrue($token4->hasUnresolvedFilters());
     try {
         $token4->applyFilters(false);
         $this->assertTrue(false);
     } catch (UnknownFilterException $e) {
         $this->assertTrue(true);
     }
     $withUnresolvedFilters = $tokens->findWithUnresolvedFilters();
     $this->assertEquals(1, count($withUnresolvedFilters));
     $tokenCollectionImp = new TokenCollection(array($token6->getTokenString() => $token6));
     $tokens->import($tokenCollectionImp);
     $this->assertTrue($tokens->has($token6->getTokenString()));
     $tokens->remove($token6);
     /** @var Token $tokenWithUnresolvedFilters */
     $tokenWithUnresolvedFilters = current($withUnresolvedFilters);
     $this->assertEquals(array('unknown_filter'), array_values($tokenWithUnresolvedFilters->getUnresolvedFilters()));
     $result = TokenInjector::injectString($jsonData, $tokens, true);
     $this->assertEquals($jsonExpectedData, $result);
     $tokens->clear();
     $this->assertEquals(0, $tokens->getCount());
 }