Create mock repository

From dykyi-roman/awesome-claude-code · View interactive version

Create mock repository is an AI agent skill from the GitHub repository dykyi-roman/awesome-claude-code. Generates InMemory repository implementations for PHP 8.4 testing. Creates fake repositories with array storage, supporting CRUD operations and queries without database.

How to use this skill

Skill file

Mock Repository Generator

Generates InMemory (Fake) repository implementations for testing.

Characteristics

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

  1. Read the repository interface:
  1. Generate InMemory implementation:
  1. Handle complex queries:
  1. Add test helpers (optional):
  1. File placement:

Best Practices

  1. Match interface exactly — same method signatures
  2. Isolate per test — use clear() in tearDown
  3. Avoid complexity — simple in-memory logic
  4. Document deviations — if behavior differs from real impl
  5. Consider thread safety — for parallel tests (usually not needed)

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.