tflori/orm

just another orm...

View the Project on GitHub tflori/orm

Build Status Test Coverage Maintainability Latest Stable Version Total Downloads

» API Reference » ORM\Testing\MocksEntityManager

ORM\Testing\MocksEntityManager

Table of Contents

Abstract

A trait to mock ORM\EntityManager

Methods

ORM\Testing\MocksEntityManager::ormAddResult

public function ormAddResult(
    string $class, 
    \ORM\Entity $entities, 
): \ORM\Testing\EntityFetcherMock\Result|\Mockery\MockInterface
Add a result to EntityFetcher for $class

You can specify the query that you expect in the returned result.

Example:

$this->ormAddResult(Article::class, $em, new Article(['title' => 'Foo']))
  ->where('deleted_at IS NULL')
  ->where('title', 'Foo');

$entity = $em->fetch('Article::class')
  ->where('deleted_at IS NULL')
  ->where('title', 'Foo')
  ->one();
Returns ORM\Testing\EntityFetcherMock\Result|Mockery\MockInterface  
Parameters
Parameter Type Description
$class string The class of an Entity
$entities ORM\Entity The entities that will be returned

ORM\Testing\MocksEntityManager::ormAddResult

↑ top


ORM\Testing\MocksEntityManager::ormAllowDelete

public function ormAllowDelete(string|\ORM\Entity $entity): \Mockery\Expectation
Allow delete on $em

If $em is not given it is determined by get_class($entity).

If $entity is a string then it is assumed to be a class name.

Returns Mockery\Expectation  
Parameters
Parameter Type Description  
$entity **string ORM\Entity**  

ORM\Testing\MocksEntityManager::ormAllowDelete

↑ top


ORM\Testing\MocksEntityManager::ormAllowFetch

public function ormAllowFetch(
    string $class, 
    array $entities = [], 
): \Mockery\Expectation[]|\ORM\EntityFetcher[]|\Mockery\MockInterface[]
Allow fetch for $class

Mocks an EntityFetcher with $entities as result.

Returns the Expectation for fetch on entityManager and the mocked EntityFetcher

Warning: this method is deprecated. | Returns | Mockery\Expectation[]|ORM\EntityFetcher[]|Mockery\MockInterface[] | |

Parameters
Parameter Type Description
$class string The class that should be fetched
$entities array The entities that get returned from fetcher

ORM\Testing\MocksEntityManager::ormAllowFetch

↑ top


ORM\Testing\MocksEntityManager::ormAllowInsert

public function ormAllowInsert(
    string $class, 
    array $defaultValues = [], 
): \Mockery\Expectation
Allow an insert for $class

Mocks the calls to sync and insert as they came for save() method for a new Entity.

If you omit the auto incremented id in defaultValues it is set to a random value between 1 and 2147483647.

The EntityManager gets determined the same way as in Entity and can be overwritten by third parameter here.

Returns Mockery\Expectation  
Parameters
Parameter Type Description
$class string The class that should get created
$defaultValues array The default values that came from database (for example: the created column
has by the default the current timestamp; the id is auto incremented…)    

ORM\Testing\MocksEntityManager::ormAllowInsert

↑ top


ORM\Testing\MocksEntityManager::ormAllowUpdate

public function ormAllowUpdate(
    \ORM\Entity|\Mockery\MockInterface $entity, 
    array $changingData = [], 
    array $updatedData = [], 
): \Mockery\Expectation
Allow save on $entity

Entity has to be a mock use emCreateMockedEntity() to create it.

Returns Mockery\Expectation  
Parameters
Parameter Type Description  
$entity **ORM\Entity Mockery\MockInterface**  
$changingData array Emulate changing data during update statement (triggers etc)  
$updatedData array Emulate data changes in database  

ORM\Testing\MocksEntityManager::ormAllowUpdate

↑ top


ORM\Testing\MocksEntityManager::ormAttributesToData

public function ormAttributesToData(string $class, array $attributes): array
Convert an array with $attributes as keys to an array of columns for $class

e. g. : assertSame(['first_name' => 'John'], ormAttributesToArray(User::class, ['firstName' => 'John'])

Note: this method is idempotent

Returns array  
Parameters
Parameter Type Description
$class string  
$attributes array  

ORM\Testing\MocksEntityManager::ormAttributesToData

↑ top


ORM\Testing\MocksEntityManager::ormCreateMockedEntity

public function ormCreateMockedEntity(
    string $class, 
    array $data = [], 
): \Mockery\MockInterface|\ORM\Entity
Create a partial mock of Entity $class

Note: the entity will get a random primary key if not predefined.

Returns Mockery\MockInterface|ORM\Entity  
Parameters
Parameter Type Description
$class string  
$data array  

ORM\Testing\MocksEntityManager::ormCreateMockedEntity

↑ top


ORM\Testing\MocksEntityManager::ormExpectDelete

public function ormExpectDelete(string|\ORM\Entity $entity): mixed
Expect delete on $em

If $em is not given it is determined by get_class($entity).

If $entity is a string then it is assumed to be a class name.

Returns mixed  
Parameters
Parameter Type Description  
$entity **string ORM\Entity**  

ORM\Testing\MocksEntityManager::ormExpectDelete

↑ top


ORM\Testing\MocksEntityManager::ormExpectFetch

public function ormExpectFetch(
    string $class, 
    array $entities = [], 
): \Mockery\Mock|\ORM\EntityFetcher
Expect fetch for $class

Mocks and expects an EntityFetcher with $entities as result.

Warning: this method is deprecated. | Returns | Mockery\Mock|ORM\EntityFetcher | |

Parameters
Parameter Type Description
$class string The class that should be fetched
$entities array The entities that get returned from fetcher

ORM\Testing\MocksEntityManager::ormExpectFetch

↑ top


ORM\Testing\MocksEntityManager::ormExpectInsert

public function ormExpectInsert(string $class, array $defaultValues = []): mixed
Expect an insert for $class

Mocks and expects the calls to sync and insert as they came for save() method for a new Entity.

If you omit the auto incremented id in defaultValues it is set to a random value between 1 and 2147483647.

The EntityManager gets determined the same way as in Entity and can be overwritten by third parameter here.

Returns mixed  
Parameters
Parameter Type Description
$class string The class that should get created
$defaultValues array The default values that came from database (for example: the created column
has by the default the current timestamp; the id is auto incremented…)    

ORM\Testing\MocksEntityManager::ormExpectInsert

↑ top


ORM\Testing\MocksEntityManager::ormExpectUpdate

public function ormExpectUpdate(
    \ORM\Entity|\Mockery\MockInterface $entity, 
    array $changingData = [], 
    array $updatedData = [], 
): mixed
Expect save on $entity

Entity has to be a mock use emCreateMockedEntity() to create it.

Returns mixed  
Parameters
Parameter Type Description  
$entity **ORM\Entity Mockery\MockInterface**  
$changingData array Emulate changing data during update statement (triggers etc)  
$updatedData array Emulate data changes in database  

ORM\Testing\MocksEntityManager::ormExpectUpdate

↑ top


ORM\Testing\MocksEntityManager::ormGetEntityManagerInstance

public function ormGetEntityManagerInstance(
    mixed $class, 
): \ORM\Testing\EntityManagerMock|\Mockery\MockInterface|\ORM\EntityManager
Get the EntityManagerMock for $class
Returns ORM\Testing\EntityManagerMock|Mockery\MockInterface|ORM\EntityManager  
Parameters
Parameter Type Description
$class mixed  

ORM\Testing\MocksEntityManager::ormGetEntityManagerInstance

↑ top


ORM\Testing\MocksEntityManager::ormInitMock

public function ormInitMock(
    array $options = [], 
    string $driver = 'mysql', 
): \Mockery\MockInterface|\ORM\EntityManager
Initialize an EntityManager mock object

The mock is partial and you can map and act with it as usual. You should overwrite your dependency injector with the returned mock object. You can also call defineFor*() on this mock to use this mock for specific classes.

The PDO object is mocked too. This object should not receive any calls except for quoting. By default it accepts quote(string), setAttribute(*) and getAttribute(ATTR_DRIVER_NAME). To retrieve and expect other calls you can use getConnection() from EntityManager mock object.

Returns Mockery\MockInterface|ORM\EntityManager  
Parameters
Parameter Type Description
$options array Options passed to EntityManager constructor
$driver string Database driver you are using (results in different dbal instance)

ORM\Testing\MocksEntityManager::ormInitMock

↑ top