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

Deprecate wpFunction and wpPassthruFunction #78

Merged
merged 21 commits into from
Feb 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
891a2e9
Add deprecated flags in docblock for wpFunction and wpPassthruFunction
Dec 16, 2014
0e3bd2e
Record uses of deprecated methods
Dec 16, 2014
2bd5d20
Start reworking deprecated listening
Apr 12, 2015
8ed13ad
Tighten up composer dependencies' versions
Apr 13, 2015
3e4d170
More deprecated method functionality
Apr 13, 2015
ca0d0c8
Move DeprecatedListener
Apr 28, 2015
30f3d08
Bump WP Mock's phpunit version requirements
johnpbloch Nov 19, 2016
117da85
Use a hooked method to run after the test instead of runBare
johnpbloch Nov 19, 2016
e6301e6
Get rid of cyclomatic complexity from backcompat
johnpbloch Nov 19, 2016
2ccc3c3
Add the risky test directly rather than throwing an exception
johnpbloch Nov 19, 2016
6d69f85
Remove default value of deprecated_listener
johnpbloch Dec 22, 2016
2c39453
Make sure the methods variable is an array
johnpbloch Dec 22, 2016
9b1711d
Add a test for verifying wpFunction is a deprecated method
johnpbloch Dec 22, 2016
7ca19cc
Add a method for wp passthru methods as well
johnpbloch Dec 22, 2016
3a92f1c
Start on tests for deprecated listener class
johnpbloch Dec 22, 2016
252faca
Add a test for reset
johnpbloch Dec 22, 2016
f44d6ed
Add a basic test for checkCalls
johnpbloch Dec 22, 2016
a3d51e1
Add a test for checkcalls when there are no calls
johnpbloch Dec 22, 2016
4d6e120
Add a test for more complex argument representations in the message
johnpbloch Dec 22, 2016
817f237
Add documentation about deprecating methods
johnpbloch Dec 22, 2016
8714d39
Merge branch 'dev' into feature/deprecate-wp-prefixes
johnpbloch Jan 16, 2017
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,10 @@ It's not uncommon for tests to need to declare "passthrough/passthru" functions:

You can still test things like invocation count by passing the `times` argument in the second parameter, just like `\WP_Mock::userFunction()`.

### Deprecated methods

Please note that `WP_Mock::wpFunction()` and `WP_Mock::wpPassthruFunction()` are both officially deprecated. Replace all uses of them with `WP_Mock::userFunction()` and `WP_Mock::passthruFunction()`. If you use either of the deprecated methods, WP_Mock will mark those tests as risky. Your tests will still count as passing, but PHPUnit will start telling you which tests are causing issues.

### Mocking actions and filters

The [hooks and filters of the WordPress Plugin API](http://codex.wordpress.org/Plugin_API) are common (and preferred) entry points for third-party scripts, and WP_Mock makes it easy to test that these are being registered and executed within your code.
Expand Down
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
"license" : "GPL-2.0+",
"require" : {
"php" : ">=5.3.2",
"phpunit/phpunit" : ">=3.7",
"phpunit/phpunit" : ">=4.3",
"mockery/mockery" : "^0.9.5",
"antecedent/patchwork": "~1.2"
"antecedent/patchwork": "~1.3.4"
},
"require-dev": {
"phpunit/phpunit" : ">=4.3",
"behat/behat" : "^3.0"
},
"autoload" : {
Expand Down
18 changes: 17 additions & 1 deletion php/WP_Mock.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class WP_Mock {

protected static $__strict_mode = false;

protected static $deprecated_listener;

/**
* @param boolean $use_patchwork
*/
Expand Down Expand Up @@ -86,7 +88,8 @@ public static function activateStrictMode() {
*/
public static function bootstrap() {
if ( ! self::$__bootstrapped ) {
self::$__bootstrapped = true;
self::$__bootstrapped = true;
static::$deprecated_listener = new \WP_Mock\DeprecatedListener();
require_once __DIR__ . '/WP_Mock/API/function-mocks.php';
require_once __DIR__ . '/WP_Mock/API/constant-mocks.php';
if ( self::usingPatchwork() ) {
Expand Down Expand Up @@ -402,12 +405,15 @@ public static function userFunction( $function_name, $arguments = array() ) {
/**
* Alias for userFunction
*
* @deprecated since 1.0
*
* @param string $function_name
* @param array $arguments
*
* @return Mockery\Expectation
*/
public static function wpFunction( $function_name, $arguments = array() ) {
static::getDeprecatedListener()->logDeprecatedCall( __METHOD__, array( $function_name, $arguments ) );
return self::userFunction( $function_name, $arguments );
}

Expand Down Expand Up @@ -458,12 +464,15 @@ public static function passthruFunction( $function_name, $arguments = array() )
/**
* Alias for passthruFunction
*
* @deprecated since 1.0
*
* @param string $function_name
* @param array $arguments
*
* @return Mockery\Expectation
*/
public static function wpPassthruFunction( $function_name, $arguments = array() ) {
static::getDeprecatedListener()->logDeprecatedCall( __METHOD__, array( $function_name, $arguments ) );
return self::passthruFunction( $function_name, $arguments );
}

Expand Down Expand Up @@ -504,4 +513,11 @@ public static function alias( $function_name, $alias, $arguments = array() ) {
public static function fuzzyObject( $thing ) {
return new FuzzyObject( $thing );
}

/**
* @return \WP_Mock\DeprecatedListener
*/
public static function getDeprecatedListener() {
return static::$deprecated_listener;
}
}
119 changes: 119 additions & 0 deletions php/WP_Mock/DeprecatedListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

namespace WP_Mock;

use PHPUnit_Framework_TestCase;

class DeprecatedListener {

protected $calls = array();

/** @var \PHPUnit_Framework_TestResult */
protected $testResult;

protected $testName;

/**
* @var PHPUnit_Framework_TestCase
*/
protected $testCase;

public function logDeprecatedCall( $method, array $args = array() ) {
$this->calls[] = array( $method, $args );
}

public function reset() {
$this->calls = array();
}

public function checkCalls() {
if ( empty( $this->calls ) ) {
return;
}
$e = new \PHPUnit_Framework_RiskyTestError( $this->getMessage() );
$this->testResult->addFailure( $this->testCase, $e, 0 );
}

/**
* @param \PHPUnit_Framework_TestResult $testResult
*/
public function setTestResult( $testResult ) {
$this->testResult = $testResult;
}

/**
* @param mixed $testName
*/
public function setTestName( $testName ) {
$this->testName = $testName;
}

public function setTestCase( PHPUnit_Framework_TestCase $testCase ) {
$this->testCase = $testCase;
}

protected function getMessage() {
$maxLength = array_reduce( $this->getDeprecatedMethods(), function ( $carry, $item ) {
return max( $carry, strlen( $item ) );
}, 0 ) + 1;
$message = 'Deprecated WP Mock calls inside ' . $this->testName . ":";
foreach ( $this->getDeprecatedMethodsWithArgs() as $method => $args ) {
$firstRun = true;
$extra = $maxLength - strlen( $method );
foreach ( $args as $arg ) {
$message .= "\n ";
if ( $firstRun ) {
$message .= $method . str_repeat( ' ', $extra );
$firstRun = false;
$extra = $maxLength;
} else {
$message .= str_repeat( ' ', $extra );
}
$message .= $arg;
}
}

return $message;
}

protected function getDeprecatedMethods() {
$methods = array();
foreach ( $this->calls as $call ) {
$methods[] = $call[0];
}

return array_unique( $methods );
}

protected function getDeprecatedMethodsWithArgs() {
$collection = array();
foreach ( $this->calls as $call ) {
$method = $call[0];
$args = json_encode( array_map( array( $this, 'scalarizeArg' ), $call[1] ) );
if ( empty( $collection[ $method ] ) ) {
$collection[ $method ] = array();
}
$collection[ $method ][] = $args;
}

return array_map( 'array_unique', $collection );
}

protected function scalarizeArg( $value ) {
if ( is_scalar( $value ) ) {
return $value;
} elseif ( is_object( $value ) ) {
return '<' . get_class( $value ) . ':' . spl_object_hash( $value ) . '>';
} elseif ( is_array( $value ) ) {
if ( is_callable( $value ) ) {
return '[' . implode( ',', array_map( array( $this, 'scalarizeArg' ), $value ) ) . ']';
} else {
return 'Array([' . count( $value ) . '] ...)';
}
} elseif ( is_resource( $value ) ) {
return 'Resource';
} else {
return 'Unknown Value';
}
}
}
20 changes: 20 additions & 0 deletions php/WP_Mock/Tools/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace WP_Mock\Tools;

use PHPUnit_Framework_TestResult;
use Exception;
use Mockery;
use ReflectionMethod;
Expand Down Expand Up @@ -295,5 +296,24 @@ protected function setUpContentFiltering() {
}
}

public function run( PHPUnit_Framework_TestResult $result = null ) {
if ( $result === null ) {
$result = $this->createResult();
}

WP_Mock::getDeprecatedListener()->setTestResult( $result );
WP_Mock::getDeprecatedListener()->setTestCase($this);

return parent::run( $result );
}

/**
* @after
*/
public function checkDeprecatedCalls() {
WP_Mock::getDeprecatedListener()->checkCalls();
WP_Mock::getDeprecatedListener()->reset();
}

}

41 changes: 41 additions & 0 deletions tests/DeprecatedMethodsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

class DeprecatedMethodsTest extends PHPUnit_Framework_TestCase {

public function setUp() {
WP_Mock::setUp();
WP_Mock::getDeprecatedListener()->reset();
}

protected function tearDown() {
WP_Mock::getDeprecatedListener()->reset();
WP_Mock::tearDown();
}

public function testWpFunctionLogsDeprecationNotice() {
$listener = WP_Mock::getDeprecatedListener();
$result = Mockery::mock( 'PHPUnit_Framework_TestResult' );
$case = Mockery::mock( 'PHPUnit_Framework_TestCase' );
$listener->setTestCase( $case );
$listener->setTestResult( $result );
$result->shouldReceive( 'addFailure' )
->once()
->with( $case, Mockery::type( 'PHPUnit_Framework_RiskyTestError' ), 0 );
WP_Mock::wpFunction( 'foobar' );
$listener->checkCalls();
}

public function testWpPassthruFunctionLogsDeprecationNotice() {
$listener = WP_Mock::getDeprecatedListener();
$result = Mockery::mock( 'PHPUnit_Framework_TestResult' );
$case = Mockery::mock( 'PHPUnit_Framework_TestCase' );
$listener->setTestCase( $case );
$listener->setTestResult( $result );
$result->shouldReceive( 'addFailure' )
->once()
->with( $case, Mockery::type( 'PHPUnit_Framework_RiskyTestError' ), 0 );
WP_Mock::wpPassthruFunction( 'foobar' );
$listener->checkCalls();
}

}
2 changes: 2 additions & 0 deletions tests/FunctionMocksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ protected function setUp() {

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testCommonFunctionsAreDefined() {
// First we assert that all common functions get removed from the returned array. If any one of these functions
Expand All @@ -54,6 +55,7 @@ public function testCommonFunctionsDefaultFunctionality( $function, $action ) {

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
* @expectedException PHPUnit_Framework_ExpectationFailedException
* @expectedExceptionMessageRegExp /No handler found for \w+/
*/
Expand Down
Loading