suin/livexample
Composer 安装命令:
composer require suin/livexample
包简介
Ensures your example codes works well by unit testing.
README 文档
README
This is a documentation oriented testing framework for from PHP 5.3 to 7.x. Its purpose is ensure your example codes works by unit testing. Inspired by Golang testing "Examples".
A simple test case example
See an example below. The first section is example code which will be tested. The comment which begins with Output is test code.
<?php function hello(string $name): string { return 'hello ' . $name; } echo hello('alice') . "\n"; echo hello('bob') . "\n"; // Output: // hello alice // hello carol
This example code is broken. The word "carol" is expected as output, however it does not appear. This test case fails, but you are able to know the example code has a problem and you may fix it.
How does it works?
Livexample does following steps:
- Find
Outputstatement in the example code by parsing it. - Run the code in PHPUnit and capture the output.
- Compare the expected output and the actual output.
Benefits
Sometimes example codes are broken, by adding new features, forgetting to update them, pull-requests which don't take care of example codes and so on.
Broken example codes are bad experience for library users.
By using Livexample, you will get benefits:
- 😆 Your example codes are protected from unexpected regression.
- 😇 You can easily check your example codes are valid.
- 😋 Your library users can use the valid example codes by copy and paste.
- 😉 You don't have to add big change to your existing sample codes; just adding some comments.
Features
- Livexample works a part of PHPUnit.
- Very low learning cost.
Getting started
Install suin/livexample via Composer:
composer require --dev suin/livexample
Add ExampleTest.php to your tests directory:
use Livexample\PHPUnit\ExampleTestCase; class ExampleTest extends ExampleTestCase { public function exampleFiles() { // specify your example code directory. return self::exampleDirectory('example'); } }
Run phpunit:
phpunit
Assertion syntax
There are two syntax for assertion:
- Long:
// Output: {expected output} - Short:
//=> {expected output}
echo 1; // Output: 1
echo "hello"; //=> hello
var_dump([1, 2, 3]); // Output: // array(3) { // [0] => // int(1) // [1] => // int(2) // [2] => // int(3) // }
Tips: var_dump($object)
The var_dump() function prints object identifiers, which is #some-number next to the class name. Since the identifiers depend on execution environment, you can not hard-code it in the Output: statement. Instead of hard-coding the identifier, you may use a placeholder %d for number:
<?php var_dump(new stdClass); // Output: // object(stdClass)#%d (0) { // }
Tips: run an example script with php command
php -d auto_prepend_file=vendor/autoload.php \
-d xdebug.overload_var_dump=0 \
example/simple-usage.php
Why supports PHP 5.3 in 2017?
The official support for PHP 5.3 has ended, however, RHEL/CentOS keeps supporting PHP 5.3 even in 2017. The old PHP environments still remain in the running service today. Livexample is also suppoed to be used for refactoring such old environments.
Changelog
Please see CHANGELOG for more details.
Contributing
Please see CONTRIBUTING for more details. Please fix my English 😂.
suin/livexample 适用场景与选型建议
suin/livexample 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 170 次下载、GitHub Stars 达 3, 最近一次更新时间为 2017 年 11 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「documentation」 「phpunit」 「TDD」 「unit testing」 「example code」 「phpunit extension」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 suin/livexample 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 suin/livexample 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 suin/livexample 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Helper that decorates any SUS with a phpspec lazy object wrapper
Provides access to magento2 object manager from behat and allows to change magento config settings temporarly
HiDev plugin for PHPUnit
Bookdown.io With Bootswatch Styles And Prism Syntax Highlighting
Testing Suite For Lumen like Laravel does.
A cli tool which generates unit tests.
统计信息
- 总下载量: 170
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 1
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-11-03