Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release 9.1.0 #219

Merged
merged 3 commits into from
Apr 14, 2024
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
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,23 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com), and this project adheres to [Semantic Versioning](https://semver.org).

## [9.1.0] - 2024-04-14
### Added
* Add `MEMORY_LIMIT` config option.

### Changed
* *Nothing*

### Deprecated
* *Nothing*

### Removed
* *Nothing*

### Fixed
* *Nothing*


## [9.0.0] - 2024-03-03
### Added
* Add QR code options for foreground color, background color and logo URL.
Expand Down
2 changes: 2 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
// commands
'SERVER' => [
Config\Option\Server\RuntimeConfigOption::class,
'Memory limit' => Config\Option\Server\MemoryLimitConfigOption::class,
],
'DATABASE' => [
'Database > Driver' => Config\Option\Database\DatabaseDriverConfigOption::class,
Expand Down Expand Up @@ -125,6 +126,7 @@

'factories' => [
Config\Option\Server\RuntimeConfigOption::class => InvokableFactory::class,
Config\Option\Server\MemoryLimitConfigOption::class => InvokableFactory::class,
Config\Option\BasePathConfigOption::class => InvokableFactory::class,
Config\Option\TimezoneConfigOption::class => InvokableFactory::class,
Config\Option\Cache\CacheNamespaceConfigOption::class => InvokableFactory::class,
Expand Down
3 changes: 1 addition & 2 deletions src/Config/Option/Redirect/BaseUrlRedirectConfigOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ public function ask(StyleInterface $io, array $currentOptions): ?string
return $io->ask(
'Custom URL to redirect to when a user hits Shlink\'s base URL (If no value is provided, the '
. 'user will see a default "404 not found" page)',
null,
ConfigOptionsValidator::validateUrl(...),
validator: ConfigOptionsValidator::validateUrl(...),
);
}
}
27 changes: 27 additions & 0 deletions src/Config/Option/Server/MemoryLimitConfigOption.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Shlinkio\Shlink\Installer\Config\Option\Server;

use Shlinkio\Shlink\Installer\Config\Option\BaseConfigOption;
use Shlinkio\Shlink\Installer\Config\Util\ConfigOptionsValidator;
use Symfony\Component\Console\Style\StyleInterface;

class MemoryLimitConfigOption extends BaseConfigOption
{
public function getEnvVar(): string
{
return 'MEMORY_LIMIT';
}

public function ask(StyleInterface $io, array $currentOptions): string
{
return $io->ask(
'What is the maximum amount of RAM every process run by Shlink should be allowed to use? (Provide a '
. 'number for bytes, a number followed by K for kilobytes, M for Megabytes or G for Gigabytes)',
'512M',
ConfigOptionsValidator::validateMemoryValue(...),
);
}
}
12 changes: 12 additions & 0 deletions src/Config/Util/ConfigOptionsValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,16 @@ public static function validateHexColor(string $color): string

return $color;
}

public static function validateMemoryValue(string $value): string
{
if (preg_match('/^\d+([KMG])?$/i', $value) === 1) {
return $value;
}

throw new InvalidConfigOptionException(
'Provided value is invalid. It should be an amount in bytes (1024), or a number followed by K, M, or G '
. '(512M, 1G)',
);
}
}
1 change: 0 additions & 1 deletion src/Util/ArrayUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public static function contains(mixed $value, array $array): bool

/**
* @param array[] $multiArray
* @return array
*/
public static function flatten(array $multiArray): array
{
Expand Down
42 changes: 42 additions & 0 deletions test/Config/Option/Server/MemoryLimitConfigOptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace ShlinkioTest\Shlink\Installer\Config\Option\Server;

use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\Installer\Config\Option\Server\MemoryLimitConfigOption;
use Symfony\Component\Console\Style\StyleInterface;

class MemoryLimitConfigOptionTest extends TestCase
{
private MemoryLimitConfigOption $configOption;

public function setUp(): void
{
$this->configOption = new MemoryLimitConfigOption();
}

#[Test]
public function returnsExpectedEnvVar(): void
{
self::assertEquals('MEMORY_LIMIT', $this->configOption->getEnvVar());
}

#[Test]
public function expectedQuestionIsAsked(): void
{
$io = $this->createMock(StyleInterface::class);
$io->expects($this->once())->method('ask')->with(
'What is the maximum amount of RAM every process run by Shlink should be allowed to use? (Provide a '
. 'number for bytes, a number followed by K for kilobytes, M for Megabytes or G for Gigabytes)',
'512M',
$this->anything(),
)->willReturn('1G');

$answer = $this->configOption->ask($io, []);

self::assertEquals('1G', $answer);
}
}
26 changes: 26 additions & 0 deletions test/Config/Util/ConfigOptionsValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\TestWith;
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\Installer\Config\Util\ConfigOptionsValidator;
use Shlinkio\Shlink\Installer\Exception\InvalidConfigOptionException;
Expand Down Expand Up @@ -148,4 +149,29 @@ public static function provideValidColors(): iterable
yield ['#aaaaaa'];
yield ['aaa'];
}

#[Test]
#[TestWith(['2048'])]
#[TestWith(['1G'])]
#[TestWith(['1g'])]
#[TestWith(['1024K'])]
#[TestWith(['1024k'])]
#[TestWith(['256M'])]
#[TestWith(['256m'])]
public function validateMemoryValueReturnsValidValues(string $value): void
{
self::assertEquals($value, ConfigOptionsValidator::validateMemoryValue($value));
}

#[Test]
#[TestWith(['aaaa'])]
#[TestWith(['foo'])]
#[TestWith(['1gb'])]
#[TestWith(['1024KB'])]
#[TestWith(['100.86'])]
public function validateMemoryValueThrowsWhenAnInvalidValueIsProvided(string $invalidValue): void
{
$this->expectException(InvalidConfigOptionException::class);
ConfigOptionsValidator::validateMemoryValue($invalidValue);
}
}
Loading