Helpers refactoring

This commit is contained in:
Frederic Guillot
2015-05-24 16:02:25 -04:00
parent 65e9e5d1be
commit eeac2329ba
239 changed files with 2441 additions and 2337 deletions

View File

@@ -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 &amp; test</div>', $h->flashMessage());
$this->assertEmpty($h->flashMessage());
$this->assertEmpty($h->flashMessage());
$s->flashError('test & test');
$this->assertEquals('<div class="alert alert-error">test &amp; test</div>', $h->flashMessage());
$this->assertEmpty($h->flashMessage());
}
}

View File

@@ -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());
}
}

View File

@@ -0,0 +1,44 @@
<?php
require_once __DIR__.'/Base.php';
use Helper\Datetime;
class DatetimeHelperTest extends Base
{
public function testGetDayHours()
{
$h = new Datetime($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 Datetime($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 Datetime($this->container);
$this->assertEquals('Monday', $h->getWeekDay(1));
$this->assertEquals('Sunday', $h->getWeekDay(7));
}
}

View File

@@ -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'));
}
}

View File

@@ -2,14 +2,13 @@
require_once __DIR__.'/Base.php';
use Core\Helper;
use Model\Config;
use Helper\Text;
class HelperTest extends Base
class TextHelperTest extends Base
{
public function testMarkdown()
{
$h = new Helper($this->container);
$h = new Text($this->container);
$this->assertEquals('<p>Test</p>', $h->markdown('Test'));
@@ -32,19 +31,34 @@ class HelperTest extends Base
);
}
public function testGetCurrentBaseUrl()
public function testFormatBytes()
{
$h = new Helper($this->container);
$h = new Text($this->container);
$_SERVER['PHP_SELF'] = '/';
$_SERVER['SERVER_NAME'] = 'localhost';
$_SERVER['SERVER_PORT'] = 1234;
$this->assertEquals('1k', $h->bytes(1024));
$this->assertEquals('33.71k', $h->bytes(34520));
}
$this->assertEquals('http://localhost:1234/', $h->getCurrentBaseUrl());
public function testTruncate()
{
$h = new Text($this->container);
$c = new Config($this->container);
$c->save(array('application_url' => 'https://mykanboard/'));
$this->assertEquals('https://mykanboard/', $c->get('application_url'));
$this->assertEquals('https://mykanboard/', $h->getCurrentBaseUrl());
$this->assertEquals('abc', $h->truncate('abc'));
$this->assertEquals(str_repeat('a', 85).' [...]', $h->truncate(str_repeat('a', 200)));
}
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')));
}
}

View File

@@ -0,0 +1,64 @@
<?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&amp;action=b&amp;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&amp;action=b&amp;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 testServer()
{
$h = new Url($this->container);
$_SERVER['PHP_SELF'] = '/';
$_SERVER['SERVER_NAME'] = 'localhost';
$_SERVER['SERVER_PORT'] = 1234;
$this->assertEquals('http://localhost:1234/', $h->server());
}
public function testBase()
{
$h = new Url($this->container);
$_SERVER['PHP_SELF'] = '/';
$_SERVER['SERVER_NAME'] = 'localhost';
$_SERVER['SERVER_PORT'] = 1234;
$this->assertEquals('http://localhost:1234/', $h->base());
$c = new Config($this->container);
$c->save(array('application_url' => 'https://mykanboard/'));
$this->assertEquals('https://mykanboard/', $c->get('application_url'));
$this->assertEquals('https://mykanboard/', $h->base());
}
}