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 a strict mode #75

Merged
merged 22 commits into from
Oct 31, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
5b1916e
Add a method to indicate strict mode
johnpbloch Oct 8, 2016
e5072fa
Reformat test
johnpbloch Oct 8, 2016
7e268fa
Add a method to activate strict mode
johnpbloch Oct 8, 2016
156b4c4
Strict mode should only be activated prior to bootstrap
johnpbloch Oct 8, 2016
c7e182b
If strict mode is on, throw an exception for undefined uses in the Ha…
johnpbloch Oct 8, 2016
c11f5b9
Reformat behat test fixtures
johnpbloch Oct 8, 2016
ee60f74
Add a test for strict mode forcing failure for sloppy use
johnpbloch Oct 8, 2016
4a0fd9a
Reformat some yaml
johnpbloch Oct 8, 2016
e0e718f
Add a test to make sure defined but unexpected tests don't fail in re…
johnpbloch Oct 9, 2016
7667de0
Move strict mode controls into the feature context class
johnpbloch Oct 9, 2016
598aec3
Add tests for unexpected action/filters in strict mode
johnpbloch Oct 9, 2016
3005bb5
Make forceStrictModeOff always turn strict mode off
johnpbloch Oct 9, 2016
e332cce
Actions fail on add_action() when strict mode is on and there's no co…
johnpbloch Oct 9, 2016
0a0cde3
Reformat some code
johnpbloch Oct 17, 2016
1eab201
Move strict_check() into HookedCallback
johnpbloch Oct 17, 2016
5f7f176
Improve add_action/filter strict error message
johnpbloch Oct 17, 2016
fe7fd42
Move some methods around to make better reuse of code
johnpbloch Oct 17, 2016
8649cb3
Reformat
johnpbloch Oct 29, 2016
5f2260c
Add a strict check to do_action()
johnpbloch Oct 29, 2016
c85bd66
Add a strict check for unexpected uses of apply_filters()
johnpbloch Oct 29, 2016
e0d080f
Add documentation for strict mode to the readme
johnpbloch Oct 29, 2016
8568683
For internal tests, require phpunit 4.0+
johnpbloch Oct 29, 2016
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
Prev Previous commit
Next Next commit
Move strict_check() into HookedCallback
  • Loading branch information
johnpbloch committed Oct 17, 2016
commit 1eab20158d98224153d8ab7633ce38ef00987d15
31 changes: 0 additions & 31 deletions php/WP_Mock/Hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,6 @@ public function __construct( $name ) {
$this->name = $name;
}

protected function callback_to_string( $callback ) {
if ( ! is_string( $callback ) ) {
if ( $callback instanceof \Closure ) {
$callback = 'Closure';
} elseif ( is_object( $callback ) ) {
$callback = get_class( $callback ) . '::__invoke';
} else {
$class = $callback[0];
$method = $callback[1];
if ( ! is_string( $class ) ) {
$class = get_class( $class );
}
$callback = "{$class}::$method";
}
}

return $callback;
}

protected function strict_check( $callback ) {
if ( \WP_Mock::strictMode() ) {
throw new \PHPUnit_Framework_ExpectationFailedException(
sprintf(
'Unexpected use of add action for action %s with callback %s',
$this->name,
$this->callback_to_string( $callback )
)
);
}
}

protected function safe_offset( $value ) {
if ( is_null( $value ) ) {
return 'null';
Expand Down
48 changes: 48 additions & 0 deletions php/WP_Mock/HookedCallback.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,54 @@ protected function safe_offset( $value ) {
return parent::safe_offset( $value );
}

/**
* Converts a callable to a string
*
* Closures get returned as 'Closure', objects (those with an __invoke() method get turned into <Class>::__invoke,
* and arrays get turned into <Class>::<method>
*
* @param callable $callback
*
* @return string
*/
protected function callback_to_string( $callback ) {
if ( ! is_string( $callback ) ) {
if ( $callback instanceof \Closure ) {
$callback = 'Closure';
} elseif ( is_object( $callback ) ) {
$callback = get_class( $callback ) . '::__invoke';
} else {
$class = $callback[0];
$method = $callback[1];
if ( ! is_string( $class ) ) {
$class = get_class( $class );
}
$callback = "{$class}::$method";
}
}

return $callback;
}

/**
* Throw an exception if strict mode is on
*
* @throws \PHPUnit_Framework_ExpectationFailedException
*
* @param callable $callback
*/
protected function strict_check( $callback ) {
if ( \WP_Mock::strictMode() ) {
throw new \PHPUnit_Framework_ExpectationFailedException(
sprintf(
'Unexpected use of add action for action %s with callback %s',
$this->name,
$this->callback_to_string( $callback )
)
);
}
}

}

class HookedCallbackResponder {
Expand Down