diff --git a/src/Gwa/MockeryWpBridge.php b/src/Gwa/MockeryWpBridge.php index 0846132..0024152 100644 --- a/src/Gwa/MockeryWpBridge.php +++ b/src/Gwa/MockeryWpBridge.php @@ -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 = []; + /* -------- */ /** @@ -32,6 +46,8 @@ class MockeryWpBridge implements WpBridgeInterface public function addShortcode($tag, $func) { $this->shortcodes[$tag] = $func; + + return $this; } /** @@ -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); diff --git a/src/Gwa/Traits/WpBridgeTrait.php b/src/Gwa/Traits/WpBridgeTrait.php index 41767d9..cef70da 100644 --- a/src/Gwa/Traits/WpBridgeTrait.php +++ b/src/Gwa/Traits/WpBridgeTrait.php @@ -19,8 +19,6 @@ trait WpBridgeTrait * Set MockeryWpBridge. * * @param WpBridgeInterface $wpbridge - * - * @return WpBridgeTrait */ public function setWpBridge(WpBridgeInterface $wpbridge) { diff --git a/tests/MockeryWpBridgeTest.php b/tests/MockeryWpBridgeTest.php index 68d0a12..51ebcc4 100644 --- a/tests/MockeryWpBridgeTest.php +++ b/tests/MockeryWpBridgeTest.php @@ -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 = [