Skip to content

Commit

Permalink
implement ormAllow* methods in MockTrait to modify expectations
Browse files Browse the repository at this point in the history
  • Loading branch information
tflori committed Apr 6, 2019
1 parent 06230de commit 1ef1375
Showing 1 changed file with 83 additions and 4 deletions.
87 changes: 83 additions & 4 deletions src/MockTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,33 @@ public function ormCreateMockedEntity($class, $data = [], $em = null)
* @param EntityManager $em
*/
public function ormExpectInsert($class, $defaultValues = [], $em = null)
{
$expectation = $this->ormAllowInsert($class, $defaultValues, $em);
$expectation->once();
}

/**
* 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.
*
* @param string $class The class that should get created
* @param array $defaultValues The default values that came from database (for example: the created column
* has by the default the current timestamp; the id is auto incremented...)
* @param EntityManager $em
* @return m\Expectation
*/
public function ormAllowInsert($class, $defaultValues = [], $em = null)
{
/** @var EntityManager|m\Mock $em */
$em = $em ?: EntityManager::getInstance($class);

/** @scrutinizer ignore-call */
$em->shouldReceive('sync')->with(m::type($class))->once()
$expectation = $em->shouldReceive('sync')->with(m::type($class))
->andReturnUsing(
function (Entity $entity) use ($class, $defaultValues, $em) {
/** @scrutinizer ignore-call */
Expand All @@ -122,6 +143,8 @@ function (Entity $entity, $useAutoIncrement = true) use ($defaultValues, $em) {
}
}
);

return $expectation;
}

/**
Expand All @@ -135,20 +158,39 @@ function (Entity $entity, $useAutoIncrement = true) use ($defaultValues, $em) {
* @return m\Mock|EntityFetcher
*/
public function ormExpectFetch($class, $entities = [], $em = null)
{
list($expectation, $fetcher) = $this->ormAllowFetch($class, $entities, $em);
$expectation->once();
return $fetcher;
}

/**
* Allow fetch for $class
*
* Mocks an EntityFetcher with $entities as result.
*
* Returns the Expectation for fetch on entityManager and the mocked EntityFetcher
*
* @param string $class The class that should be fetched
* @param array $entities The entities that get returned from fetcher
* @param EntityManager $em
* @return m\Expectation|EntityFetcher|m\Mock[]
*/
public function ormAllowFetch($class, $entities = [], $em = null)
{
/** @var EntityManager|m\Mock $em */
$em = $em ?: EntityManager::getInstance($class);

/** @var m\Mock|EntityFetcher $fetcher */
$fetcher = m::mock(EntityFetcher::class, [ $em, $class ])->makePartial();
$em->shouldReceive('fetch')->with($class)->once()->andReturn($fetcher);
$expectation = $em->shouldReceive('fetch')->with($class)->andReturn($fetcher);

/** @scrutinizer ignore-call */
$fetcher->shouldReceive('count')->with()->andReturn(count($entities))->byDefault();
array_push($entities, null);
$fetcher->shouldReceive('one')->with()->andReturnValues($entities)->byDefault();

return $fetcher;
return [$expectation, $fetcher];
}

/**
Expand All @@ -161,9 +203,25 @@ public function ormExpectFetch($class, $entities = [], $em = null)
* @param array $updatedData Emulate data changes in database
*/
public function ormExpectUpdate(Entity $entity, $changingData = [], $updatedData = [])
{
$expectation = $this->ormAllowUpdate($entity, $changingData, $updatedData);
$expectation->once();
}

/**
* Allow save on $entity
*
* Entity has to be a mock use `emCreateMockedEntity()` to create it.
*
* @param Entity|m\Mock $entity
* @param array $changingData Emulate changing data during update statement (triggers etc)
* @param array $updatedData Emulate data changes in database
* @return m\Expectation
*/
public function ormAllowUpdate(Entity $entity, $changingData = [], $updatedData = [])
{
/** @scrutinizer ignore-call */
$entity->shouldReceive('save')->once()->andReturnUsing(
$expectation = $entity->shouldReceive('save')->andReturnUsing(
function () use ($entity, $updatedData, $changingData) {
// sync with database using $updatedData
if (!empty($updatedData)) {
Expand All @@ -186,6 +244,8 @@ function () use ($entity, $updatedData, $changingData) {
return $entity;
}
);

return $expectation;
}

/**
Expand All @@ -199,6 +259,23 @@ function () use ($entity, $updatedData, $changingData) {
* @param EntityManager $em
*/
public function ormExpectDelete($entity, $em = null)
{
$expectation = $this->ormAllowDelete($entity, $em);
$expectation->once();
}

/**
* 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.
*
* @param string|Entity $entity
* @param EntityManager $em
* @return m\Expectation
*/
public function ormAllowDelete($entity, $em = null)
{
$class = is_string($entity) ? $entity : get_class($entity);

Expand All @@ -217,5 +294,7 @@ function (Entity $entity) {
return true;
}
);

return $expectation;
}
}

0 comments on commit 1ef1375

Please sign in to comment.