定制 mgleska/repositorymock 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mgleska/repositorymock

最新稳定版本:1.1.0

Composer 安装命令:

composer require mgleska/repositorymock

包简介

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

README 文档

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.

统计信息

  • 总下载量: 32
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-05-28