Skip to content

Commit 5b98a4c

Browse files
committed
Add templateVars tests for button, checkbox, file and label widgets.
Add tests to ensure the templateVars option is wired up.
1 parent 146d7c8 commit 5b98a4c

File tree

4 files changed

+109
-0
lines changed

4 files changed

+109
-0
lines changed

tests/TestCase/View/Widget/ButtonWidgetTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,32 @@ public function testRenderAttributes()
128128
];
129129
$this->assertHtml($expected, $result);
130130
}
131+
132+
/**
133+
* Ensure templateVars option is hooked up.
134+
*
135+
* @return void
136+
*/
137+
public function testRenderTemplateVars()
138+
{
139+
$this->templates->add([
140+
'button' => '<button {{attrs}} custom="{{custom}}">{{text}}</button>',
141+
]);
142+
143+
$button = new ButtonWidget($this->templates);
144+
$data = [
145+
'templateVars' => ['custom' => 'value'],
146+
'text' => 'Go',
147+
];
148+
$result = $button->render($data, $this->context);
149+
$expected = [
150+
'button' => [
151+
'type' => 'submit',
152+
'custom' => 'value'
153+
],
154+
'Go',
155+
'/button'
156+
];
157+
$this->assertHtml($expected, $result);
158+
}
131159
}

tests/TestCase/View/Widget/CheckboxWidgetTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,4 +233,33 @@ public function testRenderUnCheckedValue($checked)
233233
];
234234
$this->assertHtml($expected, $result);
235235
}
236+
237+
/**
238+
* Ensure templateVars option is hooked up.
239+
*
240+
* @return void
241+
*/
242+
public function testRenderTemplateVars()
243+
{
244+
$this->templates->add([
245+
'checkbox' => '<input type="checkbox" custom="{{custom}}" name="{{name}}" value="{{value}}"{{attrs}}>',
246+
]);
247+
248+
$checkbox = new CheckboxWidget($this->templates);
249+
$data = [
250+
'templateVars' => ['custom' => 'value'],
251+
'name' => 'Comment[spam]',
252+
'value' => 1,
253+
];
254+
$result = $checkbox->render($data, $this->context);
255+
$expected = [
256+
'input' => [
257+
'type' => 'checkbox',
258+
'custom' => 'value',
259+
'name' => 'Comment[spam]',
260+
'value' => 1,
261+
]
262+
];
263+
$this->assertHtml($expected, $result);
264+
}
236265
}

tests/TestCase/View/Widget/FileWidgetTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,31 @@ public function testRenderAttributes()
6969
];
7070
$this->assertHtml($expected, $result);
7171
}
72+
73+
/**
74+
* Ensure templateVars option is hooked up.
75+
*
76+
* @return void
77+
*/
78+
public function testRenderTemplateVars()
79+
{
80+
$this->templates->add([
81+
'file' => '<input custom="{{custom}}" type="file" name="{{name}}"{{attrs}}>',
82+
]);
83+
84+
$input = new FileWidget($this->templates);
85+
$data = [
86+
'templateVars' => ['custom' => 'value'],
87+
'name' => 'files',
88+
];
89+
$result = $input->render($data, $this->context);
90+
$expected = [
91+
'input' => [
92+
'type' => 'file',
93+
'name' => 'files',
94+
'custom' => 'value'
95+
],
96+
];
97+
$this->assertHtml($expected, $result);
98+
}
7299
}

tests/TestCase/View/Widget/LabelWidgetTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,29 @@ public function testRenderAttributes()
103103
];
104104
$this->assertHtml($expected, $result);
105105
}
106+
107+
/**
108+
* Ensure templateVars option is hooked up.
109+
*
110+
* @return void
111+
*/
112+
public function testRenderTemplateVars()
113+
{
114+
$this->templates->add([
115+
'label' => '<label custom="{{custom}}" {{attrs}}>{{text}}</label>',
116+
]);
117+
118+
$label = new LabelWidget($this->templates);
119+
$data = [
120+
'templateVars' => ['custom' => 'value'],
121+
'text' => 'Label Text',
122+
];
123+
$result = $label->render($data, $this->context);
124+
$expected = [
125+
'label' => ['custom' => 'value'],
126+
'Label Text',
127+
'/label'
128+
];
129+
$this->assertHtml($expected, $result);
130+
}
106131
}

0 commit comments

Comments
 (0)