From 7e55c990e014c8cdb3d825f91ebaff709f5df786 Mon Sep 17 00:00:00 2001 From: anolilab Date: Tue, 28 Jul 2015 10:52:01 +0200 Subject: [PATCH 1/2] add files back --- README.md | 8 +- src/Gwa/Contracts/WpBridgeInterface.php | 7 ++ src/Gwa/MockeryWpBridge.php | 105 ++++++++++++++++++++++++ src/Gwa/Traits/WpBridgeTrait.php | 41 +++++++++ src/Gwa/WpBridge.php | 40 +++++++++ 5 files changed, 197 insertions(+), 4 deletions(-) create mode 100644 src/Gwa/Contracts/WpBridgeInterface.php create mode 100644 src/Gwa/MockeryWpBridge.php create mode 100644 src/Gwa/Traits/WpBridgeTrait.php create mode 100644 src/Gwa/WpBridge.php diff --git a/README.md b/README.md index 5256fde..ccbad59 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# MockeryWPBridge +# MockeryWpBridge [![Latest Version on Packagist](https://img.shields.io/packagist/v/gwa/mockery-wp-bridge.svg?style=flat-square)](https://packagist.org/packages/gwa/mockery-wp-bridge) [![Total Downloads](https://img.shields.io/packagist/dt/gwa/mockery-wp-bridge.svg?style=flat-square)](https://packagist.org/packages/gwa/mockery-wp-bridge) @@ -20,10 +20,10 @@ $ composer require gwa/mockery-wp-bridge ## Usage -First init ```MockeryWPBridge``` class. +First init ```MockeryWpBridge``` class. ```php -$bridge = new \Gwa\Wordpress\MockeryWPBridge\MockeryWPBridge(); +$bridge = new \Gwa\Wordpress\MockeryWpBridge\MockeryWpBridge(); ``` Now it allows us to use a class to call methods in the global namespace. @@ -40,7 +40,7 @@ $bridge->wpGetAttachmentImageSrc(...); Or you like to use a trait, than set ```WpBridgeTrait``` in a class. ```php -use Gwa\Wordpress\MockeryWPBridge\Traits\WpBridgeTrait; +use Gwa\Wordpress\MockeryWpBridge\Traits\WpBridgeTrait; class TestClass { diff --git a/src/Gwa/Contracts/WpBridgeInterface.php b/src/Gwa/Contracts/WpBridgeInterface.php new file mode 100644 index 0000000..8ad8d26 --- /dev/null +++ b/src/Gwa/Contracts/WpBridgeInterface.php @@ -0,0 +1,7 @@ +shortcodes[$tag] = $func; + } + + /** + * Check if shortcode exist. + * + * @param string $tag + * + * @return boolean + */ + public function hasShortcode($tag) + { + return isset($this->shortcodes[$tag]); + } + + /** + * Get a shortcode callback. + * + * @param string $tag + * + * @return mixed + */ + public function getShortcodeCallback($tag) + { + return isset($this->shortcodes[$tag]) ? $this->shortcodes[$tag] : null; + } + + /** + * Combines shortcode attributes with known attributes and fills in defaults when needed. + * + * @param array $pairs + * @param array $atts + * @param string|null $shortcode + * + * @return array + */ + public function shortcodeAtts($pairs, $atts, $shortcode = null) + { + return array_merge($pairs, $atts); + } + + /* -------- */ + + /** + * Wordpress mock on __() func. + * + * @param string $text + * @param string $domain + * + * @return string + */ + public function __($text, $domain) + { + return $text; + } + + /* -------- */ + + public function __call($function, $args) + { + return call_user_func_array([$this->mock, $function], $args); + } + + public function mock() + { + if (!isset($this->mock)) { + $this->mock = Mockery::mock('WpBridge'); + } + + return $this->mock; + } +} diff --git a/src/Gwa/Traits/WpBridgeTrait.php b/src/Gwa/Traits/WpBridgeTrait.php new file mode 100644 index 0000000..41767d9 --- /dev/null +++ b/src/Gwa/Traits/WpBridgeTrait.php @@ -0,0 +1,41 @@ +wpbridge = $wpbridge; + + return $this; + } + + /** + * Get MockeryWpBridge. + * + * @return WpBridge + */ + public function getWpBridge() + { + return $this->wpbridge; + } +} diff --git a/src/Gwa/WpBridge.php b/src/Gwa/WpBridge.php new file mode 100644 index 0000000..d316964 --- /dev/null +++ b/src/Gwa/WpBridge.php @@ -0,0 +1,40 @@ +wpGetAttachmentImageSrc(...); + */ +class WpBridge implements WpBridgeInterface +{ + /** + * Magic call on all camel wordpress functions. + * + * @param string $function + * + * @return array + */ + public function __call($function, $args) + { + return call_user_func_array($this->camelToUnderscore($function), $args); + } + + /** + * Rename camelcase to underscore. + * + * @param string $string + * + * @return string + */ + public function camelToUnderscore($string) + { + return strtolower(preg_replace('/([a-z])([A-Z0-9])/', '$1_$2', $string)); + } +} From e27b9b7d17f1d9acbdb03e5ba5766599182ffb52 Mon Sep 17 00:00:00 2001 From: anolilab Date: Tue, 28 Jul 2015 10:54:21 +0200 Subject: [PATCH 2/2] fix WP to Wp --- tests/MockeryWpBridgeTest.php | 2 +- tests/WpBridgeTest.php | 6 +++--- tests/WpBridgeTraitTest.php | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/MockeryWpBridgeTest.php b/tests/MockeryWpBridgeTest.php index 56fe463..9f4ae27 100644 --- a/tests/MockeryWpBridgeTest.php +++ b/tests/MockeryWpBridgeTest.php @@ -8,7 +8,7 @@ class MockeryWpBridgeTest extends \PHPUnit_Framework_TestCase public function testGetMock() { $bridge = new MockeryWpBridge(); - $this->assertInstanceOf('\Mockery_0__WPBridge', $bridge->mock()); + $this->assertInstanceOf('\Mockery_0__WpBridge', $bridge->mock()); } public function testMockFunction() diff --git a/tests/WpBridgeTest.php b/tests/WpBridgeTest.php index 3caf9b2..2e7a262 100644 --- a/tests/WpBridgeTest.php +++ b/tests/WpBridgeTest.php @@ -1,13 +1,13 @@ assertEquals('foo_bar', $bridge->camelToUnderscore('fooBar')); $this->assertEquals('wp_get_attachment_image_src', $bridge->camelToUnderscore('wpGetAttachmentImageSrc')); $this->assertEquals('__', $bridge->camelToUnderscore('__')); @@ -16,7 +16,7 @@ public function testCamelToUnderscore() public function testCallGlobalFunction() { - $bridge = new WPBridge(); + $bridge = new WpBridge(); $result = $bridge->strRepeat('##', 2); $this->assertEquals('####', $result); diff --git a/tests/WpBridgeTraitTest.php b/tests/WpBridgeTraitTest.php index 1fcad7c..67b2bd4 100644 --- a/tests/WpBridgeTraitTest.php +++ b/tests/WpBridgeTraitTest.php @@ -10,7 +10,7 @@ class WpBridgeTraitTest extends \PHPUnit_Framework_TestCase public function testTraitSetWpBridge() { - $this->setWPBridge(new MockeryWpBridge()); + $this->setWpBridge(new MockeryWpBridge()); $this->assertInstanceOf('Gwa\Wordpress\MockeryWpBridge\Contracts\WpBridgeInterface', $this->getWpBridge()); } @@ -19,11 +19,11 @@ public function testTraitSetWpBridgeOutsideAClass() { $outside = new TestClass(); - $outside->setWPBridge(new MockeryWpBridge()); + $outside->setWpBridge(new MockeryWpBridge()); $this->assertInstanceOf('Gwa\Wordpress\MockeryWpBridge\Contracts\WpBridgeInterface', $outside->getWpBridge()); - (new TestClass())->setWPBridge(new MockeryWpBridge())->init(); + (new TestClass())->setWpBridge(new MockeryWpBridge())->init(); } }