PHP code example of yosimitso / mockdoctrinemanager
1. Go to this page and download the library: Download yosimitso/mockdoctrinemanager library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
yosimitso / mockdoctrinemanager example snippets
$entityManager = new Yosimitso\MockDoctrineManager\EntityManagerMock;
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();
}
}
}
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;
}
}
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());
}
}
$yourEntity = new Article; // EXAMPLE OF AN ENTITY
$yourEntity->setName('hello');
$entityManager = $this->getMockBuilder(EntityManagerMock::class)
->setMethods(['findBy'])
->getMock();
$entityManager->methods('findBy')->willReturn($yourEntity);
/* 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
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.