Skip to content

Commit

Permalink
added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinpapst committed Mar 16, 2016
1 parent eecf518 commit 21af157
Show file tree
Hide file tree
Showing 2 changed files with 180 additions and 19 deletions.
38 changes: 19 additions & 19 deletions extensions/ki_invoice/private_func.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,25 +123,6 @@ function invoice_get_data($start, $end, $projects, $filter_cleared, $short_form)
return $allEntries;
}

function ext_invoice_empty_entry()
{
return array(
'type' => null,
'desc' => null,
'hour' => null,
'duration' => null,
'amount' => null,
'date' => null,
'description' => null,
'rate' => null,
'comment' => null,
'username' => null,
'useralias' => null,
'location' => null,
'trackingNr' => null
);
}

function invoice_add_to_array(&$array, $row, $short_form)
{
global $activityIndexMap;
Expand Down Expand Up @@ -174,6 +155,25 @@ function invoice_add_to_array(&$array, $row, $short_form)
$array[] = $row;
}

function ext_invoice_empty_entry()
{
return array(
'type' => null,
'desc' => null,
'hour' => null,
'duration' => null,
'amount' => null,
'date' => null,
'description' => null,
'rate' => null,
'comment' => null,
'username' => null,
'useralias' => null,
'location' => null,
'trackingNr' => null
);
}

function ext_invoice_sort_by_date_asc($a, $b)
{
$aTime = $a['timestamp'];
Expand Down
161 changes: 161 additions & 0 deletions tests/extensions/ki_invoice/Ki_Invoice_PrivateFuncTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
<?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;

/**
* Testing functions of invoice extension
*
* @package KimaiTest
*/
class Ki_Invoice_PrivateFuncTest extends PHPUnit_Framework_TestCase
{

protected function setUp()
{
include_once TESTS_BASE_DIR . 'extensions/ki_invoice/private_func.php';
}

public function testext_invoice_empty_entry()
{
$keys = array(
'type', 'desc', 'hour', 'duration', 'amount', 'date', 'description', 'rate', 'comment',
'username', 'useralias', 'location', 'trackingNr'
);

$actual = ext_invoice_empty_entry();

foreach($keys as $key) {
$this->assertArrayHasKey($key, $actual);
}
}

public function testext_invoice_sort_by_date_asc()
{
$actual = ext_invoice_sort_by_date_asc(array('timestamp' => 10), array('timestamp' => 10));
$this->assertEquals(0, $actual);

$actual = ext_invoice_sort_by_date_asc(array('timestamp' => 10), array('timestamp' => 20));
$this->assertEquals(-1, $actual);

$actual = ext_invoice_sort_by_date_asc(array('timestamp' => 20), array('timestamp' => 10));
$this->assertEquals(1, $actual);

$input = array(
0 => array('desc' => '2', 'timestamp' => 20),
1 => array('desc' => '1', 'timestamp' => 10),
2 => array('desc' => '5', 'timestamp' => 50),
3 => array('desc' => '3', 'timestamp' => 30),
4 => array('desc' => '4', 'timestamp' => 40),
);

uasort($input, 'ext_invoice_sort_by_date_asc');

$this->assertEquals(
$input,
array(
1 => array('desc' => '1', 'timestamp' => 10),
0 => array('desc' => '2', 'timestamp' => 20),
3 => array('desc' => '3', 'timestamp' => 30),
4 => array('desc' => '4', 'timestamp' => 40),
2 => array('desc' => '5', 'timestamp' => 50),
)
);
}

public function testext_invoice_sort_by_date_desc()
{
$actual = ext_invoice_sort_by_date_desc(array('timestamp' => 10), array('timestamp' => 10));
$this->assertEquals(0, $actual);

$actual = ext_invoice_sort_by_date_desc(array('timestamp' => 10), array('timestamp' => 20));
$this->assertEquals(1, $actual);

$actual = ext_invoice_sort_by_date_desc(array('timestamp' => 20), array('timestamp' => 10));
$this->assertEquals(-1, $actual);

$input = array(
0 => array('desc' => '2', 'timestamp' => 20),
1 => array('desc' => '1', 'timestamp' => 10),
2 => array('desc' => '5', 'timestamp' => 50),
3 => array('desc' => '3', 'timestamp' => 30),
4 => array('desc' => '4', 'timestamp' => 40),
);

uasort($input, 'ext_invoice_sort_by_date_desc');

$this->assertEquals(
$input,
array(
2 => array('desc' => '5', 'timestamp' => 50),
4 => array('desc' => '4', 'timestamp' => 40),
3 => array('desc' => '3', 'timestamp' => 30),
0 => array('desc' => '2', 'timestamp' => 20),
1 => array('desc' => '1', 'timestamp' => 10),
)
);
}

public function testext_invoice_sort_by_name()
{
$actual = ext_invoice_sort_by_name(array('desc' => 'aaa'), array('desc' => 'aaa'));
$this->assertEquals(0, $actual);

$actual = ext_invoice_sort_by_name(array('desc' => 'aaa'), array('desc' => 'bbb'));
$this->assertEquals(-1, $actual);

$actual = ext_invoice_sort_by_name(array('desc' => 'bbb'), array('desc' => 'aaa'));
$this->assertEquals(1, $actual);

$input = array(
0 => array('desc' => 'b', 'timestamp' => 20),
1 => array('desc' => 'a', 'timestamp' => 10),
2 => array('desc' => 'e', 'timestamp' => 50),
3 => array('desc' => 'c', 'timestamp' => 30),
4 => array('desc' => 'd', 'timestamp' => 40),
);

uasort($input, 'ext_invoice_sort_by_name');

$this->assertEquals(
$input,
array(
2 => array('desc' => 'e', 'timestamp' => 50),
4 => array('desc' => 'd', 'timestamp' => 40),
3 => array('desc' => 'c', 'timestamp' => 30),
0 => array('desc' => 'b', 'timestamp' => 20),
1 => array('desc' => 'a', 'timestamp' => 10),
)
);
}

public function testext_invoice_round_value()
{
$this->assertSame(17.0, ext_invoice_round_value(17, 0.0));
$this->assertSame(22.0, ext_invoice_round_value(22, 0));
$this->assertSame(35.0, ext_invoice_round_value(35.37, 0));
$this->assertSame(45.5, ext_invoice_round_value(45.37, 0.5));
$this->assertSame(55.5, ext_invoice_round_value(55.25, 0.5));
$this->assertSame(65.0, ext_invoice_round_value(65.24, 0.5));
$this->assertSame(64.5, ext_invoice_round_value(65.24, 1.5));
$this->assertSame(67.2, ext_invoice_round_value(66.4, 1.6));
}
}

0 comments on commit 21af157

Please sign in to comment.