explaingit

phpspec/prophecy

8,482PHPAudience · developerComplexity · 2/5Setup · easy

TLDR

Prophecy is a PHP library for creating fake versions of your app's dependencies in tests, so you can verify your code calls them correctly without using the real thing.

Mindmap

mindmap
  root((repo))
    What it does
      Create fake objects
      Stub return values
      Verify method calls
    Fake Types
      Dummies
      Stubs
      Mocks
    Tech Stack
      PHP
      Composer
      PHPUnit
    Audience
      PHP developers
      Test writers
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Create a fake database object in a PHPUnit test so your code never hits a real database

USE CASE 2

Verify that a payment service charge method was called exactly once in a checkout test

USE CASE 3

Stub an external API client to return controlled responses during testing

Tech stack

PHPComposerPHPUnit

Getting it running

Difficulty · easy Time to first run · 30min

Requires PHP 7.2 or higher and Composer.

In plain English

Prophecy is a PHP library that helps developers write automated tests by creating stand-in objects that pretend to be real parts of an application. When you test a piece of code that depends on other components, you often do not want those real components involved in the test. Prophecy lets you create fake versions of them, control what those fakes do, and verify that your code interacted with them the way you expected. The library organizes fakes into three categories. A dummy is the simplest: it exists just to satisfy a function that requires a particular type of object, and it does nothing when called. A stub goes further and returns specific values when its methods are called, so you can simulate a database returning a record or an API returning a response. A mock adds verification on top: you declare ahead of time what calls you expect to happen, and the library checks at the end of the test that those calls actually occurred. Prophecy uses a descriptive coding style built around the word prophecy. You create a prophet object, ask it to create a prophecy for a specific class, set up what you expect that class to do, then reveal it as a usable fake object. The naming is intentional and meant to make tests read closer to plain statements about expected behavior. It integrates with PHPUnit and other PHP testing frameworks, and is installed via Composer, the standard PHP package manager. PHP 7.2 or higher is required. The library was originally built for phpspec, another PHP testing tool, but works independently. The README includes detailed documentation with code examples covering all major features: argument wildcarding, call order handling, and extending the library with custom promise types.

Copy-paste prompts

Prompt 1
Show me how to use Prophecy in PHPUnit to create a stub that returns a fake user from a repository
Prompt 2
Write a Prophecy mock that verifies my email service send method is called once when a user registers
Prompt 3
Help me use Prophecy to test a class that depends on a third-party API client, without hitting the real API
Open on GitHub → Explain another repo

← phpspec on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.