Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add files back #9

Merged
merged 2 commits into from
Jul 28, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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.
Expand All @@ -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
{
Expand Down
7 changes: 7 additions & 0 deletions src/Gwa/Contracts/WpBridgeInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
namespace Gwa\Wordpress\MockeryWpBridge\Contracts;

interface WpBridgeInterface
{
public function __call($function, $args);
}
105 changes: 105 additions & 0 deletions src/Gwa/MockeryWpBridge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php
namespace Gwa\Wordpress\MockeryWpBridge;

use Gwa\Wordpress\MockeryWpBridge\Contracts\WpBridgeInterface;
use Mockery;

class MockeryWpBridge implements WpBridgeInterface
{
/**
* Mockery instance.
*
* @var \Mockery
*/
private $mock;

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

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

/**
* Add a shortcode.
*
* @param string $tag
*
* @param mixed $func
*/
public function addShortcode($tag, $func)
{
$this->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;
}
}
41 changes: 41 additions & 0 deletions src/Gwa/Traits/WpBridgeTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
namespace Gwa\Wordpress\MockeryWpBridge\Traits;

use Gwa\Wordpress\MockeryWpBridge\Contracts\WpBridgeInterface;

/**
* Trait to be used by all classes that use MockeryWpBridge
*/
trait WpBridgeTrait
{
/**
* MockeryWpBridge instance.
*
* @var \Gwa\Wordpress\MockeryWpBridge\Contracts\WpBridgeInterface $wpbridge
*/
private $wpbridge;

/**
* Set MockeryWpBridge.
*
* @param WpBridgeInterface $wpbridge
*
* @return WpBridgeTrait
*/
public function setWpBridge(WpBridgeInterface $wpbridge)
{
$this->wpbridge = $wpbridge;

return $this;
}

/**
* Get MockeryWpBridge.
*
* @return WpBridge
*/
public function getWpBridge()
{
return $this->wpbridge;
}
}
40 changes: 40 additions & 0 deletions src/Gwa/WpBridge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
namespace Gwa\Wordpress\MockeryWpBridge;

use Gwa\Wordpress\MockeryWpBridge\Contracts\WpBridgeInterface;

/**
* Allows us to use a class to call methods in the global namespace.
* Methods should be called in camelcase.
*
* To call
* wp_get_attachment_image_src(...);
* use
* $bridge->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));
}
}
2 changes: 1 addition & 1 deletion tests/MockeryWpBridgeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions tests/WpBridgeTest.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php
namespace Gwa\Wordpress\MockeryWpBridge\Tests;

use Gwa\Wordpress\MockeryWpBridge\WPBridge;
use Gwa\Wordpress\MockeryWpBridge\WpBridge;

class WpBridgeTest extends \PHPUnit_Framework_TestCase
{
public function testCamelToUnderscore()
{
$bridge = new WPBridge();
$bridge = new WpBridge();
$this->assertEquals('foo_bar', $bridge->camelToUnderscore('fooBar'));
$this->assertEquals('wp_get_attachment_image_src', $bridge->camelToUnderscore('wpGetAttachmentImageSrc'));
$this->assertEquals('__', $bridge->camelToUnderscore('__'));
Expand All @@ -16,7 +16,7 @@ public function testCamelToUnderscore()

public function testCallGlobalFunction()
{
$bridge = new WPBridge();
$bridge = new WpBridge();
$result = $bridge->strRepeat('##', 2);
$this->assertEquals('####', $result);

Expand Down
6 changes: 3 additions & 3 deletions tests/WpBridgeTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand All @@ -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();
}
}

Expand Down