Skip to content

Commit

Permalink
improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
floriansemm committed Nov 3, 2016
1 parent 9108d15 commit 551e509
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 10 deletions.
10 changes: 1 addition & 9 deletions Doctrine/Hydration/DoctrineValueHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,11 @@ public function mapValue($fieldName, $value, MetaInformationInterface $metaInfor
return false;
}

// is object with getter
if ($metaInformation->getField($fieldName) && $metaInformation->getField($fieldName)->getter) {
return false;
}

$fieldSuffix = $this->removePrefixedKeyValues($fieldName);
if ($fieldSuffix === false) {
return false;
}

if (array_key_exists($fieldSuffix, Field::getComplexFieldMapping())) {
return false;
}

return true;
}

Expand Down
8 changes: 7 additions & 1 deletion Doctrine/Hydration/ValueHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ protected function removeFieldSuffix($property)
protected function removePrefixedKeyValues($value)
{
if (($pos = strrpos($value, '_')) !== false) {
return substr($value, ($pos+1));
return substr($value, ($pos + 1));
}

return $value;
Expand All @@ -101,6 +101,12 @@ private function toCamelCase($fieldname)
}

/**
* Check if given field and value can be mapped
*
* @param string $fieldName
* @param string $value
* @param MetaInformationInterface $metaInformation
*
* @return bool
*/
public function mapValue($fieldName, $value, MetaInformationInterface $metaInformation)
Expand Down
1 change: 1 addition & 0 deletions Tests/Doctrine/Hydration/DoctrineHydratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@


use FS\SolrBundle\Doctrine\Annotation\AnnotationReader;
use FS\SolrBundle\Doctrine\Annotation\Field;
use FS\SolrBundle\Doctrine\Hydration\DoctrineHydrator;
use FS\SolrBundle\Doctrine\Hydration\DoctrineHydratorInterface;
use FS\SolrBundle\Doctrine\Hydration\DoctrineValueHydrator;
Expand Down
55 changes: 55 additions & 0 deletions Tests/Doctrine/Hydration/DoctrineValueHydratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php


namespace FS\SolrBundle\Tests\Doctrine\Hydration;


use FS\SolrBundle\Doctrine\Annotation\Field;
use FS\SolrBundle\Doctrine\Hydration\DoctrineValueHydrator;
use FS\SolrBundle\Doctrine\Mapper\MetaInformation;

class DoctrineValueHydratorTest extends \PHPUnit_Framework_TestCase
{
/**
* @test
*/
public function skipArrays()
{
$hydrator = new DoctrineValueHydrator();

$this->assertFalse($hydrator->mapValue('createdAt', array(), new MetaInformation()));
}

/**
* @test
*/
public function skipObjects()
{
$hydrator = new DoctrineValueHydrator();

$field = new Field(array('type' => 'datetime'));
$field->name = 'createdAt';
$field->getter = 'format(\'Y-m-d\TH:i:s.z\Z\')';

$metaInformation = new MetaInformation();
$metaInformation->setFields(array($field));

$this->assertFalse($hydrator->mapValue('createdAt', new \DateTime(), $metaInformation));
}

/**
* @test
*/
public function mapCommonType()
{
$hydrator = new DoctrineValueHydrator();

$field = new Field(array('type' => 'string'));
$field->name = 'title';

$metaInformation = new MetaInformation();
$metaInformation->setFields(array($field));

$this->assertTrue($hydrator->mapValue('title_s', 'a title', $metaInformation));
}
}

0 comments on commit 551e509

Please sign in to comment.