diff --git a/composer.json b/composer.json index 88b1f24..6af0539 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/src/TreeHouse/BehatCommon/AbstractPersistenceContext.php b/src/TreeHouse/BehatCommon/AbstractPersistenceContext.php index c37fda7..038e21e 100644 --- a/src/TreeHouse/BehatCommon/AbstractPersistenceContext.php +++ b/src/TreeHouse/BehatCommon/AbstractPersistenceContext.php @@ -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 { @@ -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. * diff --git a/src/TreeHouse/BehatCommon/Alice/Instances/Instantiator/Methods/ObjectConstructor.php b/src/TreeHouse/BehatCommon/Alice/Instances/Instantiator/Methods/ObjectConstructor.php deleted file mode 100644 index f3c7d2e..0000000 --- a/src/TreeHouse/BehatCommon/Alice/Instances/Instantiator/Methods/ObjectConstructor.php +++ /dev/null @@ -1,31 +0,0 @@ -getProperties()); - } - - /** - * {@inheritDoc} - * - * @return object - */ - public function instantiate(Fixture $fixture) - { - return (object) $fixture->getProperties(); - } -} diff --git a/src/TreeHouse/BehatCommon/DoctrineOrmContext.php b/src/TreeHouse/BehatCommon/DoctrineOrmContext.php index 6e14923..1a81bc5 100644 --- a/src/TreeHouse/BehatCommon/DoctrineOrmContext.php +++ b/src/TreeHouse/BehatCommon/DoctrineOrmContext.php @@ -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); + } } /** @@ -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) + ) + ); + } } /** @@ -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) + ) + ); + } } /** @@ -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; } @@ -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)); @@ -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 @@ -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 * @@ -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; } /** diff --git a/src/TreeHouse/BehatCommon/PDOContext.php b/src/TreeHouse/BehatCommon/PDOContext.php index 1542fb0..f00e728 100644 --- a/src/TreeHouse/BehatCommon/PDOContext.php +++ b/src/TreeHouse/BehatCommon/PDOContext.php @@ -41,10 +41,10 @@ class PDOContext extends AbstractPersistenceContext */ public function __construct($dsn, $username, $password, array $options = []) { - $this->dsn = $dsn; + $this->dsn = $dsn; $this->username = $username; $this->password = $password; - $this->options = $options; + $this->options = $options; } /** @@ -54,27 +54,49 @@ protected function persistData($name, array $data) { $table = $this->convertNameToTable($this->singularize($name)); - $this->persistRows($table, $data); + foreach ($data as $row) { + $row = $this->applyMapping($this->getFieldMapping($table), $row); + $row = array_merge($this->getDefaultFixture($table), $row); + $this->transformFixture($table, $row); + $this->insert($table, $row); + } } /** * @inheritdoc */ - protected function assertDataPersisted($name, array $data) + protected function assertDataPersisted($name, array $criterias) { $table = $this->convertNameToTable($this->singularize($name)); - $this->assertRowsHaveBeenPersisted($table, $data); + foreach ($criterias as $criteria) { + $criteria = $this->applyMapping($this->getFieldMapping($table), $criteria); + $this->transformFixture($table, $criteria); + $match = $this->find($table, $criteria); + Assert::assertNotEmpty($match); + } } /** * @inheritdoc */ - protected function assertDataNotPersisted($name, array $data) + protected function assertDataNotPersisted($name, array $criterias) { $table = $this->convertNameToTable($this->singularize($name)); - $this->assertRowsHaveNotBeenPersisted($table, $data); + foreach ($criterias as $criteria) { + $criteria = $this->applyMapping($this->getFieldMapping($table), $criteria); + $this->transformFixture($table, $criteria); + $match = $this->find($table, $criteria); + Assert::assertNotEmpty( + $match, + sprintf( + 'There should not be a record in table "%s" with these criteria: %s', + $table, + json_encode($criteria) + ) + ); + } } /** @@ -91,7 +113,7 @@ protected function insert($table, array $data) $parameters = []; foreach ($data as $key => $value) { - $parameters[$key] = ':'.$key; + $parameters[$key] = ':' . $key; } $conn->beginTransaction(); $sql = sprintf( @@ -121,7 +143,7 @@ protected function insert($table, array $data) protected function find($table, array $criteria) { $parts = []; - $conn = $this->getConnection(); + $conn = $this->getConnection(); unset($criteria['profile.date_of_birth']); foreach ($criteria as $key => $value) { $parts[] = sprintf('%s = :%s', $key, $key); @@ -166,7 +188,7 @@ protected function purgeDatabase() /** * @return \PDO */ - protected function getConnection() + private function getConnection() { if (!$this->connection) { $this->connection = new \PDO( @@ -189,52 +211,4 @@ protected function convertNameToTable($name) { return $name; } - - /** - * @inheritdoc - */ - protected function persistRows($table, array $rows) - { - foreach ($rows as $row) { - $row = $this->applyMapping($this->getFieldMapping($table), $row); - $row = array_merge($this->getDefaultFixture($table), $row); - $this->transformFixture($table, $row); - $this->insert($table, $row); - } - } - - /** - * @inheritdoc - */ - protected function assertRowsHaveBeenPersisted($table, $rows) - { - foreach ($rows as $criteria) { - $criteria = $this->applyMapping($this->getFieldMapping($table), $criteria); - $this->transformFixture($table, $criteria); - $criteria = $this->parseFormatters($criteria); - $match = $this->find($table, $criteria); - Assert::assertNotEmpty($match); - } - } - - /** - * @inheritdoc - */ - protected function assertRowsHaveNotBeenPersisted($table, $rows) - { - foreach ($rows as $criteria) { - $criteria = $this->applyMapping($this->getFieldMapping($table), $criteria); - $this->transformFixture($table, $criteria); - $criteria = $this->parseFormatters($criteria); - $match = $this->find($table, $criteria); - Assert::assertNotEmpty( - $match, - sprintf( - 'There should not be a record in table "%s" with these criteria: %s', - $table, - json_encode($criteria) - ) - ); - } - } }