承接 pretzlaw/wp-integration-test 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

pretzlaw/wp-integration-test

Composer 安装命令:

composer require pretzlaw/wp-integration-test

包简介

Mocking return value of functions/filters and more for testing WordPress with PHPUnit.

README 文档

README

WordPress Integration Test Helper

Mocking return value of functions/filters and more for testing WordPress with PHPUnit.

Writing tests with WordPress is a pain as the very old official WordPress Unit Tests always require a lot of hands on for custom projects and other Testing-Frameworks try to mock the hell out of WordPress. The solution is to have a nice integration tests package that ...

  • ... can be integrated into your already existing tests (using Traits).
  • ... enabled you to test your package against other Plugins or Themes.
  • ... allows testing in advanced complex projects.

Overall the goal is simplicity and no time wasting crap (for me and you).

Install

Download or just

composer install --dev pretzlaw/wp-integration-test

Besides PHP and WordPress we do not require that much (see Packagist.org for more details):

  • phpUnit 6.5 - 9.5
  • mockery 1.3

Usage

If you start from scratch and do not have some bootstrapping already, then you can use our bootstrapping like this:

<phpunit bootstrap="vendor/Pretzlaw/WPInt/bootstrap.php">
	<testsuites>
		<testsuite name="default">
		    <!-- CHANGE THIS TO WHERE YOUR PHPUNIT TEST CLASSES ARE -->
			<directory>lib/tests</directory>
		</testsuite>
	</testsuites>
</phpunit>

Using this bootstrap.php is not mandatory. Feel free to create a custom bootstrapping file, when you need to test CLI and admin stuff too.

Example

If you know PHPUnit already then asserting and mocking shouldn't be something new. With WPInt it can be for most PHPUnit Tests by adding one Trait:

class FooTest extends \PHPUnit\Framework\TestCase {

    use \Pretzlaw\WPInt\Traits\WordPressTests;
    
    function testBar() {

        // Assertions (simple or with special constraints)
        static::assertActionHasCallback( 'init', 'my_own_init' );
        static::assertShortcodeHasCallback(
            [ new IsInstanceOf( MyOwn::class ), 'some_method' ],
            'my_shortcode'
        );
        
        // Mock posts or meta-data
        $this->mockGetPost( 1337 )->andReturn( /* your wp post mock */ );
        $this->mockPostMeta( 'some_key' )->andReturn( 'Some value!' ); // For all posts
        $this->mockMetaData( 'my-own-cpt', 'another_key', 1337 )->andReturn( 'ec' ); // Just for ID 1337
        
        // Mock actions, filter, cache, ...
        $this->mockFilter( 'user_has_cap' )
             ->andReturn( true );
        
        $this->mockCache()
            ->shouldReceive('get')
            ->with('my_own_cache')
            ->andReturn('yeah');

        // Or use one of several shortcuts and helper
        $this->disableWpDie();
    }
}

As you see above we are using mockery/mockery:~1 which is easier to use and maintain but uses different method names (e.g. shouldReceive, andReturn as seen above).

List of all Assertions

  • assert Action Has Callback
  • assert Action Not Empty
  • assert Action Not Has Callback
  • assert Filter Empty
  • assert Filter Has Callback
  • assert Filter Not Empty
  • assert Filter Not Has Callback
  • assert Plugin Is Active
  • assert Plugin Is Not Active
  • assert Post Type Args
  • assert Post Type Labels
  • assert Post Type Registered
  • assert Shortcode Exists
  • assert Shortcode Has Callback
  • assert Shortcode Not Exists
  • assert Widget Exists
  • assert Widget Is Instance Of
  • assert Widget Is Not Instance Of
  • assert Widget Not Exists

List of WordPress Expectations / Mocks

  • expect Wp Post Insert Post
  • mock Cache
  • mock Current User
  • mock Filter
  • mock Get Post
  • mock Meta Data
  • mock Post Meta
  • mock Shortcode
  • mock User Meta

Other testing helper

  • backup Widgets
  • disable wp_die()
  • get All Shortcodes
  • get Shortcode Callback
  • get Widget Factory
  • get Wp Hooks
  • unregister All Widgets
  • unregister Widgets By Id

Feel free to request for additional features or point out more common shortcuts by opening an issue.

License

Copyright 2021 Pretzlaw (rmp-up.de)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

pretzlaw/wp-integration-test 适用场景与选型建议

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

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

围绕 pretzlaw/wp-integration-test 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 29.08k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 30
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

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

其他信息

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