diff --git a/ORM/Manager.php b/ORM/Manager.php index a02c79c9..88aa50b2 100644 --- a/ORM/Manager.php +++ b/ORM/Manager.php @@ -43,10 +43,10 @@ class Manager private $typesMapping = []; /** - * @param Connection $connection - * @param MetadataCollector $metadataCollector - * @param array $typesMapping - * @param array $bundlesMapping + * @param Connection|null $connection + * @param MetadataCollector|null $metadataCollector + * @param array $typesMapping + * @param array $bundlesMapping */ public function __construct($connection, $metadataCollector, $typesMapping, $bundlesMapping) { @@ -75,7 +75,13 @@ public function getConnection() */ public function getRepository($type) { - return $this->createRepository(is_array($type) ? $type : [$type]); + $type = is_array($type) ? $type : [$type]; + + foreach ($type as $selectedType) { + $this->checkRepositoryType($selectedType); + } + + return $this->createRepository($type); } /** @@ -134,7 +140,6 @@ private function convertToArray($object, $getters) } } - foreach ($getters as $field => $getter) { if ($getter['exec']) { $value = $object->{$getter['name']}(); @@ -148,12 +153,12 @@ private function convertToArray($object, $getters) if ($getter['multiple']) { $this->isTraversable($value); foreach ($value as $item) { - $this->checkType($item, $getter['namespace']); + $this->checkVariableType($item, $getter['namespace']); $arrayValue = $this->convertToArray($item, $getter['properties']); $newValue[] = $arrayValue; } } else { - $this->checkType($value, $getter['namespace']); + $this->checkVariableType($value, $getter['namespace']); $newValue = $this->convertToArray($value, $getter['properties']); } @@ -235,6 +240,22 @@ public function getTypesMapping() return $this->typesMapping; } + /** + * Checks if specified repository and type is defined, throws exception otherwise. + * + * @param string $type + * + * @throws \InvalidArgumentException + */ + private function checkRepositoryType($type) + { + if (!array_key_exists($type, $this->bundlesMapping)) { + $exceptionMessage = "Undefined repository {$type}, valid repositories are: " . + join(', ', array_keys($this->bundlesMapping)) . '.'; + throw new \InvalidArgumentException($exceptionMessage); + } + } + /** * Check if class matches the expected one. * @@ -243,7 +264,7 @@ public function getTypesMapping() * * @throws \InvalidArgumentException */ - private function checkType($object, $expectedClass) + private function checkVariableType($object, $expectedClass) { if (!is_object($object)) { $msg = 'Expected variable of type object, got ' . gettype($object) . ". (field isn't multiple)"; diff --git a/Tests/Unit/ORM/ManagerTest.php b/Tests/Unit/ORM/ManagerTest.php index 727758a3..5467fdc1 100644 --- a/Tests/Unit/ORM/ManagerTest.php +++ b/Tests/Unit/ORM/ManagerTest.php @@ -45,13 +45,41 @@ public function testGetMetadataCollector() */ public function testGetRepositories() { - $manager = new Manager(null, null, [], []); + $manager = new Manager(null, null, [], ['rep1' => ['type' => ''], 'rep2' => ['type' => '']]); $types = [ - 'type1', - 'type2', + 'rep1', + 'rep2', ]; $repository = $manager->getRepository($types); $this->assertEquals(new Repository($manager, $types), $repository); } + + /** + * Check if an exception is thrown when an undefined repository is specified. + * + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage Undefined repository rep1, valid repositories are: rep2, rep3. + */ + public function testGetRepositoriesException() + { + $manager = new Manager(null, null, [], ['rep2' => '', 'rep3' => '']); + $types = [ + 'rep1', + 'rep4', + ]; + $manager->getRepository($types); + } + + /** + * Check if an exception is thrown when an undefined repository is specified and only a single rep is specified. + * + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage Undefined repository rep1, valid repositories are: rep2, rep3. + */ + public function testGetRepositoriesExceptionSingle() + { + $manager = new Manager(null, null, [], ['rep2' => '', 'rep3' => '']); + $manager->getRepository('rep1'); + } }