Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
gmazzap committed Mar 28, 2016
2 parents c6e7387 + 9ef08dc commit a9142f0
Show file tree
Hide file tree
Showing 12 changed files with 171 additions and 41 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Auto detect text files and perform LF normalization
* text=auto
text eol=lf

tests/ export-ignore
.travis.yml export-ignore
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ php:
- 5.5
- 5.6
- hhvm

matrix:
allow_failures:
- php: hhvm

before_script:
- composer self-update
- composer install --no-progress --prefer-source

script:
Expand Down
7 changes: 5 additions & 2 deletions src/Monkey/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,10 @@ public function echoArg($n = 1)

$value = func_num_args() > $n0 ? func_get_arg($n0) : '';

if (! is_scalar($value) && ! (is_object($value) && method_exists($value, '__toString'))) {
if (
! is_scalar($value)
&& ! (is_object($value) && method_exists($value, '__toString'))
) {
throw new RuntimeException(
sprintf(
"%s received as argument %d a %s, can't echo it.",
Expand All @@ -211,7 +214,7 @@ public function alias(callable $callback)
}

/**
* @param int $n
* @param int $n
* @return int
*/
private function ensureArg($n)
Expand Down
1 change: 1 addition & 0 deletions src/Monkey/MockeryBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public function __construct(ExpectationInterface $expectation, $parent = null)
$this->expectation = $expectation;
$name = $this->expectation->__toString();
if (is_string($parent) && class_exists($parent)) {
$parent = trim($parent, '\\');
$reflection = new \ReflectionClass($parent);
$this->isHook = $reflection->isSubclassOf('Brain\Monkey\WP\Hooks');
$this->isAction =
Expand Down
17 changes: 2 additions & 15 deletions src/Monkey/WP/Actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

namespace Brain\Monkey\WP;

use Brain\Monkey\MockeryBridge;
use Mockery;
use InvalidArgumentException;
use LogicException;
Expand All @@ -31,13 +30,7 @@ class Actions extends Hooks
*/
public static function expectFired($action)
{
$type = self::ACTION;
$sanitized = self::sanitizeHookName($action);
$mock = Mockery::mock("do_{$sanitized}");
$expectation = $mock->shouldReceive("do_{$type}_{$sanitized}");
parent::instance($type)->mocks[$sanitized]['run'] = $mock;

return new MockeryHookBridge(new MockeryBridge($expectation, __CLASS__));
return self::createBridgeFor(self::ACTION, $action, 'run');
}

/**
Expand All @@ -48,13 +41,7 @@ public static function expectFired($action)
*/
public static function expectAdded($action)
{
$type = self::ACTION;
$sanitized = self::sanitizeHookName($action);
$mock = Mockery::mock("add_{$sanitized}");
$expectation = $mock->shouldReceive("add_{$type}_{$sanitized}");
parent::instance($type)->mocks[$sanitized]['add'] = $mock;

return new MockeryHookBridge(new MockeryBridge($expectation, __CLASS__));
return self::createBridgeFor(self::ACTION, $action, 'add');
}

/**
Expand Down
44 changes: 28 additions & 16 deletions src/Monkey/WP/Filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

namespace Brain\Monkey\WP;

use Brain\Monkey\MockeryBridge;
use Mockery;
use LogicException;
use InvalidArgumentException;

Expand All @@ -22,28 +20,27 @@
*/
class Filters extends Hooks
{
/**
* @param string $filter
* @return \Brain\Monkey\WP\MockeryHookBridge
*/
public static function expectApplied($filter)
{
$type = self::FILTER;
$sanitized = self::sanitizeHookName($filter);
$mock = Mockery::mock("apply_{$sanitized}");
$expectation = $mock->shouldReceive("apply_{$type}_{$sanitized}");
parent::instance($type)->mocks[$sanitized]['run'] = $mock;

return new MockeryHookBridge(new MockeryBridge($expectation, __CLASS__));
return self::createBridgeFor(self::FILTER, $filter, 'run');
}

/**
* @param string $filter
* @return \Brain\Monkey\WP\MockeryHookBridge
*/
public static function expectAdded($filter)
{
$type = self::FILTER;
$sanitized = self::sanitizeHookName($filter);
$mock = Mockery::mock("add_{$sanitized}");
$expectation = $mock->shouldReceive("add_{$type}_{$sanitized}");
parent::instance($type)->mocks[$sanitized]['add'] = $mock;

return new MockeryHookBridge(new MockeryBridge($expectation, __CLASS__));
return self::createBridgeFor(self::FILTER, $filter, 'add');
}

/**
* @inheritdoc
*/
public function add()
{
$args = func_get_args();
Expand All @@ -52,6 +49,9 @@ public function add()
return call_user_func_array([$this, 'addHook'], $args);
}

/**
* @inheritdoc
*/
public function remove()
{
$args = func_get_args();
Expand All @@ -60,6 +60,9 @@ public function remove()
return call_user_func_array([$this, 'removeHook'], $args);
}

/**
* @inheritdoc
*/
public function run()
{
$args = func_get_args();
Expand All @@ -68,6 +71,9 @@ public function run()
return call_user_func_array([$this, 'runHook'], $args);
}

/**
* @inheritdoc
*/
public function runRef()
{
if (func_num_args() < 2 || ! is_array(func_get_arg(1))) {
Expand All @@ -79,6 +85,9 @@ public function runRef()
return call_user_func_array([$this, 'run'], $args);
}

/**
* @inheritdoc
*/
public function has()
{
$args = func_get_args();
Expand All @@ -102,6 +111,9 @@ public function applied($filter)
return in_array($filter, $this->done, true) ? array_count_values($this->done)[$filter] : 0;
}

/**
* @inheritdoc
*/
public function clean()
{
$this->cleanInstance($this);
Expand Down
35 changes: 33 additions & 2 deletions src/Monkey/WP/Hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace Brain\Monkey\WP;

use Brain\Monkey\MockeryBridge;
use Mockery;
use Closure;
use InvalidArgumentException;
Expand Down Expand Up @@ -238,7 +239,7 @@ protected function runHook($type)
if ($rawHook !== $hook && ! isset(self::$names[$hook])) {
self::$names[$hook] = $rawHook;
}
// returning value is always null for functions
// returning value is always null for actions
$value = $type === self::FILTER && func_num_args() > 2 ? func_get_arg(2) : null;
self::$current = $hook;
$instance->done[] = $hook;
Expand Down Expand Up @@ -366,6 +367,36 @@ private function callbackId(callable $callback)
return [$id, $hash];
}

/**
* @param string $type
* @param string $hook
* @param string $action
* @return \Brain\Monkey\WP\MockeryHookBridge
*/
protected static function createBridgeFor($type, $hook, $action = 'add')
{
$hook = self::sanitizeHookName($hook);
/** @var static $instance */
$instance = self::instance($type);
$prefix = $action;
($action === 'run') and $prefix = $type === self::FILTER ? 'apply' : 'do';
$method = "{$prefix}_{$type}_{$hook}";

$mock = null;
is_array($instance->mocks) or $instance->mocks = [];
if (! isset($instance->mocks[$hook]) || ! isset($instance->mocks[$hook][$action])) {
isset($instance->mocks[$hook]) or $instance->mocks[$hook] = [];
$mock = Mockery::mock("{$prefix}_{$hook}");
$instance->mocks[$hook][$action] = $mock;
}

$mock = $instance->mocks[$hook][$action];
$expectation = $mock->shouldReceive($method);
$parent = $type === self::FILTER ? '\Brain\Monkey\WP\Filters' : '\Brain\Monkey\WP\Actions';

return new MockeryHookBridge(new MockeryBridge($expectation, $parent));
}

/**
* @param string $name
* @return string
Expand All @@ -377,7 +408,7 @@ protected static function sanitizeHookName($name)
array_values(self::$sanitize_map),
$name
);
$clean = preg_replace('/[^\w]/i', '__', $replaced);
$clean = preg_replace('/[^a-z0-9_]/i', '__', $replaced);
if (is_numeric($clean[0])) {
$clean = '_'.$clean;
}
Expand Down
5 changes: 2 additions & 3 deletions tests/unit/FunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
*/
class FunctionsTest extends PHPUnit_Framework_TestCase
{

protected function tearDown()
{
Monkey::tearDown();
Expand Down Expand Up @@ -128,11 +127,13 @@ public function testSameFunctionDifferentArguments()
Functions::expect('issue5')
->with(true)
->once()
->ordered()
->andReturn('First!');

Functions::expect('issue5')
->with(false)
->once()
->ordered()
->andReturn('Second!');

assertSame('First!', issue5(true));
Expand All @@ -153,7 +154,6 @@ public function testJustEchoEmptyString()
i_do_not_exists();
}


/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessageRegExp /can't echo a var of type array/
Expand Down Expand Up @@ -183,7 +183,6 @@ public function testJustEchoObject()
i_do_not_exists();
}


public function testEchoArg()
{
Functions::when('i_do_not_exists')->echoArg(2);
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/WP/ActionAddTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,32 @@ public function testExpectAdded()
assertTrue(Monkey::actions()->has('init', 'strtoupper', 20));
}

public function testAddedSameActionDifferentArguments()
{
$f1 = function () {

};

$f2 = function () {

};

Monkey::actions()
->expectAdded('double_action')
->once()
->ordered()
->with($f1);

Monkey::actions()
->expectAdded('double_action')
->once()
->ordered()
->with($f2);

add_action('double_action', $f1);
add_action('double_action', $f2);
}

public function testRemoveAction()
{
Actions::expectAdded('init')->once();
Expand Down
19 changes: 19 additions & 0 deletions tests/unit/WP/ActionFireTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ public function testDoWithExpectationWhenHappen()
->whenHappen(function ($yes) use (&$works) {
$works = $yes;
});

$sum = 0;
Monkey::actions()->expectFired('sum')
->times(3)
Expand Down Expand Up @@ -162,4 +163,22 @@ public function testDoWithExpectationFailIfTryToReturn()
// well... that unicorns exist is a valid logic exception
Monkey::actions()->expectFired('foo')->zeroOrMoreTimes()->andReturn('Unicorns exist!');
}

public function testFiredSameActionDifferentArguments()
{
Monkey::actions()
->expectFired('double_action')
->once()
->ordered()
->with('arg_1');

Monkey::actions()
->expectFired('double_action')
->once()
->ordered()
->with('arg_2');

do_action('double_action', 'arg_1');
do_action('double_action', 'arg_2');
}
}
26 changes: 26 additions & 0 deletions tests/unit/WP/FilterAddTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,32 @@ public function testExpectAdded()
add_filter('the_excerpt', [$this, __FUNCTION__], 30);
}

public function testAddedSameFilterDifferentArguments()
{
$f1 = function () {

};

$f2 = function () {

};

Monkey::filters()
->expectAdded('double_filter')
->once()
->ordered()
->with($f1, 10);

Monkey::filters()
->expectAdded('double_filter')
->once()
->ordered()
->with($f2, 20);

add_filter('double_filter', $f1, 10);
add_filter('double_filter', $f2, 20);
}

public function testRemoveAction()
{
Filters::expectAdded('the_title')->once();
Expand Down
Loading

0 comments on commit a9142f0

Please sign in to comment.