From 1baea0dbd1a23251fe7f08d9044fb7c1dc2106b3 Mon Sep 17 00:00:00 2001 From: Eric Mann Date: Tue, 18 Jul 2017 20:16:31 -0700 Subject: [PATCH 01/13] Update Travis-CI Build Matrix As PHP 7.2 is now feature-frozen, we should ensure that we're testing against the nightly build. The final version should ship in a few weeks and we want to ensure things are compatible _before_ it hits the market. --- .travis.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 1fefc0ad..0992a087 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,12 +8,16 @@ matrix: env: WP_MOCK_INSTALL_LOWEST=0 - php: '7.1' env: WP_MOCK_INSTALL_LOWEST=0 + - php: nightly + env: WP_MOCK_INSTALL_LOWEST=0 - php: '5.6' env: WP_MOCK_INSTALL_LOWEST=1 - php: '7.0' env: WP_MOCK_INSTALL_LOWEST=1 - php: '7.1' - env: WP_MOCK_INSTALL_LOWEST=0 + env: WP_MOCK_INSTALL_LOWEST=1 + - php: nightly + env: WP_MOCK_INSTALL_LOWEST=1 before_script: - if [ 1 -eq $WP_MOCK_INSTALL_LOWEST ]; then composer update --prefer-lowest; else composer update; fi From a9f64e87d9e1fc24dc04e557b06b64d5acaf01b4 Mon Sep 17 00:00:00 2001 From: John P Bloch Date: Wed, 6 Sep 2017 08:45:55 -0500 Subject: [PATCH 02/13] Add an expectFilter method and tests --- features/bootstrap/HooksContext.php | 30 +++++++++++++++++++++++- features/hooks.feature | 36 +++++++++++++++++++++++++++++ php/WP_Mock.php | 19 +++++++++++++++ php/WP_Mock/Filter.php | 6 ++++- php/WP_Mock/InvokedFilterValue.php | 25 ++++++++++++++++++++ 5 files changed, 114 insertions(+), 2 deletions(-) create mode 100644 php/WP_Mock/InvokedFilterValue.php diff --git a/features/bootstrap/HooksContext.php b/features/bootstrap/HooksContext.php index 92a1709e..f8d095e9 100644 --- a/features/bootstrap/HooksContext.php +++ b/features/bootstrap/HooksContext.php @@ -65,6 +65,25 @@ public function iExpectTheActionWith( $action, TableNode $table ) { call_user_func_array( array( 'WP_Mock', 'expectAction' ), $args ); } + /** + * @Given I expect the :filter filter with :value + */ + public function iExpectTheFilterWith( $filter, $value ) { + $this->iExpectTheFilterWithValues( $filter, new TableNode( array( array( $value ) ) ) ); + } + + /** + * @When I expect the :filter filter with: + */ + public function iExpectTheFilterWithValues( $filter, TableNode $table ) { + $args = array( $filter ); + $rows = $table->getRows(); + if ( isset( $rows[0] ) && is_array( $rows[0] ) ) { + $args = array_merge( $args, $rows[0] ); + } + call_user_func_array( array( 'WP_Mock', 'expectFilter' ), $args ); + } + /** * @When I add the following actions: */ @@ -161,7 +180,16 @@ public function iExpectFilterToRespondWith( $filter, $response ) { * @When I apply the filter :filter with :with */ public function iApplyFilterWith( $filter, $with ) { - $this->filterResults[ $filter ] = apply_filters( $filter, $with ); + $this->iApplyFilterWithData( $filter, new TableNode( array( array( $with ) ) ) ); + } + + /** + * @When I apply the filter :filter with: + */ + public function iApplyFilterWithData( $filter, TableNode $table ) { + $row = $table->getRow( 0 ); + array_unshift( $row, $filter ); + $this->filterResults[ $filter ] = call_user_func_array( 'apply_filters', $row ); } /** diff --git a/features/hooks.feature b/features/hooks.feature index 3301482d..45f169a8 100644 --- a/features/hooks.feature +++ b/features/hooks.feature @@ -118,6 +118,42 @@ Feature: Hook mocking | data | plus | Then tearDown should fail + Scenario: expectFilter sets up expectation + Given I expect the "foobar" filter with "bazbat" + When I apply the filter "foobar" with "bazbat" + Then tearDown should not fail + + Scenario: expectFilter fails when unmet + Given I expect the "foobar" filter with "bazbat" + When I do nothing + Then tearDown should fail + + Scenario: expectFilter with extra arguments + Given I expect the "foobar" filter with: + | some | extra | data | + When I apply the filter "foobar" with: + | some | extra | data | + Then tearDown should not fail + + Scenario: filter with the wrong arguments fails + Given I expect the "bazbat" filter with: + | the correct data | + When I apply the filter "bazbat" with: + | Invalid information | + Then tearDown should fail + + Scenario: expectFilter fails when called with wrong argument + Given I expect the "foobar" filter with "bazbat" + When I apply the filter "foobar" with "bimbam" + Then tearDown should fail + + Scenario: filter with extra arguments fails + Given I expect the "bazbat" filter with: + | data | + When I apply the filter "bazbat" with: + | data | plus | + Then tearDown should fail + @strictmode Scenario: Unexpected action fails in strict mode Given strict mode is on diff --git a/php/WP_Mock.php b/php/WP_Mock.php index b5c7ceab..71a92f9e 100644 --- a/php/WP_Mock.php +++ b/php/WP_Mock.php @@ -233,6 +233,25 @@ public static function expectAction( $action ) { $responder->perform( array( $intercept, 'intercepted' ) ); } + /** + * Set up the expectation that a filter will be applied during the test. + * + * Mock a WordPress filter with specific arguments. You need all arguments that you expect + * in order to fulfill the expectation. + * + * @param string $filter + * @param mixed $value + */ + public static function expectFilter( $filter, $value ) { + $intercept = \Mockery::mock( 'intercept' ); + $intercept->shouldReceive( 'intercepted' )->atLeast()->once()->andReturn( $value ); + $args = array_slice( func_get_args(), 1 ); + + $mocked_filter = self::onFilter( $filter ); + $responder = call_user_func_array( array( $mocked_filter, 'with' ), $args ); + $responder->reply( new \WP_Mock\InvokedFilterValue( array( $intercept, 'intercepted' ) ) ); + } + public static function assertActionsCalled() { if ( ! self::$event_manager->allActionsCalled() ) { $failed = implode( ', ', self::$event_manager->expectedActions() ); diff --git a/php/WP_Mock/Filter.php b/php/WP_Mock/Filter.php index 85d78c90..c79c2d94 100644 --- a/php/WP_Mock/Filter.php +++ b/php/WP_Mock/Filter.php @@ -32,7 +32,7 @@ public function apply( $args ) { $processors = $this->processors; foreach ( $args as $arg ) { $key = $this->safe_offset( $arg ); - if ( ! isset( $processors[ $key ] ) ) { + if ( ! is_array( $processors ) || ! isset( $processors[ $key ] ) ) { $this->strict_check(); return $arg; @@ -67,6 +67,10 @@ public function reply( $value ) { } public function send() { + if ( $this->value instanceof InvokedFilterValue ) { + return call_user_func( $this->value ); + } + return $this->value; } } diff --git a/php/WP_Mock/InvokedFilterValue.php b/php/WP_Mock/InvokedFilterValue.php new file mode 100644 index 00000000..bf7a56a5 --- /dev/null +++ b/php/WP_Mock/InvokedFilterValue.php @@ -0,0 +1,25 @@ +callback = $callable; + } + + public function __invoke() { + return call_user_func_array( $this->callback, func_get_args() ); + } + +} From eb43f340e9b6095087ff46d84915c71169b41a4d Mon Sep 17 00:00:00 2001 From: John P Bloch Date: Wed, 6 Sep 2017 09:03:57 -0500 Subject: [PATCH 03/13] Send data through the processor send() method --- php/WP_Mock.php | 9 +++++---- php/WP_Mock/Filter.php | 7 +++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/php/WP_Mock.php b/php/WP_Mock.php index 71a92f9e..a7bcac91 100644 --- a/php/WP_Mock.php +++ b/php/WP_Mock.php @@ -240,12 +240,13 @@ public static function expectAction( $action ) { * in order to fulfill the expectation. * * @param string $filter - * @param mixed $value */ - public static function expectFilter( $filter, $value ) { + public static function expectFilter( $filter ) { $intercept = \Mockery::mock( 'intercept' ); - $intercept->shouldReceive( 'intercepted' )->atLeast()->once()->andReturn( $value ); - $args = array_slice( func_get_args(), 1 ); + $intercept->shouldReceive( 'intercepted' )->atLeast()->once()->andReturnUsing( function( $value ) { + return $value; + } ); + $args = func_num_args() > 1 ? array_slice( func_get_args(), 1 ) : array( null ); $mocked_filter = self::onFilter( $filter ); $responder = call_user_func_array( array( $mocked_filter, 'with' ), $args ); diff --git a/php/WP_Mock/Filter.php b/php/WP_Mock/Filter.php index c79c2d94..d1503e98 100644 --- a/php/WP_Mock/Filter.php +++ b/php/WP_Mock/Filter.php @@ -19,7 +19,7 @@ class Filter extends Hook { * @return mixed */ public function apply( $args ) { - if ( $args[0] === null ) { + if ( $args[0] === null && count( $args ) === 1 ) { if ( isset( $this->processors['argsnull'] ) ) { return $this->processors['argsnull']->send(); } @@ -27,7 +27,6 @@ public function apply( $args ) { return null; } - $arg_num = count( $args ); $processors = $this->processors; foreach ( $args as $arg ) { @@ -41,7 +40,7 @@ public function apply( $args ) { $processors = $processors[ $key ]; } - return $processors->send(); + return call_user_func_array( array($processors, 'send'), $args ); } protected function new_responder() { @@ -68,7 +67,7 @@ public function reply( $value ) { public function send() { if ( $this->value instanceof InvokedFilterValue ) { - return call_user_func( $this->value ); + return call_user_func_array( $this->value, func_get_args() ); } return $this->value; From 84ca08caf25980baa3d4675d55490f78eefd18f0 Mon Sep 17 00:00:00 2001 From: John P Bloch Date: Wed, 6 Sep 2017 09:04:16 -0500 Subject: [PATCH 04/13] Add documentation for expectFilter() --- README.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/README.md b/README.md index ef54da6c..b35249a4 100644 --- a/README.md +++ b/README.md @@ -331,6 +331,30 @@ public function test_filter_content() { } ``` +Alternatively, there is a method `\WP_Mock::expectFilter()` that will add a bare assertion that the filter will be applied without changing the value: + +```php +class SUT { + public function filter_content() { + $value = apply_filters( 'custom_content_filter', 'Default' ); + if ( $value === 'Default' ) { + do_action( 'default_value' ); + } + + return $value; + } +} + +class SUTTest { + public function test_filter_content() { + \WP_Mock::expectFilter( 'custom_content_filter', 'Default' ); + \WP_Mock::expectAction( 'default_value' ); + + $this->assertEquals( 'Default', (new SUT)->filter_content() ); + } +} +``` + ### Mocking WordPress objects Mocking calls to `wpdb`, `WP_Query`, etc. can be done using the [mockery](https://github.com/padraic/mockery) framework. While this isn't part of WP Mock itself, complex code will often need these objects and this framework will let you incorporate those into your tests. Since WP Mock requires Mockery, it should already be included as part of your install. From 6968ff84b25ee80349325b881acb3cdad5d2f064 Mon Sep 17 00:00:00 2001 From: John P Bloch Date: Wed, 6 Sep 2017 09:24:54 -0500 Subject: [PATCH 05/13] Require at least the version of comparator that fixed incompatible method signatures --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index ff3c9047..139d9cc2 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,8 @@ "antecedent/patchwork": "~2.0.3" }, "require-dev": { - "behat/behat" : "^3.0" + "behat/behat" : "^3.0", + "sebastian/comparator": ">=1.2.3" }, "conflict": { "phpunit/phpunit": ">=6.0" From 6082d05cdecd82b4f3a580c1950df939c99dd47d Mon Sep 17 00:00:00 2001 From: Sudar Muthu Date: Sat, 2 Dec 2017 23:25:13 +0530 Subject: [PATCH 06/13] Update phpunit requirement to v6.0 or above --- composer.json | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/composer.json b/composer.json index 139d9cc2..35210b5e 100644 --- a/composer.json +++ b/composer.json @@ -4,27 +4,19 @@ "license" : "GPL-2.0+", "version" : "0.2.0", "require" : { - "php" : ">=5.6", - "phpunit/phpunit" : ">=4.3", - "mockery/mockery" : "^0.9.5", - "antecedent/patchwork": "~2.0.3" + "php" : ">=7.0", + "phpunit/phpunit" : ">=6.0", + "mockery/mockery" : "^1.0", + "antecedent/patchwork": "^2.1" }, "require-dev": { "behat/behat" : "^3.0", "sebastian/comparator": ">=1.2.3" }, - "conflict": { - "phpunit/phpunit": ">=6.0" - }, "autoload" : { "psr-4" : {"WP_Mock\\": "./php/WP_Mock"}, "classmap": ["php/WP_Mock.php"] }, - "config": { - "platform": { - "php": "5.6.0" - } - }, "scripts": { "test:behat": "behat", "test:phpunit": "phpunit", From c6260c14873bd7238c9718021bc02dcd036ef786 Mon Sep 17 00:00:00 2001 From: Sudar Muthu Date: Sun, 3 Dec 2017 12:45:23 +0530 Subject: [PATCH 07/13] Make code and tests compatible with phpunit 6.0 --- features/bootstrap/FeatureContext.php | 2 +- features/bootstrap/FunctionsContext.php | 14 +++--- features/bootstrap/HooksContext.php | 4 +- php/WP_Mock.php | 4 +- php/WP_Mock/DeprecatedListener.php | 12 ++--- php/WP_Mock/Handler.php | 2 +- php/WP_Mock/Hook.php | 4 +- .../Tools/Constraints/ExpectationsMet.php | 4 +- php/WP_Mock/Tools/Constraints/IsEqualHtml.php | 4 +- php/WP_Mock/Tools/TestCase.php | 6 +-- tests/DeprecatedMethodsTest.php | 14 +++--- tests/FunctionMocksTest.php | 4 +- tests/WP_Mock/DeprecatedListenerTest.php | 44 +++++++++---------- tests/WP_MockTest.php | 2 +- 14 files changed, 60 insertions(+), 60 deletions(-) diff --git a/features/bootstrap/FeatureContext.php b/features/bootstrap/FeatureContext.php index 97695fb4..8d02f6e3 100644 --- a/features/bootstrap/FeatureContext.php +++ b/features/bootstrap/FeatureContext.php @@ -80,7 +80,7 @@ public function iDoNothing() { public function teardownShouldFail() { try { $this->teardownShouldNotFail(); - throw new PHPUnit_Framework_ExpectationFailedException( 'WP_Mock Teardown should have failed!' ); + throw new \PHPUnit\Framework\ExpectationFailedException( 'WP_Mock Teardown should have failed!' ); } catch ( \Mockery\Exception\InvalidCountException $e ) { // Move along } diff --git a/features/bootstrap/FunctionsContext.php b/features/bootstrap/FunctionsContext.php index e03d5726..8dd3bc8d 100644 --- a/features/bootstrap/FunctionsContext.php +++ b/features/bootstrap/FunctionsContext.php @@ -10,7 +10,7 @@ class FunctionsContext implements Context { * @Given function :function does not exist */ public function functionDoesNotExist( $function ) { - PHPUnit_Framework_Assert::assertFalse( function_exists( $function ) ); + \PHPUnit\Framework\Assert::assertFalse( function_exists( $function ) ); } /** @@ -48,7 +48,7 @@ public function iMockFunctionWpMockTestToEcho( $function ) { */ public function strictModeIsOn() { FeatureContext::forceStrictModeOn(); - PHPUnit_Framework_Assert::assertTrue( WP_Mock::strictMode() ); + \PHPUnit\Framework\Assert::assertTrue( WP_Mock::strictMode() ); } /** @@ -56,7 +56,7 @@ public function strictModeIsOn() { */ public function strictModeIsOff() { FeatureContext::forceStrictModeOff(); - PHPUnit_Framework_Assert::assertFalse( WP_Mock::strictMode() ); + \PHPUnit\Framework\Assert::assertFalse( WP_Mock::strictMode() ); } /** @@ -77,14 +77,14 @@ public function iTearDownTheTest() { * @Then function :function should exist */ public function functionShouldExist( $function ) { - PHPUnit_Framework_Assert::assertTrue( function_exists( $function ) ); + \PHPUnit\Framework\Assert::assertTrue( function_exists( $function ) ); } /** * @Then I expect :return when I run :function with args: */ public function iExpectWhenIRunWithArgs( $return, $function, TableNode $args ) { - PHPUnit_Framework_Assert::assertEquals( $return, call_user_func_array( $function, $args->getRow( 0 ) ) ); + \PHPUnit\Framework\Assert::assertEquals( $return, call_user_func_array( $function, $args->getRow( 0 ) ) ); } /** @@ -102,7 +102,7 @@ public function iExpectAnErrorWhenIRunWithArgs( $function, TableNode $args ) { $this->iExpectWhenIRunWithArgs( null, $function, $args ); } catch ( NoMatchingExpectationException $e ) { // Move along... - } catch ( \PHPUnit_Framework_ExpectationFailedException $e ) { + } catch ( \PHPUnit\Framework\ExpectationFailedException $e ) { // Move along... } } @@ -114,7 +114,7 @@ public function iExpectFunctionToEcho( $function, $input ) { ob_start(); $function( $input ); $output = trim( ob_get_clean() ); - PHPUnit_Framework_Assert::assertEquals( trim( $input ), $output ); + \PHPUnit\Framework\Assert::assertEquals( trim( $input ), $output ); } /** diff --git a/features/bootstrap/HooksContext.php b/features/bootstrap/HooksContext.php index f8d095e9..ac0d4be4 100644 --- a/features/bootstrap/HooksContext.php +++ b/features/bootstrap/HooksContext.php @@ -196,8 +196,8 @@ public function iApplyFilterWithData( $filter, TableNode $table ) { * @Then The filter :filter should return :value */ public function theFilterShouldReturn( $filter, $value ) { - PHPUnit_Framework_Assert::assertArrayHasKey( $filter, $this->filterResults ); - PHPUnit_Framework_Assert::assertEquals( $this->filterResults[ $filter ], $value ); + \PHPUnit\Framework\Assert::assertArrayHasKey( $filter, $this->filterResults ); + \PHPUnit\Framework\Assert::assertEquals( $this->filterResults[ $filter ], $value ); } private function getActionsWithDefaults( TableNode $table ) { diff --git a/php/WP_Mock.php b/php/WP_Mock.php index a7bcac91..c14c8af8 100644 --- a/php/WP_Mock.php +++ b/php/WP_Mock.php @@ -256,7 +256,7 @@ public static function expectFilter( $filter ) { 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 ); + throw new \PHPUnit\Framework\ExpectationFailedException( 'Method failed to invoke actions: ' . $failed, null ); } } @@ -350,7 +350,7 @@ public static function expectHookNotAdded( $type, $action, $callback ) { 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 ); + throw new \PHPUnit\Framework\ExpectationFailedException( 'Method failed to add hooks: ' . $failed, null ); } } diff --git a/php/WP_Mock/DeprecatedListener.php b/php/WP_Mock/DeprecatedListener.php index a0bef4fd..155a6403 100644 --- a/php/WP_Mock/DeprecatedListener.php +++ b/php/WP_Mock/DeprecatedListener.php @@ -2,19 +2,19 @@ namespace WP_Mock; -use PHPUnit_Framework_TestCase; +use \PHPUnit\Framework\TestCase; class DeprecatedListener { protected $calls = array(); - /** @var \PHPUnit_Framework_TestResult */ + /** @var \PHPUnit\Framework\TestCase */ protected $testResult; protected $testName; /** - * @var PHPUnit_Framework_TestCase + * @var \PHPUnit\Framework\TestCase */ protected $testCase; @@ -30,12 +30,12 @@ public function checkCalls() { if ( empty( $this->calls ) ) { return; } - $e = new \PHPUnit_Framework_RiskyTestError( $this->getMessage() ); + $e = new \PHPUnit\Framework\RiskyTestError( $this->getMessage() ); $this->testResult->addFailure( $this->testCase, $e, 0 ); } /** - * @param \PHPUnit_Framework_TestResult $testResult + * @param \PHPUnit\Framework\TestResult $testResult */ public function setTestResult( $testResult ) { $this->testResult = $testResult; @@ -48,7 +48,7 @@ public function setTestName( $testName ) { $this->testName = $testName; } - public function setTestCase( PHPUnit_Framework_TestCase $testCase ) { + public function setTestCase( \PHPUnit\Framework\TestCase $testCase ) { $this->testCase = $testCase; } diff --git a/php/WP_Mock/Handler.php b/php/WP_Mock/Handler.php index d6e89e6b..9b701668 100644 --- a/php/WP_Mock/Handler.php +++ b/php/WP_Mock/Handler.php @@ -42,7 +42,7 @@ public static function handle_function( $function_name, $args = array() ) { return call_user_func_array( $callback, $args ); } elseif ( \WP_Mock::strictMode() ) { - throw new \PHPUnit_Framework_ExpectationFailedException( + throw new \PHPUnit\Framework\ExpectationFailedException( sprintf( 'No handler found for %s', $function_name ) ); } diff --git a/php/WP_Mock/Hook.php b/php/WP_Mock/Hook.php index 6113e417..57055990 100644 --- a/php/WP_Mock/Hook.php +++ b/php/WP_Mock/Hook.php @@ -72,11 +72,11 @@ protected abstract function new_responder(); /** * Throw an exception if strict mode is on * - * @throws \PHPUnit_Framework_ExpectationFailedException + * @throws \PHPUnit\Framework\ExpectationFailedException */ protected function strict_check() { if ( \WP_Mock::strictMode() ) { - throw new \PHPUnit_Framework_ExpectationFailedException( $this->get_strict_mode_message() ); + throw new \PHPUnit\Framework\ExpectationFailedException( $this->get_strict_mode_message() ); } } diff --git a/php/WP_Mock/Tools/Constraints/ExpectationsMet.php b/php/WP_Mock/Tools/Constraints/ExpectationsMet.php index df0d31ed..862ef998 100644 --- a/php/WP_Mock/Tools/Constraints/ExpectationsMet.php +++ b/php/WP_Mock/Tools/Constraints/ExpectationsMet.php @@ -2,11 +2,11 @@ namespace WP_Mock\Tools\Constraints; -use PHPUnit_Framework_Constraint; +use PHPUnit\Framework\Constraint\Constraint; use Mockery; use Exception; -class ExpectationsMet extends PHPUnit_Framework_Constraint { +class ExpectationsMet extends \PHPUnit\Framework\Constraint\Constraint { private $_mockery_message; diff --git a/php/WP_Mock/Tools/Constraints/IsEqualHtml.php b/php/WP_Mock/Tools/Constraints/IsEqualHtml.php index 40e26e16..cb188f7d 100644 --- a/php/WP_Mock/Tools/Constraints/IsEqualHtml.php +++ b/php/WP_Mock/Tools/Constraints/IsEqualHtml.php @@ -2,9 +2,9 @@ namespace WP_Mock\Tools\Constraints; -use PHPUnit_Framework_Constraint_IsEqual; +use PHPUnit\Framework\Constraint\IsEqual; -class IsEqualHtml extends PHPUnit_Framework_Constraint_IsEqual { +class IsEqualHtml extends \PHPUnit\Framework\Constraint\IsEqual { private function clean( $thing ) { $thing = preg_replace( '/\n\s+/', '', $thing ); diff --git a/php/WP_Mock/Tools/TestCase.php b/php/WP_Mock/Tools/TestCase.php index 28fc3d5b..d569782f 100644 --- a/php/WP_Mock/Tools/TestCase.php +++ b/php/WP_Mock/Tools/TestCase.php @@ -2,7 +2,7 @@ namespace WP_Mock\Tools; -use PHPUnit_Framework_TestResult; +use PHPUnit\Framework\TestResult; use Exception; use Mockery; use ReflectionMethod; @@ -11,7 +11,7 @@ use WP_Mock\Tools\Constraints\ExpectationsMet; use WP_Mock\Tools\Constraints\IsEqualHtml; -abstract class TestCase extends \PHPUnit_Framework_TestCase { +abstract class TestCase extends \PHPUnit\Framework\TestCase { protected $mockedStaticMethods = array(); @@ -296,7 +296,7 @@ protected function setUpContentFiltering() { } } - public function run( PHPUnit_Framework_TestResult $result = null ) { + public function run( \PHPUnit\Framework\TestResult $result = null ) { if ( $result === null ) { $result = $this->createResult(); } diff --git a/tests/DeprecatedMethodsTest.php b/tests/DeprecatedMethodsTest.php index ebf79820..5251bac1 100644 --- a/tests/DeprecatedMethodsTest.php +++ b/tests/DeprecatedMethodsTest.php @@ -1,6 +1,6 @@ setTestCase( $case ); $listener->setTestResult( $result ); $result->shouldReceive( 'addFailure' ) ->once() - ->with( $case, Mockery::type( 'PHPUnit_Framework_RiskyTestError' ), 0 ); + ->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' ); + $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 ); + ->with( $case, Mockery::type( '\PHPUnit\Framework\RiskyTestError' ), 0 ); WP_Mock::wpPassthruFunction( 'foobar' ); $listener->checkCalls(); } diff --git a/tests/FunctionMocksTest.php b/tests/FunctionMocksTest.php index 36c38324..accf28c9 100644 --- a/tests/FunctionMocksTest.php +++ b/tests/FunctionMocksTest.php @@ -1,6 +1,6 @@ shouldReceive( 'addFailure' )->never(); - /** @var \PHPUnit_Framework_TestResult $result */ + /** @var \\PHPUnit\Framework\TestResult $result */ $this->object->setTestResult( $result ); $this->object->checkCalls(); @@ -48,23 +48,23 @@ public function testCheckCallsNoCalls() { public function testCheckCalls_scalar_only() { $this->object->logDeprecatedCall( 'FooBar::bazBat', array( 'string', true, 42 ) ); $this->object->setTestName( 'TestName' ); - $testCase = Mockery::mock( 'PHPUnit_Framework_TestCase' ); - /** @var PHPUnit_Framework_TestCase $testCase */ + $testCase = Mockery::mock( '\PHPUnit\Framework\TestCase' ); + /** @var \PHPUnit\Framework\TestCase $testCase */ $this->object->setTestCase( $testCase ); - $result = Mockery::mock( 'PHPUnit_Framework_TestResult' ); + $result = Mockery::mock( '\PHPUnit\Framework\TestResult' ); $result->shouldReceive( 'addFailure' ) ->once() ->andReturnUsing( function ( $case, $exception, $int ) use ( $testCase ) { - PHPUnit_Framework_Assert::assertSame( $testCase, $case ); - PHPUnit_Framework_Assert::assertTrue( $exception instanceof PHPUnit_Framework_RiskyTest ); + \PHPUnit\Framework\Assert::assertSame( $testCase, $case ); + \PHPUnit\Framework\Assert::assertTrue( $exception instanceof \PHPUnit\Framework\RiskyTest ); $message = <<getMessage() ); - PHPUnit_Framework_Assert::assertTrue( 0 === $int ); + \PHPUnit\Framework\Assert::assertEquals( $message, $exception->getMessage() ); + \PHPUnit\Framework\Assert::assertTrue( 0 === $int ); } ); - /** @var \PHPUnit_Framework_TestResult $result */ + /** @var \\PHPUnit\Framework\TestResult $result */ $this->object->setTestResult( $result ); $this->object->checkCalls(); @@ -81,15 +81,15 @@ public function testCheckCalls_non_scalars() { $this->object->logDeprecatedCall( 'LongerClassName::callback', array( array( $object1, 'shouldReceive' ) ) ); $this->object->logDeprecatedCall( 'BazBat::fooBar', array( range( 1, $range ), $resource ) ); $this->object->setTestName( 'OtherTest' ); - $testCase = Mockery::mock( 'PHPUnit_Framework_TestCase' ); - /** @var PHPUnit_Framework_TestCase $testCase */ + $testCase = Mockery::mock( '\PHPUnit\Framework\TestCase' ); + /** @var \PHPUnit\Framework\TestCase $testCase */ $this->object->setTestCase( $testCase ); - $result = Mockery::mock( 'PHPUnit_Framework_TestResult' ); + $result = Mockery::mock( '\PHPUnit\Framework\TestResult' ); $testClosure = function ( $case, $exception, $int ) use ( $testCase, $callback1, $object1, $range ) { $callback1 = get_class( $callback1 ) . ':' . spl_object_hash( $callback1 ); $object1 = get_class( $object1 ) . ':' . spl_object_hash( $object1 ); - PHPUnit_Framework_Assert::assertSame( $testCase, $case ); - PHPUnit_Framework_Assert::assertTrue( $exception instanceof PHPUnit_Framework_RiskyTest ); + \PHPUnit\Framework\Assert::assertSame( $testCase, $case ); + \PHPUnit\Framework\Assert::assertTrue( $exception instanceof \PHPUnit\Framework\RiskyTest ); $message = <<"] @@ -97,13 +97,13 @@ public function testCheckCalls_non_scalars() { ["Array([$range] ...)","Resource"] LongerClassName::callback ["[<$object1>,shouldReceive]"] EOT; - PHPUnit_Framework_Assert::assertEquals( $message, $exception->getMessage() ); - PHPUnit_Framework_Assert::assertTrue( 0 === $int ); + \PHPUnit\Framework\Assert::assertEquals( $message, $exception->getMessage() ); + \PHPUnit\Framework\Assert::assertTrue( 0 === $int ); }; $result->shouldReceive( 'addFailure' ) ->once() ->andReturnUsing( $testClosure ); - /** @var \PHPUnit_Framework_TestResult $result */ + /** @var \\PHPUnit\Framework\TestResult $result */ $this->object->setTestResult( $result ); try { diff --git a/tests/WP_MockTest.php b/tests/WP_MockTest.php index 9bd6d35d..c129a83b 100644 --- a/tests/WP_MockTest.php +++ b/tests/WP_MockTest.php @@ -1,6 +1,6 @@ Date: Sun, 3 Dec 2017 10:46:22 -0800 Subject: [PATCH 08/13] Update Travis config --- .travis.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 0992a087..9e114540 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,20 +2,20 @@ language: php matrix: include: - - php: '5.6' - env: WP_MOCK_INSTALL_LOWEST=0 - php: '7.0' env: WP_MOCK_INSTALL_LOWEST=0 - php: '7.1' env: WP_MOCK_INSTALL_LOWEST=0 + - php: '7.2' + env: WP_MOCK_INSTALL_LOWEST=0 - php: nightly env: WP_MOCK_INSTALL_LOWEST=0 - - php: '5.6' - env: WP_MOCK_INSTALL_LOWEST=1 - php: '7.0' env: WP_MOCK_INSTALL_LOWEST=1 - php: '7.1' env: WP_MOCK_INSTALL_LOWEST=1 + - php: '7.2' + env: WP_MOCK_INSTALL_LOWEST=1 - php: nightly env: WP_MOCK_INSTALL_LOWEST=1 From 104e1454a6da45381a26bc8b1143fef186ead3fd Mon Sep 17 00:00:00 2001 From: Eric Mann Date: Sun, 3 Dec 2017 10:55:55 -0800 Subject: [PATCH 09/13] Reference the appropriate base test case. Fixes #113 --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b35249a4..280f847c 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ require_once 'vendor/autoload.php'; Finally, register calls inside your test class to instantiate and clean up the `WP_Mock` object: ```php -class MyTestClass extends PHPUnit_Framework_TestCase { +class MyTestClass extends \WP_Mock\Tools\TestCase { public function setUp() { \WP_Mock::setUp(); } @@ -85,7 +85,7 @@ WP_Mock will ignore any attempts to activate strict mode after the first time it Write your tests as you normally would. If you desire specific responses from WordPress API calls, wire those specifically. ```php -class MyTestClass extends PHPUnit_Framework_TestCase { +class MyTestClass extends \WP_Mock\Tools\TestCase { public function setUp() { \WP_Mock::setUp(); } From f3d07deb457de8ee98886aa0dd1e7bdfafef6421 Mon Sep 17 00:00:00 2001 From: Eric Mann Date: Sun, 3 Dec 2017 11:05:35 -0800 Subject: [PATCH 10/13] Add Coverage tests --- .travis.yml | 10 +++++++++- composer.json | 14 ++++++++++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index 9e114540..605827b6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -22,7 +22,12 @@ matrix: before_script: - if [ 1 -eq $WP_MOCK_INSTALL_LOWEST ]; then composer update --prefer-lowest; else composer update; fi -script: composer test +script: + - mkdir -p build/logs + - composer test + +after_script: + - php vendor/bin/coveralls -v cache: directories: @@ -32,3 +37,6 @@ branches: only: - dev - master + +after_success: + - travis_retry php vendor/bin/coveralls diff --git a/composer.json b/composer.json index 35210b5e..94f45c64 100644 --- a/composer.json +++ b/composer.json @@ -11,18 +11,24 @@ }, "require-dev": { "behat/behat" : "^3.0", - "sebastian/comparator": ">=1.2.3" + "sebastian/comparator": ">=1.2.3", + "satooshi/php-coveralls": "^1.0" }, "autoload" : { "psr-4" : {"WP_Mock\\": "./php/WP_Mock"}, "classmap": ["php/WP_Mock.php"] }, "scripts": { - "test:behat": "behat", - "test:phpunit": "phpunit", - "test" : [ + "test:behat" : "behat", + "test:phpunit" : "phpunit", + "test:phpunitcov": "phpunit --coverage-clover build/logs/clover.xml", + "test" : [ "@test:behat", "@test:phpunit" + ], + "testcov" : [ + "@test:behat", + "@test:phpunitcov" ] } } From df518dc9c39475ee83fbdaa3ad8dba9768dc89cd Mon Sep 17 00:00:00 2001 From: Eric Mann Date: Sun, 3 Dec 2017 11:15:41 -0800 Subject: [PATCH 11/13] Fix testrunner --- .travis.yml | 2 +- composer.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 605827b6..e838aace 100644 --- a/.travis.yml +++ b/.travis.yml @@ -24,7 +24,7 @@ before_script: script: - mkdir -p build/logs - - composer test + - composer run-script coverage after_script: - php vendor/bin/coveralls -v diff --git a/composer.json b/composer.json index 94f45c64..6e2025e4 100644 --- a/composer.json +++ b/composer.json @@ -26,7 +26,7 @@ "@test:behat", "@test:phpunit" ], - "testcov" : [ + "coverage" : [ "@test:behat", "@test:phpunitcov" ] From 69dc05a6133e4b6a2c9eb501daef3232ac3e5919 Mon Sep 17 00:00:00 2001 From: Eric Mann Date: Sun, 3 Dec 2017 11:21:45 -0800 Subject: [PATCH 12/13] Update test whitelist --- phpunit.xml.dist | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 1215421b..068917ee 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,9 +1,22 @@ - + ./tests/ + + + + php + + From 0354413d63cbae920ffc0676443c6d9dd330ce40 Mon Sep 17 00:00:00 2001 From: Eric Mann Date: Sun, 3 Dec 2017 11:27:57 -0800 Subject: [PATCH 13/13] Bump for 0.3.0 --- README.md | 14 ++++++++++---- composer.json | 2 +- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 280f847c..f95e2a41 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,15 @@ -# WP_Mock +# WP_Mock ![PHP 7.0+][php-image] [![Build Status][travis-image]][travis-url] [![Coverage Status][coveralls-image]][coveralls-url] [![Packagist][packagist-image]][packagist-url] WP_Mock is an API mocking framework, built and maintained by [10up](http://10up.com) for the purpose of making it possible to properly unit test within WordPress. -[![Build Status](https://travis-ci.org/10up/wp_mock.svg?branch=dev)](https://travis-ci.org/10up/wp_mock) - ## Installation First, add WP Mock as a dev-dependency with [Composer](http://getcomposer.org): ```bash -composer require --dev 10up/wp_mock:0.2.0 +composer require --dev 10up/wp_mock:0.3.0 ``` Then, make sure your bootstrap file is loading the composer autoloader: @@ -399,3 +397,11 @@ function test_get_post_ids() { ## Contributing Thanks so much for being interested in contributing! Please read over our [guidelines](https://github.com/10up/wp_mock/blob/dev/CONTRIBUTING.md) before you get started. + +[php-image]: https://img.shields.io/badge/php-7.0%2B-green.svg +[packagist-image]: https://img.shields.io/packagist/dt/10up/wp_mock.svg +[packagist-url]: https://packagist.org/packages/10up/wp_mock +[travis-image]: https://travis-ci.org/10up/wp_mock.svg?branch=master +[travis-url]: https://travis-ci.org/10up/wp_mock +[coveralls-image]: https://coveralls.io/repos/github/10up/wp_mock/badge.svg?branch=master +[coveralls-url]: https://coveralls.io/github/10up/wp_mock?branch=master diff --git a/composer.json b/composer.json index 6e2025e4..9b23cebf 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+", - "version" : "0.2.0", + "version" : "0.3.0", "require" : { "php" : ">=7.0", "phpunit/phpunit" : ">=6.0",