From 0d8a4ffbdd027079225ba986a6199bf668ea16ee Mon Sep 17 00:00:00 2001 From: crynobone Date: Tue, 6 Nov 2012 22:33:29 +0800 Subject: [PATCH] Add unit testing coverage for Laravel\HTML and improve setUp and tearDown for Form unit-test Signed-off-by: crynobone --- laravel/html.php | 2 + laravel/tests/cases/form.test.php | 9 ++ laravel/tests/cases/html.test.php | 242 ++++++++++++++++++++++++++++++ laravel/tests/cases/url.test.php | 1 + 4 files changed, 254 insertions(+) create mode 100644 laravel/tests/cases/html.test.php diff --git a/laravel/html.php b/laravel/html.php index 211e4022..33d91dd6 100644 --- a/laravel/html.php +++ b/laravel/html.php @@ -173,6 +173,8 @@ public static function link_to_secure($url, $title = null, $attributes = array() public static function link_to_asset($url, $title = null, $attributes = array(), $https = null) { $url = URL::to_asset($url, $https); + + if (is_null($title)) $title = $url; return ''.static::entities($title).''; } diff --git a/laravel/tests/cases/form.test.php b/laravel/tests/cases/form.test.php index 266b74b5..5f6f7fa3 100644 --- a/laravel/tests/cases/form.test.php +++ b/laravel/tests/cases/form.test.php @@ -9,6 +9,15 @@ public function setUp() { URL::$base = null; Config::set('application.url', 'http://localhost'); + Config::set('application.index', 'index.php'); + } + /** + * Destroy the test enviornment. + */ + public function tearDown() + { + Config::set('application.url', ''); + Config::set('application.index', 'index.php'); } /** diff --git a/laravel/tests/cases/html.test.php b/laravel/tests/cases/html.test.php new file mode 100644 index 00000000..3af4efde --- /dev/null +++ b/laravel/tests/cases/html.test.php @@ -0,0 +1,242 @@ + 'text/javascript')); + + $this->assertEquals(''."\n", $html1); + $this->assertEquals(''."\n", $html2); + $this->assertEquals(''."\n", $html3); + } + + /** + * Test generating a link to CSS files + * + * @group laravel + */ + public function testGeneratingStyle() + { + $html1 = HTML::style('foo.css'); + $html2 = HTML::style('http://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.1/js/bootstrap.min.js'); + $html3 = HTML::style('foo.css', array('media' => 'print')); + + $this->assertEquals(''."\n", $html1); + $this->assertEquals(''."\n", $html2); + $this->assertEquals(''."\n", $html3); + } + + /** + * Test generating proper span + * + * @group laravel + */ + public function testGeneratingSpan() + { + $html1 = HTML::span('foo'); + $html2 = HTML::span('foo', array('class' => 'badge')); + + $this->assertEquals('foo', $html1); + $this->assertEquals('foo', $html2); + } + + /** + * Test generating proper link + * + * @group laravel + */ + public function testGeneratingLink() + { + $html1 = HTML::link('foo'); + $html2 = HTML::link('foo', 'Foobar'); + $html3 = HTML::link('foo', 'Foobar', array('class' => 'btn')); + $html4 = HTML::link('http://google.com', 'Google'); + + $this->assertEquals('http://localhost/index.php/foo', $html1); + $this->assertEquals('Foobar', $html2); + $this->assertEquals('Foobar', $html3); + $this->assertEquals('Google', $html4); + } + + /** + * Test generating proper link to secure + * + * @group laravel + */ + public function testGeneratingLinkToSecure() + { + $html1 = HTML::link_to_secure('foo'); + $html2 = HTML::link_to_secure('foo', 'Foobar'); + $html3 = HTML::link_to_secure('foo', 'Foobar', array('class' => 'btn')); + $html4 = HTML::link_to_secure('http://google.com', 'Google'); + + $this->assertEquals('https://localhost/index.php/foo', $html1); + $this->assertEquals('Foobar', $html2); + $this->assertEquals('Foobar', $html3); + $this->assertEquals('Google', $html4); + } + + /** + * Test generating proper link to asset + * + * @group laravel + */ + public function testGeneratingAssetLink() + { + $html1 = HTML::link_to_asset('foo.css'); + $html2 = HTML::link_to_asset('foo.css', 'Foobar'); + $html3 = HTML::link_to_asset('foo.css', 'Foobar', array('class' => 'btn')); + $html4 = HTML::link_to_asset('http://google.com/images.jpg', 'Google'); + + $this->assertEquals('http://localhost/foo.css', $html1); + $this->assertEquals('Foobar', $html2); + $this->assertEquals('Foobar', $html3); + $this->assertEquals('Google', $html4); + } + + /** + * Test generating proper link to secure asset + * + * @group laravel + */ + public function testGeneratingAssetLinkToSecure() + { + $html1 = HTML::link_to_secure_asset('foo.css'); + $html2 = HTML::link_to_secure_asset('foo.css', 'Foobar'); + $html3 = HTML::link_to_secure_asset('foo.css', 'Foobar', array('class' => 'btn')); + $html4 = HTML::link_to_secure_asset('http://google.com/images.jpg', 'Google'); + + $this->assertEquals('https://localhost/foo.css', $html1); + $this->assertEquals('Foobar', $html2); + $this->assertEquals('Foobar', $html3); + $this->assertEquals('Google', $html4); + } + + /** + * Test generating proper link to route + * + * @group laravel + */ + public function testGeneratingLinkToRoute() + { + Route::get('dashboard', array('as' => 'foo')); + + $html1 = HTML::link_to_route('foo'); + $html2 = HTML::link_to_route('foo', 'Foobar'); + $html3 = HTML::link_to_route('foo', 'Foobar', array(), array('class' => 'btn')); + + $this->assertEquals('http://localhost/index.php/dashboard', $html1); + $this->assertEquals('Foobar', $html2); + $this->assertEquals('Foobar', $html3); + } + + /** + * Test generating proper link to action + * + * @group laravel + */ + public function testGeneratingLinkToAction() + { + $html1 = HTML::link_to_action('foo@bar'); + $html2 = HTML::link_to_action('foo@bar', 'Foobar'); + $html3 = HTML::link_to_action('foo@bar', 'Foobar', array(), array('class' => 'btn')); + + $this->assertEquals('http://localhost/index.php/foo/bar', $html1); + $this->assertEquals('Foobar', $html2); + $this->assertEquals('Foobar', $html3); + } + + /** + * Test generating proper listing + * + * @group laravel + */ + public function testGeneratingListing() + { + $list = array( + 'foo', + 'foobar' => array( + 'hello', + 'hello world', + ), + ); + + $html1 = HTML::ul($list); + $html2 = HTML::ul($list, array('class' => 'nav')); + $html3 = HTML::ol($list); + $html4 = HTML::ol($list, array('class' => 'nav')); + + $this->assertEquals('', $html1); + $this->assertEquals('', $html2); + $this->assertEquals('
  1. foo
  2. foobar
    1. hello
    2. hello world
', $html3); + $this->assertEquals('', $html4); + } + + /** + * Test generating proper listing + * + * @group laravel + */ + public function testGeneratingDefinition() + { + $definition = array( + 'foo' => 'foobar', + 'hello' => 'hello world', + ); + + $html1 = HTML::dl($definition); + $html2 = HTML::dl($definition, array('class' => 'nav')); + + $this->assertEquals('
foo
foobar
hello
hello world
', $html1); + $this->assertEquals('', $html2); + } + + /** + * Test generating proper image link + * + * @group laravel + */ + public function testGeneratingAssetLinkImage() + { + $html1 = HTML::image('foo.jpg'); + $html2 = HTML::image('foo.jpg', 'Foobar'); + $html3 = HTML::image('foo.jpg', 'Foobar', array('class' => 'btn')); + $html4 = HTML::image('http://google.com/images.jpg', 'Google'); + + $this->assertEquals('', $html1); + $this->assertEquals('Foobar', $html2); + $this->assertEquals('Foobar', $html3); + $this->assertEquals('Google', $html4); + } +} \ No newline at end of file diff --git a/laravel/tests/cases/url.test.php b/laravel/tests/cases/url.test.php index 603265ab..3bf37b40 100644 --- a/laravel/tests/cases/url.test.php +++ b/laravel/tests/cases/url.test.php @@ -15,6 +15,7 @@ public function setUp() Router::$uses = array(); Router::$fallback = array(); Config::set('application.url', 'http://localhost'); + Config::set('application.index', 'index.php'); } /**