aaronhipple/sampler

使用PHPUnit测试您的docblocks中的内联代码示例

0.2.0 2017-11-18 23:46 UTC

This package is not auto-updated.

Last update: 2024-09-15 04:31:27 UTC


README

Latest Stable Version Build Status Maintainability Test Coverage

使用PHPUnit测试您的docblocks中的内联代码示例!

注意事项

  • 这并不是特别成熟。欢迎提出问题和提交拉取请求。
  • 提取的示例代码会通过eval进行测试,存在所有这些安全问题。像往常一样,只在敏感环境中运行受信任的代码。
  • 目前它只适用于类、类方法和函数。它不会魔法般地公开私有或受保护的方法,因此适用正常规则。

使用方法

首先,将sampler作为依赖项安装。

$ composer require aaronhipple/sampler

然后,在您的PHPUnit测试套件中实现一个扩展AbstractSampleTestCase的用例。

use AaronHipple\Sampler\AbstractSampleTestCase;

class SampleTest extends AbstractSampleTestCase
{
    /**
     * Provide absolute paths to the directories to scan for samples.
     * 
     * @return []string An array of folder paths.
     */
    protected function paths() 
    {
        return [__DIR__ . '/../src'];
    }
   
    /**
     * (Optional) Provide a list of file extensions to scan.
     */
    protected function extensions() 
    {
        return ['php', 'inc'];
    }
}

最后,编写一些示例!您可以使用我们的@sample注释...

/**
 * Say hello to someone!
 *
 * @sample
 *   use PHPUnit\Framework\Assert;
 *   Assert::assertInternalType(
 *     'string',
 *     say_hello('Frank')
 *   );
 * @sample
 *   use PHPUnit\Framework\Assert;
 *   Assert::assertEquals(
 *     'Hello, Aaron!',
 *     say_hello('Aaron')
 *   );
 */
function say_hello($name) {
  return "Hello, $name!";
}

...或者传统的内联markdown。

/**
 * Say hello to someone!
 *
 * # Examples
 *
 * ```
 * use PHPUnit\Framework\Assert;
 * Assert::assertEquals(
 *   'Hello, Aaron!',
 *   say_hello('Aaron')
 * );
 * ```
 *
 * ```
 * use PHPUnit\Framework\Assert;
 * Assert::assertEquals(
 *   'Hello, Frank!',
 *   say_hello('Frank')
 * );
 * ```
 */
function say_hello($name) {
  return "Hello, $name!";
}

使用phpunit像往常一样运行您的测试套件。

贡献

  • 欢迎提交拉取请求。
  • 请提供更多测试。
  • PSR-2是好的。使用phpcbf