Skip to content

Commit

Permalink
Merge pull request #18 from gwa/issue-15-extend-wp-mockery
Browse files Browse the repository at this point in the history
added addAction and addFilter
  • Loading branch information
prisis committed Oct 1, 2015
2 parents f85521f + 2c3e2a8 commit 3c12f91
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 3 deletions.
95 changes: 94 additions & 1 deletion src/Gwa/MockeryWpBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,26 @@ class MockeryWpBridge implements WpBridgeInterface
private $mock;

/**
* All shortcodes
* All shortcodes.
*
* @var array
*/
private $shortcodes = [];

/**
* All filters.
*
* @var array
*/
private $filters = [];

/**
* All actions.
*
* @var array
*/
private $actions = [];

/* -------- */

/**
Expand All @@ -32,6 +46,8 @@ class MockeryWpBridge implements WpBridgeInterface
public function addShortcode($tag, $func)
{
$this->shortcodes[$tag] = $func;

return $this;
}

/**
Expand Down Expand Up @@ -89,6 +105,83 @@ public function __($text, $domain)

/* -------- */

/**
* Wordpress mock on add_filter func.
*
* @param string $filterName
* @param callback $filterCall
* @param int $prio
* @param int $numVars
*
* @return self
*/
public function addFilter($filterName, $filterCall, $prio, $numVars)
{
$this->filters[] = $this->add($filterName, $filterCall, $prio, $numVars);

return $this;
}

/**
* Get all added filters.
*
* @return array
*/
public function getAddedFilters()
{
return $this->filters;
}

/**
* Wordpress mock on add_action func.
*
* @param string $filterName
* @param callback $filterCall
* @param int $prio
* @param int $numVars
*
* @return self
*/
public function addAction($filterName, $filterCall, $prio, $numVars)
{
$this->actions[] = $this->add($filterName, $filterCall, $prio, $numVars);

return $this;
}

/**
* Get added actions
*
* @return array
*/
public function getAddedActions()
{
return $this->actions;
}

/**
* add
*
* @param string $filterName
* @param callback $filterCall
* @param int $prio
* @param int $numVars
*
* @return \stdClass
*/
private function add($filterName, $filterCall, $prio, $numVars)
{
$data = new \stdClass();
$data->filtername = $filterName;
$data->callback = $filterCall;
$data->prio = $prio;
$data->numvars = $numVars;

return $data;
}

/* -------- */

public function __call($function, $args)
{
return call_user_func_array([$this->mock, $function], $args);
Expand Down
2 changes: 0 additions & 2 deletions src/Gwa/Traits/WpBridgeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ trait WpBridgeTrait
* Set MockeryWpBridge.
*
* @param WpBridgeInterface $wpbridge
*
* @return WpBridgeTrait
*/
public function setWpBridge(WpBridgeInterface $wpbridge)
{
Expand Down
28 changes: 28 additions & 0 deletions tests/MockeryWpBridgeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,34 @@ public function testGetText()
$this->assertEquals('test', $bridge->__('test', 'MockTest'));
}

public function testAddFilter()
{
$bridge = new MockeryWpBridge();
$bridge->addFilter('network_admin_url', [$this, 'testGetText'], 1, 1);

$filters = $bridge->getAddedFilters();

$this->assertEquals(1, count($filters));
$this->assertEquals(1, $filters[0]->prio);
$this->assertEquals(1, $filters[0]->numvars);
$this->assertEquals('network_admin_url', $filters[0]->filtername);
$this->assertInternalType('array', $filters[0]->callback);
}

public function testAddAction()
{
$bridge = new MockeryWpBridge();
$bridge->addAction('network_admin_url', [$this, 'testGetText'], 1, 1);

$actions = $bridge->getAddedActions();

$this->assertEquals(1, count($actions));
$this->assertEquals(1, $actions[0]->prio);
$this->assertEquals(1, $actions[0]->numvars);
$this->assertEquals('network_admin_url', $actions[0]->filtername);
$this->assertInternalType('array', $actions[0]->callback);
}

public function testShortcodeAtts()
{
$atts = [
Expand Down

0 comments on commit 3c12f91

Please sign in to comment.