initRuntime() public method

Initializes the runtime environment.
public initRuntime ( )
 protected function setUp()
 {
     parent::setUp();
     $loader = new \Twig_Loader_Filesystem([__DIR__ . '/../../Resources/theme']);
     $cacheDir = sys_get_temp_dir() . '/twig-perf';
     if (!file_exists($cacheDir)) {
         @mkdir($cacheDir);
     }
     $this->twig = new \Twig_Environment($loader, ['debug' => true, 'strict_variables' => true, 'cache' => $cacheDir]);
     $this->extension = new DatagridExtension('datagrid.html.twig');
     $this->twig->addExtension($this->extension);
     $this->twig->initRuntime();
     // load template to ensure a compile, we are only interested in the rendering speed
     $this->twig->loadTemplate('datagrid.html.twig');
 }
 protected function setUp()
 {
     parent::setUp();
     $loader = new \Twig_Loader_Filesystem([__DIR__ . '/../../Resources/theme', __DIR__ . '/../Resources/views']);
     $cacheDir = sys_get_temp_dir() . '/twig' . microtime(false);
     if (!file_exists($cacheDir)) {
         @mkdir($cacheDir);
     }
     $twig = new \Twig_Environment($loader, ['debug' => true, 'cache' => $cacheDir]);
     $twig->addGlobal('global_var', 'global_value');
     $this->twig = $twig;
     $this->extension = new DatagridExtension('datagrid.html.twig');
     $this->twig->addExtension($this->extension);
     $this->twig->initRuntime();
 }
 public function testDataSourceRenderBlockFromParent()
 {
     $this->twig->addExtension($this->extension);
     $this->twig->initRuntime();
     $parent = $this->getMock('\\Twig_Template', array('hasBlock', 'render', 'display', 'getEnvironment', 'displayBlock', 'getParent', 'getTemplateName', 'doDisplay'), array($this->twig));
     $template = $this->getMock('\\Twig_Template', array('hasBlock', 'render', 'display', 'getEnvironment', 'displayBlock', 'getParent', 'getTemplateName', 'doDisplay'), array($this->twig));
     $template->expects($this->at(0))->method('hasBlock')->with('datasource_datasource_filter')->will($this->returnValue(false));
     $template->expects($this->at(1))->method('getParent')->with(array())->will($this->returnValue(false));
     $template->expects($this->at(2))->method('hasBlock')->with('datasource_filter')->will($this->returnValue(false));
     $template->expects($this->at(3))->method('getParent')->with(array())->will($this->returnValue($parent));
     $parent->expects($this->at(0))->method('hasBlock')->with('datasource_filter')->will($this->returnValue(true));
     $datasourceView = $this->getDataSourceView('datasource');
     $this->extension->setTheme($datasourceView, $template);
     $parent->expects($this->at(1))->method('displayBlock')->with('datasource_filter', array('datasource' => $datasourceView, 'vars' => array(), 'global_var' => 'global_value'))->will($this->returnValue(true));
     $this->extension->datasourceFilter($datasourceView);
 }
 public function testDataGridColumnActionCellActionRenderBlock()
 {
     $this->twig->addExtension($this->extension);
     $this->twig->initRuntime();
     $template = $this->getMock('\\Twig_Template', array('hasBlock', 'render', 'display', 'getEnvironment', 'displayBlock', 'getParent', 'getTemplateName', 'doDisplay'), array($this->twig));
     $template->expects($this->at(0))->method('hasBlock')->with('datagrid_grid_column_type_action_cell_action_edit')->will($this->returnValue(false));
     $template->expects($this->at(1))->method('getParent')->with(array())->will($this->returnValue(false));
     $template->expects($this->at(2))->method('hasBlock')->with('datagrid_column_type_action_cell_action_edit')->will($this->returnValue(false));
     $template->expects($this->at(3))->method('getParent')->with(array())->will($this->returnValue(false));
     $template->expects($this->at(4))->method('hasBlock')->with('datagrid_grid_column_type_action_cell_action')->will($this->returnValue(false));
     $template->expects($this->at(5))->method('getParent')->with(array())->will($this->returnValue(false));
     $template->expects($this->at(6))->method('hasBlock')->with('datagrid_column_type_action_cell_action')->will($this->returnValue(true));
     $this->extension->setBaseTheme($template);
     $datagridView = $this->getDataGridView('grid');
     $cellView = $this->getColumnCellView($datagridView, 'action', 'actions', array());
     $cellView->expects($this->any())->method('getAttribute')->with('translation_domain')->will($this->returnValue(null));
     $template->expects($this->at(7))->method('displayBlock')->with('datagrid_column_type_action_cell_action', array('content' => 'content', 'attr' => array(), 'translation_domain' => null, 'field_mapping_values' => array(), 'global_var' => 'global_value'))->will($this->returnValue(true));
     $this->extension->datagridColumnActionCellActionWidget($cellView, 'edit', 'content');
 }
Example #5
0
 public function testGlobals()
 {
     // globals can be added after calling getGlobals
     $twig = new Twig_Environment($this->getMock('Twig_LoaderInterface'));
     $twig->addGlobal('foo', 'foo');
     $twig->getGlobals();
     $twig->addGlobal('foo', 'bar');
     $globals = $twig->getGlobals();
     $this->assertEquals('bar', $globals['foo']);
     // globals can be modified after runtime init
     $twig = new Twig_Environment($this->getMock('Twig_LoaderInterface'));
     $twig->addGlobal('foo', 'foo');
     $twig->getGlobals();
     $twig->initRuntime();
     $twig->addGlobal('foo', 'bar');
     $globals = $twig->getGlobals();
     $this->assertEquals('bar', $globals['foo']);
     // globals can be modified after extensions init
     $twig = new Twig_Environment($this->getMock('Twig_LoaderInterface'));
     $twig->addGlobal('foo', 'foo');
     $twig->getGlobals();
     $twig->getFunctions();
     $twig->addGlobal('foo', 'bar');
     $globals = $twig->getGlobals();
     $this->assertEquals('bar', $globals['foo']);
     // globals can be modified after extensions and runtime init
     $twig = new Twig_Environment($loader = new Twig_Loader_Array(array('index' => '{{foo}}')));
     $twig->addGlobal('foo', 'foo');
     $twig->getGlobals();
     $twig->getFunctions();
     $twig->initRuntime();
     $twig->addGlobal('foo', 'bar');
     $globals = $twig->getGlobals();
     $this->assertEquals('bar', $globals['foo']);
     $twig = new Twig_Environment($loader);
     $twig->getGlobals();
     $twig->addGlobal('foo', 'bar');
     $template = $twig->loadTemplate('index');
     $this->assertEquals('bar', $template->render(array()));
     // globals cannot be added after runtime init
     $twig = new Twig_Environment($this->getMock('Twig_LoaderInterface'));
     $twig->addGlobal('foo', 'foo');
     $twig->getGlobals();
     $twig->initRuntime();
     try {
         $twig->addGlobal('bar', 'bar');
         $this->fail();
     } catch (LogicException $e) {
         $this->assertFalse(array_key_exists('bar', $twig->getGlobals()));
     }
     // globals cannot be added after extensions init
     $twig = new Twig_Environment($this->getMock('Twig_LoaderInterface'));
     $twig->addGlobal('foo', 'foo');
     $twig->getGlobals();
     $twig->getFunctions();
     try {
         $twig->addGlobal('bar', 'bar');
         $this->fail();
     } catch (LogicException $e) {
         $this->assertFalse(array_key_exists('bar', $twig->getGlobals()));
     }
     // globals cannot be added after extensions and runtime init
     $twig = new Twig_Environment($this->getMock('Twig_LoaderInterface'));
     $twig->addGlobal('foo', 'foo');
     $twig->getGlobals();
     $twig->getFunctions();
     $twig->initRuntime();
     try {
         $twig->addGlobal('bar', 'bar');
         $this->fail();
     } catch (LogicException $e) {
         $this->assertFalse(array_key_exists('bar', $twig->getGlobals()));
     }
     // test adding globals after initRuntime without call to getGlobals
     $twig = new Twig_Environment($this->getMock('Twig_LoaderInterface'));
     $twig->initRuntime();
     try {
         $twig->addGlobal('bar', 'bar');
         $this->fail();
     } catch (LogicException $e) {
         $this->assertFalse(array_key_exists('bar', $twig->getGlobals()));
     }
 }
 public function testGlobals()
 {
     // globals can be added after calling getGlobals
     $twig = new Twig_Environment(new Twig_Loader_String());
     $twig->addGlobal('foo', 'foo');
     $globals = $twig->getGlobals();
     $twig->addGlobal('foo', 'bar');
     $globals = $twig->getGlobals();
     $this->assertEquals('bar', $globals['foo']);
     // globals can be modified after runtime init
     $twig = new Twig_Environment(new Twig_Loader_String());
     $twig->addGlobal('foo', 'foo');
     $globals = $twig->getGlobals();
     $twig->initRuntime();
     $twig->addGlobal('foo', 'bar');
     $globals = $twig->getGlobals();
     $this->assertEquals('bar', $globals['foo']);
     // globals can be modified after extensions init
     $twig = new Twig_Environment(new Twig_Loader_String());
     $twig->addGlobal('foo', 'foo');
     $globals = $twig->getGlobals();
     $twig->getFunctions();
     $twig->addGlobal('foo', 'bar');
     $globals = $twig->getGlobals();
     $this->assertEquals('bar', $globals['foo']);
     // globals can be modified after extensions and runtime init
     $twig = new Twig_Environment(new Twig_Loader_String());
     $twig->addGlobal('foo', 'foo');
     $globals = $twig->getGlobals();
     $twig->getFunctions();
     $twig->initRuntime();
     $twig->addGlobal('foo', 'bar');
     $globals = $twig->getGlobals();
     $this->assertEquals('bar', $globals['foo']);
     // globals cannot be added after runtime init
     $twig = new Twig_Environment(new Twig_Loader_String());
     $twig->addGlobal('foo', 'foo');
     $globals = $twig->getGlobals();
     $twig->initRuntime();
     try {
         $twig->addGlobal('bar', 'bar');
         $this->fail();
     } catch (LogicException $e) {
         $this->assertFalse(array_key_exists('bar', $twig->getGlobals()));
     }
     // globals cannot be added after extensions init
     $twig = new Twig_Environment(new Twig_Loader_String());
     $twig->addGlobal('foo', 'foo');
     $globals = $twig->getGlobals();
     $twig->getFunctions();
     try {
         $twig->addGlobal('bar', 'bar');
         $this->fail();
     } catch (LogicException $e) {
         $this->assertFalse(array_key_exists('bar', $twig->getGlobals()));
     }
     // globals cannot be added after extensions and runtime init
     $twig = new Twig_Environment(new Twig_Loader_String());
     $twig->addGlobal('foo', 'foo');
     $globals = $twig->getGlobals();
     $twig->getFunctions();
     $twig->initRuntime();
     try {
         $twig->addGlobal('bar', 'bar');
         $this->fail();
     } catch (LogicException $e) {
         $this->assertFalse(array_key_exists('bar', $twig->getGlobals()));
     }
     // test adding globals after initRuntime without call to getGlobals
     $twig = new Twig_Environment(new Twig_Loader_String());
     $twig->initRuntime();
     try {
         $twig->addGlobal('bar', 'bar');
         $this->fail();
     } catch (LogicException $e) {
         $this->assertFalse(array_key_exists('bar', $twig->getGlobals()));
     }
 }
 /**
  * @requires PHP 5.3
  */
 public function testInitRuntimeWithAnExtensionUsingInitRuntimeDeprecation()
 {
     $twig = new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock());
     $twig->addExtension(new Twig_Tests_EnvironmentTest_ExtensionWithDeprecationInitRuntime());
     $this->deprecations = array();
     set_error_handler(array($this, 'handleError'));
     $twig->initRuntime();
     $this->assertCount(1, $this->deprecations);
     $this->assertContains('Defining the initRuntime() method in the "Twig_Tests_EnvironmentTest_ExtensionWithDeprecationInitRuntime" extension is deprecated since version 1.23.', $this->deprecations[0]);
     restore_error_handler();
 }
Example #8
0
 public function testInitRuntimeWithAnExtensionUsingInitRuntimeNoDeprecation()
 {
     $twig = new Twig_Environment($this->getMock('Twig_LoaderInterface'));
     $twig->addExtension(new Twig_Tests_EnvironmentTest_ExtensionWithoutDeprecationInitRuntime());
     $twig->initRuntime();
 }