Skip to content

Commit

Permalink
[+]: add more tests v3
Browse files Browse the repository at this point in the history
  • Loading branch information
Lars Moelleken committed Jul 24, 2016
1 parent d0de3fa commit e5362d4
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/voku/helper/Hooks.php
Expand Up @@ -777,12 +777,18 @@ public function __call_all_hook($args)
*
* @param string $tag Shortcode tag to be searched in post content.
* @param callable $func Hook to run when shortcode is found.
*
* @return bool
*/
public function add_shortcode($tag, $func)
{
if (is_callable($func)) {
self::$shortcode_tags[$tag] = $func;

return true;
}

return false;
}

/**
Expand All @@ -791,10 +797,18 @@ public function add_shortcode($tag, $func)
* @since 1.0.0
*
* @param string $tag shortcode tag to remove hook for.
*
* @return bool
*/
public function remove_shortcode($tag)
{
unset(self::$shortcode_tags[$tag]);
if (isset(self::$shortcode_tags[$tag])) {
unset(self::$shortcode_tags[$tag]);

return true;
} else {
return false;
}
}

/**
Expand All @@ -803,10 +817,14 @@ public function remove_shortcode($tag)
* for removing all shortcodes.
*
* @since 1.0.0
*
* @return bool
*/
public function remove_all_shortcodes()
{
self::$shortcode_tags = array();

return true;
}

/**
Expand Down
33 changes: 33 additions & 0 deletions tests/HooksFooBar.php
@@ -0,0 +1,33 @@
<?php

/**
* Class HooksFooBar
*/
class HooksFooBar extends PHPUnit_Framework_TestCase
{
protected $foo = '';

/**
* @param $attrs
* @param string $content
*
* @return string
*/
public function doSomethingFunction($attrs, $content = '')
{
// init
$foo = '';

extract(
\voku\helper\Hooks::getInstance()->shortcode_atts(
array(
'foo',
),
$attrs
),
EXTR_OVERWRITE
);

return $this->foo . '<li class="' . $foo . '">' . $content . '</li>';
}
}
22 changes: 22 additions & 0 deletions tests/HooksTest.php
Expand Up @@ -191,6 +191,28 @@ public function testRunHookFunctions()
self::assertSame('foo', $hooks->apply_filters_ref_array('testFilter', array('Foo')));
}

public function testRunShortcodeFunctions()
{
require_once __DIR__ . '/HooksFooBar.php';

$hooks = Hooks::getInstance();

self::assertSame(true, $hooks->remove_all_shortcodes());

self::assertSame('testAction', $hooks->do_shortcode('testAction'));

$testClass = new HooksFooBar();
self::assertSame(true, $hooks->add_shortcode('testAction', array($testClass, 'doSomethingFunction')));
self::assertTrue($hooks->shortcode_exists('testAction'));

self::assertSame('foo bar <li class="">content</li>', $hooks->do_shortcode('foo bar [testAction foo="bar"]content[/testAction]'));

self::assertSame('foo bar ', $hooks->strip_shortcodes('foo bar [testAction foo="bar"]content[/testAction]'));

self::assertSame(true, $hooks->remove_shortcode('testAction'));
self::assertSame(false, $hooks->shortcode_exists('testAction'));
}

/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
Expand Down

0 comments on commit e5362d4

Please sign in to comment.