Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some API violations #359

Merged
merged 1 commit into from
Apr 14, 2020
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
18 changes: 13 additions & 5 deletions src/Filter/AbstractDateFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,29 +55,37 @@ public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data)

switch ($data['type']) {
case DateType::TYPE_EQUAL:
return $this->applyTypeIsEqual($queryBuilder, $field, $data);
$this->applyTypeIsEqual($queryBuilder, $field, $data);

return;

case DateType::TYPE_GREATER_THAN:
if (!\array_key_exists('value', $data) || !$data['value']) {
return;
}

return $this->applyTypeIsGreaterThan($queryBuilder, $field, $data);
$this->applyTypeIsGreaterThan($queryBuilder, $field, $data);

return;

case DateType::TYPE_LESS_EQUAL:
if (!\array_key_exists('value', $data) || !$data['value']) {
return;
}

return $this->applyTypeIsLessEqual($queryBuilder, $field, $data);
$this->applyTypeIsLessEqual($queryBuilder, $field, $data);

return;

case DateType::TYPE_NULL:
case DateType::TYPE_NOT_NULL:
return $this->applyType($queryBuilder, $this->getOperator($data['type']), $field, null);
$this->applyType($queryBuilder, $this->getOperator($data['type']), $field, null);

return;

case DateType::TYPE_GREATER_EQUAL:
case DateType::TYPE_LESS_THAN:
return $this->applyType($queryBuilder, $this->getOperator($data['type']), $field, $data['value']);
$this->applyType($queryBuilder, $this->getOperator($data['type']), $field, $data['value']);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Guesser/AbstractTypeGuesser.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ protected function getParentMetadataForProperty($baseClass, $propertyFullName, M
try {
return $modelManager->getParentMetadataForProperty($baseClass, $propertyFullName);
} catch (MappingException $e) {
// no metadata not found.
return;
// no metadata found.
return null;
}
}
}
18 changes: 11 additions & 7 deletions src/Model/ModelManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public function delete($object)
public function find($class, $id)
{
if (!isset($id)) {
return;
return null;
}

$documentManager = $this->getDocumentManager($class);
Expand Down Expand Up @@ -185,7 +185,7 @@ public function findOneBy($class, array $criteria = [])
}

/**
* @param string $class
* @param object|string $class
*
* @throw \RuntimeException
*
Expand Down Expand Up @@ -275,13 +275,17 @@ public function getIdentifierFieldNames($class)
*/
public function getNormalizedIdentifier($document)
{
if (is_scalar($document)) {
if (null === $document) {
return null;
}

if (!\is_object($document)) {
throw new \RunTimeException('Invalid argument, object or null required');
}

// the document is not managed
if (!$document || !$this->getDocumentManager($document)->getUnitOfWork()->isInIdentityMap($document)) {
return;
if (!$this->getDocumentManager($document)->getUnitOfWork()->isInIdentityMap($document)) {
return null;
}

$values = $this->getIdentifierValues($document);
Expand All @@ -292,9 +296,9 @@ public function getNormalizedIdentifier($document)
/**
* {@inheritdoc}
*/
public function getUrlsafeIdentifier($entity)
public function getUrlSafeIdentifier($document)
{
return $this->getNormalizedIdentifier($entity);
return $this->getNormalizedIdentifier($document);
}

/**
Expand Down
38 changes: 37 additions & 1 deletion tests/Model/ModelManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,42 @@ public function testFilterEmpty(): void
{
$registry = $this->createMock(ManagerRegistry::class);

$manager = new ModelManager($registry);
new ModelManager($registry);
}

/**
* @dataProvider getWrongDocuments
*
* @param mixed $document
*/
public function testNormalizedIdentifierException($document): void
{
$registry = $this->createMock(ManagerRegistry::class);

$model = new ModelManager($registry);

$this->expectException(\RuntimeException::class);

$model->getNormalizedIdentifier($document);
}

public function getWrongDocuments(): iterable
{
yield [0];
yield [1];
yield [false];
yield [true];
yield [[]];
yield [''];
yield ['sonata-project'];
}

public function testGetNormalizedIdentifierNull(): void
{
$registry = $this->createMock(ManagerRegistry::class);

$model = new ModelManager($registry);

$this->assertNull($model->getNormalizedIdentifier(null));
}
}