Repository interfaces separate persistence behavior from business logic and allow in-memory test implementations.
The examples below begin with the core behavior, then move to an application-oriented use.
How Implementing Repository Interfaces for Testability Works
Repository interfaces separate persistence behavior from business logic and allow in-memory test implementations.
Example 1: Basic Usage
This example uses a small input to make the main behavior visible.
<?php
interface ProductRepository { public function title(int $id): ?string; }
class FakeProducts implements ProductRepository { public function title(int $id): ?string { return $id === 1 ? "Book" : null; } }
echo (new FakeProducts())->title(1);
Output:
Book
Repository interfaces separate persistence behavior from business logic and allow in-memory test implementations.
Example 2: A Different Input
A different set of values shows the same operation in another situation.
<?php
interface UserRepository { public function exists(string $email): bool; }
class MemoryUsers implements UserRepository { public function exists(string $email): bool { return $email === "[email protected]"; } }
var_dump((new MemoryUsers())->exists("[email protected]"));
Output:
bool(true)
Define behavior at the domain boundary, not one-to-one wrappers around every database operation.
Practical Example: Applying the Feature
This example shows an application-style use of the feature, with its surrounding setup visible.
<?php
interface ArticleRepository { public function findTitle(int $identifier): ?string; }
final class InMemoryArticles implements ArticleRepository { public function findTitle(int $identifier): ?string { return [1 => "Introduction", 2 => "Arrays"][$identifier] ?? null; } }
function articleHeading(ArticleRepository $repository, int $identifier): string { return $repository->findTitle($identifier) ?? "Not found"; }
echo articleHeading(new InMemoryArticles(), 2);
Output:
Arrays
The code is shown for learning. Adjust the class names, inputs and error handling to suit your application.
Important Behavior and Edge Cases
- Define behavior at the domain boundary, not one-to-one wrappers around every database operation.
- Repository interfaces separate persistence behavior from business logic and allow in-memory test implementations.
- Observe differences in empty inputs, invalid states and optional extension availability where relevant.
Comparison With Related Approaches
| Approach | Important Difference |
|---|---|
Implementing Repository Interfaces for Testability | Repository interfaces separate persistence behavior from business logic and allow in-memory test implementations. |
| Alternative approach | A repository hides storage details; directly calling PDO from services tightly couples tests to a database. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
Ignoring the documented state or precondition of Implementing Repository Interfaces for Testability | Define behavior at the domain boundary, not one-to-one wrappers around every database operation. |
| Expecting a different type of result | Read the declared return type and distinguish returned values from changes to the source object. |
| Assuming a related API works identically | A repository hides storage details; directly calling PDO from services tightly couples tests to a database. |
Key Takeaways
- Repository interfaces separate persistence behavior from business logic and allow in-memory test implementations.
- Define behavior at the domain boundary, not one-to-one wrappers around every database operation.
- Use the example that matches the values, runtime version and dependencies in your application.
Implementing Repository Interfaces for Testability is useful when its behavior matches the data structure and lifecycle of your PHP application. Choose it with a clear understanding of its preconditions and returned values.