Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/ProgressBar.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,12 @@ public function getCurrent(): int
return $this->iteration;
}

/** Returns the memory usage in bytes (`memory_get_usage(true)`) */
protected function getMemoryUsage(): int
{
return memory_get_usage(true);
}

/**
* Displays the total time of the progression and PHP memory on bottom of the progress bar
*/
Expand All @@ -268,7 +274,7 @@ private function printTime(): void
});

// Allocated memory by its usage
$memoryUsage = memory_get_usage(true);
$memoryUsage = $this->getMemoryUsage();
$memory = Style::stylize(self::getFormattedMemory($memoryUsage), fg: match (true) {
$memoryUsage <= 268435456 => Foreground::LIGHT_GREEN, // 256MB
$memoryUsage <= 536870912 => Foreground::YELLOW, // 512MB
Expand Down
130 changes: 74 additions & 56 deletions tests/Arguments/ArgumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@

namespace RayanLevert\Cli\Tests\Arguments;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\TestDox;
use RayanLevert\Cli\Arguments\Argument;
use RayanLevert\Cli\Arguments\Exception;
use RayanLevert\Cli\Arguments\ParseException;

#[CoversClass(Argument::class)]
class ArgumentTest extends \PHPUnit\Framework\TestCase
{
/**
* @test __construct() and exception throws of incompatible options
*/
public function testConstructIncompatiblesOptions(): void
#[Test]
#[TestDox('__construct() and exception throws of incompatible options')]
public function constructIncompatiblesOptions(): void
{
try {
new Argument('testArgument', [
Expand Down Expand Up @@ -62,10 +65,9 @@ public function testConstructIncompatiblesOptions(): void
}
}

/**
* @test __construct() each option with getters
*/
public function testConstructGetters(): void
#[Test]
#[TestDox('__construct() each option with getters')]
public function constructGetters(): void
{
// with no option
$oArgument = new Argument('test');
Expand Down Expand Up @@ -133,10 +135,9 @@ public function testConstructGetters(): void
}
}

/**
* @test option defaultValue
*/
public function testDefaultValue(): void
#[Test]
#[TestDox('option defaultValue')]
public function defaultValue(): void
{
// integer
$oArgument = new Argument('test', ['castTo' => 'int', 'defaultValue' => 10]);
Expand Down Expand Up @@ -234,20 +235,46 @@ public function testDefaultValue(): void
}
}

/**
* @test __construct with an incorrect castTo
*/
public function testCastToNotCorrectType(): void
#[Test]
#[TestDox('__construct with an incorrect castTo')]
public function castToNotCorrectType(): void
{
$this->expectExceptionObject(new Exception('incorrect-type is not a native PHP type'));

new Argument('test', ['castTo' => 'incorrect-type']);
}

/**
* @test ->setValueParsed() with a string argument
*/
public function testParseArgumentString(): void
#[Test]
public function getValueNotParsed(): void
{
$oArgument = new Argument('test', ['castTo' => 'int', 'defaultValue' => 12]);

$this->assertSame(12, $oArgument->getValue());

$oArgument = new Argument('test', ['noValue' => true]);

$this->assertFalse($oArgument->getValue());
}

#[Test]
public function setValueParsedBoolNoValue(): void
{
$oArgument = new Argument('test', ['noValue' => true]);
$oArgument->setValueParsed(true);

$this->assertTrue($oArgument->getValue());
$this->assertTrue($oArgument->hasBeenHandled());

$oArgument = new Argument('test', ['noValue' => true]);
$oArgument->setValueParsed(false);

$this->assertFalse($oArgument->getValue());
$this->assertTrue($oArgument->hasBeenHandled());
}

#[Test]
#[TestDox('->setValueParsed() with a string argument')]
public function parseArgumentString(): void
{
// parses a string argument without castTo => string
$oArgument = new Argument('test');
Expand All @@ -257,10 +284,9 @@ public function testParseArgumentString(): void
$this->assertTrue($oArgument->hasBeenHandled());
}

/**
* @test ->setValueParsed() with an integer argument
*/
public function testParseArgumentInteger(): void
#[Test]
#[TestDox('->setValueParsed() with an integer argument')]
public function parseArgumentInteger(): void
{
// parses a numeric string argument with castTo integer => value int
$oArgument = new Argument('test', ['castTo' => 'integer']);
Expand All @@ -283,10 +309,9 @@ public function testParseArgumentInteger(): void
}
}

/**
* @test ->setValueParsed() with a float argument
*/
public function testParseArgumentFloat(): void
#[Test]
#[TestDox('->setValueParsed() with a float argument')]
public function parseArgumentFloat(): void
{
// parses a integer string with castTo float => value float
$oArgument = new Argument('test', ['castTo' => 'float']);
Expand Down Expand Up @@ -337,28 +362,25 @@ public function testParseArgumentFloat(): void
}
}

/**
* @test ->getInfos() with no option
*/
public function testGetInfosOptionsEmpty(): void
#[Test]
#[TestDox('->getInfos() with no option')]
public function getInfosOptionsEmpty(): void
{
$this->assertSame('test (type: string)', (new Argument('test'))->getInfos());
}

/**
* @test ->getInfos() with required arguments
*/
public function testGetInfosRequired(): void
#[Test]
#[TestDox('->getInfos() with required arguments')]
public function getInfosRequired(): void
{
$this->assertSame('test (type: string)', (new Argument('test'))->getInfos());

$this->assertSame('test (type: double)', (new Argument('test', ['castTo' => 'float']))->getInfos());
}

/**
* @test ->getInfos() with only prefixes
*/
public function testGetInfosWithPrefixes(): void
#[Test]
#[TestDox('->getInfos() with only prefixes')]
public function getInfosWithPrefixes(): void
{
// Only shortPrefix
$oArgument = new Argument('test', ['prefix' => 't']);
Expand All @@ -385,40 +407,36 @@ public function testGetInfosWithPrefixes(): void
$this->assertSame('test -t, --longtest', $oArgument->getInfos());
}

/**
* @test ->getInfos() with a description
*/
public function testGetInfosDescription(): void
#[Test]
#[TestDox('->getInfos() with a description')]
public function getInfosDescription(): void
{
$oArgument = new Argument('test', ['description' => 'Test description']);

$this->assertSame("test (type: string)\n\t Test description", $oArgument->getInfos());
}

/**
* @test ->getInfos() with a default value
*/
public function testGetInfosDefaultValue(): void
#[Test]
#[TestDox('->getInfos() with a default value')]
public function getInfosDefaultValue(): void
{
$oArgument = new Argument('test', ['castTo' => 'float', 'defaultValue' => 56.56]);

$this->assertSame('test (type: double) (default: 56.56)', $oArgument->getInfos());
}

/**
* @test ->getInfos() with a cast type
*/
public function testGetInfosCastTo(): void
#[Test]
#[TestDox('->getInfos() with a cast type')]
public function getInfosCastTo(): void
{
$oArgument = new Argument('test', ['castTo' => 'float']);

$this->assertSame('test (type: double)', $oArgument->getInfos());
}

/**
* @test ->getInfos() with every option mixed
*/
public function testGetInfosAllOptions(): void
#[Test]
#[TestDox('->getInfos() with every option mixed')]
public function getInfosAllOptions(): void
{
// With prefix noValue
$oArgument = new Argument('test', [
Expand Down
Loading
Loading