Passed
Push — main ( b4ea3d...ecbac5 )
by Tim
13:33
created

AbstractUnitTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 17
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 17
rs 10
c 0
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A assertExecutionTimeLessThenOrEqual() 0 7 1
A assertExecutionMemoryLessThenOrEqual() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Lochmueller\LanguageDetection\Tests\Unit;
6
7
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
8
9
abstract class AbstractUnitTest extends UnitTestCase
10
{
11
    public static function assertExecutionTimeLessThenOrEqual(float $timeInSeconds, callable $workload): void
12
    {
13
        $beforeTime = microtime(true);
14
        $workload();
15
        $timeUsage = microtime(true) - $beforeTime;
16
17
        self::assertLessThanOrEqual($timeInSeconds, $timeUsage, 'Execution time of this workload should be less then ' . $timeInSeconds . ' seconds.');
18
    }
19
20
    public static function assertExecutionMemoryLessThenOrEqual(float $memoryInKb, callable $workload): void
21
    {
22
        $beforeMemory = memory_get_usage();
23
        $workload();
24
        $memoryUsage = memory_get_usage() - $beforeMemory;
25
        self::assertLessThanOrEqual(1024 * $memoryInKb, $memoryUsage, 'Execution memory of this workload should be less then ' . $memoryInKb . 'KB.');
26
    }
27
}
28