diff options
author | Frederic Guillot <fred@kanboard.net> | 2015-09-05 23:30:56 -0400 |
---|---|---|
committer | Frederic Guillot <fred@kanboard.net> | 2015-09-05 23:30:56 -0400 |
commit | 710f2c7bb046b43ec9878ae795a181101f6d7515 (patch) | |
tree | b62723b6b49c3b6bf2b3ca41a772f552464a9031 /tests/units/Helper | |
parent | 94b38dd94bd819168163003beec8ef693f9d9839 (diff) |
Improve unit tests
Diffstat (limited to 'tests/units/Helper')
-rw-r--r-- | tests/units/Helper/AppHelperTest.php | 38 | ||||
-rw-r--r-- | tests/units/Helper/AssetHelperTest.php | 21 | ||||
-rw-r--r-- | tests/units/Helper/DatetimeHelperTest.php | 56 | ||||
-rw-r--r-- | tests/units/Helper/FileHelperText.php | 15 | ||||
-rw-r--r-- | tests/units/Helper/TextHelperTest.php | 56 | ||||
-rw-r--r-- | tests/units/Helper/UrlHelperTest.php | 86 | ||||
-rw-r--r-- | tests/units/Helper/UserHelperTest.php | 16 |
7 files changed, 288 insertions, 0 deletions
diff --git a/tests/units/Helper/AppHelperTest.php b/tests/units/Helper/AppHelperTest.php new file mode 100644 index 00000000..54ccd697 --- /dev/null +++ b/tests/units/Helper/AppHelperTest.php @@ -0,0 +1,38 @@ +<?php + +require_once __DIR__.'/../Base.php'; + +use Core\Session; +use Helper\App; +use Model\Config; + +class AppHelperTest extends Base +{ + public function testJsLang() + { + $h = new App($this->container); + $this->assertEquals('en', $h->jsLang()); + } + + public function testTimezone() + { + $h = new App($this->container); + $this->assertEquals('UTC', $h->getTimezone()); + } + + public function testFlashMessage() + { + $h = new App($this->container); + $s = new Session; + + $this->assertEmpty($h->flashMessage()); + $s->flash('test & test'); + $this->assertEquals('<div class="alert alert-success alert-fade-out">test & test</div>', $h->flashMessage()); + $this->assertEmpty($h->flashMessage()); + + $this->assertEmpty($h->flashMessage()); + $s->flashError('test & test'); + $this->assertEquals('<div class="alert alert-error">test & test</div>', $h->flashMessage()); + $this->assertEmpty($h->flashMessage()); + } +} diff --git a/tests/units/Helper/AssetHelperTest.php b/tests/units/Helper/AssetHelperTest.php new file mode 100644 index 00000000..ece81d04 --- /dev/null +++ b/tests/units/Helper/AssetHelperTest.php @@ -0,0 +1,21 @@ +<?php + +require_once __DIR__.'/../Base.php'; + +use Helper\Asset; +use Model\Config; + +class AssetHelperTest extends Base +{ + public function testCustomCss() + { + $h = new Asset($this->container); + $c = new Config($this->container); + + $this->assertEmpty($h->customCss()); + + $this->assertTrue($c->save(array('application_stylesheet' => 'p { color: red }'))); + + $this->assertEquals('<style>p { color: red }</style>', $h->customCss()); + } +} diff --git a/tests/units/Helper/DatetimeHelperTest.php b/tests/units/Helper/DatetimeHelperTest.php new file mode 100644 index 00000000..d4fc3c3d --- /dev/null +++ b/tests/units/Helper/DatetimeHelperTest.php @@ -0,0 +1,56 @@ +<?php + +require_once __DIR__.'/../Base.php'; + +use Helper\Dt; + +class DatetimeHelperTest extends Base +{ + public function testAge() + { + $h = new Dt($this->container); + + $this->assertEquals('<15m', $h->age(0, 30)); + $this->assertEquals('<30m', $h->age(0, 1000)); + $this->assertEquals('<1h', $h->age(0, 3000)); + $this->assertEquals('~2h', $h->age(0, 2*3600)); + $this->assertEquals('1d', $h->age(0, 30*3600)); + $this->assertEquals('2d', $h->age(0, 65*3600)); + } + + public function testGetDayHours() + { + $h = new Dt($this->container); + + $slots = $h->getDayHours(); + + $this->assertNotEmpty($slots); + $this->assertCount(48, $slots); + $this->assertArrayHasKey('00:00', $slots); + $this->assertArrayHasKey('00:30', $slots); + $this->assertArrayHasKey('01:00', $slots); + $this->assertArrayHasKey('01:30', $slots); + $this->assertArrayHasKey('23:30', $slots); + $this->assertArrayNotHasKey('24:00', $slots); + } + + public function testGetWeekDays() + { + $h = new Dt($this->container); + + $slots = $h->getWeekDays(); + + $this->assertNotEmpty($slots); + $this->assertCount(7, $slots); + $this->assertContains('Monday', $slots); + $this->assertContains('Sunday', $slots); + } + + public function testGetWeekDay() + { + $h = new Dt($this->container); + + $this->assertEquals('Monday', $h->getWeekDay(1)); + $this->assertEquals('Sunday', $h->getWeekDay(7)); + } +} diff --git a/tests/units/Helper/FileHelperText.php b/tests/units/Helper/FileHelperText.php new file mode 100644 index 00000000..cd5ebffd --- /dev/null +++ b/tests/units/Helper/FileHelperText.php @@ -0,0 +1,15 @@ +<?php + +require_once __DIR__.'/../Base.php'; + +use Helper\File; + +class FileHelperTest extends Base +{ + public function testIcon() + { + $h = new File($this->container); + $this->assertEquals('fa-file-image-o', $h->icon('test.png')); + $this->assertEquals('fa-file-o', $h->icon('test')); + } +} diff --git a/tests/units/Helper/TextHelperTest.php b/tests/units/Helper/TextHelperTest.php new file mode 100644 index 00000000..5495cd1c --- /dev/null +++ b/tests/units/Helper/TextHelperTest.php @@ -0,0 +1,56 @@ +<?php + +require_once __DIR__.'/../Base.php'; + +use Helper\Text; + +class TextHelperTest extends Base +{ + public function testMarkdown() + { + $h = new Text($this->container); + + $this->assertEquals('<p>Test</p>', $h->markdown('Test')); + + $this->assertEquals( + '<p>Task #123</p>', + $h->markdown('Task #123') + ); + + $this->assertEquals( + '<p>Task <a href="?controller=a&action=b&c=d&task_id=123">#123</a></p>', + $h->markdown('Task #123', array('controller' => 'a', 'action' => 'b', 'params' => array('c' => 'd'))) + ); + + $this->assertEquals( + '<p>Check that: <a href="http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454">http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454</a></p>', + $h->markdown( + 'Check that: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454', + array('controller' => 'a', 'action' => 'b', 'params' => array('c' => 'd')) + ) + ); + } + + public function testFormatBytes() + { + $h = new Text($this->container); + + $this->assertEquals('1k', $h->bytes(1024)); + $this->assertEquals('33.71k', $h->bytes(34520)); + } + + public function testContains() + { + $h = new Text($this->container); + + $this->assertTrue($h->contains('abc', 'b')); + $this->assertFalse($h->contains('abc', 'd')); + } + + public function testInList() + { + $h = new Text($this->container); + $this->assertEquals('?', $h->in('a', array('b' => 'c'))); + $this->assertEquals('c', $h->in('b', array('b' => 'c'))); + } +} diff --git a/tests/units/Helper/UrlHelperTest.php b/tests/units/Helper/UrlHelperTest.php new file mode 100644 index 00000000..5c2477b5 --- /dev/null +++ b/tests/units/Helper/UrlHelperTest.php @@ -0,0 +1,86 @@ +<?php + +require_once __DIR__.'/../Base.php'; + +use Helper\Url; +use Model\Config; + +class UrlHelperTest extends Base +{ + public function testLink() + { + $h = new Url($this->container); + $this->assertEquals( + '<a href="?controller=a&action=b&d=e" class="f" title="g" target="_blank">label</a>', + $h->link('label', 'a', 'b', array('d' => 'e'), false, 'f', 'g', true) + ); + } + + public function testHref() + { + $h = new Url($this->container); + $this->assertEquals( + '?controller=a&action=b&d=e', + $h->href('a', 'b', array('d' => 'e')) + ); + } + + public function testTo() + { + $h = new Url($this->container); + $this->assertEquals( + '?controller=a&action=b&d=e', + $h->to('a', 'b', array('d' => 'e')) + ); + } + + public function testDir() + { + $h = new Url($this->container); + $this->assertEquals('', $h->dir()); + + $_SERVER['REQUEST_METHOD'] = 'GET'; + $_SERVER['PHP_SELF'] = '/plop/index.php'; + $h = new Url($this->container); + $this->assertEquals('/plop/', $h->dir()); + + $_SERVER['REQUEST_METHOD'] = 'GET'; + $_SERVER['PHP_SELF'] = ''; + $h = new Url($this->container); + $this->assertEquals('/', $h->dir()); + } + + public function testServer() + { + $h = new Url($this->container); + + $this->assertEquals('http://localhost/', $h->server()); + + $_SERVER['PHP_SELF'] = '/'; + $_SERVER['SERVER_NAME'] = 'kb'; + $_SERVER['SERVER_PORT'] = 1234; + + $this->assertEquals('http://kb:1234/', $h->server()); + } + + public function testBase() + { + $h = new Url($this->container); + + $this->assertEquals('http://localhost/', $h->base()); + + $_SERVER['PHP_SELF'] = '/'; + $_SERVER['SERVER_NAME'] = 'kb'; + $_SERVER['SERVER_PORT'] = 1234; + + $h = new Url($this->container); + $this->assertEquals('http://kb:1234/', $h->base()); + + $c = new Config($this->container); + $c->save(array('application_url' => 'https://mykanboard/')); + + $h = new Url($this->container); + $this->assertEquals('https://mykanboard/', $c->get('application_url')); + $this->assertEquals('https://mykanboard/', $h->base()); + } +} diff --git a/tests/units/Helper/UserHelperTest.php b/tests/units/Helper/UserHelperTest.php new file mode 100644 index 00000000..947f606a --- /dev/null +++ b/tests/units/Helper/UserHelperTest.php @@ -0,0 +1,16 @@ +<?php + +require_once __DIR__.'/../Base.php'; + +use Helper\User; + +class UserHelperTest extends Base +{ + public function testInitials() + { + $h = new User($this->container); + + $this->assertEquals('CN', $h->getInitials('chuck norris')); + $this->assertEquals('A', $h->getInitials('admin')); + } +} |