phpspec
Test-driven development with PHP
TLDR
Create a specification for a class
Run all specifications in the "spec" directory
Run a single specification
Run specifications using a specific configuration file
Run specifications using a specific bootstrap file
Disable code generation prompts
Enable fake return values
SYNOPSIS
phpspec [global_options] [command] [command_arguments]
Common commands:
phpspec run [options] [spec_path]
phpspec desc <class_name> [options]
phpspec init
PARAMETERS
--help, -h
Displays help for the current command.
--quiet, -q
Do not output any message.
--verbose, -v|-vv|-vvv
Increase the verbosity of messages. -v for normal output, -vv for more verbose output, -vvv for debug.
--version, -V
Displays the application version.
--ansi
Force ANSI output.
--no-ansi
Disable ANSI output.
--no-interaction, -n
Do not ask any interactive question.
--config, -c <path>
Specify a custom path to the configuration file (e.g., phpspec.yml).
--no-progress
Do not show the progress bar during spec execution.
--format, -f <format>
Specify the output format for the 'run' command (e.g., pretty, junit, html).
--stop-on-failure, --fail-fast
Stop running upon the first encountered failure.
--no-code-generation
Prevents phpspec from generating missing classes or methods during the 'run' command.
--filter <pattern>
Filter specifications to run based on a regular expression pattern.
DESCRIPTION
phpspec is a Behavior-Driven Development (BDD) framework for PHP. Unlike traditional unit testing frameworks that focus on testing individual units of code, phpspec helps developers describe and verify the behavior of their applications from the perspective of their specifications.
It encourages a design-first approach, where specifications (written as executable examples) drive the development process. phpspec focuses on how objects collaborate and respond to messages, making it excellent for understanding object interactions and designing robust, well-defined APIs.
A key feature is its ability to generate code (classes and methods) based on the specifications, facilitating a red-green-refactor cycle. It also provides powerful test doubles (called 'spec doubles') to isolate the code under specification from its dependencies, promoting clean, testable design. By focusing on behavior, phpspec helps ensure that code not only works but also meets the intended requirements and expectations.
CAVEATS
phpspec is typically installed via Composer as a dev dependency within a project. It focuses strongly on Behavior-Driven Development and design by example, which may require a shift in mindset compared to traditional unit testing. While it can verify individual units, its strength lies in defining and verifying object collaborations and behaviors.
CONFIGURATION FILE (PHPSPEC.YML)
phpspec typically uses a phpspec.yml or phpspec.yml.dist file in the project root for configuration. This file allows you to define suites, formatters, code generation rules, and other global settings, tailoring phpspec's behavior to your project's needs.
CODE GENERATION WORKFLOW
One of phpspec's unique features is its ability to generate code. When you run a spec (e.g., phpspec run) for a class or method that doesn't exist, phpspec will prompt you to create it. This 'red-green' workflow encourages you to define the desired behavior first (red test), then generate the necessary code (passing the test), and finally refactor.
SPEC DOUBLES (TEST DOUBLES)
phpspec uses 'spec doubles' (similar to mocks or stubs) to manage dependencies. These are objects that stand in for real dependencies, allowing you to isolate the behavior under test. You define their expected interactions and return values, ensuring that your specifications focus solely on the object being specified, not its collaborators' internal workings.
HISTORY
phpspec was created by Konstantin Kudryashov, the same developer behind Behat. It was inspired by RSpec from the Ruby world, bringing similar BDD principles to PHP development. Initially gaining traction in the early 2010s, it provided an alternative to PHPUnit for developers wanting to adopt a more behavior-driven approach to testing and design, emphasizing specifications and example-based development over pure unit isolation.