makeweb/wordpress-test-environment 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

makeweb/wordpress-test-environment

Composer 安装命令:

composer require makeweb/wordpress-test-environment

包简介

Boot wordpress to test your code in an actual wordpress environment

README 文档

README

Easily pull Wordpress in to your plugin, library or package as a composer development dependency and test your code in an actual wordpress environment.

This package aims to support developers who wish to test code which depends on Wordpress in two key ways:

  1. Boot wordpress to allow usage of wordpress database and global functions

  2. Simulate actual HTTP requests to the wordpress application

Installation

From command line:

composer require --dev makeweb/wordpress-test-environment

Basic Usage

  1. Once installed, Create a new database in your local MySQL installation.

  2. Create a .env file in the base directory of the codebase you are testing and add the following to it (updating the credentials to match your newly created database and local environment):

DATABASE_NAME=database_name_here
DATABASE_USER=database_user_here
DATABASE_PASSWORD=database_password_here
DATABASE_HOST=127.0.0.1
SITE_URL=example.test
  1. Initialise Wordpress in your parent test case class

Pass the relative path to the directory which contains your .env file into loadEnvFrom()

use MakeWeb\WordpressTestEnvironment\Wordpress;
use PHPUnit\Framework\TestCase as BaseTestCase;

class TestCase extends BaseTestCase;
{
    public function setUp()
    {
        $this->wordpress = (new Wordpress)
            ->loadEnvFrom(__DIR__.'/..');
    } 
}
  1. Usage in your tests

Boot only

If you simply wish to boot wordpress to allow access to the database and Wordpress global functions. Add the following lines to the start of your test.

In your test file

class MyPluginTest extends TestCase
{
    public function the_main_plugin_class_can_use_wordpress_global_functions()
    {
        $this->wordpress->boot();

        $this->assertEquals('http://example.test', (new MyPluginClass)->url());
    }
}

In your main plugin file or source code

Here we are just calling a native Wordpress function get_site_url(). If everything is set up correctly the test above will pass with the code below because get_site_url() has been defined by wordpress.

Note: Make sure you are including the plugin file in your test bootstrap file manually or with an autoloader like composer

class MyPluginClass
{
    public function url()
    {
        return get_site_url();
    }
}

Test HTTP requests to your wordpress application

Sometimes your theme, plugin or library might actually handle HTTP requests (for example, if you are creating an API), or perhaps you just want to test the way your code affects the actual html output of the application. The Wordpress class exposes a get method which simulates an actual HTTP GET request to the given url. Wordpress, and your dependent code will be booted and executed as it would be in production. We don't need to call $this->wordpress->boot() when using the HTTP methods because it is done for us automatically.

In your test file

class MyPluginTest extends TestCase
{
    public function the_main_plugin_class_can_use_wordpress_global_functions()
    {
        // To boot the plugin which we are testing when booting wordpress, pass the relative path
        // to the main plugin file into the withPlugin() method. Installation and activation hooks
        // will not be called.
        $this->wordpress->withPlugin(__DIR__.'/../edd-sl-deployer.php');

        // Request the front page of the wordpress site and capture the result
        $response = $this->wordpress->get('/');

        // Assert that the response returned a 200 http status code
        $response->assertSuccessful();

        // Assert that our text was output in the html
        $response->assertSee('Hello World');
    }
}

In your plugin file or source code

/**
 * Plugin Name: Hello World Outputter
 * Plugin Description: An enterprise ready solution to output the text 'Hello World' on your website
 * Plugin URI: https://hello-world-outputter.com
 * Version: 1.0.0
 * Author: Andrew Feeney
**/

class MyPluginClass
{
    public function boot()
    {
        echo('Hello World');
    }
}

(new MyPluginClass)->boot();

makeweb/wordpress-test-environment 适用场景与选型建议

makeweb/wordpress-test-environment 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7 次下载、GitHub Stars 达 0, 最近一次更新时间为 2018 年 04 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 makeweb/wordpress-test-environment 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 makeweb/wordpress-test-environment 我们能提供哪些服务?
定制开发 / 二次开发

基于 makeweb/wordpress-test-environment 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 7
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 2
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-04-10