summaryrefslogtreecommitdiffstats
path: root/tests/codeigniter/libraries/Table_test.php
blob: 0208a465acccb811252f4c51c5b6ad489c716147 (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<?php

require BASEPATH.'libraries/Table.php';

class Table_test extends CI_TestCase
{

	public function set_up()
	{
		$obj = new StdClass;
		$obj->table = new CI_table();
		
		$this->ci_instance($obj);
		
		$this->table = $obj->table;
	}

	
	// Setter Methods
	// --------------------------------------------------------------------
	
	public function test_set_template()
	{
		$this->assertFalse($this->table->set_template('not an array'));
		
		$template = array(
			'a' => 'b'
		);
		
		$this->table->set_template($template);
		$this->assertEquals($template, $this->table->template);
	}
	
	public function test_set_empty()
	{
		$this->table->set_empty('nada');
		$this->assertEquals('nada', $this->table->empty_cells);
	}
	
	public function test_set_caption()
	{
		$this->table->set_caption('awesome cap');
		$this->assertEquals('awesome cap', $this->table->caption);
	}
	
	
	/*
	 * @depends testPrepArgs
	 */
	public function test_set_heading()
	{
		// uses _prep_args internally, so we'll just do a quick
		// check to verify that func_get_args and prep_args are
		// being called.
		
		$this->table->set_heading('name', 'color', 'size');
		
		$this->assertEquals(
			array(
				array('data' => 'name'),
				array('data' => 'color'),
				array('data' => 'size')
			),
			$this->table->heading
		);
	}
	
	
	/*
	 * @depends testPrepArgs
	 */
	public function test_add_row()
	{
		// uses _prep_args internally, so we'll just do a quick
		// check to verify that func_get_args and prep_args are
		// being called.
		
		$this->table->add_row('my', 'pony', 'sings');
		$this->table->add_row('your', 'pony', 'stinks');
		$this->table->add_row('my pony', '>', 'your pony');
		
		$this->assertEquals(count($this->table->rows), 3);
		
		$this->assertEquals(
			array(
				array('data' => 'your'),
				array('data' => 'pony'),
				array('data' => 'stinks')
			),
			$this->table->rows[1]
		);
	}
	
	
	// Uility Methods
	// --------------------------------------------------------------------
	
	public function test_prep_args()
	{
		$expected = array(
			array('data' => 'name'),
			array('data' => 'color'),
			array('data' => 'size')
		);
		
		// test what would be discreet args,
		// basically means a single array as the calling method
		// will use func_get_args()
		
		$reflectionOfTable = new ReflectionClass($this->table);
		$method = $reflectionOfTable->getMethod('_prep_args');
		
		$method->setAccessible(true);
			
		$this->assertEquals(
			$expected,
			$method->invokeArgs(
				$this->table, array(array('name', 'color', 'size'), 'discreet')
			)
		);
		
		// test what would be a single array argument. Again, nested
		// due to func_get_args on calling methods
		$this->assertEquals(
			$expected,
			$method->invokeArgs(
				$this->table, array(array('name', 'color', 'size'), 'array')
			)
		);
		
		
		// with cell attributes
		
		// need to add that new argument row to our expected outcome
		$expected[] = array('data' => 'weight', 'class' => 'awesome');

		$this->assertEquals(
			$expected,
			$method->invokeArgs(
				$this->table, array(array('name', 'color', 'size', array('data' => 'weight', 'class' => 'awesome')), 'attributes')
			)
		);
	}
	
	public function test_default_template_keys()
	{
		$reflectionOfTable = new ReflectionClass($this->table);
		$method = $reflectionOfTable->getMethod('_default_template');
		
		$method->setAccessible(true);
		
		$deft_template = $method->invoke($this->table);
		$keys = array(
			'table_open',
			'thead_open', 'thead_close',
			'heading_row_start', 'heading_row_end', 'heading_cell_start', 'heading_cell_end',
			'tbody_open', 'tbody_close',
			'row_start', 'row_end', 'cell_start', 'cell_end',
			'row_alt_start', 'row_alt_end', 'cell_alt_start', 'cell_alt_end',
			'table_close'
		);
		
		foreach ($keys as $key)
		{
			$this->assertArrayHasKey($key, $deft_template);
		}
	}
	
	public function test_compile_template()
	{
		$reflectionOfTable = new ReflectionClass($this->table);
		$method = $reflectionOfTable->getMethod('_compile_template');
		
		$method->setAccessible(true);
		
		$this->assertFalse($this->table->set_template('invalid_junk'));
		
		// non default key
		$this->table->set_template(array('nonsense' => 'foo'));
		$method->invoke($this->table);
		
		$this->assertArrayHasKey('nonsense', $this->table->template);
		$this->assertEquals('foo', $this->table->template['nonsense']);
		
		// override default
		$this->table->set_template(array('table_close' => '</table junk>'));
		$method->invoke($this->table);
		
		$this->assertArrayHasKey('table_close', $this->table->template);
		$this->assertEquals('</table junk>', $this->table->template['table_close']);
	}
	
	public function test_make_columns()
	{
		// Test bogus parameters
		$this->assertFalse($this->table->make_columns('invalid_junk'));
		$this->assertFalse($this->table->make_columns(array()));
		$this->assertFalse($this->table->make_columns(array('one', 'two'), '2.5'));
		
		
		// Now on to the actual column creation
		
		$five_values = array(
			'Laura', 'Red', '15',
			'Katie', 'Blue'
		);
		
		// No column count - no changes to the array
		$this->assertEquals(
			$five_values,
			$this->table->make_columns($five_values)
		);
		
		// Column count of 3 leaves us with one &nbsp;
		$this->assertEquals(
			array(
				array('Laura', 'Red', '15'),
				array('Katie', 'Blue', '&nbsp;')				
			),
			$this->table->make_columns($five_values, 3)
		);
	}
	
	public function test_clear()
	{
		$this->table->set_heading('Name', 'Color', 'Size');
		
		// Make columns changes auto_heading
		$rows = $this->table->make_columns(array(
			'Laura', 'Red', '15',
			'Katie', 'Blue'
		), 3);
		
		foreach ($rows as $row)
		{
			$this->table->add_row($row);
		}
		
		$this->assertFalse($this->table->auto_heading);
		$this->assertEquals(count($this->table->heading), 3);
		$this->assertEquals(count($this->table->rows), 2);
		
		$this->table->clear();
		
		$this->assertTrue($this->table->auto_heading);
		$this->assertEmpty($this->table->heading);
		$this->assertEmpty($this->table->rows);
	}
	
	
	public function test_set_from_array()
	{
		$reflectionOfTable = new ReflectionClass($this->table);
		$method = $reflectionOfTable->getMethod('_set_from_array');
		
		$method->setAccessible(true);
				
		$this->assertFalse($method->invokeArgs($this->table, array('bogus')));
		$this->assertFalse($method->invoke($this->table, array()));
		
		$data = array(
			array('name', 'color', 'number'),
			array('Laura', 'Red', '22'),
			array('Katie', 'Blue')				
		);
		
		$method->invokeArgs($this->table, array($data, FALSE));
		$this->assertEmpty($this->table->heading);
		
		$this->table->clear();
		
		$expected_heading = array(
			array('data' => 'name'),
			array('data' => 'color'),
			array('data' => 'number')
		);
		
		$expected_second = array(
			array('data' => 'Katie'),
			array('data' => 'Blue'),
		);
		
		$method->invokeArgs($this->table, array($data));
		$this->assertEquals(count($this->table->rows), 2);
		
		$this->assertEquals(
			$expected_heading,
			$this->table->heading
		);
		
		$this->assertEquals(
			$expected_second,
			$this->table->rows[1]
		);
	}
	
	function test_set_from_object()
	{
		$reflectionOfTable = new ReflectionClass($this->table);
		$method = $reflectionOfTable->getMethod('_set_from_object');
		
		$method->setAccessible(true);

		// Make a stub of query instance
		$query = new CI_TestCase();
		$query->list_fields = function(){
			return array('name', 'email');
		};
		$query->result_array = function(){
			return array(
					array('name' => 'John Doe', 'email' => 'john@doe.com'),
					array('name' => 'Foo Bar', 'email' => 'foo@bar.com'),
				);
		};
		$query->num_rows = function(){
			return 2;
		};

		$expected_heading = array(
			array('data' => 'name'),
			array('data' => 'email')
		);

		$expected_second = array(
			'name' => array('data' => 'Foo Bar'),
			'email' => array('data' => 'foo@bar.com'),
		);

		$method->invokeArgs($this->table, array($query));

		$this->assertEquals(
			$expected_heading,
			$this->table->heading
		);
		
		$this->assertEquals(
			$expected_second,
			$this->table->rows[1]
		);
	}
	
	// Test main generate method
	// --------------------------------------------------------------------
}