Skip to content
This repository has been archived by the owner on Feb 13, 2020. It is now read-only.

Commit

Permalink
removed alice and added support for json references
Browse files Browse the repository at this point in the history
  • Loading branch information
Cas Leentfaar committed Jul 27, 2015
1 parent 1b9353a commit 9085b02
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 210 deletions.
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"doctrine/orm": "~2.2",
"doctrine/common": "~2.2",
"doctrine/data-fixtures": "~1.1",
"nelmio/alice": "~2.0",
"phpunit/phpunit": "~4.7",
"swiftmailer/swiftmailer": ">=4.2.0,~5.0",
"symfony/dependency-injection": "~2.7",
Expand Down
33 changes: 0 additions & 33 deletions src/TreeHouse/BehatCommon/AbstractPersistenceContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
use Behat\Gherkin\Node\TableNode;
use Behat\MinkExtension\Context\RawMinkContext;
use Doctrine\Common\Inflector\Inflector;
use Nelmio\Alice\Fixtures\Loader;
use TreeHouse\BehatCommon\Alice\Instances\Instantiator\Methods\ObjectConstructor;

abstract class AbstractPersistenceContext extends RawMinkContext
{
Expand Down Expand Up @@ -48,37 +46,6 @@ public function theFollowingDataShouldNotHaveBeenPersisted($name, TableNode $dat
$this->assertDataNotPersisted($name, $data->getHash());
}

/**
* @param array $data
* @param string|null $class
*
* @return array|object
*/
protected function parseFormatters(array $data, $class = null)
{
if ($class === null) {
$fixtureData = [[$data]];
} else {
$fixtureData = [$class => [$data]];
}

$loader = new Loader();

if ($class === null) {
$loader->addInstantiator(new ObjectConstructor());
}

$objectsOrArrays = $loader->load($fixtureData);

$parsed = reset($objectsOrArrays);

if ($class === null) {
return (array) $parsed;
}

return $parsed;
}

/**
* Singularifies a given value.
*
Expand Down

This file was deleted.

134 changes: 47 additions & 87 deletions src/TreeHouse/BehatCommon/DoctrineOrmContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ protected function persistData($name, array $data)
{
$alias = $this->convertNameToAlias($this->singularize($name));

$this->persistRows($alias, $data);
foreach ($data as $row) {
$row = $this->applyMapping($this->getFieldMapping($alias), $row);
$row = $this->rowToEntityData($alias, $row, true);
$this->persistEntityData($alias, $row);
}
}

/**
Expand All @@ -52,7 +56,20 @@ public function assertDataPersisted($name, array $data)
{
$alias = $this->convertNameToAlias($this->singularize($name));

$this->assertRowsHaveBeenPersisted($alias, $data);
foreach ($data as $row) {
$criteria = $this->applyMapping($this->getFieldMapping($alias), $row);
$criteria = $this->rowToEntityData($alias, $criteria, false);
$entity = $this->getEntityManager()->getRepository($alias)->findOneBy($criteria);

Assert::assertNotNull(
$entity,
sprintf(
'The repository should find data of type "%s" with these criteria: %s',
$alias,
json_encode($criteria)
)
);
}
}

/**
Expand All @@ -62,7 +79,20 @@ public function assertDataNotPersisted($name, array $data)
{
$alias = $this->convertNameToAlias($this->singularize($name));

$this->assertRowsHaveNotBeenPersisted($alias, $data);
foreach ($data as $criteria) {
$criteria = $this->applyMapping($this->getFieldMapping($alias), $criteria);
$criteria = $this->rowToEntityData($alias, $criteria, false);
$entity = $this->getEntityManager()->getRepository($alias)->findOneBy($criteria);

Assert::assertNull(
$entity,
sprintf(
'The repository should not find data of type "%s" with these criteria: %s',
$alias,
json_encode($criteria)
)
);
}
}

/**
Expand Down Expand Up @@ -91,10 +121,6 @@ protected function transformEntityValues($entityName, array $row)
$propertyName = Inflector::camelize($property);
$fieldType = $meta->getTypeOfField($propertyName);

if (stristr($value, '.')) {
continue;
}

if (mb_strtolower($value) === 'null') {
$value = null;
}
Expand All @@ -114,8 +140,14 @@ protected function transformEntityValues($entityName, array $row)
break;
case null:
if ($value && $meta->hasAssociation($propertyName)) {
if (is_array($jsonValue = json_decode($value, true))) {
$criteria = $jsonValue;
} else {
$criteria = ['id' => $value];
}

$class = $meta->getAssociationTargetClass($propertyName);
$associatedValue = $this->getEntityManager()->getRepository($class)->find($value);
$associatedValue = $this->getEntityManager()->getRepository($class)->findOneBy($criteria);

if ($associatedValue === null) {
throw new \RuntimeException(sprintf('There is no %s entity with ID %s to associate with this %s entity', $class, $value, $entityName));
Expand All @@ -134,18 +166,6 @@ protected function transformEntityValues($entityName, array $row)
return $row;
}

/**
* @inheritdoc
*/
protected function persistRows($alias, array $rows)
{
foreach ($rows as $row) {
$row = $this->applyMapping($this->getFieldMapping($alias), $row);
$row = $this->rowToEntityData($alias, $row, true);
$this->persistEntityData($alias, $row);
}
}

/**
* @param string $entityName
* @param array $row
Expand All @@ -159,57 +179,12 @@ protected function rowToEntityData($entityName, array $row, $useDefaults = true)
$row = array_merge($this->getDefaultFixture($entityName), $row);
}


$this->transformFixture($entityName, $row);
$row = $this->transformEntityValues($entityName, $row);

return $row;
}

/**
* @inheritdoc
*/
protected function assertRowsHaveBeenPersisted($alias, $rows)
{
foreach ($rows as $row) {
$criteria = $this->applyMapping($this->getFieldMapping($alias), $row);
$criteria = $this->rowToEntityData($alias, $criteria, false);
$criteria = $this->parseFormatters($criteria);
$entity = $this->getEntityManager()->getRepository($alias)->findOneBy($criteria);

Assert::assertNotNull(
$entity,
sprintf(
'The repository should find data of type "%s" with these criteria: %s',
$alias,
json_encode($criteria)
)
);
}
}

/**
* @inheritdoc
*/
protected function assertRowsHaveNotBeenPersisted($alias, $rows)
{
foreach ($rows as $criteria) {
$criteria = $this->applyMapping($this->getFieldMapping($alias), $criteria);
$criteria = $this->rowToEntityData($alias, $criteria, false);
$criteria = $this->parseFormatters($criteria);
$entity = $this->getEntityManager()->getRepository($alias)->findOneBy($criteria);

Assert::assertNull(
$entity,
sprintf(
'The repository should not find data of type "%s" with these criteria: %s',
$alias,
json_encode($criteria)
)
);
}
}

/**
* @param string $name
*
Expand Down Expand Up @@ -264,34 +239,19 @@ protected function persistEntityData($alias, array $entityData)
protected function entityDataToEntity($alias, array $entityData)
{
$class = $this->getEntityManager()->getClassMetadata($alias)->getName();
$embedded = $this->stripEmbeddedData($entityData);
$object = $this->parseFormatters($entityData, $class);
$object = new $class();

$accessor = new PropertyAccessor();
foreach ($embedded as $key => $value) {
$accessor->setValue($object, $key, $value);
}

return $object;
}

/**
* @param array $entityData
*
* @return array
*/
protected function stripEmbeddedData(array &$entityData)
{
$embedded = [];

foreach ($entityData as $key => $value) {
if (stristr($key, '.')) {
$embedded[$key] = $value;
unset($entityData[$key]);
if ($key === 'id') {
$this->setId($object, $value);
continue;
}

$accessor->setValue($object, $key, $value);
}

return $embedded;
return $object;
}

/**
Expand Down
Loading

0 comments on commit 9085b02

Please sign in to comment.