2017 © Pedro Peláez
 

library sampler

Test inline code samples from your docblocks using PHPUnit

image

aaronhipple/sampler

Test inline code samples from your docblocks using PHPUnit

  • Sunday, November 19, 2017
  • by ahipple
  • Repository
  • 1 Watchers
  • 0 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 8 Versions
  • 0 % Grown

The README.md

sampler

Latest Stable Version Build Status Maintainability Test Coverage, (*1)

Test inline code samples from your docblocks using PHPUnit!, (*2)

caveats

  • This is not particularly mature. Issues and pull requests welcome.
  • The extracted sample code is eval'd for testing, with all those security implications. As always, be mindful only to run trusted code in sensitive environments.
  • Right now it only works on classes, class methods, and functions. It does no magic to expose private or protected methods, so normal rules apply.

usage

First, install sampler as a dependency., (*3)

$ composer require aaronhipple/sampler

Then, in your PHPUnit suite, implement a case extending AbstractSampleTestCase., (*4)

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'];
    }
}

Finally, write some samples! You can use our @sample annotation..., (*5)

/**
 * 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!";
}

...or good ol' inline markdown., (*6)

/**
 * 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!";
}

Run your test suite as usual, with phpunit., (*7)

contributing

  • Pull requests are welcome.
  • More tests please.
  • PSR-2 is good. Use phpcbf.

The Versions

19/11 2017
19/11 2017

dev-feature/markdown-samples

dev-feature/markdown-samples

Test inline code samples from your docblocks using PHPUnit

  Sources   Download

MIT

The Requires

 

The Development Requires

18/11 2017
18/11 2017

dev-hotfix/echo-eval

dev-hotfix/echo-eval

Test inline code samples from your docblocks using PHPUnit

  Sources   Download

MIT

The Requires

 

The Development Requires

18/11 2017

dev-meta/composer

dev-meta/composer

Test inline code samples from your docblocks using PHPUnit

  Sources   Download

MIT

The Requires

 

The Development Requires