Example #1
0
 /**
  * Creates an instance with decorators specified in a YAML file
  * @param string $path The path to the file
  * @return Context
  * @throws Exception when the given file is not readable
  */
 public static function fromYaml($path)
 {
     if (!is_file($path)) {
         throw new Exception('File ' . $path . ' not found');
     }
     $parser = new Parser();
     $contexts = $parser->parse(file_get_contents($path));
     reset($contexts);
     $mainContextName = key($contexts);
     $mainContextDecorators = array_shift($contexts);
     if (is_array($mainContextDecorators)) {
         $mainContext = new Context($mainContextName);
         $mainSelector = $mainContext->select($mainContextName);
         $mainSelector->addMultiple($mainContextDecorators);
     } else {
         $file = dirname($path) . '/' . $mainContextDecorators . '.yaml';
         if (!is_file($file)) {
             throw new Exception('Cannot extend file ' . $file);
         }
         $mainContext = ContextFactory::fromYaml($file);
     }
     foreach ($contexts as $selector => $decorators) {
         $selector = $mainContext->select($selector);
         $selector->addMultiple($decorators);
     }
     return $mainContext;
 }
Example #2
0
 public function testNoContentRendersEmpty()
 {
     $decorator = new Content('{baz}');
     $context = new Context();
     $context->setContent('foo', 'bar');
     $decorator->context($context);
     $expected = '';
     $actual = $decorator->render();
     $this->assertEquals($expected, $actual);
 }
Example #3
0
 public function testComputedTags()
 {
     $context = new Context('foo');
     $context->setContent(array('one' => 'value', 'two' => '', 'three' => 'value'));
     $context->select('sub')->add('content', array('computedTags' => true));
     $decorator = new Contexts(array('name' => 'sub', 'separator' => ';'));
     $decorator->context($context);
     $actual = $decorator->render();
     $expected = 'first,even;empty,odd;last,even;';
     $this->assertEquals($expected, $actual);
 }
Example #4
0
 public function testSimple()
 {
     $context = new MainContext('foo');
     $context->add('dummy');
     $context->setContent(array('one', 'two'));
     $decorator = new Contexts('foo');
     $decorator->context($context);
     $actual = $decorator->render();
     $expected = 'dummy()dummy()';
     $this->assertEquals($actual, $expected);
 }
 public function testComputedTagSelectors()
 {
     $context = new Context('baz');
     $context->addComputedTag('foo');
     $this->assertTrue($context->applies('baz:foo'));
     $this->assertFalse($context->applies('baz:bar'));
     $context->addComputedTag('bar');
     $this->assertTrue($context->applies('baz:foo:bar'));
     $this->assertTrue($context->applies('baz:bar:foo'));
     $this->assertTrue($context->applies('baz:bar'));
     $this->assertTrue($context->applies('baz:foo'));
 }