otgs/unit-tests-framework
Composer 安装命令:
composer require otgs/unit-tests-framework
包简介
A framework to build unit tests with OTGS products
关键字:
README 文档
README
- Create a
testsdirectory in your project - Create a
phpunitsubdirectory intests1 - Copy
samples/phpunit.xmlfile in the root of your project2- Change
test-suite-nameinto something appropriated - Most likely, you won't need to change anything else in that file
- Change
- Copy
samples/bootstrap.phpintotests/phpunit- Read the comments in this file and make the appropriated changes
- Run
composer require --dev otgs/unit-tests-framework3 - Create another
testssubdirectory intests/phpunit- Write your tests there
- Unless you have customized the
phpunit.xmlfile, you don't need to name the test files and classes in any particular way
- Run
phpunitfrom your project's root to start your tests4
How to use the OTGS_TestCase class
In order to take the most of this library, all your tests classes should extext OTGS_TestCase.
Once you do that, this will happen:
\OTGS_TestCase::setupBeforeClass:
$_GETand$_POSTare set to an empty array- An instance of
FactoryMuffinis provided: you can refer to it asself::$fm(see "Resources and dependencies" for more details)
\OTGS_TestCase::setUp
FunctionMockeris initialized (see "Resources and dependencies" for more details)WP_Mockis initialized (see "Resources and dependencies" for more details)
\OTGS_TestCase::tearDown
WP_Mockis destroyedFunctionMockeris destroyedMockeryis destroyed (just in case it has been used)
\OTGS_TestCase::tearDownAfterClass
- Deletes all models created with
FactoryMuffin
Mock WP Core functions
This class also provide an helper method to quickly mock the functions defined by WordPress by using the \OTGS_TestCase::get_mocked_wp_core_functions which returns an instance of OTGS_Mocked_WP_Core_Functions.
OTGS_Mocked_WP_Core_Functions organize mocks in methods named using the same name of the file where the function is defined in WordPress codebase.
For instance, to mock of all functions defined in post.php like get_post, in your test you should simply call $this->get_mocked_wp_core_functions()->post().
To mock add_query_arg yo call $this->get_mocked_wp_core_functions()->functions() because add_query_arg is defined in functions.php.
OTGS_Mocked_WP_Core_Functions tries to handle dependencies.
So, if you call $this->get_mocked_wp_core_functions()->post() to mock wp_insert_post, you automatically call $this->get_mocked_wp_core_functions()->post(), so to get all the meta related functions mocked as well.
Finally, there is a "mock all" method you could use (though is discouraged) with $this->mock_all_core_functions().
Stub WP common classes
\OTGS_TestCase provides a helpful way to quickly get a stub of some of the most commonly used classes in WordPress.
By calling $this->stubs->wpdb() you will get a stub you can pass as a dependency of the classes you are testing.
If you need to control the behavior of this stub, you just use the standard PHPUnit mock helpers.
E.g. 1:
$wpdb = $this->stubs->wpdb(); $wpdb->method( 'get_var' )->willReturn( 1 );
E.g. 2:
$results = array( array( 'translation_id' => 1, 'element_id' => 1, 'language_code' => 'en', 'source_language_code' => null, 'trid' => 1, 'element_type' => 'post_page' ), array( 'translation_id' => 2, 'element_id' => 2, 'language_code' => 'fr', 'source_language_code' => 'en', 'trid' => 1, 'element_type' => 'post_page' ), ); $wpdb = $this->this->wpdb(); $wpdb->expects( $this->exactly( 2 ) )->method( 'get_results' )->willReturn( $results );
Other stubs you can get:
WP_Widgetwith$this->stubs->WP_Widget()WP_Themewith$this->stubs->WP_Theme()WP_Filesystem_Directwith$this->stubs->WP_Filesystem_Direct()WP_Querywith$this->stubs->WP_Query()
It is important to know that, if you only need the class to be defined (e.g. hard-dependency, or sub-classing), you don't need to assig the stub to a variable: just call the method.
A good example is with WordPress' widgets, where you may have your own widget which is supposed to extend WP_Widget.
In this case, unless you want to mock some of the WP_Widget methods, you simply call $this->stubs->WP_Widget(), then write your tests.
The class which extends WP_Widget will find a definition of this class, with all the methods (doing nothing).
Resources and dependencies
Below are some resources on writing unit tests which lead to the creation of this library and links to the libraries included here:
- Start from here for a general explanation: http://wordpress.stackexchange.com/a/164138/7291
- 10up's WP_Mock`: https://github.com/10up/wp_mock
- Mockery: https://github.com/padraic/mockery
- Function mocker: https://github.com/lucatume/function-mocker
- Factory Muffin: https://github.com/thephpleague/factory-muffin
- Factory Muffin Faker: https://github.com/thephpleague/factory-muffin-faker
- The DomCrawler Component: http://symfony.com/doc/current/components/dom_crawler.html
- The CssSelector Component: http://symfony.com/doc/current/components/css_selector.html
- php-loremipsum: https://github.com/joshtronic/php-loremipsum
Footnotes
-
it is recommended to keep your PHPUnit tests into a subfolder, because there are good changes you want to add other kind of tests (e.g. QUnit, PHPSpec, etc.) ↩
-
in case, for whatever reason, you can't do that, copy this file in
tests/phpunitor wherever you find it more convenient ↩ -
In some cases, you may want to use
composer require --dev otgs/unit-tests-framework:dev-develop. When you do that, you may also need to run this command firstcomposer config minimum-stability dev↩ -
If you've placed the
phpunit.xmlyou need to either move to that directory first, or tell phpunit where the file is withphpunit --configuration path/to/phpunit.xml↩
otgs/unit-tests-framework 适用场景与选型建议
otgs/unit-tests-framework 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 488.84k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2018 年 01 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「phpunit」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 otgs/unit-tests-framework 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 otgs/unit-tests-framework 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 otgs/unit-tests-framework 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
HiDev plugin for PHPUnit
Testing Suite For Lumen like Laravel does.
A cli tool which generates unit tests.
PHPUnit DbUnit Native Array-based Fixtures
PhpUnit test cases for Yii2 Framework
PHPUnit Pretty Result Printer
统计信息
- 总下载量: 488.84k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 19
- 依赖项目数: 10
- 推荐数: 0
其他信息
- 授权协议: GPL-2.0
- 更新时间: 2018-01-16