Skip to content

Commit

Permalink
Merging 0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ericmann committed Dec 3, 2017
2 parents ad46c9c + 0354413 commit 6495655
Show file tree
Hide file tree
Showing 21 changed files with 263 additions and 97 deletions.
24 changes: 18 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,32 @@ 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: '5.6'
env: WP_MOCK_INSTALL_LOWEST=1
- php: '7.2'
env: WP_MOCK_INSTALL_LOWEST=0
- php: nightly
env: WP_MOCK_INSTALL_LOWEST=0
- 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: '7.2'
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

script: composer test
script:
- mkdir -p build/logs
- composer run-script coverage

after_script:
- php vendor/bin/coveralls -v

cache:
directories:
Expand All @@ -28,3 +37,6 @@ branches:
only:
- dev
- master

after_success:
- travis_retry php vendor/bin/coveralls
42 changes: 36 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.

<a href="http://10up.com/contact/"><img src="https://10updotcom-wpengine.s3.amazonaws.com/uploads/2016/10/10up-Github-Banner.png" width="850"></a>

[![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:
Expand All @@ -23,7 +21,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();
}
Expand Down Expand Up @@ -85,7 +83,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();
}
Expand Down Expand Up @@ -331,6 +329,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.
Expand Down Expand Up @@ -375,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
33 changes: 16 additions & 17 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,33 @@
"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" : ">=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"
},
"conflict": {
"phpunit/phpunit": ">=6.0"
"behat/behat" : "^3.0",
"sebastian/comparator": ">=1.2.3",
"satooshi/php-coveralls": "^1.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",
"test" : [
"test:behat" : "behat",
"test:phpunit" : "phpunit",
"test:phpunitcov": "phpunit --coverage-clover build/logs/clover.xml",
"test" : [
"@test:behat",
"@test:phpunit"
],
"coverage" : [
"@test:behat",
"@test:phpunitcov"
]
}
}
2 changes: 1 addition & 1 deletion features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
14 changes: 7 additions & 7 deletions features/bootstrap/FunctionsContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) );
}

/**
Expand Down Expand Up @@ -48,15 +48,15 @@ public function iMockFunctionWpMockTestToEcho( $function ) {
*/
public function strictModeIsOn() {
FeatureContext::forceStrictModeOn();
PHPUnit_Framework_Assert::assertTrue( WP_Mock::strictMode() );
\PHPUnit\Framework\Assert::assertTrue( WP_Mock::strictMode() );
}

/**
* @Given strict mode is off
*/
public function strictModeIsOff() {
FeatureContext::forceStrictModeOff();
PHPUnit_Framework_Assert::assertFalse( WP_Mock::strictMode() );
\PHPUnit\Framework\Assert::assertFalse( WP_Mock::strictMode() );
}

/**
Expand All @@ -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 ) ) );
}

/**
Expand All @@ -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...
}
}
Expand All @@ -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 );
}

/**
Expand Down
34 changes: 31 additions & 3 deletions features/bootstrap/HooksContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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:
*/
Expand Down Expand Up @@ -161,15 +180,24 @@ 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 );
}

/**
* @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 ) {
Expand Down
36 changes: 36 additions & 0 deletions features/hooks.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 22 additions & 2 deletions php/WP_Mock.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,30 @@ 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
*/
public static function expectFilter( $filter ) {
$intercept = \Mockery::mock( 'intercept' );
$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 );
$responder->reply( new \WP_Mock\InvokedFilterValue( array( $intercept, 'intercepted' ) ) );
}

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 );
}
}

Expand Down Expand Up @@ -330,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 );
}
}

Expand Down
Loading

0 comments on commit 6495655

Please sign in to comment.