From 57fb5180092fef3de867b88a06c4e3ec232b4cd5 Mon Sep 17 00:00:00 2001 From: Greg Aker Date: Thu, 21 Apr 2011 14:58:26 -0500 Subject: Cleanup in Parser_test...Adding Table_test Pascal did earlier. --- tests/codeigniter/libraries/Table_test.php | 291 +++++++++++++++++++++++++++++ 1 file changed, 291 insertions(+) create mode 100644 tests/codeigniter/libraries/Table_test.php (limited to 'tests/codeigniter/libraries/Table_test.php') diff --git a/tests/codeigniter/libraries/Table_test.php b/tests/codeigniter/libraries/Table_test.php new file mode 100644 index 000000000..ded4c22c1 --- /dev/null +++ b/tests/codeigniter/libraries/Table_test.php @@ -0,0 +1,291 @@ +table = new CI_table(); + + $this->ci_instance($obj); + + $this->table = $obj->table; + } + + + // Setter Methods + // -------------------------------------------------------------------- + + public function testSetTemplate() + { + $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 testSetEmpty() + { + $this->table->set_empty('nada'); + $this->assertEquals('nada', $this->table->empty_cells); + } + + public function testSetCaption() + { + $this->table->set_caption('awesome cap'); + $this->assertEquals('awesome cap', $this->table->caption); + } + + + /* + * @depends testPrepArgs + */ + public function testSetHeading() + { + // 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 testAddRow() + { + // 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 testPrepArgs() + { + $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() + $this->assertEquals( + $expected, + $this->table->_prep_args(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, + $this->table->_prep_args(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, + $this->table->_prep_args(array( + array('name', 'color', 'size', + array('data' => 'weight', 'class' => 'awesome') + ) + )), + 'attributes'); + } + + public function testDefaultTemplateKeys() + { + $deft_template = $this->table->_default_template(); + $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 testCompileTemplate() + { + $this->assertFalse($this->table->set_template('invalid_junk')); + + // non default key + $this->table->set_template(array('nonsense' => 'foo')); + $this->table->_compile_template(); + + $this->assertArrayHasKey('nonsense', $this->table->template); + $this->assertEquals('foo', $this->table->template['nonsense']); + + // override default + $this->table->set_template(array('table_close' => '')); + $this->table->_compile_template(); + + $this->assertArrayHasKey('table_close', $this->table->template); + $this->assertEquals('', $this->table->template['table_close']); + } + + public function testMakeColumns() + { + // 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' // not an integer! + // ); + + + // 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   + $this->assertEquals( + array( + array('Laura', 'Red', '15'), + array('Katie', 'Blue', ' ') + ), + $this->table->make_columns($five_values, 3) + ); + + $this->markTestSkipped('Look at commented assertFalse above'); + } + + public function testClear() + { + $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 testSetFromArray() + { + $this->assertFalse($this->table->_set_from_array('bogus')); + $this->assertFalse($this->table->_set_from_array(array())); + + $data = array( + array('name', 'color', 'number'), + array('Laura', 'Red', '22'), + array('Katie', 'Blue') + ); + + $this->table->_set_from_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'), + ); + + $this->table->_set_from_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 testSetFromObject() + { + $this->markTestSkipped('Not yet implemented.'); + } + + // Test main generate method + // -------------------------------------------------------------------- +} \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 68286a4dcc1ed4d904ad992173c1b3621bf6fced Mon Sep 17 00:00:00 2001 From: Eric Barnes Date: Thu, 21 Apr 2011 22:00:33 -0400 Subject: Reworked unit tests to match rest of framework and added a few more. --- tests/codeigniter/libraries/Table_test.php | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'tests/codeigniter/libraries/Table_test.php') diff --git a/tests/codeigniter/libraries/Table_test.php b/tests/codeigniter/libraries/Table_test.php index ded4c22c1..133179f3a 100644 --- a/tests/codeigniter/libraries/Table_test.php +++ b/tests/codeigniter/libraries/Table_test.php @@ -5,7 +5,7 @@ require BASEPATH.'libraries/Table.php'; class Table_test extends CI_TestCase { - public function setUp() + public function set_up() { $obj = new StdClass; $obj->table = new CI_table(); @@ -19,7 +19,7 @@ class Table_test extends CI_TestCase // Setter Methods // -------------------------------------------------------------------- - public function testSetTemplate() + public function test_set_template() { $this->assertFalse($this->table->set_template('not an array')); @@ -31,13 +31,13 @@ class Table_test extends CI_TestCase $this->assertEquals($template, $this->table->template); } - public function testSetEmpty() + public function test_set_empty() { $this->table->set_empty('nada'); $this->assertEquals('nada', $this->table->empty_cells); } - public function testSetCaption() + public function test_set_caption() { $this->table->set_caption('awesome cap'); $this->assertEquals('awesome cap', $this->table->caption); @@ -47,7 +47,7 @@ class Table_test extends CI_TestCase /* * @depends testPrepArgs */ - public function testSetHeading() + 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 @@ -69,7 +69,7 @@ class Table_test extends CI_TestCase /* * @depends testPrepArgs */ - public function testAddRow() + 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 @@ -95,7 +95,7 @@ class Table_test extends CI_TestCase // Uility Methods // -------------------------------------------------------------------- - public function testPrepArgs() + public function test_prep_args() { $expected = array( array('data' => 'name'), @@ -139,7 +139,7 @@ class Table_test extends CI_TestCase 'attributes'); } - public function testDefaultTemplateKeys() + public function test_default_template_keys() { $deft_template = $this->table->_default_template(); $keys = array( @@ -158,7 +158,7 @@ class Table_test extends CI_TestCase } } - public function testCompileTemplate() + public function test_compile_template() { $this->assertFalse($this->table->set_template('invalid_junk')); @@ -177,7 +177,7 @@ class Table_test extends CI_TestCase $this->assertEquals('', $this->table->template['table_close']); } - public function testMakeColumns() + public function test_make_columns() { // Test bogus parameters $this->assertFalse($this->table->make_columns('invalid_junk')); @@ -213,7 +213,7 @@ class Table_test extends CI_TestCase $this->markTestSkipped('Look at commented assertFalse above'); } - public function testClear() + public function test_clear() { $this->table->set_heading('Name', 'Color', 'Size'); @@ -240,7 +240,7 @@ class Table_test extends CI_TestCase } - public function testSetFromArray() + public function test_set_from_array() { $this->assertFalse($this->table->_set_from_array('bogus')); $this->assertFalse($this->table->_set_from_array(array())); @@ -281,7 +281,7 @@ class Table_test extends CI_TestCase ); } - function testSetFromObject() + function test_set_from_object() { $this->markTestSkipped('Not yet implemented.'); } -- cgit v1.2.3-24-g4f1b From e39728c55d3745ff60742d7dd1c5114ec690d1db Mon Sep 17 00:00:00 2001 From: tiyowan Date: Sat, 10 Mar 2012 02:10:44 +0400 Subject: Fix issue #1148 Rewrote tests to use reflection to access protected/private functions of Table class. This fixes fatal errors that prevent the test suite from executing other tests. Signed-off-by: tiyowan --- tests/codeigniter/libraries/Table_test.php | 64 +++++++++++++++++++----------- 1 file changed, 41 insertions(+), 23 deletions(-) (limited to 'tests/codeigniter/libraries/Table_test.php') diff --git a/tests/codeigniter/libraries/Table_test.php b/tests/codeigniter/libraries/Table_test.php index 133179f3a..045216b16 100644 --- a/tests/codeigniter/libraries/Table_test.php +++ b/tests/codeigniter/libraries/Table_test.php @@ -106,42 +106,50 @@ class Table_test extends CI_TestCase // 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, - $this->table->_prep_args(array( - 'name', 'color', 'size' - )), - 'discreet'); - + $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, - $this->table->_prep_args(array( - array('name', 'color', 'size') - )), - 'array'); + $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, - $this->table->_prep_args(array( - array('name', 'color', 'size', - array('data' => 'weight', 'class' => 'awesome') - ) - )), - 'attributes'); + $method->invokeArgs( + $this->table, array(array('name', 'color', 'size', array('data' => 'weight', 'class' => 'awesome')), 'attributes') + ) + ); } public function test_default_template_keys() { - $deft_template = $this->table->_default_template(); + $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', @@ -160,18 +168,23 @@ class Table_test extends CI_TestCase 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')); - $this->table->_compile_template(); + $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' => '')); - $this->table->_compile_template(); + $method->invoke($this->table); $this->assertArrayHasKey('table_close', $this->table->template); $this->assertEquals('', $this->table->template['table_close']); @@ -242,8 +255,13 @@ class Table_test extends CI_TestCase public function test_set_from_array() { - $this->assertFalse($this->table->_set_from_array('bogus')); - $this->assertFalse($this->table->_set_from_array(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'), @@ -251,7 +269,7 @@ class Table_test extends CI_TestCase array('Katie', 'Blue') ); - $this->table->_set_from_array($data, FALSE); + $method->invokeArgs($this->table, array($data, FALSE)); $this->assertEmpty($this->table->heading); $this->table->clear(); @@ -267,7 +285,7 @@ class Table_test extends CI_TestCase array('data' => 'Blue'), ); - $this->table->_set_from_array($data); + $method->invokeArgs($this->table, array($data)); $this->assertEquals(count($this->table->rows), 2); $this->assertEquals( -- cgit v1.2.3-24-g4f1b From 8749bc7e836c196dfef37d3b7b5a67736a15092c Mon Sep 17 00:00:00 2001 From: Taufan Aditya Date: Sun, 11 Mar 2012 05:43:45 +0700 Subject: Fix incomplete and skipped test --- tests/codeigniter/libraries/Table_test.php | 51 +++++++++++++++++++++++++----- 1 file changed, 43 insertions(+), 8 deletions(-) (limited to 'tests/codeigniter/libraries/Table_test.php') diff --git a/tests/codeigniter/libraries/Table_test.php b/tests/codeigniter/libraries/Table_test.php index 045216b16..0208a465a 100644 --- a/tests/codeigniter/libraries/Table_test.php +++ b/tests/codeigniter/libraries/Table_test.php @@ -194,11 +194,8 @@ class Table_test extends CI_TestCase { // 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' // not an integer! - // ); + $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 @@ -222,8 +219,6 @@ class Table_test extends CI_TestCase ), $this->table->make_columns($five_values, 3) ); - - $this->markTestSkipped('Look at commented assertFalse above'); } public function test_clear() @@ -301,7 +296,47 @@ class Table_test extends CI_TestCase function test_set_from_object() { - $this->markTestSkipped('Not yet implemented.'); + $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 -- cgit v1.2.3-24-g4f1b