codingpaws/pspec

This package is abandoned and no longer maintained. No replacement package was suggested.

A modern behavior-driven test framework, influenced by RSpec and jest.

v1.0 2022-07-20 16:37 UTC

This package is auto-updated.

Last update: 2024-01-31 00:39:40 UTC


README

PSpec is a testing framework for PHP, influenced by RSpec and jest.

This project is experimental and still needs a lot of work.

Example

// src/Counter.php
class Counter {
  public int $value = 0;

  function increment() {
    $this->value++;
  }
}

// spec/Counter.spec.php
describe(Counter::class, function () {
  subject(fn () => new Counter);

  let('base_value', 10);

  before(function () {
    subject()->value = $this->base_value;
  });

  describe('#increment', function () {
    it('increments by 1', function () {
      expect(subject()->value)->toBe($this->base_value);

      subject()->increment();

      expect(subject()->value)->toBe($this->base_value + 1);
    });
  });
});

Getting started

  1. Install PSpec: composer require --dev codingpaws/pspec
  2. In your project root, create a spec directory
  3. For a class in your project, for example Counter, create a file spec/Counter.spec.php
  4. Write some tests, like the example
  5. Run PSpec: vendor/bin/pspec

Why?

PSpec models your application with natural language. In PHPUnit—the de facto standard in PHP testing—a test file contains a sequential list of tests. If two tests are related in PHPUnit, such as one authenticated and one unauthenticated request, it’s hard to understand.

In PSpec, tests are nested under real-world conditions, such as being signed-in or when a network error occurs. You use describe blocks to organize tests.

Further reading