summaryrefslogtreecommitdiff
path: root/tests/unit/Web/UI/THtmlWriterTest.php
blob: cf3b27e3257c43100086409ecfc242055e66c894 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php


Prado::using('System.Web.UI.THtmlWriter');

/**
 * Implement a writer that flush the content to a variable, to simulate a real flush
 */

class TestWriter extends TComponent implements ITextWriter
{
	private $_str='';
	private $_flushedContent=null;


	public function flush()
	{
		$this->_flushedContent=$this->_str;
		$this->_str='';
		return $this->_flushedContent;
	}


	public function write($str)
	{
		$this->_str.=$str;
	}


	public function writeLine($str='')
	{
		$this->write($str."\n");
	}

	// Accessors to get value of private vars during tests
	public function getFlushedContent() { return $this->_flushedContent; }
	public function getStr() { return $this->_str; }
}


/**
 * @package System.Web.UI
 */
class THtmlWriterTest extends PHPUnit_Framework_TestCase {

	private static $output=null;

	public function setUp () {
		// We need an output writer, use a TestWriter for this
		if (self::$output===null) self::$output=new TestWriter();
	}

	public function testConstruct() {
		$writer=new THtmlWriter(self::$output);
		self::assertEquals(self::$output, $writer->getWriter());
	}

	public function testSetAndGetWriter() {
		$writer=new THtmlWriter(null);
		self::assertNull($writer->getWriter());
		$writer->setWriter(self::$output);
		self::assertEquals(self::$output, $writer->getWriter());
	}

	public function testAddAttributes() {
		$writer=new THtmlWriter(self::$output);
		$writer->addAttributes(array ('type' => 'text', 'value' => 'Prado & Cie'));
		// get the private var _attributes
		$result=self::readAttribute($writer, '_attributes');
		self::assertEquals('text',$result['type']);
		self::assertEquals(THttpUtility::htmlEncode('Prado & Cie'), $result['value']);
	}

	public function testAddAttribute() {
		$writer=new THtmlWriter(self::$output);
		$writer->addAttribute('type','text');
		$writer->addAttribute('value','Prado & Cie');
		$result=self::readAttribute($writer, '_attributes');
		self::assertEquals('text',$result['type']);
		self::assertEquals(THttpUtility::htmlEncode('Prado & Cie'), $result['value']);
	}

	public function testRemoveAttribute() {
		$writer=new THtmlWriter(self::$output);
		$writer->addAttribute('type','text');
		$writer->addAttribute('value','Prado & Cie');
		$writer->removeAttribute('value');
		$result=self::readAttribute($writer, '_attributes');
		// 'type' should be present, 'value' not
		self::assertTrue(isset($result['type']));
		self::assertFalse(isset($result['value']));
	}

	public function testAddStyleAttributes() {
		$writer=new THtmlWriter(self::$output);
		$writer->addStyleAttributes(array ('font-size' => '1em', 'background-image'=>'url(image.gif)'));
		$result=self::readAttribute($writer, '_styles');
		self::assertEquals('1em', $result['font-size']);
		self::assertEquals(THttpUtility::htmlEncode('url(image.gif)'), $result['background-image']);
	}

	public function testAddStyleAttribute() {
		$writer=new THtmlWriter(self::$output);
		$writer->addStyleAttribute('font-size','1em');
		$writer->addStyleAttribute('background-image','url(image.gif)');
		$result=self::readAttribute($writer, '_styles');
		self::assertEquals('1em', $result['font-size']);
		self::assertEquals(THttpUtility::htmlEncode('url(image.gif)'), $result['background-image']);
	}

	public function testRemoveStyleAttribute() {
		$writer=new THtmlWriter(self::$output);
		$writer->addStyleAttribute('font-size','1em');
		$writer->addStyleAttribute('background-image','url(image.gif)');
		$writer->removeStyleAttribute('font-size');
		$result=self::readAttribute($writer, '_styles');
		self::assertTrue(isset($result['background-image']));
		self::assertFalse(isset($result['font-size']));
	}

	public function testFlush() {
		$writer=new THtmlWriter(self::$output);
		$writer->write('Some Text');
		$writer->flush();
		self::assertEquals('Some Text', self::$output->getFlushedContent());
	}

	public function testWrite() {
		$writer=new THtmlWriter(self::$output);
		$writer->write('Some Text');;
		self::assertEquals('Some Text', self::$output->flush());

	}

	public function testWriteLine() {
		$writer=new THtmlWriter(self::$output);
		$writer->writeLine('Some Text');;
		self::assertEquals("Some Text\n", self::$output->flush());

	}

	public function testWriteBreak() {
		$writer=new THtmlWriter(self::$output);
		$writer->writeBreak();
		self::assertEquals("<br/>", self::$output->flush());

	}

	public function testRenderBeginTag() {
		$writer=new THtmlWriter(self::$output);
		$writer->addAttribute('type','text');
		$writer->addAttribute('value','Prado');
		$writer->addStyleAttribute('font-size','1em');
		$writer->renderBeginTag('input');
		self::assertEquals('<input type="text" value="Prado" style="font-size:1em;" />', self::$output->flush());
	}

	public function testRenderEndTag() {
		$writer=new THtmlWriter(self::$output);
		$writer->renderBeginTag('div');
		$writer->write('Div Content');
		$writer->renderEndTag();
		self::assertEquals('<div>Div Content</div>', self::$output->flush());
	}

}