-
Notifications
You must be signed in to change notification settings - Fork 70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Trouble getting a simple check to work #47
Comments
Or this one also: Test:
Code:
It feels like WP_Mock is there but isn't running or something. Although the |
Hey @bfintal thanks for filing the issue. This certainly seems serious. Could you please provide the version of WP Mock you're using? To get that, you can run Also, could you give us a little more information your setup? Judging by your use of |
I'm using 1.0.x-dev (also tried dev-master before)
My setup is:
Yup that's what I'm trying to do. Although I have also tried just extending The project I'm trying to build unit tests on is Titan Framework, it's grown quite a bit so it definitely needs unit testing. The whole setup with the bootstrap and install scripts are in the repo under the |
Hello, I have exactly the same error with the similar simple test case. I am using vccw on Mac. I googled the error but could not figure out the cause of this error. I am new to WP_Mock so I might be missing something though... Thanks for your help!
My test is class WashokuAdminTest extends PHPUnit_Framework_TestCase {
public function setUp() {
\WP_Mock::setUp();
}
public function tearDown() {
\WP_Mock::tearDown();
}
public function test_init() {
$this->wadmin = new WashokuAdmin;
\WP_Mock::expectActionAdded( 'admin_menu', array ($this->wadmin, 'remove_sidemenu' ) );
$this->wadmin->init();
}
} The plugin function is: class WashokuAdmin {
function init() {
add_action( 'admin_menu', array( $this, 'remove_sidemenu' ) );
add_action( 'do_meta_boxes', array( $this, 'remove_plugin_metaboxes' ) );
register_activation_hook( __FILE__, array( $this, 'permit_upload_to_contributor' ) );
add_filter( 'get_user_metadata', array( $this, 'disable_tgmpa_notice'), 10, 4);
}
} |
@goodpic Glad to know I'm not alone in this one :) I think you should do this though to catch the action:
|
@bfintal Oh! thank you! I fixed the line (and updated the comment), but still see the same error. |
@johnpbloch Any clues on this? |
At this time, use of WP Mock in conjunction with the WordPress core testing framework is not supported. WP Mock was created to remove WordPress core and all of its state from the unit testing equation, so it doesn't really work well together with the code it was meant to replace. |
I haven't closed this yet because I'm open to suggestions for use-cases that would make this pairing worth supporting. I can certainly see the value of using both separately, one for unit tests and one for integration tests, I'm just having trouble imagining a scenario where using both together at the same time in the same context is a value-add. But like I said, I'm happy to hear ideas. :D |
Sorry, I'm really new to PHP Unit and unit testing in general so I don't quite understand what you mean by not supporting the core testing framework. Does extending How would you use WP_Mock without the core testing framework? Is there something in the bootstrap file or the unit test installer that needs to be removed? |
The core test framework itself triggers this. The reason being it loads WordPress itself. WP Mock works by expecting WordPress not to be present and then defining any WordPress functions or classes as needed.
To see how you can use WP Mock, check out WP Async Task.
All of it. The bootstrap from the core framework is what loads WordPress. So the whole thing needs to go. Again, WP Async Task has a good example of what a WP Mock bootstrap file ought to look like. And to be perfectly honest, you don't even need that. You could just use |
I see, so this means that the main problem within my setup is that I ran: Okay new problem. I tried removing the existing bootstrap and just used the one in |
Are your tests still extending |
Hmmm - I too am having some issues getting a simple example to work. I've attached an image of my plugins directory structure: I'm using the simple bootstrap file <?php
require_once __DIR__ . '/vendor/autoload.php'; I'm using examples from the documentation in my test-restaurant-connect.php file (although I extended from TestCase instead) <?php
/**
* Class MyTestClass
* @test
*/
class MyTestClass extends \WP_Mock\Tools\TestCase {
public function setUp() {
\WP_Mock::setUp();
}
public function tearDown() {
\WP_Mock::tearDown();
}
/**
* Assume that my_permalink_function() is meant to do all of the following:
* - Run the given post ID through absint()
* - Call get_permalink() on the $post_id
* - Pass the permalink through the 'special_filter' filter
* - Trigger the 'special_action' WordPress action
*/
public function test_my_permalink_function() {
\WP_Mock::wpFunction( 'get_permalink', array(
'args' => 42,
'times' => 1,
'return' => 'http://example.com/foo'
) );
\WP_Mock::wpPassthruFunction( 'absint', array( 'times' => 1 ) );
\WP_Mock::onFilter( 'special_filter' )
->with( 'http://example.com/foo' )
->reply( 'https://example.com/bar' );
\WP_Mock::expectAction( 'special_action', 'https://example.com/bar' );
$result = my_permalink_function( 42 );
$this->assertEquals( 'https://example.com/bar', $result, 'Result was ' . $result );
}
public function test_uses_get_post() {
global $post;
$post = new \stdClass;
$post->ID = 42;
$post->special_meta = '<p>I am on the end</p>';
\WP_Mock::wpFunction( 'get_post', array(
'times' => 1,
'args' => array( $post->ID ),
'return' => $post,
) );
/*
* Let's say our function gets the post and appends a value stored in
* 'special_meta' to the content.
*/
$results = special_the_content( '<p>Some content</p>' );
/*
* In addition to failing if this assertion is false, the test will fail
* if get_post is not called with the arguments above.
*/
$this->assertEquals( '<p>Some content</p><p>I am on the end</p>', $results );
}
} In my main plugin file I define the following functions: function my_permalink_function( $post_id ) {
$permalink = get_permalink( absint( $post_id ) );
$permalink = apply_filters( 'special_filter', $permalink );
do_action( 'special_action', $permalink );
return $permalink;
}
function special_the_content( $content ) {
return $content . '<p>I am on the end</p>';
} When I run phpunit from under the plugin directory I receive the following error
So this looks like my main plugin file isn't being loaded (with WP_UnitTestCase I'd pull it in in the bootstrap file). If I cheat and try to define the functions right before the MyTestClass class the tests run but I get the following error:
@johnpbloch can you point me in the right direction? I looked at WP Async Task but it doesn't look like they are doing anything special to pull in their plugin / WordPress files. I assume this is done by WP_Mock? Can you point me to where? |
You could try either putting your main plugin file in the <?php
require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/restaurant-connect.php'; |
I ran into this issue today. Here is my test method
Mind you, i already have Any help please? cc @johnpbloch |
These issues are all highly related, and can be solved with some more in-depth docs and tutorials. Closing not to say it's an invalid issue. You're all definitely heard here, and we'll ship some more solid documentation in the coming weeks to help move past these issues. |
Okay so I dunno if I'm just missing something here. I've trimmed everything down to the very very basics. I have WP_Mock setup, and I have a few working unit tests already. I wanted to check if an action was being done
do_action
in a function although with anything I do, I get this error after the test:This is my back to basics test file:
Now this is the code being checked:
That test code above gives out the same error for me. I can't get this simple one to work. I've also tried with other functions like
expectFilterAdded
.I would appreciate any help on this.
The text was updated successfully, but these errors were encountered: