acc-create-mock-repository
Mock Repository Generator
Generates InMemory (Fake) repository implementations for testing.
Characteristics
- No database — stores entities in memory
- Fast — no I/O operations
- Isolated — fresh state per test
- Deterministic — predictable behavior
- Implements interface — same contract as real repository
Template
<?php
declare(strict_types=1);
namespace Tests\Fake;
use {RepositoryInterface};
use {Entity};
use {EntityId};
final class InMemory{Entity}Repository implements {RepositoryInterface}
{
/** @var array<string, {Entity}> */
private array $entities = [];
public function save({Entity} $entity): void
{
$this->entities[$entity->id()->toString()] = $entity;
}
public function findById({EntityId} $id): ?{Entity}
{
return $this->entities[$id->toString()] ?? null;
}
public function delete({Entity} $entity): void
{
unset($this->entities[$entity->id()->toString()]);
}
/** @return list<{Entity}> */
public function findAll(): array
{
return array_values($this->entities);
}
public function clear(): void
{
$this->entities = [];
}
}
Generation Instructions
-
Read the repository interface:
- Extract all method signatures
- Identify entity type
- Identify ID type
-
Generate InMemory implementation:
- Array storage keyed by ID
- Implement all interface methods
- Add
clear()for test cleanup
-
Handle complex queries:
- Use
array_filterfor criteria - Support specifications if used
- Implement pagination with
array_slice
- Use
-
Add test helpers (optional):
getAll()— access internal statehas(Id $id)— check existencecount()— entity count
-
File placement:
tests/Fake/InMemory{Entity}Repository.php- Or
tests/Double/directory
Best Practices
- Match interface exactly — same method signatures
- Isolate per test — use
clear()in tearDown - Avoid complexity — simple in-memory logic
- Document deviations — if behavior differs from real impl
- Consider thread safety — for parallel tests (usually not needed)
References
references/examples.md— Complete repository examples (User, Order, Product)references/other-fakes.md— EventDispatcher, Mailer, Clock fakes
More from dykyi-roman/awesome-claude-code
psr-overview-knowledge
PHP Standards Recommendations (PSR) overview knowledge base. Provides comprehensive reference for all accepted PSRs including PSR-1,3,4,6,7,11,12,13,14,15,16,17,18,20. Use for PSR selection decisions and compliance audits.
22detect-code-smells
Detects code smells in PHP codebases. Identifies God Class, Feature Envy, Data Clumps, Long Parameter List, Long Method, Primitive Obsession, Message Chains, Inappropriate Intimacy. Generates actionable reports with refactoring recommendations.
15clean-arch-knowledge
Clean Architecture knowledge base. Provides patterns, antipatterns, and PHP-specific guidelines for Clean Architecture and Hexagonal Architecture audits.
15ddd-knowledge
DDD architecture knowledge base. Provides patterns, antipatterns, and PHP-specific guidelines for Domain-Driven Design audits.
14testing-knowledge
Testing knowledge base for PHP 8.4 projects. Provides testing pyramid, AAA pattern, naming conventions, isolation principles, DDD testing guidelines, and PHPUnit patterns.
12bug-root-cause-finder
Root cause analysis methods for PHP bugs. Provides 5 Whys technique, fault tree analysis, git bisect guidance, and stack trace parsing.
12