mgleska/repositorymock

A package to facilitate the testing of classes/methods which uses ORM Repository and ORM Entity objects.

Installs: 32

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

pkg:composer/mgleska/repositorymock

1.1.0 2024-10-22 10:15 UTC

This package is auto-updated.

Last update: 2025-12-16 12:19:22 UTC


README

A package to facilitate the testing of classes/methods which uses ORM Repository and ORM Entity objects.

Installation

composer require --dev mgleska/repositorymock:"^1"

Features

  1. Provides ready-made mocks for functions:

    • find()
    • findOneBy()
    • findBy()
      • by scalar value
      • by list of values
      • by object
    • save()
    • remove()
  2. Allow to create mocks for Entity objects from array of values.

    • simple Entity (without relations)
    • multi-level Entity
      • Doctrine\ORM\Mapping\OneToOne
      • Doctrine\ORM\Mapping\ManyToOne
      • Doctrine\ORM\Mapping\OneToMany
  3. Full compatibility with PHPUnit mock. An object of type RepositoryMock can be treated as a regular mock created by PHPUnit.
    For example, you can use $mockedRepository->method('myCustomMethod')->willReturn(...).

  4. Provides the createFakeObject(string $className, array $objData) method which can be used to create Entity object for use in willReturn().
    Particularly useful for multi-level Entities and those where the id field has no setter (because it is autoincrement).

  5. Provides the ability to create test scenarios for update/edit and create/delete actions.
    The mock for the save() function stores the data sent towards the database. Which gives the possibility to observe the "state of the database" after the tested operation $sut->action() is completed.

    • save() does:
      • update if entity exists
      • insert with autoincrement for id
    • than getStoreContent() method allow to make assertions on entities stored in database.

Usage

  1. Mock for simple find().
// src/Service/SutService.php

class SutService
{
    public function __construct(
        private readonly Repository $repository,
    ) {
    }

    public function getFirst(): ?Entity
    {
        return $this->repository->find(1);
    }
}
// tests/Service/SutServiceTest.php

use RepositoryMock\RepositoryMockObject;
use RepositoryMock\RepositoryMockTrait;
...

class SutServiceTest extends TestCase
{
    use RepositoryMockTrait;

    private Sutservice $sut;

    private Repository|RepositoryMockObject $repository;

    protected function setUp(): void
    {
        $this->repository = $this->createRepositoryMock(Repository::class);

        $this->sut = new SutService(
            $this->repository,
        );
    }

    #[Test]
    public function getFirstFound(): void
    {
        $this->repository->loadStore([
            [ // values of selected properties of Entity
                'id' => 1,
                'name' => 'test',
            ],
        ]);

        $result = $this->sut->getFirst();

        $this->assertInstanceOf(Entity::class, $result);
    }

    #[Test]
    public function getFirstNotFound(): void
    {
        $this->repository->loadStore([
            [
                'id' => 22,
                'name' => 'test 22',
            ],
        ]);

        $result = $this->sut->getFirst();

        $this->assertNull($result);
    }
}
  1. More usage examples in file tests/SutServiceTest.php.