1<?php
2
3
4
5/**
6 * Test class for LinkCell.
7 * @group Core
8 * @group ListComponent
9 */
10class LinkCellTest extends PHPUnit_Framework_TestCase {
11
12    /**
13     * @var LinkCell
14     */
15    protected $linkCell;
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->linkCell = new LinkCell;
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    /**
34     * @todo Implement test__toString().
35     */
36    public function test__toString() {
37        $dataObject = new LinkCellTestDataObject();
38
39        $this->linkCell->setDataObject($dataObject);
40        $this->linkCell->setProperties(array(
41            'labelGetter' => 'getLabel',
42            'placeholderGetters' => array(
43                'id' => 'getId',
44                'status' => 'getCurrentState',
45            ),
46            'urlPattern' => 'index.php?id={id}&amp;status={status}'
47        ));
48
49        $expectedLink = '<a href="http://' . $_SERVER['PHP_SELF'] . '/index.php?id=1&amp;status=active">Label</a>';
50        $this->assertEquals($expectedLink, $this->linkCell->__toString());
51    }
52
53    public function testPopulateFromArray() {
54        $properties = array(
55            'dataObject' => new stdClass(),
56        );
57
58        $this->linkCell->populateFromArray($properties);
59
60        $this->assertEquals($properties['dataObject'], $this->linkCell->getDataObject());
61    }
62
63    public function testGetProperties() {
64        $properties = array(
65            'dataObject' => new stdClass(),
66        );
67        $this->linkCell->setProperties($properties);
68        $this->assertEquals($properties, $this->linkCell->getProperties());
69    }
70
71    public function testSetProperties() {
72        $properties = array(
73            'dataObject' => new stdClass(),
74        );
75        $this->linkCell->setProperties($properties);
76        $this->assertEquals($properties, $this->linkCell->getProperties());
77    }
78
79    public function testUnlinkableCells() {
80        $dataObject = new LinkCellTestDataObject();
81
82        $this->linkCell->setDataObject($dataObject);
83        $this->linkCell->setProperties(array(
84            'labelGetter' => 'getLabel',
85            'linkable' => false,
86            'placeholderGetters' => array(
87                'id' => 'getId',
88                'status' => 'getCurrentState',
89            ),
90            'urlPattern' => 'index.php?id={id}&amp;status={status}'
91        ));
92
93        $expectedLink = 'Label';
94        $this->assertEquals($expectedLink, $this->linkCell->__toString());
95    }
96
97        public function testConditionalLinkableCells() {
98        $dataObject = new LinkCellTestDataObject();
99        $conditionalParams = new sfOutputEscaperArrayDecorator('htmlentities', array(3));
100
101        $this->linkCell->setDataObject($dataObject);
102        $this->linkCell->setProperties(array(
103            'labelGetter' => 'getLabel',
104            'linkable' => array('isEven', $conditionalParams),
105            'placeholderGetters' => array(
106                'id' => 'getId',
107                'status' => 'getCurrentState',
108            ),
109            'urlPattern' => 'index.php?id={id}&amp;status={status}'
110        ));
111
112        $expectedLink = 'Label';
113        $this->assertEquals($expectedLink, $this->linkCell->__toString());
114
115        $conditionalParams = new sfOutputEscaperArrayDecorator('htmlentities', array(2));
116        $this->linkCell->setProperties(array(
117            'labelGetter' => 'getLabel',
118            'linkable' => array('isEven', $conditionalParams),
119            'placeholderGetters' => array(
120                'id' => 'getId',
121                'status' => 'getCurrentState',
122            ),
123            'urlPattern' => 'index.php?id={id}&amp;status={status}'
124        ));
125        $expectedLink = '<a href="http://' . $_SERVER['PHP_SELF'] . '/index.php?id=1&amp;status=active">Label</a>';
126        $this->assertEquals($expectedLink, $this->linkCell->__toString());
127    }
128
129}
130
131class LinkCellTestDataObject {
132
133    public function getLabel() {
134        return 'Label';
135    }
136
137    public function getId() {
138        return 1;
139    }
140
141    public function getCurrentState() {
142        return 'active';
143    }
144
145    public function isEven($number) {
146        return ($number % 2 === 0);
147    }
148
149}
150
151?>
152