Skip to content

Commit

Permalink
Add support for nette\utils:4.0.0 (#115) (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
zeleznypa committed Mar 13, 2023
1 parent 2ab10a7 commit a819298
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 18 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
"php": "^8.0.2",
"latte/latte": "^2.6|^3.0",
"nette/di": "^3.0.6",
"nette/finder": "^2.5.2",
"nette/finder": "^2.5.2|~3.0.0",
"nette/http": "^3.0.7",
"nette/neon": "^3.3.1",
"nette/schema": "^1.0",
"nette/routing": "^3.0",
"nette/utils": "^3.2.1",
"nette/utils": "^3.2.1|~4.0.0",
"symfony/translation": "^6.0",
"symfony/config": "^6.0"
},
Expand All @@ -33,7 +33,7 @@
"nette/application": "^3.1.0",
"nette/bootstrap": "^3.0",
"nette/database": "^3.1.1",
"nette/robot-loader": " ^3.4.0",
"nette/robot-loader": "^3.4.0|~4.0.0",
"nette/tester": "^2.3.1",
"ninjify/nunjuck": "^0.3.0",
"ninjify/qa": "^0.13",
Expand Down
4 changes: 1 addition & 3 deletions src/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Contributte\Translation;

use Nette\Utils\Strings;

class Helpers
{

Expand Down Expand Up @@ -41,7 +39,7 @@ public static function isAbsoluteMessage(
string $message
): bool
{
return Strings::startsWith($message, '//');
return str_starts_with($message, '//');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Latte/TranslatorExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function getTags(): array
public function getFilters(): array
{
return [
'translate' => fn(FilterInfo $fi, ...$args): string => $this->translator->translate(...$args),
'translate' => fn(FilterInfo $fi, ...$args): string => (string) $this->translator->translate(...$args),
];
}

Expand Down
21 changes: 10 additions & 11 deletions tests/Tests/DI/TranslationExtensionTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use Nette\DI\Compiler;
use Nette\DI\CompilerExtension;
use Nette\DI\ContainerLoader;
use Nette\DI\MissingServiceException;
use Nette\InvalidStateException;
use Nette\Localization\ITranslator;
use Nette\Utils\Strings;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -79,12 +80,12 @@ final class TranslationExtensionTest extends TestAbstract
]);
}, InvalidArgument::class, 'Loader must implement interface "' . LoaderInterface::class . '".');
Assert::exception(function (): void {
Helpers::createContainerFromConfigurator($this->container->getParameters()['tempDir'], [
'translation' => [
'dirs' => [__DIR__ . '/__no_exists__'],
],
]);
}, UnexpectedValueException::class);
Helpers::createContainerFromConfigurator($this->container->getParameters()['tempDir'], [
'translation' => [
'dirs' => [__DIR__ . '/__no_exists__'],
],
]);
}, $this->isNewNetteUtils ? InvalidStateException::class : UnexpectedValueException::class);
Assert::exception(function (): void {
Helpers::createContainerFromConfigurator($this->container->getParameters()['tempDir'], [
'translation' => [
Expand Down Expand Up @@ -120,7 +121,7 @@ final class TranslationExtensionTest extends TestAbstract

public function test02(): void
{
try {
$e = Assert::exception(function (): void {
$loader = new ContainerLoader($this->container->getParameters()['tempDir'], true);

$loader->load(function (Compiler $compiler): void {
Expand All @@ -138,10 +139,8 @@ final class TranslationExtensionTest extends TestAbstract
});
$compiler->addConfig(['parameters' => $this->container->getParameters(), 'translation' => ['dirs' => [__DIR__ . '__config_dir__']]]);
});

} catch (UnexpectedValueException $e) {
Assert::true(Strings::contains($e->getMessage(), __DIR__ . '/__translation_provider_dir__'));// translation provider dirs first !!
}
}, $this->isNewNetteUtils ? InvalidStateException::class : UnexpectedValueException::class);
Assert::true(Strings::contains($e->getMessage(), __DIR__ . '/__translation_provider_dir__'));// translation provider dirs first !!
}

public function test03(): void
Expand Down
18 changes: 18 additions & 0 deletions tests/Tests/TestAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,29 @@ abstract class TestAbstract extends TestCase

protected Container $container;

protected bool $isNewNetteUtils;

public function __construct(
Container $container
)
{
if (class_exists('\Composer\InstalledVersions')) { // Composer 2
$netteUtilsVersion = \Composer\InstalledVersions::getPrettyVersion('nette/utils');
} else { // Composer 1
$composerRaw = \Nette\Utils\FileSystem::read(__DIR__ . '/../../composer.lock');
$composerData = \Nette\Utils\Json::decode($composerRaw);
$netteUtilsVersion = '0.0.0';
foreach ($composerData->packages as $package) {
if ($package->name !== 'nette/utils') {
continue;
}

$netteUtilsVersion = ltrim($package->version, 'v');
}
}

$this->container = $container;
$this->isNewNetteUtils = version_compare($netteUtilsVersion, '4.0.0', '>=');
}

}

0 comments on commit a819298

Please sign in to comment.