Skip to content

Commit

Permalink
Merge branch '1.5' into 1.7
Browse files Browse the repository at this point in the history
  • Loading branch information
andrerom committed Jun 2, 2017
2 parents f365a3d + 756e95f commit d3427e0
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/FieldType/DataTransformer/FieldValueTransformer.php
Expand Up @@ -19,7 +19,7 @@
class FieldValueTransformer implements DataTransformerInterface
{
/**
* @var FieldType
* @var eZ\Publish\API\Repository\FieldType
*/
private $fieldType;

Expand All @@ -32,7 +32,7 @@ public function __construct(FieldType $fieldType)
* Transforms a FieldType Value into a hash using `FieldTpe::toHash()`.
* This hash is compatible with `reverseTransform()`.
*
* @param \eZ\Publish\SPI\FieldType\Value $value
* @param mixed $value
*
* @return array|null the value's hash, or null if $value was not a FieldType Value
*/
Expand All @@ -49,13 +49,13 @@ public function transform($value)
* Transforms a hash into a FieldType Value using `FieldType::fromHash()`.
* The FieldValue is compatible with `transform()`.
*
* @param array $value
* @param mixed $value
*
* @return \eZ\Publish\SPI\FieldType\Value
*/
public function reverseTransform($value)
{
if (!$value) {
if ($value === null) {
return $this->fieldType->getEmptyValue();
}

Expand Down
@@ -0,0 +1,88 @@
<?php

/**
* This file is part of the eZ RepositoryForms package.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*
* @version //autogentag//
*/
namespace EzSystems\RepositoryForms\Tests\FieldType\DataTransformer;

use eZ\Publish\API\Repository\FieldType;
use eZ\Publish\SPI\FieldType\Value;
use EzSystems\RepositoryForms\FieldType\DataTransformer\FieldValueTransformer;
use PHPUnit_Framework_TestCase;
use stdClass;

class FieldValueTransformerTest extends PHPUnit_Framework_TestCase
{
public function testTransformNull()
{
$value = new stdClass();

$fieldType = $this->getMock(FieldType::class);
$fieldType
->expects($this->never())
->method('toHash');

$result = (new FieldValueTransformer($fieldType))->transform($value);

$this->assertNull($result);
}

public function testTransform()
{
$value = $this->getMock(Value::class);
$valueHash = ['lorem' => 'Lorem ipsum dolor...'];

$fieldType = $this->getMock(FieldType::class);
$fieldType
->expects($this->once())
->method('toHash')
->with($value)
->willReturn($valueHash);

$result = (new FieldValueTransformer($fieldType))->transform($value);

$this->assertEquals($result, $valueHash);
}

public function testReverseTransformNull()
{
$emptyValue = $this->getMock(Value::class);

$fieldType = $this->getMock(FieldType::class);
$fieldType
->expects($this->once())
->method('getEmptyValue')
->willReturn($emptyValue);
$fieldType
->expects($this->never())
->method('fromHash');

$result = (new FieldValueTransformer($fieldType))->reverseTransform(null);

$this->assertSame($emptyValue, $result);
}

public function testReverseTransform()
{
$value = 'Lorem ipsum dolor...';
$expected = $this->getMock(Value::class);

$fieldType = $this->getMock(FieldType::class);
$fieldType
->expects($this->never())
->method('getEmptyValue');
$fieldType
->expects($this->once())
->method('fromHash')
->willReturn($expected);

$result = (new FieldValueTransformer($fieldType))->reverseTransform($value);

$this->assertSame($expected, $result);
}
}

0 comments on commit d3427e0

Please sign in to comment.