$ show phpspec/prophecy
Prophecy is a PHP object mocking framework for creating test doubles (stubs, mocks, spies) in unit testing. It enables developers to define prophecies about future object interactions, specifying expected behavior through promises and verifying those expectations with predictions. The framework is designed to be framework-agnostic, integrating with tools like PHPUnit and PhpSpec. The architecture centers around a Prophet service that creates object prophecies. These prophecies utilize a doubler system to generate test double classes on-the-fly, applying patches for special cases (such as handling Throwable or Traversable objects). Argument matching is handled through a flexible token system, enabling precise control over which method invocations match a prophecy. Behavior and expectations are separated into promises (defining what the double should do) and predictions (defining what is expected to occur), promoting clear and maintainable test code. Notable for its robust handling of modern PHP features, Prophecy supports union types, intersection types, nullable types, readonly classes, and the never type. Its extensible design, particularly the strategy-based argument token system and the patchable class generation, demonstrates strong software engineering practices. The framework emphasizes lazy initialization and caching of generated doubles to optimize performance, making it suitable for extensive test suites.
The LLM identified 6 files worth highlighting for reuse:
Defines the TokenInterface for argument matching. Each token (e.g., ExactValueToken, TypeToken) implements a specific matching strategy. This allows flexible and extensible argument matching in prophecies, and the pattern can be reused in any system requiring configurable value matching.
Implements exact value matching for argument tokens. Demonstrates a simple, reusable strategy for comparing arguments against a fixed value, showcasing the token pattern's applicability to primitive types.
Implements callback-based argument matching. Allows arbitrary matching logic via user-provided functions, exemplifying how the token system encapsulates complex matching strategies while maintaining a uniform interface.
Defines the interface for class patches that modify generated double classes. Enables pluggable behavior for special cases (e.g., handling constructors, magic methods) without modifying core doubling logic, demonstrating a clean extension point for code generation systems.
Patch for handling Throwable objects in generated doubles. Shows how to specialize class generation for interface implementation nuances, providing a reusable pattern for adapting generated code to specific type constraints.
Patch for handling Traversable objects in generated doubles. Illustrates how to modify generated classes to correctly implement iterator interfaces, offering a reusable solution for adapting doubles to PHP's SPL interfaces.