diff options
Diffstat (limited to 'tests/codeigniter/libraries')
-rw-r--r-- | tests/codeigniter/libraries/Parser_test.php | 113 | ||||
-rw-r--r-- | tests/codeigniter/libraries/Table_test.php | 344 | ||||
-rw-r--r-- | tests/codeigniter/libraries/Typography_test.php | 191 | ||||
-rw-r--r-- | tests/codeigniter/libraries/User_agent_test.php | 91 |
4 files changed, 739 insertions, 0 deletions
diff --git a/tests/codeigniter/libraries/Parser_test.php b/tests/codeigniter/libraries/Parser_test.php new file mode 100644 index 000000000..b4580a4b1 --- /dev/null +++ b/tests/codeigniter/libraries/Parser_test.php @@ -0,0 +1,113 @@ +<?php + +require BASEPATH.'libraries/Parser.php'; + +class Parser_test extends CI_TestCase +{ + + public function set_up() + { + $obj = new StdClass; + $obj->parser = new CI_Parser(); + + $this->ci_instance($obj); + + $this->parser = $obj->parser; + } + // -------------------------------------------------------------------- + + public function test_set_delimiters() + { + // Make sure default delimiters are there + $this->assertEquals('{', $this->parser->l_delim); + $this->assertEquals('}', $this->parser->r_delim); + + // Change them to square brackets + $this->parser->set_delimiters('[', ']'); + + // Make sure they changed + $this->assertEquals('[', $this->parser->l_delim); + $this->assertEquals(']', $this->parser->r_delim); + + // Reset them + $this->parser->set_delimiters(); + + // Make sure default delimiters are there + $this->assertEquals('{', $this->parser->l_delim); + $this->assertEquals('}', $this->parser->r_delim); + } + + // -------------------------------------------------------------------- + + public function test_parse_simple_string() + { + $data = array( + 'title' => 'Page Title', + 'body' => 'Lorem ipsum dolor sit amet.' + ); + + $template = "{title}\n{body}"; + + $result = implode("\n", $data); + + $this->assertEquals($result, $this->parser->parse_string($template, $data, TRUE)); + } + + // -------------------------------------------------------------------- + + public function test_parse() + { + $this->_parse_no_template(); + $this->_parse_var_pair(); + $this->_mismatched_var_pair(); + } + + // -------------------------------------------------------------------- + + private function _parse_no_template() + { + $this->assertFalse($this->parser->parse_string('', '', TRUE)); + } + + // -------------------------------------------------------------------- + + private function _parse_var_pair() + { + $data = array( + 'title' => 'Super Heroes', + 'powers' => array( + array( + 'invisibility' => 'yes', + 'flying' => 'no'), + ) + ); + + $template = "{title}\n{powers}{invisibility}\n{flying}{/powers}"; + + $result = "Super Heroes\nyes\nno"; + + $this->assertEquals($result, $this->parser->parse_string($template, $data, TRUE)); + } + + // -------------------------------------------------------------------- + + private function _mismatched_var_pair() + { + $data = array( + 'title' => 'Super Heroes', + 'powers' => array( + array( + 'invisibility' => 'yes', + 'flying' => 'no'), + ) + ); + + $template = "{title}\n{powers}{invisibility}\n{flying}"; + + $result = "Super Heroes\n{powers}{invisibility}\n{flying}"; + + $this->assertEquals($result, $this->parser->parse_string($template, $data, TRUE)); + } + + // -------------------------------------------------------------------- +}
\ No newline at end of file diff --git a/tests/codeigniter/libraries/Table_test.php b/tests/codeigniter/libraries/Table_test.php new file mode 100644 index 000000000..0208a465a --- /dev/null +++ b/tests/codeigniter/libraries/Table_test.php @@ -0,0 +1,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 + $this->assertEquals( + array( + array('Laura', 'Red', '15'), + array('Katie', 'Blue', ' ') + ), + $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 + // -------------------------------------------------------------------- +}
\ No newline at end of file diff --git a/tests/codeigniter/libraries/Typography_test.php b/tests/codeigniter/libraries/Typography_test.php new file mode 100644 index 000000000..a0533bae0 --- /dev/null +++ b/tests/codeigniter/libraries/Typography_test.php @@ -0,0 +1,191 @@ +<?php + +require BASEPATH.'libraries/Typography.php'; + +class Typography_test extends CI_TestCase +{ + + public function set_up() + { + $obj = new StdClass; + $obj->type = new CI_Typography(); + + $this->ci_instance($obj); + + $this->type = $obj->type; + } + + // -------------------------------------------------------------------- + + /** + * Tests the format_characters() function. + * + * this can and should grow. + */ + public function test_format_characters() + { + $strs = array( + '"double quotes"' => '“double quotes”', + '"testing" in "theory" that is' => '“testing” in “theory” that is', + "Here's what I'm" => 'Here’s what I’m', + '&' => '&', + '&' => '&', + ' ' => ' ', + '--' => '—', + 'foo...' => 'foo…', + 'foo..' => 'foo..', + 'foo...bar.' => 'foo…bar.', + 'test. new' => 'test. new', + ); + + foreach ($strs as $str => $expected) + { + $this->assertEquals($expected, $this->type->format_characters($str)); + } + } + + // -------------------------------------------------------------------- + + public function test_nl2br_except_pre() + { + $str = <<<EOH +Hello, I'm a happy string with some new lines. + +I like to skip. + +Jump + +and sing. + +<pre> +I am inside a pre tag. Please don't mess with me. + +k? +</pre> + +That's my story and I'm sticking to it. + +The End. +EOH; + + $expected = <<<EOH +Hello, I'm a happy string with some new lines. <br /> +<br /> +I like to skip.<br /> +<br /> +Jump<br /> +<br /> +and sing.<br /> +<br /> +<pre> +I am inside a pre tag. Please don't mess with me. + +k? +</pre><br /> +<br /> +That's my story and I'm sticking to it.<br /> +<br /> +The End. +EOH; + + $this->assertEquals($expected, + $this->type->nl2br_except_pre($str)); + } + + // -------------------------------------------------------------------- + + public function test_auto_typography() + { + $this->_blank_string(); + $this->_standardize_new_lines(); + $this->_reduce_linebreaks(); + $this->_remove_comments(); + $this->_protect_pre(); + $this->_no_opening_block(); + $this->_protect_braced_quotes(); + } + + // -------------------------------------------------------------------- + + private function _blank_string() + { + // Test blank string + $this->assertEquals('', $this->type->auto_typography('')); + } + + // -------------------------------------------------------------------- + + private function _standardize_new_lines() + { + $strs = array( + "My string\rhas return characters" => "<p>My string<br />\nhas return characters</p>", + 'This one does not!' => '<p>This one does not!</p>' + ); + + foreach ($strs as $str => $expect) + { + $this->assertEquals($expect, $this->type->auto_typography($str)); + } + } + + // -------------------------------------------------------------------- + + private function _reduce_linebreaks() + { + $str = "This has way too many linebreaks.\n\n\n\nSee?"; + $expect = "<p>This has way too many linebreaks.</p>\n\n<p>See?</p>"; + + $this->assertEquals($expect, $this->type->auto_typography($str, TRUE)); + } + + // -------------------------------------------------------------------- + + private function _remove_comments() + { + $str = '<!-- I can haz comments? --> But no!'; + $expect = '<p><!-- I can haz comments? --> But no!</p>'; + + $this->assertEquals($expect, $this->type->auto_typography($str)); + } + + // -------------------------------------------------------------------- + + private function _protect_pre() + { + $str = '<p>My Sentence</p><pre>var_dump($this);</pre>'; + $expect = '<p>My Sentence</p><pre>var_dump($this);</pre>'; + + $this->assertEquals($expect, $this->type->auto_typography($str)); + } + + // -------------------------------------------------------------------- + + private function _no_opening_block() + { + $str = 'My Sentence<pre>var_dump($this);</pre>'; + $expect = '<p>My Sentence</p><pre>var_dump($this);</pre>'; + + $this->assertEquals($expect, $this->type->auto_typography($str)); + } + + // -------------------------------------------------------------------- + + public function _protect_braced_quotes() + { + $this->type->protect_braced_quotes = TRUE; + + $str = 'Test {parse="foobar"}'; + $expect = '<p>Test {parse="foobar"}</p>'; + + $this->assertEquals($expect, $this->type->auto_typography($str)); + + $this->type->protect_braced_quotes = FALSE; + + $str = 'Test {parse="foobar"}'; + $expect = '<p>Test {parse=“foobar”}</p>'; + + $this->assertEquals($expect, $this->type->auto_typography($str)); + + + } +}
\ No newline at end of file diff --git a/tests/codeigniter/libraries/User_agent_test.php b/tests/codeigniter/libraries/User_agent_test.php new file mode 100644 index 000000000..6f9e87196 --- /dev/null +++ b/tests/codeigniter/libraries/User_agent_test.php @@ -0,0 +1,91 @@ +<?php + +require BASEPATH.'libraries/User_agent.php'; + +// This class needs some work... + +class UserAgent_test extends CI_TestCase +{ + protected $_user_agent = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_7; en-us) AppleWebKit/533.20.25 (KHTML, like Gecko) Version/5.0.4 Safari/533.20.27'; + protected $_mobile_ua = 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_1 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8B117 Safari/6531.22.7'; + + public function set_up() + { + // set a baseline user agent + $_SERVER['HTTP_USER_AGENT'] = $this->_user_agent; + + $obj = new StdClass; + $obj->agent = new CI_User_agent(); + + $this->ci_instance($obj); + + $this->agent = $obj->agent; + } + + // -------------------------------------------------------------------- + + public function test_accept_lang() + { + $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'en'; + $this->assertTrue($this->agent->accept_lang()); + unset($_SERVER['HTTP_ACCEPT_LANGUAGE']); + $this->assertTrue($this->agent->accept_lang('en')); + $this->assertFalse($this->agent->accept_lang('fr')); + } + + // -------------------------------------------------------------------- + + public function test_mobile() + { + // Mobile Not Set + $_SERVER['HTTP_USER_AGENT'] = $this->_mobile_ua; + $this->assertEquals('', $this->agent->mobile()); + unset($_SERVER['HTTP_USER_AGENT']); + } + + // -------------------------------------------------------------------- + + public function test_util_is_functions() + { + $this->assertTrue($this->agent->is_browser()); + $this->assertFalse($this->agent->is_robot()); + $this->assertFalse($this->agent->is_mobile()); + $this->assertFalse($this->agent->is_referral()); + } + + // -------------------------------------------------------------------- + + public function test_agent_string() + { + $this->assertEquals($this->_user_agent, $this->agent->agent_string()); + } + + // -------------------------------------------------------------------- + + public function test_browser_info() + { + $this->assertEquals('Mac OS X', $this->agent->platform()); + $this->assertEquals('Safari', $this->agent->browser()); + $this->assertEquals('533.20.27', $this->agent->version()); + $this->assertEquals('', $this->agent->robot()); + $this->assertEquals('', $this->agent->referrer()); + } + + // -------------------------------------------------------------------- + + public function test_charsets() + { + $_SERVER['HTTP_ACCEPT_CHARSET'] = 'utf8'; + + $charsets = $this->agent->charsets(); + + $this->assertEquals('utf8', $charsets[0]); + + unset($_SERVER['HTTP_ACCEPT_CHARSET']); + + $this->assertFalse($this->agent->accept_charset()); + } + + // -------------------------------------------------------------------- + +}
\ No newline at end of file |