wp_mock | WordPress API Mocking Framework | Mock library
kandi X-RAY | wp_mock Summary
kandi X-RAY | wp_mock Summary
WP_Mock is an API mocking framework, built and maintained by 10up for the purpose of making it possible to properly unit test within WordPress.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Compares two objects
- Return a new instance with the given arguments .
- Boot up WordPress .
- Applies one or more processors .
- Get the message for the deprecated php methods .
- Load a class .
- Convert callback to string
- Expect that the given filters are added .
- Wrapper for predefined functions
- Add a callback
wp_mock Key Features
wp_mock Examples and Code Snippets
Community Discussions
Trending Discussions on wp_mock
QUESTION
I'm writing tests using phpunit and WP_Mock for a WordPress plugin and I want to return a WP_Error
if the method in my class isn't being called correctly (receiving all of the arguments it needs).
The standard way of doing this of course, would be to create and return a new WP_Error
at that point - but as WordPress isn't loaded when I run the tests, the WP_Error
class doesn't exist.
Other WordPress classes are being mocked and injected with Mockery, but this seems like overkill to test the WP_Error
is being thrown; but I'm not seeing any other half-way sensible plan.
Is there a good way to mock the WP_Error
?
ANSWER
Answered 2018-Nov-26 at 05:47Easiest way:
Just include_once
the file with the definition for WP_Error
(wp-includes/class-wp-error.php) when bootstrapping your tests. That way the class will be defined when you need to instantiate the class.
WP_Error
doesn't have any other dependencies, so including it should be a big deal.
You are not not testing actual behaviour of the class, but only an object of that class is returned. So no need to mock anything if the proper definition exists.
Better way: Use WP-CLI to scaffold. Doing that you'd have access to wordpress functions and classes.
You'll need to install WP-CLI and have a working WP installation, with your plugin files in there.
First you'd need to do the plugin scaffolding. In your WP directory run
QUESTION
I've come back after the weekend to the following error:
...ANSWER
Answered 2018-Jul-10 at 10:37In the end I updated my composer file requirements to this
QUESTION
I'm currently running some unit tests on a WordPress plug-in, using PHPUnit
and WP_Mock
(without connecting to WordPress database).
As the title states, I need to mock a WP_REST_Request
Object, to give it as a parameter to a method under test.
Maybe I could do it by mocking the return of the URL used by the WP_REST_Request::from_url()
method, or maybe with the use of getMockFromWsdl()
.
I'm open to every kind of ideas
ANSWER
Answered 2017-Jun-07 at 14:35The WP_REST_Request class implements the ArrayAccess interface so you can create a mock in this way:
QUESTION
I know that creating an instance of a Class
in the middle of a method
it's a bad practice since it makes code hard to test.
But I can't refactor the code, so I need to find a way to mock an Object
created with new
in the middle of a method
under test.
Used Frameworks: PHPUnit
, Mockery
, WP_Mock
Example: Here I need to mock the get_second_string()
method from the instance of the class ExternalClass
ANSWER
Answered 2017-Jun-05 at 11:08IMHO there is no way to do such a thing. You should pass the object as parameter to the method.
QUESTION
My Test includes the wp_verify_nonce and implemented that in OOP environment. Now the problem is: If verifyNonce() has no arguemnts passed, then i want to expect return false due to the function:
...ANSWER
Answered 2017-Mar-30 at 07:20PHPUnit tests your returning value of $myWPNonce->verifyNonce()
. Before this can be accomplished you must pass the parameter, as it is defined in the method as mandatory parameter.
PHPUnit does not test forgotten/missing parameters. PHP itself validates this.
So pass something what should return false there as result.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install wp_mock
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page