acc-create-mock-repository

Generates InMemory repository implementations for PHP 8.5 testing. Creates fake repositories with array storage, supporting CRUD operations and queries without database.

16 stars

Best use case

acc-create-mock-repository is best used when you need a repeatable AI agent workflow instead of a one-off prompt.

Generates InMemory repository implementations for PHP 8.5 testing. Creates fake repositories with array storage, supporting CRUD operations and queries without database.

Teams using acc-create-mock-repository should expect a more consistent output, faster repeated execution, less prompt rewriting.

When to use this skill

  • You want a reusable workflow that can be run more than once with consistent structure.

When not to use this skill

  • You only need a quick one-off answer and do not need a reusable workflow.
  • You cannot install or maintain the underlying files, dependencies, or repository context.

Installation

Claude Code / Cursor / Codex

$curl -o ~/.claude/skills/acc-create-mock-repository/SKILL.md --create-dirs "https://raw.githubusercontent.com/diegosouzapw/awesome-omni-skill/main/skills/development/acc-create-mock-repository/SKILL.md"

Manual Installation

  1. Download SKILL.md from GitHub
  2. Place it in .claude/skills/acc-create-mock-repository/SKILL.md inside your project
  3. Restart your AI agent — it will auto-discover the skill

How acc-create-mock-repository Compares

Feature / Agentacc-create-mock-repositoryStandard Approach
Platform SupportNot specifiedLimited / Varies
Context Awareness High Baseline
Installation ComplexityUnknownN/A

Frequently Asked Questions

What does this skill do?

Generates InMemory repository implementations for PHP 8.5 testing. Creates fake repositories with array storage, supporting CRUD operations and queries without database.

Where can I find the source code?

You can find the source code on GitHub using the link provided at the top of the page.

SKILL.md Source

# 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
<?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 = [];
    }
}
```

## Complete Examples

### User Repository

```php
<?php

declare(strict_types=1);

namespace Tests\Fake;

use App\Domain\User\User;
use App\Domain\User\UserId;
use App\Domain\User\Email;
use App\Domain\User\UserRepositoryInterface;

final class InMemoryUserRepository implements UserRepositoryInterface
{
    /** @var array<string, User> */
    private array $users = [];

    public function save(User $user): void
    {
        $this->users[$user->id()->toString()] = $user;
    }

    public function findById(UserId $id): ?User
    {
        return $this->users[$id->toString()] ?? null;
    }

    public function findByEmail(Email $email): ?User
    {
        foreach ($this->users as $user) {
            if ($user->email()->equals($email)) {
                return $user;
            }
        }
        return null;
    }

    public function delete(User $user): void
    {
        unset($this->users[$user->id()->toString()]);
    }

    public function existsByEmail(Email $email): bool
    {
        return $this->findByEmail($email) !== null;
    }

    /** @return list<User> */
    public function findAll(): array
    {
        return array_values($this->users);
    }

    public function count(): int
    {
        return count($this->users);
    }

    public function clear(): void
    {
        $this->users = [];
    }
}
```

### Order Repository with Queries

```php
<?php

declare(strict_types=1);

namespace Tests\Fake;

use App\Domain\Order\Order;
use App\Domain\Order\OrderId;
use App\Domain\Order\OrderStatus;
use App\Domain\Order\OrderRepositoryInterface;
use App\Domain\Customer\CustomerId;
use DateTimeImmutable;

final class InMemoryOrderRepository implements OrderRepositoryInterface
{
    /** @var array<string, Order> */
    private array $orders = [];

    public function save(Order $order): void
    {
        $this->orders[$order->id()->toString()] = $order;
    }

    public function findById(OrderId $id): ?Order
    {
        return $this->orders[$id->toString()] ?? null;
    }

    public function delete(Order $order): void
    {
        unset($this->orders[$order->id()->toString()]);
    }

    /** @return list<Order> */
    public function findByCustomer(CustomerId $customerId): array
    {
        return array_values(array_filter(
            $this->orders,
            fn(Order $order) => $order->customerId()->equals($customerId)
        ));
    }

    /** @return list<Order> */
    public function findByStatus(OrderStatus $status): array
    {
        return array_values(array_filter(
            $this->orders,
            fn(Order $order) => $order->status() === $status
        ));
    }

    /** @return list<Order> */
    public function findPending(): array
    {
        return $this->findByStatus(OrderStatus::Pending);
    }

    /** @return list<Order> */
    public function findCreatedBefore(DateTimeImmutable $date): array
    {
        return array_values(array_filter(
            $this->orders,
            fn(Order $order) => $order->createdAt() < $date
        ));
    }

    /** @return list<Order> */
    public function findAll(int $limit = 100, int $offset = 0): array
    {
        return array_slice(array_values($this->orders), $offset, $limit);
    }

    public function count(): int
    {
        return count($this->orders);
    }

    public function countByStatus(OrderStatus $status): int
    {
        return count($this->findByStatus($status));
    }

    public function clear(): void
    {
        $this->orders = [];
    }

    // Test helpers
    public function getAll(): array
    {
        return $this->orders;
    }

    public function has(OrderId $id): bool
    {
        return isset($this->orders[$id->toString()]);
    }
}
```

### Repository with Specifications

```php
<?php

declare(strict_types=1);

namespace Tests\Fake;

use App\Domain\Product\Product;
use App\Domain\Product\ProductId;
use App\Domain\Product\ProductRepositoryInterface;
use App\Domain\Shared\Specification\SpecificationInterface;

final class InMemoryProductRepository implements ProductRepositoryInterface
{
    /** @var array<string, Product> */
    private array $products = [];

    public function save(Product $product): void
    {
        $this->products[$product->id()->toString()] = $product;
    }

    public function findById(ProductId $id): ?Product
    {
        return $this->products[$id->toString()] ?? null;
    }

    public function delete(Product $product): void
    {
        unset($this->products[$product->id()->toString()]);
    }

    /** @return list<Product> */
    public function findBySpecification(SpecificationInterface $spec): array
    {
        return array_values(array_filter(
            $this->products,
            fn(Product $product) => $spec->isSatisfiedBy($product)
        ));
    }

    /** @return list<Product> */
    public function findAll(): array
    {
        return array_values($this->products);
    }

    public function clear(): void
    {
        $this->products = [];
    }
}
```

## Other Fake Implementations

### Collecting Event Dispatcher

```php
<?php

declare(strict_types=1);

namespace Tests\Fake;

use Psr\EventDispatcher\EventDispatcherInterface;

final class CollectingEventDispatcher implements EventDispatcherInterface
{
    /** @var list<object> */
    private array $events = [];

    public function dispatch(object $event): object
    {
        $this->events[] = $event;
        return $event;
    }

    /** @return list<object> */
    public function dispatchedEvents(): array
    {
        return $this->events;
    }

    /** @return list<object> */
    public function dispatchedEventsOf(string $eventClass): array
    {
        return array_values(array_filter(
            $this->events,
            fn(object $event) => $event instanceof $eventClass
        ));
    }

    public function hasDispatched(string $eventClass): bool
    {
        return count($this->dispatchedEventsOf($eventClass)) > 0;
    }

    public function clear(): void
    {
        $this->events = [];
    }
}
```

### Collecting Mailer

```php
<?php

declare(strict_types=1);

namespace Tests\Fake;

use App\Infrastructure\Email\MailerInterface;
use App\Infrastructure\Email\EmailMessage;

final class InMemoryMailer implements MailerInterface
{
    /** @var list<EmailMessage> */
    private array $sent = [];

    public function send(EmailMessage $message): void
    {
        $this->sent[] = $message;
    }

    /** @return list<EmailMessage> */
    public function sentMessages(): array
    {
        return $this->sent;
    }

    /** @return list<EmailMessage> */
    public function sentTo(string $email): array
    {
        return array_values(array_filter(
            $this->sent,
            fn(EmailMessage $msg) => $msg->to === $email
        ));
    }

    public function hasNotSentAny(): bool
    {
        return empty($this->sent);
    }

    public function clear(): void
    {
        $this->sent = [];
    }
}
```

### Frozen Clock

```php
<?php

declare(strict_types=1);

namespace Tests\Fake;

use Psr\Clock\ClockInterface;
use DateTimeImmutable;

final class FrozenClock implements ClockInterface
{
    public function __construct(
        private DateTimeImmutable $now
    ) {}

    public function now(): DateTimeImmutable
    {
        return $this->now;
    }

    public static function at(string $datetime): self
    {
        return new self(new DateTimeImmutable($datetime));
    }

    public static function now(): self
    {
        return new self(new DateTimeImmutable());
    }

    public function advance(string $interval): self
    {
        return new self($this->now->modify($interval));
    }
}
```

## Usage in Tests

```php
final class PlaceOrderHandlerTest extends TestCase
{
    private PlaceOrderHandler $handler;
    private InMemoryOrderRepository $orderRepository;
    private InMemoryProductRepository $productRepository;
    private CollectingEventDispatcher $eventDispatcher;

    protected function setUp(): void
    {
        $this->orderRepository = new InMemoryOrderRepository();
        $this->productRepository = new InMemoryProductRepository();
        $this->eventDispatcher = new CollectingEventDispatcher();

        $this->handler = new PlaceOrderHandler(
            $this->orderRepository,
            $this->productRepository,
            $this->eventDispatcher
        );
    }

    public function test_places_order(): void
    {
        // Arrange
        $product = ProductMother::book();
        $this->productRepository->save($product);

        // Act
        $orderId = $this->handler->handle(new PlaceOrderCommand(
            customerId: 'customer-123',
            items: [['productId' => $product->id()->toString(), 'quantity' => 2]]
        ));

        // Assert - check repository
        $order = $this->orderRepository->findById(OrderId::fromString($orderId));
        self::assertNotNull($order);

        // Assert - check events
        self::assertTrue($this->eventDispatcher->hasDispatched(OrderPlacedEvent::class));
    }
}
```

## Generation Instructions

1. **Read the repository interface:**
   - Extract all method signatures
   - Identify entity type
   - Identify ID type

2. **Generate InMemory implementation:**
   - Array storage keyed by ID
   - Implement all interface methods
   - Add `clear()` for test cleanup

3. **Handle complex queries:**
   - Use `array_filter` for criteria
   - Support specifications if used
   - Implement pagination with `array_slice`

4. **Add test helpers (optional):**
   - `getAll()` — access internal state
   - `has(Id $id)` — check existence
   - `count()` — entity count

5. **File placement:**
   - `tests/Fake/InMemory{Entity}Repository.php`
   - Or `tests/Double/` directory

## 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)

Related Skills

acc-create-use-case

16
from diegosouzapw/awesome-omni-skill

Generates Application Use Cases for PHP 8.5. Creates orchestration services that coordinate domain objects, handle transactions, and dispatch events. Includes unit tests.

acc-create-state

16
from diegosouzapw/awesome-omni-skill

Generates State pattern for PHP 8.5. Creates state machines with context, state interface, and concrete states for behavior changes. Includes unit tests.

acc-create-saga-pattern

16
from diegosouzapw/awesome-omni-skill

Generates Saga pattern components for PHP 8.5. Creates Saga interfaces, steps, orchestrator, state management, and compensation logic with unit tests.

acc-create-retry-pattern

16
from diegosouzapw/awesome-omni-skill

Generates Retry pattern for PHP 8.5. Creates resilience component with exponential backoff, jitter, and configurable retry strategies. Includes unit tests.

acc-create-responder

16
from diegosouzapw/awesome-omni-skill

Generates ADR Responder classes for PHP 8.5. Creates HTTP response builders with PSR-7/PSR-17 support. Includes unit tests.

acc-create-repository

16
from diegosouzapw/awesome-omni-skill

Generates DDD Repository interfaces and implementation stubs for PHP 8.5. Creates domain interfaces in Domain layer, implementation in Infrastructure.

acc-create-rate-limiter

16
from diegosouzapw/awesome-omni-skill

Generates Rate Limiter pattern for PHP 8.5. Creates request throttling with token bucket, sliding window, and fixed window algorithms. Includes unit tests.

acc-create-psr6-cache

16
from diegosouzapw/awesome-omni-skill

Generates PSR-6 Cache implementation for PHP 8.5. Creates CacheItemPoolInterface and CacheItemInterface implementations with TTL handling and deferred saves. Includes unit tests.

acc-create-psr3-logger

16
from diegosouzapw/awesome-omni-skill

Generates PSR-3 Logger implementation for PHP 8.5. Creates LoggerInterface implementations with log levels, context interpolation, and LoggerAwareTrait usage. Includes unit tests.

acc-create-psr20-clock

16
from diegosouzapw/awesome-omni-skill

Generates PSR-20 Clock implementation for PHP 8.5. Creates ClockInterface implementations including SystemClock, FrozenClock, and OffsetClock for time abstraction and testing. Includes unit tests.

acc-create-psr17-http-factory

16
from diegosouzapw/awesome-omni-skill

Generates PSR-17 HTTP Factories implementation for PHP 8.5. Creates RequestFactoryInterface, ResponseFactoryInterface, StreamFactoryInterface, UriFactoryInterface, ServerRequestFactoryInterface, UploadedFileFactoryInterface. Includes unit tests.

acc-create-psr16-simple-cache

16
from diegosouzapw/awesome-omni-skill

Generates PSR-16 Simple Cache implementation for PHP 8.5. Creates CacheInterface with get/set/delete operations and TTL handling. Includes unit tests.