Skip to content

Commit

Permalink
Merge pull request #7 from timber/5_tests
Browse files Browse the repository at this point in the history
add more coverage
  • Loading branch information
jarednova committed May 26, 2016
2 parents c6c5c0d + 28c91a5 commit b1bbc73
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
18 changes: 18 additions & 0 deletions tests/test-timber-sugar.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,24 @@ function testDummy() {
$this->assertEquals(10, str_word_count($str));
}

function testDummyFilter() {
$post_id = $this->factory->post->create(array('post_content' => ''));
$post = new TimberPost($post_id);
$template = '{{post.post_content|dummy(10)}}';
$str = Timber::compile_string($template, array('post' => $post));
$str = str_replace('.', '', $str);
$str = str_replace(',', '', $str);
$this->assertEquals(10, str_word_count($str));
}

function testDummyFilterNadda() {
$post_id = $this->factory->post->create(array('post_content' => 'Jared is Cool'));
$post = new TimberPost($post_id);
$template = '{{post.post_content|dummy(10)}}';
$str = Timber::compile_string($template, array('post' => $post));
$this->assertEquals('Jared is Cool', $str);
}

function testTwitterify() {
$tweet = 'Love this thing from @jaredNova about #timberwp http://upstatement.com';
$template = '{{tweet|twitterify}}';
Expand Down
7 changes: 3 additions & 4 deletions timber-sugar.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
class TimberSugar {

function __construct(){
add_filter('get_twig', function($twig){
add_filter('get_twig', function( $twig ){
$twig->addFilter('dummy', new Twig_Filter_Function(array(__CLASS__, 'apply_dummy_filter')));
$twig->addFunction(new Twig_SimpleFunction('dummy', array(__CLASS__, 'apply_dummy')));

Expand All @@ -21,21 +21,20 @@ function __construct(){
});
}

static function apply_dummy_filter($text, $words){
static function apply_dummy_filter( $text, $words = 100 ){
if (!strlen(trim($text))){
return self::apply_dummy($words);
}
return $text;
}

static function apply_dummy($word_count){
static function apply_dummy( $word_count = 100 ){
$text = file_get_contents(__DIR__.'/assets/lorem-ipsum.txt');
$words = explode(' ', $text);
$starting_word = rand(0, count($words));
$more_words = array_merge($words, $words);
$resulting_words = array_slice($more_words, $starting_word, $word_count);
$text = implode(' ', $resulting_words);
error_log('words='.$text.'/');
return ucfirst($text);
}

Expand Down

0 comments on commit b1bbc73

Please sign in to comment.