HEX
Server: Apache/2.4.41 (Ubuntu)
System: Linux wordpress-ubuntu-s-2vcpu-4gb-fra1-01 5.4.0-169-generic #187-Ubuntu SMP Thu Nov 23 14:52:28 UTC 2023 x86_64
User: root (0)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/delta/wp-content/themes/delta/vendor/timber/timber/tests/test-timber-function-wrapper.php
<?php

class TestTimberFunctionWrapper extends Timber_UnitTestCase {

	function testToStringWithException() {
		ob_start();
		$wrapper = new TimberFunctionWrapper('TestTimberFunctionWrapper::isNum', array('hi'));
		echo $wrapper;
		$content = trim(ob_get_contents());
		ob_end_clean();
		$this->assertEquals('Caught exception: Argument must be of type integer', $content);
	}

	function testToStringWithoutException() {
		ob_start();
		$wrapper = new TimberFunctionWrapper('TestTimberFunctionWrapper::isNum', array(4));
		echo $wrapper;
		$content = trim(ob_get_contents());
		ob_end_clean();
		$this->assertEquals(1, $content);
	}

	function testToStringWithClassObject() {
		ob_start();
		$wrapper = new TimberFunctionWrapper(array($this, 'isNum'), array(4));
		echo $wrapper;
		$content = trim(ob_get_contents());
		ob_end_clean();
		$this->assertEquals(1, $content);
	}

	function testToStringWithClassString() {
		ob_start();
		$wrapper = new TimberFunctionWrapper(array(get_class($this), 'isNum'), array(4));
		echo $wrapper;
		$content = trim(ob_get_contents());
		ob_end_clean();
		$this->assertEquals(1, $content);
	}

	function testWPHead() {
		$context = Timber::context();
		$str = Timber::compile_string('{{ wp_head }}', $context);
		$this->assertMatchesRegularExpression('/<title>Test Blog/', trim($str));
	}

	function testFunctionInTemplate() {
		$context = Timber::context();
		$str = Timber::compile_string("{{ function('my_boo') }}", $context);
		$this->assertEquals('bar!', trim($str));
	}

	function testSoloFunctionUsingWrapper() {
		new TimberFunctionWrapper('my_boo');
		$str = Timber::compile_string("{{ my_boo() }}");
		$this->assertEquals('bar!', trim($str));
	}

	function testNakedSoloFunction() {
		add_filter('timber/twig/functions', function( $twig ) {
			$twig->addFunction(new Timber\Twig_Function('your_boo', array($this, 'your_boo')) );
			return $twig;
		});
		$context = Timber::context();
		$str = Timber::compile_string("{{ your_boo() }}", $context);
		$this->assertEquals('yourboo', trim($str));
	}

	/* Sample function to test exception handling */

	static function isNum($num) {
		if(!is_int($num)) {
			throw new Exception("Argument must be of type integer");
		} else {
			return true;
		}
	}

	function your_boo() {
		return 'yourboo';
	}

}



function my_boo() {
	return 'bar!';
}