1. Go to this page and download the library: Download leoloso/faker library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
class MyPostCase extends FakerTestCase
{
public function testGetPostCanBeFaked()
{
$this->wpFaker->post(['id' => 42, 'title' => 'The Answer']);
static::assertSame('The Answer', get_post(42)->post_title);
static::assertFalse(get_post(1));
static::assertIsString(get_post_field('post_content', 42));
}
}
class MyUserCase extends FakerTestCase
{
public function testCurrentUserCanBeFaked()
{
$this->wpFaker->user(['role' => 'editor'])->__monkeyMakeCurrent();
static::assertTrue(current_user_can('edit_others_pages'));
}
}
function maybeAddError(\WP_Error $error): \WP_Error
{
if (is_error_there()) {
$error->add('code', 'A message', 'some data');
}
return $error;
}
class MyErrorCase extends FakerTestCase
{
/**
* Test that error have expected message and data
* when `is_error_there` returns true
*/
public function testMaybeAddErrorWhenErrorIsThere()
{
\Brain\Monkey\Functions\when('is_error_there')->justReturn(true);
$error = maybeAddError($this->wpFaker->error);
static::assertTrue($error->has_errors());
static::assertSame('A message', $error->get_error_message());
static::assertSame('some data', $error->get_error_data());
}
/**
* Test that error is empty when `is_error_there` returns false
*/
public function testMaybeAddErrorWhenErrorIsNotThere()
{
\Brain\Monkey\Functions\when('is_error_there')->justReturn(false);
$error = maybeAddError($this->wpFaker->error);
static::assertFalse($error->has_errors());
static::assertSame([], $error->get_error_messages());
}
}