Skip to content

Commit

Permalink
Refactoring for version 2
Browse files Browse the repository at this point in the history
  • Loading branch information
gmazzap committed Apr 30, 2017
1 parent b066ca3 commit db4ec92
Show file tree
Hide file tree
Showing 46 changed files with 3,076 additions and 1,425 deletions.
11 changes: 2 additions & 9 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
Thumbs.db
ehthumbs.db
Desktop.ini
$RECYCLE.BIN/
.DS_Store
.coverage
.tox
.idea/
vendor/
composer.lock
/composer.lock
/phpunit.xml
website/
couscous-theme/
couscous.*
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2015 Giuseppe Mazzapica
Copyright (c) 2017 Giuseppe Mazzapica

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ Brain Monkey is a tests utility for PHP.
It provides **two set of helpers**:

- the first are framework-agnostic tools that allow to mock (or *monkey patch*) and to test behavior of any PHP function
- the second are specific to WordPress and make unit test WordPress extensions a no brainer.
- the second are specific to WordPress and make unit test WordPress extensions a no-brainer.

# Requirements

- PHP 5.4+
- PHP 5.6+
- [Composer](https://getcomposer.org/) to install

# License
Expand All @@ -28,4 +28,6 @@ Brain Monkey is hosted on GitHub. Feel free to open issues there for suggestions

# Who's Behind

I'm Giuseppe, I deal with PHP since 2005. For questions, rants or chat ping me on Twitter ([@gmazzap](https://twitter.com/gmazzap)) or on ["The Loop"](http://chat.stackexchange.com/rooms/6/the-loop) (Stack Exchange) chat. Well, it's possible I'll ignore rants.
I'm Giuseppe, I deal with PHP since 2005. For questions, rants or chat ping me on Twitter ([@gmazzap](https://twitter.com/gmazzap))
or on ["The Loop"](http://chat.stackexchange.com/rooms/6/the-loop) (Stack Exchange) chat.
Well, it's possible I'll ignore rants.
28 changes: 19 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
{
"name": "Giuseppe Mazzapica",
"email": "[email protected]",
"homepage": "http://gm.zoomlab.it",
"homepage": "https://gmazzap.me",
"role": "Developer"
}
],
Expand All @@ -27,21 +27,30 @@
},
"license": "MIT",
"require": {
"php": ">=5.4.0",
"mockery/mockery": "*",
"php": ">=5.6.0",
"mockery/mockery": "1.*",
"antecedent/patchwork": "2.0.*"
},
"require-dev": {
"phpunit/phpunit": "~4.8",
"mockery/mockery": "0.9.3"
"phpunit/phpunit": "~5.7.9"
},
"autoload": {
"psr-4": {
"Brain\\": "src/"
}
"Brain\\Monkey\\": "src/"
},
"files": [
"vendor/antecedent/patchwork/Patchwork.php",
"inc/api.php"
]
},
"autoload-dev": {
"files": [ "inc/wp-functions.php" ]
"psr-4": {
"Brain\\Monkey\\Tests\\": "tests/src/"
},
"files": [
"inc/wp-helper-functions.php",
"inc/wp-hook-functions.php"
]
},
"minimum-stability": "dev",
"prefer-stable": true,
Expand All @@ -50,7 +59,8 @@
},
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
"dev-master": "1.0.x-dev",
"dev-version/2": "2.0.x-dev"
}
}
}
195 changes: 195 additions & 0 deletions inc/api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
<?php # -*- coding: utf-8 -*-
/*
* This file is part of the BrainMonkey package.
*
* (c) Giuseppe Mazzapica
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Brain\Monkey {

function setUp()
{
require_once dirname(__DIR__).'/inc/wp-hook-functions.php';
require_once dirname(__DIR__).'/inc/wp-helper-functions.php';
}

function tearDown()
{
Container::instance()->reset();
\Mockery::close();
\Patchwork\restoreAll();
}
}

namespace Brain\Monkey\Functions {

use Brain\Monkey\Container;
use Brain\Monkey\Expectation\FunctionStubFactory;
use Brain\Monkey\Names\FunctionName;

/**
* Factory method: receives the name of the function to mock and returns an instance of
* FunctionStub.
*
* @param string $function_name the name of the function to mock
* @return \Brain\Monkey\Expectation\FunctionStub
*/
function when($function_name)
{
return Container::instance()
->functionStubFactory()
->create(new FunctionName($function_name), FunctionStubFactory::SCOPE_STUB);
}

/**
* Returns a Mockery Expectation object, where is possible to set all the expectations, using
* Mockery methods.
*
* @param string $function_name
* @return \Brain\Monkey\Expectation\Expectation
*/
function expect($function_name)
{
$name = new FunctionName($function_name);
$expectation = Container::instance()
->expectationFactory()
->forFunctionExecuted($function_name);

$factory = Container::instance()->functionStubFactory();
if ( ! $factory->has($name)) {
$factory->create($name, FunctionStubFactory::SCOPE_EXPECTATION)
->replaceUsingExpectation($expectation);

}

return $expectation;
}
}

namespace Brain\Monkey\Actions {

use Brain\Monkey\Container;
use Brain\Monkey\Hooks;

/**
* @param string $action
* @return \Brain\Monkey\Expectation\Expectation
*/
function expectAdded($action)
{
return Container::instance()
->expectationFactory()
->forActionAdded($action);
}

/**
* @param string $action
* @return \Brain\Monkey\Expectation\Expectation
*/
function expectDone($action)
{
return Container::instance()
->expectationFactory()
->forActionDone($action);
}

/**
* @param string $action
* @param null $callback
* @return bool
*/
function has($action, $callback = null)
{
return Container::instance()
->hookStorage()
->isHookAdded(Hooks\HookStorage::ACTIONS, $action, $callback);
}

/**
* @param string $action
* @return int
*/
function did($action)
{
return Container::instance()
->hookStorage()
->isHookDone(Hooks\HookStorage::ACTIONS, $action);
}

/**
* @param string $action
* @return bool
*/
function doing($action)
{
return Container::instance()
->hookRunningStack()
->has($action);
}
}

namespace Brain\Monkey\Filters {

use Brain\Monkey\Container;
use Brain\Monkey\Hooks;

/**
* @param string $filter
* @return \Brain\Monkey\Expectation\Expectation
*/
function expectAdded($filter)
{
return Container::instance()
->expectationFactory()
->forFilterAdded($filter);
}

/**
* @param string $filter
* @return \Brain\Monkey\Expectation\Expectation
*/
function expectApplied($filter)
{
return Container::instance()
->expectationFactory()
->forFilterApplied($filter);
}

/**
* @param string $filter
* @param null $callback
* @return bool
*/
function has($filter, $callback = null)
{
return Container::instance()
->hookStorage()
->isHookAdded(Hooks\HookStorage::FILTERS, $filter, $callback);
}

/**
* @param string $filter
* @return int
*/
function applied($filter)
{
return Container::instance()
->hookStorage()
->isHookDone(Hooks\HookStorage::FILTERS, $filter);
}

/**
* @param string $filter
* @return bool
*/
function doing($filter)
{
return Container::instance()
->hookRunningStack()
->has($filter);
}
}

Loading

0 comments on commit db4ec92

Please sign in to comment.