1<?php
2
3
4
5/**
6 * Test class for LabelCell.
7 * @group Core
8 * @group ListComponent
9 */
10class LabelCellTest extends PHPUnit_Framework_TestCase {
11
12    /**
13     * @var LabelCell
14     */
15    protected $labelCell;
16
17    /**
18     * Sets up the fixture, for example, opens a network connection.
19     * This method is called before a test is executed.
20     */
21    protected function setUp() {
22        $this->labelCell = new LabelCell;
23    }
24
25    /**
26     * Tears down the fixture, for example, closes a network connection.
27     * This method is called after a test is executed.
28     */
29    protected function tearDown() {
30
31    }
32
33    public function test__toString() {
34        $dataObject = new LabelCellTestDataObject();
35        $dataObject->getName = 'Kayla Abbey';
36
37        $this->labelCell->setDataObject($dataObject);
38        $this->labelCell->setProperties(array('getter' => 'getDescription'));
39
40        $this->assertEquals('Sample class', $this->labelCell->__toString());
41    }
42
43    public function testToValue() {
44        $dataObject = new LabelCellTestDataObject();
45
46        $this->labelCell->setDataObject($dataObject);
47        $this->labelCell->setProperties(array('getter' => 'getDescription'));
48
49        $this->assertEquals('Sample class', $this->labelCell->toValue());
50    }
51
52    public function testGetterChain() {
53        $dataObject = new LabelCellTestDataObject();
54
55        $this->labelCell->setDataObject($dataObject);
56        $this->labelCell->setProperties(array('getter' => array('getObject', 'getDescription')));
57
58        $this->assertEquals('Sample class', $this->labelCell->toValue());
59    }
60
61    public function testCellshHiddenField() {
62        $dataObject = new LabelCellTestDataObject();
63
64        $this->labelCell->setDataObject($dataObject);
65        $this->labelCell->setProperties(array(
66            'getter' => 'getDescription',
67            'hasHiddenField' => true,
68            'hiddenFieldName' => 'hdnTest',
69            'hiddenFieldId' => 'hdnTest[]',
70            'hiddenFieldValueGetter' => array('getObject', 'getDescription'),
71        ));
72
73        $this->assertEquals('Sample class<input type="hidden" name="hdnTest" id="hdnTest[]" class="" value="Sample class" />', $this->labelCell->__toString());
74    }
75
76    public function testCellshPlaceholderGetters() {
77        $dataObject = new LabelCellTestDataObject();
78
79        $this->labelCell->setDataObject($dataObject);
80        $this->labelCell->setProperties(array(
81            'getter' => 'getDescription',
82            'hasHiddenField' => true,
83            'placeholderGetters' => array('id' => 'getId'),
84            'hiddenFieldName' => 'hdnTest',
85            'hiddenFieldId' => 'hdnTest[{id}]',
86            'hiddenFieldValueGetter' => array('getObject', 'getDescription'),
87        ));
88
89        $xpectedOutput = 'Sample class<input type="hidden" name="hdnTest" id="hdnTest[1]" class="" value="Sample class" />';
90        $this->assertEquals($xpectedOutput, $this->labelCell->__toString());
91    }
92
93    public function testToStringWithArrayDataSource() {
94        $dataSource = array('name' => 'Kayla Abbey', 'age' => 25);
95
96        $this->labelCell->setDataObject($dataSource);
97
98        $this->labelCell->setProperties(array('getter' => 'name'));
99        $this->assertEquals('Kayla Abbey', $this->labelCell->__toString());
100
101        $this->labelCell->setProperties(array('getter' => 'age'));
102        $this->assertEquals('25', $this->labelCell->__toString());
103
104        $dataSource = array('Kayla Abbey', 25);
105
106        $this->labelCell->setDataObject($dataSource);
107
108        $this->labelCell->setProperties(array('getter' => 0));
109        $this->assertEquals('Kayla Abbey', $this->labelCell->__toString());
110
111        $this->labelCell->setProperties(array('getter' => 1));
112        $this->assertEquals('25', $this->labelCell->__toString());
113    }
114
115    public function testToStringWithDecoratedArrayDataSource() {
116        $dataSource = new sfOutputEscaperArrayDecorator('htmlentities', array('name' => 'Kayla Abbey', 'age' => 25));
117
118        $this->labelCell->setDataObject($dataSource);
119
120        $this->labelCell->setProperties(array('getter' => 'name'));
121        $this->assertEquals('Kayla Abbey', $this->labelCell->__toString());
122
123        $this->labelCell->setProperties(array('getter' => 'age'));
124        $this->assertEquals('25', $this->labelCell->__toString());
125
126        $dataSource = new sfOutputEscaperArrayDecorator('htmlentities', array('Kayla Abbey', 25));
127
128        $this->labelCell->setDataObject($dataSource);
129
130        $this->labelCell->setProperties(array('getter' => 0));
131        $this->assertEquals('Kayla Abbey', $this->labelCell->__toString());
132
133        $this->labelCell->setProperties(array('getter' => 1));
134        $this->assertEquals('25', $this->labelCell->__toString());
135    }
136
137}
138
139class LabelCellTestDataObject {
140
141    public function getId() {
142        return 1;
143    }
144
145    public function getDescription() {
146        return 'Sample class';
147    }
148
149    public function getObject() {
150        return new self;
151    }
152
153}
154
155?>
156