Skip to content

Commit

Permalink
added some unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinpapst committed Mar 19, 2016
1 parent 17c3bf9 commit 2e04bfa
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 9 deletions.
2 changes: 1 addition & 1 deletion libraries/Kimai/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public static function hourminsec($sek)
* @param string $end
* @return string
*/
public static function addEllipsis($string, $length, $end = '')
public static function addEllipsis($string, $length, $end = '...')
{
if (strlen($string) > $length) {
$length -= strlen($end); // $length = $length - strlen($end);
Expand Down
22 changes: 20 additions & 2 deletions libraries/Kimai/View.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
<?php
/**
* This file is part of
* Kimai - Open Source Time Tracking // http://www.kimai.org
* (c) Kimai-Development-Team since 2006
*
* Kimai is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; Version 3, 29 June 2007
*
* Kimai is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Kimai; If not, see <http://www.gnu.org/licenses/>.
*/

/**
* View object for Kimai.
*
Expand All @@ -10,8 +28,8 @@ public function init()
{
global $kga;

$this->setBasePath(APPLICATION_PATH . '/templates');
$this->addHelperPath(APPLICATION_PATH . '/templates/helpers', 'Zend_View_Helper');
$this->setBasePath(APPLICATION_PATH . '/templates/');
$this->addHelperPath(APPLICATION_PATH . '/templates/helpers/', 'Zend_View_Helper');

parent::init();
$this->kga = $kga;
Expand Down
56 changes: 56 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* This file is part of
* Kimai - Open Source Time Tracking // http://www.kimai.org
* (c) Kimai-Development-Team since 2006
*
* Kimai is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; Version 3, 29 June 2007
*
* Kimai is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Kimai; If not, see <http://www.gnu.org/licenses/>.
*/

namespace KimaiTest;

use PHPUnit_Framework_TestCase;

/**
* Base and helper class for Kimai Unittests.
*/
class TestCase extends PHPUnit_Framework_TestCase
{
/**
* @var array
*/
private $kgaLast;


protected function setKga($kgaNew)
{
global $kga;

if (null !== $kga) {
$this->kgaLast = clone $kga;
}
$kga = $kgaNew;
}

protected function resetKga()
{
if (null === $this->kgaLast) {
return;
}

global $kga;

$kga = $this->kgaLast;
}

}
3 changes: 2 additions & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@
die('You have to execute "composer install" or "composer update" before executing unit tests!');
}
require_once __DIR__ . '/../libraries/autoload.php';
require_once __DIR__ . '/TestCase.php';

define('TESTS_BASE_DIR', realpath(__DIR__.'/../') . '/');
defined('APPLICATION_PATH') || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../'));
76 changes: 76 additions & 0 deletions tests/library/Kimai/FormatTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/**
* This file is part of
* Kimai - Open Source Time Tracking // http://www.kimai.org
* (c) Kimai-Development-Team since 2006
*
* Kimai is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; Version 3, 29 June 2007
*
* Kimai is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Kimai; If not, see <http://www.gnu.org/licenses/>.
*/

namespace KimaiTest;

use Kimai_Format;

/**
* Class UserTest
*
* @coversDefaultClass Kimai_Format
*/
class FormatTest extends TestCase
{

/**
* @covers ::hourminsec
*/
public function testhourminsec()
{
$actual = Kimai_Format::hourminsec(100000);
$this->assertArrayHasKey('h', $actual);
$this->assertArrayHasKey('i', $actual);
$this->assertArrayHasKey('s', $actual);
$this->assertEquals(3, $actual['h']);
$this->assertEquals(46, $actual['i']);
$this->assertEquals(40, $actual['s']);

$actual = Kimai_Format::hourminsec(74159);
$this->assertArrayHasKey('h', $actual);
$this->assertArrayHasKey('i', $actual);
$this->assertArrayHasKey('s', $actual);
$this->assertEquals(20, $actual['h']);
$this->assertEquals(35, $actual['i']);
$this->assertEquals(59, $actual['s']);

$actual = Kimai_Format::hourminsec(1);
$this->assertArrayHasKey('h', $actual);
$this->assertArrayHasKey('i', $actual);
$this->assertArrayHasKey('s', $actual);
$this->assertEquals(0, $actual['h']);
$this->assertEquals(0, $actual['i']);
$this->assertEquals(1, $actual['s']);
}

/**
* @covers ::addEllipsis
*/
public function testaddEllipsis()
{
$actual = Kimai_Format::addEllipsis('HelloWorldThisIsMe', 40);
$this->assertEquals('HelloWorldThisIsMe', $actual);

$actual = Kimai_Format::addEllipsis('HelloWorldThisIsMe', 13);
$this->assertEquals('HelloWorld...', $actual);

$actual = Kimai_Format::addEllipsis('HelloWorldThisIsMe', 13, ' -/');
$this->assertEquals('HelloWorld -/', $actual);
}
}
8 changes: 3 additions & 5 deletions tests/library/Kimai/UserTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<?php
/**
* This file is part of
* Kimai - Open Source Time Tracking
* Kimai - Open Source Time Tracking // http://www.kimai.org
* (c) Kimai-Development-Team since 2006
* http://www.kimai.org
*
* Kimai is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -20,15 +19,14 @@

namespace KimaiTest;

use PHPUnit_Framework_TestCase;
use Kimai_User;

/**
* Class UserTest
*
* @package KimaiTest
* @coversDefaultClass Kimai_User
*/
class UserTest extends PHPUnit_Framework_TestCase
class UserTest extends TestCase
{

public function testIsAdmin()
Expand Down
54 changes: 54 additions & 0 deletions tests/library/Kimai/ViewTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* This file is part of
* Kimai - Open Source Time Tracking // http://www.kimai.org
* (c) Kimai-Development-Team since 2006
*
* Kimai is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; Version 3, 29 June 2007
*
* Kimai is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Kimai; If not, see <http://www.gnu.org/licenses/>.
*/

namespace KimaiTest;

use Kimai_View;

/**
* Class UserTest
*
* @coversDefaultClass Kimai_View
*/
class ViewTest extends TestCase
{

/**
* @covers ::init
*/
public function testInit()
{
$myKga = array('foo' => 'bar');
$this->setKga($myKga);

$view = new Kimai_View();

$helperPaths = $view->getHelperPaths();
$this->assertArrayHasKey('Zend_View_Helper_', $helperPaths);
$this->assertContains(APPLICATION_PATH . '/templates/helpers/', $helperPaths['Zend_View_Helper_']);

$scriptsPaths = $view->getScriptPaths();
$this->assertContains(APPLICATION_PATH . '/templates/scripts/', $scriptsPaths);

$vars = $view->getVars();
$this->assertArrayHasKey('kga', $vars);

$this->assertEquals($myKga, $vars['kga']);
}
}

0 comments on commit 2e04bfa

Please sign in to comment.