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 @@ -212,6 +212,12 @@ public function setTitle(string $title, Foreground $fg = Foreground::BLUE): self
return $this;
}

/** 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 */
private function printTime(): void
{
Expand All @@ -223,7 +229,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
22 changes: 22 additions & 0 deletions tests/Arguments/ArgumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -449,4 +449,26 @@ public function getInfosAllOptions(): void
$oArgument->getInfos()
);
}

#[Test]
public function getValueNotParsed(): void
{
$oArgument = new Argument('test', ['castTo' => 'int', 'defaultValue' => 12]);

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

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

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

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

$this->assertTrue($oArgument->value);
$this->assertTrue($oArgument->hasBeenHandled);
}
}
71 changes: 71 additions & 0 deletions tests/Arguments/OptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace RayanLevert\Cli\Tests\Arguments;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\TestDox;
use RayanLevert\Cli\Arguments\Option;

#[CoversClass(Option::class)]
class OptionTest extends \PHPUnit\Framework\TestCase
{
#[Test]
#[TestDox('Option cases return expected enum and values')]
public function cases(): void
{
$this->assertSame(Option::DESCRIPTION, Option::tryFrom('description'));
$this->assertSame('description', Option::DESCRIPTION->value);

$this->assertSame(Option::REQUIRED, Option::tryFrom('required'));
$this->assertSame('required', Option::REQUIRED->value);

$this->assertSame(Option::NO_VALUE, Option::tryFrom('noValue'));
$this->assertSame('noValue', Option::NO_VALUE->value);

$this->assertSame(Option::PREFIX, Option::tryFrom('prefix'));
$this->assertSame('prefix', Option::PREFIX->value);

$this->assertSame(Option::LONG_PREFIX, Option::tryFrom('longPrefix'));
$this->assertSame('longPrefix', Option::LONG_PREFIX->value);

$this->assertSame(Option::CAST_TO, Option::tryFrom('castTo'));
$this->assertSame('castTo', Option::CAST_TO->value);

$this->assertSame(Option::DEFAULT_VALUE, Option::tryFrom('defaultValue'));
$this->assertSame('defaultValue', Option::DEFAULT_VALUE->value);
}

#[Test]
#[TestDox('verifiesType() returns correct boolean for allowed values')]
public function verifiesType(): void
{
$this->assertTrue(Option::DESCRIPTION->verifiesType('test'));

$this->assertTrue(Option::REQUIRED->verifiesType(true));
$this->assertTrue(Option::REQUIRED->verifiesType(false));

$this->assertTrue(Option::NO_VALUE->verifiesType(false));

$this->assertTrue(Option::PREFIX->verifiesType('test'));
$this->assertTrue(Option::LONG_PREFIX->verifiesType('test'));
$this->assertTrue(Option::CAST_TO->verifiesType('test'));

$this->assertTrue(Option::DEFAULT_VALUE->verifiesType('test'));
$this->assertTrue(Option::DEFAULT_VALUE->verifiesType(12));
$this->assertTrue(Option::DEFAULT_VALUE->verifiesType(12.5));
}

#[Test]
#[TestDox('getPhpProperty() returns expected PHP property names')]
public function getPhpProperty(): void
{
$this->assertSame('description', Option::DESCRIPTION->getPhpProperty());
$this->assertSame('isRequired', Option::REQUIRED->getPhpProperty());
$this->assertSame('noValue', Option::NO_VALUE->getPhpProperty());
$this->assertSame('prefix', Option::PREFIX->getPhpProperty());
$this->assertSame('longPrefix', Option::LONG_PREFIX->getPhpProperty());
$this->assertSame('castTo', Option::CAST_TO->getPhpProperty());
$this->assertSame('defaultValue', Option::DEFAULT_VALUE->getPhpProperty());
}
}
88 changes: 88 additions & 0 deletions tests/ProgressBar/ProgressBarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,94 @@ public function getFormattedTime(): void
$this->assertSame('1h23min20s', ProgressBar::getFormattedTime(5000000));
}

#[Test]
public function withYellowMemoryUsage(): void
{
$oProgressBar = new class(10) extends ProgressBar {
protected function getMemoryUsage(): int
{
return 536870912;
}
};

$oProgressBar->start();
$oProgressBar->advance(7);

$this->assertStringContainsString(
Style::stylize('512 MB', fg: Foreground::YELLOW),
ob_get_contents()
);

ob_clean();
}

#[Test]
public function withRedMemoryUsage(): void
{
$oProgressBar = new class(10) extends ProgressBar {
protected function getMemoryUsage(): int
{
return 1073741824;
}
};

$oProgressBar->start();
$oProgressBar->advance(7);

$this->assertStringContainsString(
Style::stylize('1024 MB', fg: Foreground::RED),
ob_get_contents()
);

ob_clean();
}

#[Test]
public function withTotalTimeYellow(): void
{
$oProgressBar = new class(10) extends ProgressBar {
public function advance(int $toAdvance = 1): void
{
$this->totalTime += 1500;

parent::advance($toAdvance);
}
};

$oProgressBar->start();
$oProgressBar->advance(7);

$this->assertStringContainsString(
Style::stylize('1.5sec', fg: Foreground::YELLOW),
ob_get_contents()
);

ob_clean();
}

#[Test]
public function withTotalTimeRed(): void
{
$oProgressBar = new class(10) extends ProgressBar {
public function advance(int $toAdvance = 1): void
{
$this->totalTime += 2000;

parent::advance($toAdvance);
}
};

$oProgressBar->start();
$oProgressBar->advance(7);

$this->assertStringContainsString(
Style::stylize('2sec', fg: Foreground::RED),
ob_get_contents()
);

ob_clean();
}

/** Include functional.php, does not test anything, only displays progress bars */
#[Test]
public function functional(): void
Expand Down
Loading