yosimitso/mockdoctrinemanager

Mock and log Doctrine's manager

Installs: 121

Dependents: 1

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

Type:symfony-bundle

V1.0 2019-11-29 12:47 UTC

This package is auto-updated.

Last update: 2024-04-29 04:33:32 UTC


README

Build Status

Yosimitso/DoctrineManagerMock

Usage

Mock of Doctrine\ORM\EntityManager which implements EntityManagerInterface, this bundle logs persisted, removed and flushed entities, and basic operations like transactions. It enables you to check if entities are correctly registered and if your workflow is respected

Please note that this bundle is new and focus on most current operations, including :

  • persist
  • flush
  • remove
  • beginTransaction
  • commit
  • rollback
  • transactionnal

Feel free to submit a PR to expand the features

Installation

composer require "yosimitso/doctrinemanagermock:^1.0" --dev

Then just instanciate it

 $entityManager = new Yosimitso\MockDoctrineManager\EntityManagerMock;

Example

Simple example to check if your entity was actually persisted and with good data,

Example from the actual test of this bundle :

The class to test

<?php

namespace Yosimitso\MockDoctrineManager\Tests;
use Doctrine\ORM\EntityManagerInterface;
use Yosimitso\MockDoctrineManager\Tests\Entity\EntityToTest;

class ClassToTestService
{
    /**
     * @var EntityManagerInterface
     */
    private $entityManager;

    public function __construct(EntityManagerInterface $entityManager) // YOU CAN TYPE ENTITYMANAGERINTERFACE
    {
        $this->entityManager = $entityManager;
    }

    public function methodToTest($nb)
    {
        $newEntity = new EntityToTest;
        $newEntity->setNb($nb);

        try {
            $this->entityManager->beginTransaction();
            $this->entityManager->persist($newEntity);
            $this->entityManager->flush();
            $this->entityManager->commit();
        } catch (\Exception $e) {
            $this->entityManager->rollback();
        }


    }
}

Example of entity

<?php
namespace Yosimitso\MockDoctrineManager\Tests\Entity;
class EntityToTest
{
    private $nb;

    public function setNb($nb)      // CLASSIC SETTER
    {
        $this->nb = $nb;
    }

    public function getNb()         // CLASSIC GETTER
    {
        return $this->nb;
    }
}

The Test

<?php
namespace Yosimitso\MockDoctrineManager\Tests\Test;

use Yosimitso\MockDoctrineManager\EntityManagerMock;
use Yosimitso\MockDoctrineManager\Tests\Entity\EntityToTest;
use Yosimitso\MockDoctrineManager\Tests\ClassToTestService;
use PHPUnit\Framework\TestCase;  // ASSUMING YOU'RE USING PHPUNIT, BUT IT WORKS WITH ANY TESTING FRAMEWORK

class ClassToTestServiceTest extends TestCase {
    public function testMethodToTest()
    {
        $entityManagerMock = new EntityManagerMock(); // THIS BUNDLE 
        $testedClass = new ClassToTestService($entityManagerMock); // THE CLASS TO TEST
        $testedClass->methodToTest(10);     // THE METHOD TO TEST

        // ASSERT WE BEGAN THE TRANSACTION
        $this->assertTrue($entityManagerMock->hasBegunTransaction());

        // ASSERT WE PERSISTED THE GOOD ENTITY WITH THE GOOD DATA
        $this->assertEquals(10, $entityManagerMock->getPersistedEntity(EntityToTest::class)->getNb());

        // ASSERT WE FLUSHED THE GOOD ENTITY WITH THE GOOD DATA
        $this->assertEquals(10, $entityManagerMock->getFlushedEntity(EntityToTest::class)->getNb());

        // ASSERT WE COMITTED
        $this->assertTrue($entityManagerMock->hasCommitted());

        // ASSERT WE DIDN'T ROLLBACK
        $this->assertFalse($entityManagerMock->hasRolledback());
    }
}

What if I persisted two or more instance of a class ?

Use the 'position' argument of getPersistedEntity, example if I want to get the second persisted "YourClass" : getPersistedEntity(YourClass::class, 2);

My code use 'findBy' methods

This bundle can't guess what you're expecting from 'findBy', so just create a mock of this bundle like any other class, in order to mock 'findBy'

Example with PHPUnit :

$yourEntity = new Article; // EXAMPLE OF AN ENTITY

$yourEntity->setName('hello');

$entityManager = $this->getMockBuilder(EntityManagerMock::class)
                    ->setMethods(['findBy'])
                    ->getMock();

$entityManager->methods('findBy')->willReturn($yourEntity);

API

/* returns this entity persisted in n position (among its namespace) */
getPersistedEntity(object $className, int $position = 1): mixed

/* returns this entity removed in n position (among its namespace) */
getRemovedEntity(object $className, int $position = 1): mixed

/* returns this entity flushed in n position (among its namespace) */
getFlushedEntity(object $className, int $position = 1): mixed

/** returns the list of persisted entites */
getPersistedEntities(): array

/** returns the list of flushed entites */
getFlushedEntities(): array

hasBegunTransaction(): boolean
hasComitted(): boolean
hasRolledback(): boolean