Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 30 additions & 9 deletions ORM/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -134,7 +140,6 @@ private function convertToArray($object, $getters)
}
}


foreach ($getters as $field => $getter) {
if ($getter['exec']) {
$value = $object->{$getter['name']}();
Expand All @@ -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']);
}

Expand Down Expand Up @@ -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.
*
Expand All @@ -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)";
Expand Down
34 changes: 31 additions & 3 deletions Tests/Unit/ORM/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}