Skip to content

Commit

Permalink
Update 0.4.2
Browse files Browse the repository at this point in the history
* Update issue templates and contributing guidelines.
* Fix some spacing and documentation from PR #136
* assertActionsCalled and assertHooksAdded now actually assert. Added assertFiltersCalled Added tests for these functions.  Changed risky tests

Fixes #136
  • Loading branch information
ericmann committed Mar 16, 2019
1 parent 5807261 commit 9019226
Show file tree
Hide file tree
Showing 10 changed files with 199 additions and 21 deletions.
38 changes: 38 additions & 0 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
name: Bug report
about: Create a report to help us improve
title: ''
labels: ''
assignees: ''

---

**Describe the bug**
A clear and concise description of what the bug is.

**To Reproduce**
Steps to reproduce the behavior:
1. Go to '...'
2. Click on '....'
3. Scroll down to '....'
4. See error

**Expected behavior**
A clear and concise description of what you expected to happen.

**Screenshots**
If applicable, add screenshots to help explain your problem.

**Desktop (please complete the following information):**
- OS: [e.g. iOS]
- Browser [e.g. chrome, safari]
- Version [e.g. 22]

**Smartphone (please complete the following information):**
- Device: [e.g. iPhone6]
- OS: [e.g. iOS8.1]
- Browser [e.g. stock browser, safari]
- Version [e.g. 22]

**Additional context**
Add any other context about the problem here.
20 changes: 20 additions & 0 deletions .github/ISSUE_TEMPLATE/feature_request.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
name: Feature request
about: Suggest an idea for this project
title: ''
labels: ''
assignees: ''

---

**Is your feature request related to a problem? Please describe.**
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

**Describe the solution you'd like**
A clear and concise description of what you want to happen.

**Describe alternatives you've considered**
A clear and concise description of any alternative solutions or features you've considered.

**Additional context**
Add any other context or screenshots about the feature request here.
14 changes: 7 additions & 7 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ We accept contributions via Pull Requests on [Github](https://github.com/10up/wp
## Branches

* We try to follow [SemVer](http://semver.org/) in WP Mock
* The current minor version lives on the **master** branch. Until a new minor (or major) version is released, the master branch will be aliased to appear as the dev package of the current minor version in Packagist (e.g. if the current minor version is `1.0`, master will be aliased to `1.0.x-dev`).
* The development release lives on the **dev** branch. Until it is officially released, the dev branch will be aliased to appear as the dev package of the next minor version in Packagist (e.g. if the next minor version is `1.2`, the dev branch will be aliased to `1.2.x-dev`).
* Old minor versions will live in their own version branch (e.g. if the current minor version is `1.2`, the `1.1` major version will live in a `1.1` branch
* The current "stable" release version lives on the **master** branch.
* If there is a current development release, it will live on a **{version}-dev** branch.

## Pull Requests

* New features must be submitted against the **dev** branch
* Bug fixes should be submitted against the branch in which the bug exists. If the bug exists in multiple releases, please submit the Pull Request against the most recent branch and make a note of which other major versions need the fix (e.g. if the bug exists in all versions, submit against dev; if it no longer exists in dev, submit against master). Please do not open multiple pull requests for the same fix against different branches.
* New features must be submitted against the the **master** branch
* Bug fixes should be submitted against the branch in which the bug exists, which is likely **master**.
* If you're not sure whether a feature idea would be something we'd be interested in, please open an issue before you start working on it. We'd be happy to discuss your idea with you.

## Tests
## Merging

We know. We're kind of working on it. Want to start writing them for us? :D
* As of 2019, all merges to the **master** branch will be squash merges of features.
* If there are multiple features pending in a release, we will create a **{version}-dev** branch to track development against that version. Once the version is ready, that branch will be squash-merged into **master** as well.

## Thanks

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ WP_Mock is an API mocking framework, built and maintained by [10up](http://10up.
First, add WP Mock as a dev-dependency with [Composer](http://getcomposer.org):

```bash
composer require --dev 10up/wp_mock:0.4.1
composer require --dev 10up/wp_mock:0.4.2
```

Then, make sure your bootstrap file is loading the composer autoloader:
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name" : "10up/wp_mock",
"description": "A mocking library to take the pain out of unit testing for WordPress",
"license" : "GPL-2.0-or-later",
"version" : "0.4.1",
"version" : "0.4.2",
"require" : {
"php" : ">=7.1",
"phpunit/phpunit" : ">=7.0",
Expand Down
30 changes: 22 additions & 8 deletions php/WP_Mock.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ public static function tearDown() : void {
* @return \WP_Mock\Filter
*/
public static function onFilter( $filter ) {
self::$event_manager->called($filter,'filter');
return self::$event_manager->filter( $filter );
}

Expand Down Expand Up @@ -253,11 +254,22 @@ public static function expectFilter( $filter ) {
$responder->reply( new \WP_Mock\InvokedFilterValue( array( $intercept, 'intercepted' ) ) );
}

/**
* Assert that all actions are called.
*/
public static function assertActionsCalled() {
if ( ! self::$event_manager->allActionsCalled() ) {
$failed = implode( ', ', self::$event_manager->expectedActions() );
throw new \PHPUnit\Framework\ExpectationFailedException( 'Method failed to invoke actions: ' . $failed, null );
}
$allActionsCalled = self::$event_manager->allActionsCalled();
$failed = implode( ', ', self::$event_manager->expectedActions() );
\PHPUnit\Framework\Assert::assertTrue( $allActionsCalled, 'Method failed to invoke actions: ' . $failed );
}

/**
* Assert that all filters are called.
*/
public static function assertFiltersCalled() {
$allFiltersCalled = self::$event_manager->allFiltersCalled();
$failed = implode( ', ', self::$event_manager->expectedFilters() );
\PHPUnit\Framework\Assert::assertTrue( $allFiltersCalled, 'Method failed to invoke filters: ' . $failed );
}

/**
Expand Down Expand Up @@ -347,11 +359,13 @@ public static function expectHookNotAdded( $type, $action, $callback ) {
$responder->perform( array( $intercept, 'intercepted' ) );
}

/**
* Assert that all hooks are added.
*/
public static function assertHooksAdded() {
if ( ! self:: $event_manager->allHooksAdded() ) {
$failed = implode( ', ', self::$event_manager->expectedHooks() );
throw new \PHPUnit\Framework\ExpectationFailedException( 'Method failed to add hooks: ' . $failed, null );
}
$allHooksAdded = self::$event_manager->allHooksAdded();
$failed = implode( ', ', self::$event_manager->expectedHooks() );
PHPUnit\Framework\Assert::assertTrue( $allHooksAdded, 'Method failed to add hooks: ' . $failed );
}

/**
Expand Down
32 changes: 32 additions & 0 deletions php/WP_Mock/EventManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,18 @@ public function expectedActions() {
return array_keys( $this->actions );
}

/**
* Return a list of all the filters we're expecting a test to invoke.
* @return array
*/
public function expectedFilters() {
return array_keys( $this->filters );
}

/**
* Return a list of all the hooks we're expecting a test to invoke.
* @return array
*/
public function expectedHooks() {
return array_keys( $this->callbacks );
}
Expand All @@ -112,6 +124,26 @@ public function allActionsCalled() {
return true;
}

/**
* Check whether or not all filters have been invoked at least once.
*
* @return bool
*/
public function allFiltersCalled() {
foreach ( $this->expected as $hook ) {
if ( 0 === strpos( $hook, 'filter::' ) ) {
return false;
}
}

return true;
}

/**
* Check whether or not all hooks have been invoked at least once.
*
* @return bool
*/
public function allHooksAdded() {
foreach( $this->expected as $hook ) {
if ( 0 === strpos( $hook, 'callback::' ) ) {
Expand Down
4 changes: 2 additions & 2 deletions tests/DeprecatedMethodsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function testWpFunctionLogsDeprecationNotice() {
->once()
->with( $case, Mockery::type( '\PHPUnit\Framework\RiskyTestError' ), 0 );
WP_Mock::wpFunction( 'foobar' );
$listener->checkCalls();
$this->assertNull($listener->checkCalls());
}

public function testWpPassthruFunctionLogsDeprecationNotice() {
Expand All @@ -37,7 +37,7 @@ public function testWpPassthruFunctionLogsDeprecationNotice() {
->once()
->with( $case, Mockery::type( '\PHPUnit\Framework\RiskyTestError' ), 0 );
WP_Mock::wpPassthruFunction( 'foobar' );
$listener->checkCalls();
$this->assertNull( $listener->checkCalls() );
}

}
3 changes: 2 additions & 1 deletion tests/WP_Mock/DeprecatedListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public function testCheckCallsNoCalls() {
/** @var \\PHPUnit\Framework\TestResult $result */
$this->object->setTestResult( $result );

$this->object->checkCalls();
/** @noinspection PhpVoidFunctionResultUsedInspection */
$this->assertNull($this->object->checkCalls());
}

public function testCheckCalls_scalar_only() {
Expand Down
75 changes: 74 additions & 1 deletion tests/WP_MockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public function test_activateStrictMode_does_not_work_after_bootstrap() {
$this->assertFalse( WP_Mock::strictMode() );
}

/**
* @runInSeparateProcess
*/
public function test_userFunction_returns_expectation() {
WP_Mock::bootstrap();
$this->assertInstanceOf(
Expand All @@ -34,4 +37,74 @@ public function test_userFunction_returns_expectation() {
);
}

}
/**
* @runInSeparateProcess
*/
public function test_assertHooksAdded_for_filters_and_actions() {
WP_Mock::bootstrap();
WP_Mock::expectFilterAdded( 'testFilter', 'testCallback' ,10, 1);
WP_Mock::expectActionAdded( 'testAction', 'testCallback', 10, 1 );
add_action( 'testAction', 'testCallback',10, 1 );
add_filter('testFilter','testCallback', 10, 1);
WP_Mock::assertHooksAdded();
\Mockery::close();
}

/**
* @runInSeparateProcess
*/
public function test_assertHooksAdded_for_filters_and_actions_fails() {
WP_Mock::bootstrap();
WP_Mock::expectFilterAdded( 'testFilter', 'testCallback', 10, 1 );
WP_Mock::expectActionAdded( 'testAction', 'testCallback', 10, 1 );
$this->expectException('PHPUnit\Framework\ExpectationFailedException');
WP_Mock::assertHooksAdded();
\Mockery::close();
}

/**
* @runInSeparateProcess
*/
public function test_assertActionsCalled_actions() {
WP_Mock::bootstrap();
WP_Mock::expectAction( 'testAction' );
do_action('testAction');
WP_Mock::assertActionsCalled();
\Mockery::close();
}

/**
* @runInSeparateProcess
*/
public function test_assertActionsCalled_actions_fails() {
WP_Mock::bootstrap();
WP_Mock::expectAction( 'testAction' );
$this->expectException( 'PHPUnit\Framework\ExpectationFailedException' );
WP_Mock::assertActionsCalled();
\Mockery::close();
}

/**
* @runInSeparateProcess
*/
public function test_assertActionsCalled_filters() {

WP_Mock::bootstrap();
WP_Mock::expectFilter( 'testFilter','testVal' );
apply_filters( 'testFilter','testVal' );
WP_Mock::assertFiltersCalled();
\Mockery::close();
}

/**
* @runInSeparateProcess
*/
public function test_assertActionsCalled_filters_fails() {

WP_Mock::bootstrap();
WP_Mock::expectFilter( 'testFilter2', 'testVal' );

$this->expectException( 'Mockery\Exception\InvalidCountException' );
\Mockery::close();
}
}

0 comments on commit 9019226

Please sign in to comment.