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\EntityManagerMock

ORM\Testing\EntityManagerMock

Table of Contents

Abstract

Extends: ORM\EntityManager

The EntityManager that manages the instances of Entities.

Methods

ORM\Testing\EntityManagerMock::__construct

public function __construct(array $options = [])
Constructor
Parameters
Parameter Type Description
$options array Options for the new EntityManager

ORM\Testing\EntityManagerMock::__construct

↑ top


ORM\Testing\EntityManagerMock::addEntity

public function addEntity(\ORM\Entity $entity): mixed
Add an entity to be fetched by primary key

The entity needs to have a primary key if not it will be filled with random values between RANDOM_KEY_MIN and RANDOM_KEY_MAX (at the time writing this it is 1000000000 and 1000999999).

You can pass mocks from Entity too but we need to call Entity::getPrimaryKey().

Returns mixed  
Parameters
Parameter Type Description
$entity ORM\Entity  

ORM\Testing\EntityManagerMock::addEntity

↑ top


ORM\Testing\EntityManagerMock::addResult

public function addResult(
    mixed $class, 
    \ORM\Entity $entities, 
): \ORM\Testing\EntityFetcherMock\Result|\Mockery\MockInterface
Create and add a EntityFetcherMock\Result for $class

As the results are mocked to come from the database they will also get a primary key if they don’t have already.

Returns ORM\Testing\EntityFetcherMock\Result|Mockery\MockInterface  
Parameters
Parameter Type Description
$class mixed  
$entities ORM\Entity  

ORM\Testing\EntityManagerMock::addResult

↑ top


ORM\Testing\EntityManagerMock::beginTransaction

public function beginTransaction(): bool
Begin a transaction or create a savepoint
Returns bool  

ORM\Testing\EntityManagerMock::beginTransaction

↑ top


ORM\Testing\EntityManagerMock::commit

public function commit(bool $all = false): bool
Commit the current transaction or decrease the savepoint counter

Actually nothing will be committed if there are savepoints. Instead the counter will be decreased and the commited savepoint will still be rolled back when you call rollback afterwards.

Hopefully that gives a hint why save points are no transactions and what the limitations are.

Begin transaction
  updates / inserts for transaction1
  Create savepoint transaction1
    updates / inserts for transaction2
    Create savepoint transaction2
      updates / inserts for transaction3
    <no commit here but you called commit for transaction3>
    updates / inserts for transaction2
  rollback of transaction2 to savepoint of transaction1
  update / inserts for transaction1
commit of transaction1
Returns bool  
Parameters
Parameter Type Description
$all bool Commit all opened transactions and savepoints

ORM\Testing\EntityManagerMock::commit

↑ top


ORM\Testing\EntityManagerMock::defineForNamespace

public function defineForNamespace(mixed $nameSpace): $this
Define $this EntityManager as the default EntityManager for $nameSpace
Returns $this  
Parameters
Parameter Type Description
$nameSpace mixed  

ORM\Testing\EntityManagerMock::defineForNamespace

↑ top


ORM\Testing\EntityManagerMock::defineForParent

public function defineForParent(mixed $class): $this
Define $this EntityManager as the default EntityManager for subClasses of $class
Returns $this  
Parameters
Parameter Type Description
$class mixed  

ORM\Testing\EntityManagerMock::defineForParent

↑ top


ORM\Testing\EntityManagerMock::delete

public function delete(\ORM\Entity $entity): bool
Delete $entity from database

This method does not delete from the map - you can still receive the entity via fetch.

Returns bool  
Parameters
Parameter Type Description
$entity ORM\Entity  

ORM\Testing\EntityManagerMock::delete

↑ top


ORM\Testing\EntityManagerMock::describe

public function describe(string $table): \ORM\Dbal\Column[]|\ORM\Dbal\Table
Returns an array of columns from $table.
Returns ORM\Dbal\Column[]|ORM\Dbal\Table  
Parameters
Parameter Type Description
$table string  

ORM\Testing\EntityManagerMock::describe

↑ top


ORM\Testing\EntityManagerMock::detach

public function detach(
    \ORM\ObserverInterface $observer, 
    string|null $from = null, 
): bool
Detach $observer from all classes

If the observer is attached to multiple classes all are removed except the optional parameter $from defines from which class to remove the $observer.

Returns whether or not an observer got detached.

Returns bool  
Parameters
Parameter Type Description  
$observer ORM\ObserverInterface    
$from **string null**  

ORM\Testing\EntityManagerMock::detach

↑ top


ORM\Testing\EntityManagerMock::eagerLoad

public function eagerLoad(string $relation, \ORM\Entity $entities): mixed
Load the $relation on all $entities with the least amount of queries

$relation can be nested by dividing them with a dot.

Returns mixed  
Throws ORM\Exception\UndefinedRelation  
Parameters
Parameter Type Description
$relation string  
$entities ORM\Entity  

ORM\Testing\EntityManagerMock::eagerLoad

↑ top


ORM\Testing\EntityManagerMock::escapeIdentifier

public function escapeIdentifier(string $identifier): string
Returns $identifier quoted for use in a sql statement
Returns string  
Parameters
Parameter Type Description
$identifier string Identifier to quote

ORM\Testing\EntityManagerMock::escapeIdentifier

↑ top


ORM\Testing\EntityManagerMock::escapeValue

public function escapeValue(mixed $value): string
Returns $value formatted to use in a sql statement.
Returns string  
Parameters
Parameter Type Description
$value mixed The variable that should be returned in SQL syntax

ORM\Testing\EntityManagerMock::escapeValue

↑ top


ORM\Testing\EntityManagerMock::fetch

public function fetch(
    string $class, 
    mixed $primaryKey = null, 
): \ORM\Entity|\ORM\EntityFetcher
Fetch one or more entities

With $primaryKey it tries to find this primary key in the entity map (carefully: mostly the database returns a string and we do not convert them). If there is no entity in the entity map it tries to fetch the entity from the database. The return value is then null (not found) or the entity.

Without $primaryKey it creates an entityFetcher and returns this.

Returns ORM\Entity|ORM\EntityFetcher  
Throws ORM\Exception\IncompletePrimaryKey If a combined primary key is incomplete
Throws ORM\Exception\NoEntity If $class is not a subclass of Entity
Parameters
Parameter Type Description
$class string The entity class you want to fetch
$primaryKey mixed The primary key of the entity you want to fetch

ORM\Testing\EntityManagerMock::fetch

↑ top


ORM\Testing\EntityManagerMock::finishBulkInserts

public function finishBulkInserts(string $class): \ORM\Entity[]
Finish the bulk insert for $class.

Returns an array of entities added.

Returns ORM\Entity[]  
Parameters
Parameter Type Description
$class string  

ORM\Testing\EntityManagerMock::finishBulkInserts

↑ top


ORM\Testing\EntityManagerMock::fire

public function fire(\ORM\Event $event): bool
Fire $event on $entity
Returns bool  
Parameters
Parameter Type Description
$event ORM\Event  

ORM\Testing\EntityManagerMock::fire

↑ top


ORM\Testing\EntityManagerMock::getConnection

public function getConnection(): \PDO
Get the pdo connection.
Returns PDO  
Throws ORM\Exception\NoConnection  
Throws ORM\Exception\NoConnection  

ORM\Testing\EntityManagerMock::getConnection

↑ top


ORM\Testing\EntityManagerMock::getDbal

public function getDbal(): \ORM\Dbal\Dbal
Get the Database Abstraction Layer
Returns ORM\Dbal\Dbal  

ORM\Testing\EntityManagerMock::getDbal

↑ top


ORM\Testing\EntityManagerMock::getInstance

public static function getInstance(string $class = null): \ORM\EntityManager
Get an instance of the EntityManager.

If no class is given it gets $class from backtrace.

It first tries to get the EntityManager for the Namespace of $class, then for the parents of $class. If no EntityManager is found it returns the last created EntityManager (null if no EntityManager got created).

Static   This method is static.
Returns ORM\EntityManager The entity manager instance to use for $class
Throws ORM\Exception When no entity manager got initialized
Parameters
Parameter Type Description
$class string  

ORM\Testing\EntityManagerMock::getInstance

↑ top


ORM\Testing\EntityManagerMock::getNamer

public function getNamer(): \ORM\Namer
Get the Namer instance
Returns ORM\Namer  

ORM\Testing\EntityManagerMock::getNamer

↑ top


ORM\Testing\EntityManagerMock::getOption

public function getOption(mixed $option): mixed
Get $option
Returns mixed  
Parameters
Parameter Type Description
$option mixed  

ORM\Testing\EntityManagerMock::getOption

↑ top


ORM\Testing\EntityManagerMock::getResults

public function getResults(string $class, \ORM\EntityFetcher $fetcher): array
Get the results for $class and $query

The EntityFetcherMock\Result gets a quality for matching this query. Only the highest quality will be used.

Returns array  
Parameters
Parameter Type Description
$class string  
$fetcher ORM\EntityFetcher  

ORM\Testing\EntityManagerMock::getResults

↑ top


ORM\Testing\EntityManagerMock::has

public function has(\ORM\Entity|string $entity, mixed $primaryKey = null): bool
Check if the entity map has $entity

If you want to know if the entity already exists in the map use this method.

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

ORM\Testing\EntityManagerMock::has

↑ top


ORM\Testing\EntityManagerMock::map

public function map(
    \ORM\Entity $entity, 
    bool $update = false, 
    string $class = null, 
): \ORM\Entity
Map $entity in the entity map

Returns the given entity or an entity that previously got mapped. This is useful to work in every function with the same object.

$user = $enitityManager->map(new User(['id' => 42]));
Returns ORM\Entity  
Parameters
Parameter Type Description
$entity ORM\Entity  
$update bool Update the entity map
$class string Overwrite the class

ORM\Testing\EntityManagerMock::map

↑ top


ORM\Testing\EntityManagerMock::observe

public function observe(
    string $class, 
    \ORM\ObserverInterface|null $observer = null, 
): \ORM\Observer\CallbackObserver|null
Observe $class using $observer

If AbstractObserver is omitted it returns a new CallbackObserver. Usage example:

$em->observe(User::class)
    ->on('inserted', function (User $user) { ... })
    ->on('deleted', function (User $user) { ... });

For more information about model events please consult the [documentation](https://tflori.github.io/

Returns ORM\Observer\CallbackObserver|null  
Throws ORM\Exception\InvalidArgument  
Parameters
Parameter Type Description  
$class string    
$observer **ORM\ObserverInterface null**  

ORM\Testing\EntityManagerMock::observe

↑ top


ORM\Testing\EntityManagerMock::query

public function query(
    string $table, 
    string $alias = '', 
): \ORM\QueryBuilder\QueryBuilder
Get a query builder for $table
Returns ORM\QueryBuilder\QueryBuilder  
Parameters
Parameter Type Description
$table string  
$alias string  

ORM\Testing\EntityManagerMock::query

↑ top


ORM\Testing\EntityManagerMock::raw

public static function raw(string $expression): \ORM\Dbal\Expression
Create a raw expression from $expression to disable escaping
Static   This method is static.
Returns ORM\Dbal\Expression  
Parameters
Parameter Type Description
$expression string  

ORM\Testing\EntityManagerMock::raw

↑ top


ORM\Testing\EntityManagerMock::retrieve

public function retrieve(string $class, array $primaryKey): \ORM\Entity|null
Retrieve an entity by $primaryKey
Returns ORM\Entity|null  
Parameters
Parameter Type Description
$class string  
$primaryKey array  

ORM\Testing\EntityManagerMock::retrieve

↑ top


ORM\Testing\EntityManagerMock::rollback

public function rollback(): bool
Rollback the current transaction or save point
Returns bool  

ORM\Testing\EntityManagerMock::rollback

↑ top


ORM\Testing\EntityManagerMock::setConnection

public function setConnection(mixed $connection): mixed
Add connection after instantiation

The connection can be an array of parameters for DbConfig::__construct(), a callable function that returns a PDO instance, an instance of DbConfig or a PDO instance itself.

When it is not a PDO instance the connection get established on first use.

Returns mixed  
Throws ORM\Exception\InvalidConfiguration  
Parameters
Parameter Type Description
$connection mixed A configuration for (or a) PDO instance

ORM\Testing\EntityManagerMock::setConnection

↑ top


ORM\Testing\EntityManagerMock::setOption

public function setOption(string $option, mixed $value): $this
Set $option to $value
Returns $this  
Parameters
Parameter Type Description
$option string One of OPT_* constants
$value mixed  

ORM\Testing\EntityManagerMock::setOption

↑ top


ORM\Testing\EntityManagerMock::setResolver

public static function setResolver(callable $resolver): mixed
Overwrite the functionality of ::getInstance($class) by $resolver($class)
Static   This method is static.
Returns mixed  
Parameters
Parameter Type Description
$resolver callable  

ORM\Testing\EntityManagerMock::setResolver

↑ top


ORM\Testing\EntityManagerMock::sync

public function sync(\ORM\Entity $entity, bool $reset = false): bool
Synchronizing $entity with database

If $reset is true it also calls reset() on $entity.

Returns bool  
Parameters
Parameter Type Description
$entity ORM\Entity  
$reset bool Reset entities current data

ORM\Testing\EntityManagerMock::sync

↑ top


ORM\Testing\EntityManagerMock::useBulkInserts

public function useBulkInserts(string $class, int $limit = 20): \ORM\BulkInsert
Force $class to use bulk insert.

At the end you should call finish bulk insert otherwise you may loose data.

Returns ORM\BulkInsert  
Parameters
Parameter Type Description
$class string  
$limit int Maximum number of rows per insert

ORM\Testing\EntityManagerMock::useBulkInserts

↑ top