-
Notifications
You must be signed in to change notification settings - Fork 29
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
Expect a stub doesn't work #150
Comments
I think this is failing because you are both doing a stub and an expectation from the same function. On the same test, you can't do both things to the same function. This should be documented IIRC. I would suggest to use the expectations only, like this: class MyTest extends TestCase
{
public function testHasContentCallGetPostOnce(): void
{
$id = random_int(1, 100);
$content = <<<'TXT'
<!-- wp:heading {"level":1} -->
<h1 class="wp-block-heading">Heading 1</h1>
<!-- /wp:heading -->
<!-- wp:paragraph -->
<p>This is content.</p>
<!-- /wp:paragraph -->
TXT;
$post = $this->stubPost($id, ['content' => $content]);
\Brain\Monkey\Functions\expect('get_queried_object_id')
->once()
->andReturn($id);
\Brain\Monkey\Functions\expect('get_post')
->once()
->with($id)
->andReturn($post);
$class = My_Class::get_instance();
static::assertTrue($class->has_content($content));
static::assertTrue($class->has_content($content));
}
private function stubPost(int $id = 1, array $properties = []): \WP_Post
{
$post = \Mockery::mock(\WP_Post::class);
$post->ID = $id;
foreach ($properties as $key => $value) {
$property = "post_{$key}";
$post->{$property} = $value;
}
$post->post_content ??= '';
$post->post_type ??= 'page';
$post->post_status ??= 'publish';
return $post;
}
} |
Thank you, that worked just fine! I’ll leave it open since adding it to the documentation would be indeed nice. 🙂 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I use a stub to make
get_post
available, which returns a mock ofWP_Post
:This is my test:
My_Class
:Since I want to test whether I already searched for the content, which would result in an early return for the second run, I thought
expect('get_post')->once();
would be correct. However, the error message implies it is not:Am I doing wrong here?
As a workaround, I currently test for
expect('strpos')->once();
, which works when allowing redefining internals forstrpos
in thepatchwork.json
.The text was updated successfully, but these errors were encountered: