1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Netgen\Bundle\InformationCollectionBundle\Tests\Factory; |
4
|
|
|
|
5
|
|
|
use eZ\Publish\Core\FieldType\TextLine\Value as TextValue; |
6
|
|
|
use eZ\Publish\Core\Repository\Values\ContentType\FieldDefinition; |
7
|
|
|
use Netgen\Bundle\InformationCollectionBundle\Factory\FieldDataFactory; |
8
|
|
|
use Netgen\Bundle\InformationCollectionBundle\FieldHandler\Custom\CustomFieldHandlerInterface; |
9
|
|
|
use Netgen\Bundle\InformationCollectionBundle\FieldHandler\FieldHandlerRegistry; |
10
|
|
|
use Netgen\Bundle\InformationCollectionBundle\Value\LegacyData; |
11
|
|
|
use PHPUnit\Framework\TestCase; |
12
|
|
|
|
13
|
|
|
class FieldDataFactoryTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var FieldDataFactory |
17
|
|
|
*/ |
18
|
|
|
protected $factory; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
22
|
|
|
*/ |
23
|
|
|
protected $registry; |
24
|
|
|
|
25
|
|
|
public function setUp() |
26
|
|
|
{ |
27
|
|
|
$this->registry = $this->getMockBuilder(FieldHandlerRegistry::class) |
28
|
|
|
->disableOriginalConstructor() |
29
|
|
|
->setMethods(array('handle')) |
30
|
|
|
->getMock(); |
31
|
|
|
|
32
|
|
|
$this->factory = new FieldDataFactory($this->registry); |
33
|
|
|
|
34
|
|
|
parent::setUp(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testGetLegacyValueWithoutCustomHandler() |
38
|
|
|
{ |
39
|
|
|
$value = new TextValue('some value'); |
40
|
|
|
$definition = new FieldDefinition(array( |
41
|
|
|
'id' => 123, |
42
|
|
|
)); |
43
|
|
|
|
44
|
|
|
$this->registry->expects($this->once()) |
45
|
|
|
->method('handle') |
46
|
|
|
->with($value) |
47
|
|
|
->willReturn(null); |
48
|
|
|
|
49
|
|
|
$data = $this->factory->getLegacyValue($value, $definition); |
50
|
|
|
|
51
|
|
|
$this->assertInstanceOf(LegacyData::class, $data); |
52
|
|
|
$this->assertEquals(123, $data->getContentClassAttributeId()); |
53
|
|
|
$this->assertEquals(0.0, $data->getDataFloat()); |
54
|
|
|
$this->assertEquals(0, $data->getDataInt()); |
55
|
|
|
$this->assertEquals((string) $value, $data->getDataText()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testGetLegacyValueWithCustomHandler() |
59
|
|
|
{ |
60
|
|
|
$value = new TextValue('some value'); |
61
|
|
|
$definition = new FieldDefinition(array( |
62
|
|
|
'id' => 123, |
63
|
|
|
)); |
64
|
|
|
|
65
|
|
|
$handler = $this->getMockBuilder(CustomFieldHandlerInterface::class) |
66
|
|
|
->disableOriginalConstructor() |
67
|
|
|
->setMethods(array('toString', 'supports')) |
68
|
|
|
->getMock(); |
69
|
|
|
|
70
|
|
|
$handler->expects($this->once()) |
71
|
|
|
->method('toString') |
72
|
|
|
->with($value, $definition) |
73
|
|
|
->willReturn((string) $value); |
74
|
|
|
|
75
|
|
|
$this->registry->expects($this->once()) |
76
|
|
|
->method('handle') |
77
|
|
|
->with($value) |
78
|
|
|
->willReturn($handler); |
79
|
|
|
|
80
|
|
|
$data = $this->factory->getLegacyValue($value, $definition); |
81
|
|
|
|
82
|
|
|
$this->assertInstanceOf(LegacyData::class, $data); |
83
|
|
|
$this->assertEquals(123, $data->getContentClassAttributeId()); |
84
|
|
|
$this->assertEquals(0.0, $data->getDataFloat()); |
85
|
|
|
$this->assertEquals(0, $data->getDataInt()); |
86
|
|
|
$this->assertEquals((string) $value, $data->getDataText()); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|