From 9019226eb50df275aa86bb15bfc98a619601ee49 Mon Sep 17 00:00:00 2001 From: Eric Mann Date: Fri, 15 Mar 2019 20:44:39 -0700 Subject: [PATCH] Update 0.4.2 * 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 --- .github/ISSUE_TEMPLATE/bug_report.md | 38 ++++++++++++ .github/ISSUE_TEMPLATE/feature_request.md | 20 ++++++ CONTRIBUTING.md | 14 ++--- README.md | 2 +- composer.json | 2 +- php/WP_Mock.php | 30 ++++++--- php/WP_Mock/EventManager.php | 32 ++++++++++ tests/DeprecatedMethodsTest.php | 4 +- tests/WP_Mock/DeprecatedListenerTest.php | 3 +- tests/WP_MockTest.php | 75 ++++++++++++++++++++++- 10 files changed, 199 insertions(+), 21 deletions(-) create mode 100644 .github/ISSUE_TEMPLATE/bug_report.md create mode 100644 .github/ISSUE_TEMPLATE/feature_request.md diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md new file mode 100644 index 00000000..dd84ea78 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -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. diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md new file mode 100644 index 00000000..bbcbbe7d --- /dev/null +++ b/.github/ISSUE_TEMPLATE/feature_request.md @@ -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. diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 04f07e50..43c81513 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -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 diff --git a/README.md b/README.md index 1d4323f6..c91aacc2 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/composer.json b/composer.json index 01aa3c09..90c0b66c 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/php/WP_Mock.php b/php/WP_Mock.php index 3411ae65..37db9373 100644 --- a/php/WP_Mock.php +++ b/php/WP_Mock.php @@ -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 ); } @@ -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 ); } /** @@ -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 ); } /** diff --git a/php/WP_Mock/EventManager.php b/php/WP_Mock/EventManager.php index 885c2420..652daa81 100644 --- a/php/WP_Mock/EventManager.php +++ b/php/WP_Mock/EventManager.php @@ -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 ); } @@ -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::' ) ) { diff --git a/tests/DeprecatedMethodsTest.php b/tests/DeprecatedMethodsTest.php index 9544e9da..b953ab73 100644 --- a/tests/DeprecatedMethodsTest.php +++ b/tests/DeprecatedMethodsTest.php @@ -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() { @@ -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() ); } } diff --git a/tests/WP_Mock/DeprecatedListenerTest.php b/tests/WP_Mock/DeprecatedListenerTest.php index fe70c63c..67cfd659 100644 --- a/tests/WP_Mock/DeprecatedListenerTest.php +++ b/tests/WP_Mock/DeprecatedListenerTest.php @@ -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() { diff --git a/tests/WP_MockTest.php b/tests/WP_MockTest.php index c129a83b..d82d1a1c 100644 --- a/tests/WP_MockTest.php +++ b/tests/WP_MockTest.php @@ -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( @@ -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(); + } +} \ No newline at end of file