Skip to content

Commit

Permalink
improvements
Browse files Browse the repository at this point in the history
- requires PHP >= 8.1
- added native property types
- improved CS
  • Loading branch information
petrparolek committed Nov 12, 2024
1 parent d863377 commit a8e7630
Show file tree
Hide file tree
Showing 21 changed files with 127 additions and 132 deletions.
7 changes: 3 additions & 4 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,18 @@ jobs:

strategy:
matrix:
php-version: [ "7.2", "7.3", "7.4", "8.0", "8.1" ]
php-version: [ "8.1", "8.2", "8.3" ]
operating-system: [ "ubuntu-latest" ]
composer-args: [ "" ]
include:
- php-version: "7.2"
- php-version: "8.1"
operating-system: "ubuntu-latest"
composer-args: "--prefer-lowest"
- php-version: "8.0"
- php-version: "8.3"
operating-system: "ubuntu-latest"
composer-args: ""
fail-fast: false


steps:
- name: "Checkout"
uses: "actions/checkout@v2"
Expand Down
21 changes: 14 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@
"type": "library",
"license": "MIT",
"require": {
"php": ">=7.1",
"nette/di": "^2.4.17 || ~3.0",
"nette/tester": "^2.3.1"
"php": ">=8.2",
"nette/bootstrap": "^3.2",
"nette/di": "~3.2",
"nette/tester": "^2.3.1",
"nette/utils": "^4.0"
},
"require-dev": {
"mockery/mockery": "^1.0",
"nette/bootstrap": "^3.0",
"nette/utils": "^3.0",
"ninjify/qa": "^0.13",
"contributte/qa": "^0.3",
"phpstan/phpstan": "^1.0",
"tracy/tracy": "~2.6"
"phpstan/phpstan-deprecation-rules": "^1.0",
"phpstan/phpstan-nette": "^1.0",
"tracy/tracy": "~2.10"
},
"autoload": {
"classmap": [
Expand All @@ -24,5 +26,10 @@
"classmap": [
"tests/src"
]
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
}
22 changes: 7 additions & 15 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
includes:
- vendor/phpstan/phpstan-deprecation-rules/rules.neon
- vendor/phpstan/phpstan-nette/extension.neon
- vendor/phpstan/phpstan-nette/rules.neon

parameters:
level: 8

treatPhpDocTypesAsCertain: false
ignoreErrors:
-
message: "#^Call to an undefined method Nette\\\\DI\\\\Container\\:\\:initialize\\(\\)\\.$#"
count: 1
path: src/InfrastructureConfigurator.php

-
message: "#^Call to an undefined static method Nette\\\\DI\\\\Helpers\\:\\:autowireArguments\\(\\)\\.$#"
count: 1
path: src/MethodArgumentsResolver.php

-
message: "#^Call to function method_exists\\(\\) with 'Nette\\\\\\\\DI\\\\\\\\Helpers' and 'autowireArguments' will always evaluate to false\\.$#"
count: 1
path: src/MethodArgumentsResolver.php

-
message: "#^Parameter \\#3 \\$getter of static method Nette\\\\DI\\\\Resolver\\:\\:autowireArguments\\(\\) expects callable\\(string, bool\\)\\: array\\<object\\>\\|object\\|null, Nette\\\\DI\\\\Container given\\.$#"
count: 1
path: src/MethodArgumentsResolver.php

-
message: "#^Call to an undefined method Nette\\\\DI\\\\Container\\:\\:setAppContainer\\(\\)\\.$#"
count: 1
Expand Down
7 changes: 5 additions & 2 deletions ruleset.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0"?>
<ruleset>
<!-- Contributte Coding Standard -->
<rule ref="./vendor/ninjify/coding-standard/contributte.xml"/>
<rule ref="./vendor/contributte/qa/ruleset.xml"/>

<!-- Specific rules -->
<rule ref="SlevomatCodingStandard.Files.TypeNameMatchesFileName">
Expand All @@ -13,7 +13,10 @@
"/>
<property name="extensions" type="array" value="php,phpt"/>
</properties>
<exclude name="Generic.NamingConventions.CamelCapsFunctionName.ScopeNotCamelCaps" />
<exclude name="SlevomatCodingStandard.Variables.DisallowSuperGlobalVariable.DisallowedSuperGlobalVariable" />
<exclude name="SlevomatCodingStandard.Functions.DisallowEmptyFunction.EmptyFunction" />
<exclude name="SlevomatCodingStandard.Functions.RequireArrowFunction.RequiredArrowFunction" />
<exclude name="SlevomatCodingStandard.Classes.ClassStructure.IncorrectGroupOrder" />
</rule>

<!-- Exclude folders -->
Expand Down
3 changes: 1 addition & 2 deletions src/Bridges/Mockery/MockeryContainerHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
class MockeryContainerHook extends AppContainerHook
{

/** @var TestContext */
private $testContext;
private TestContext $testContext;

public function __construct(TestContext $testContext)
{
Expand Down
7 changes: 3 additions & 4 deletions src/Container/AppContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Webnazakazku\MangoTester\Infrastructure\Container;

use Nette\Configurator;
use Nette\Bootstrap\Configurator;
use Nette\DI\Compiler;
use Nette\DI\Container;
use Nette\DI\ContainerBuilder;
Expand All @@ -11,8 +11,7 @@
class AppContainerFactory
{

/** @var IAppConfiguratorFactory */
private $appConfiguratorFactory;
private IAppConfiguratorFactory $appConfiguratorFactory;

public function __construct(IAppConfiguratorFactory $appConfiguratorFactory)
{
Expand Down Expand Up @@ -45,7 +44,7 @@ protected function setupConfigurator(Container $testContainer, Configurator $app
$compiler->addExtension('mango.tester.beforeCompile', $compilerExtension);
};

$appConfigurator->addParameters([
$appConfigurator->addStaticParameters([
'hookHash' => $hook->getHash(),
'testContainerParameters' => $testContainer->getParameters(),
]);
Expand Down
2 changes: 1 addition & 1 deletion src/Container/AppContainerHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Webnazakazku\MangoTester\Infrastructure\Container;

use Nette\Configurator;
use Nette\Bootstrap\Configurator;
use Nette\DI\Container;
use Nette\DI\ContainerBuilder;

Expand Down
8 changes: 3 additions & 5 deletions src/Container/AppContainerHookList.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

namespace Webnazakazku\MangoTester\Infrastructure\Container;

use Nette\Configurator;
use Nette\Bootstrap\Configurator;
use Nette\DI\Container;
use Nette\DI\ContainerBuilder;

class AppContainerHookList implements IAppContainerHook
{

/** @var IAppContainerHook[] */
private $hooks;
private array $hooks;

/**
* @param IAppContainerHook[] $hooks
Expand All @@ -23,9 +23,7 @@ public function __construct(array $hooks)
public function getHash(): string
{
return md5(serialize(array_map(
static function (IAppContainerHook $hook): string {
return $hook->getHash();
},
static fn (IAppContainerHook $hook): string => $hook->getHash(),
$this->hooks
)));
}
Expand Down
4 changes: 2 additions & 2 deletions src/Container/CompilerHookExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ class CompilerHookExtension extends CompilerExtension
{

/** @var callable[] */
public $onBeforeCompile = [];
public array $onBeforeCompile = [];

public function beforeCompile()
public function beforeCompile(): void
{
foreach ($this->onBeforeCompile as $fn) {
call_user_func($fn, $this->getContainerBuilder());
Expand Down
6 changes: 3 additions & 3 deletions src/Container/ConfigAppContainerHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@

namespace Webnazakazku\MangoTester\Infrastructure\Container;

use Nette\Configurator;
use Nette\Bootstrap\Configurator;
use Nette\DI\Container;
use Nette\DI\ContainerBuilder;

class ConfigAppContainerHook implements IAppContainerHook
{

/** @var array<mixed>|string */
private $config;
private array|string $config;

/**
* @param array<mixed>|string $config
*/
public function __construct($config)
public function __construct(array|string $config)
{
$this->config = $config;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Container/IAppConfiguratorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Webnazakazku\MangoTester\Infrastructure\Container;

use Nette\Configurator;
use Nette\Bootstrap\Configurator;
use Nette\DI\Container;

interface IAppConfiguratorFactory
Expand Down
2 changes: 1 addition & 1 deletion src/Container/IAppContainerHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Webnazakazku\MangoTester\Infrastructure\Container;

use Nette\Configurator;
use Nette\Bootstrap\Configurator;
use Nette\DI\Container;
use Nette\DI\ContainerBuilder;

Expand Down
11 changes: 5 additions & 6 deletions src/DefaultAppConfiguratorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Webnazakazku\MangoTester\Infrastructure;

use Nette\Configurator;
use Nette\Bootstrap\Configurator;
use Nette\DI\Container;
use Nette\DI\Extensions\ExtensionsExtension;
use Webnazakazku\MangoTester\Infrastructure\Container\IAppConfiguratorFactory;
Expand All @@ -19,13 +19,12 @@ class DefaultAppConfiguratorFactory implements IAppConfiguratorFactory
];

/** @var string[] */
private $configFiles;
private array $configFiles;

/** @var string[] */
private $copiedParameters;
private array $copiedParameters;

/** @var bool */
private $defaultExtensionsOverride = true;
private bool $defaultExtensionsOverride = true;

/**
* @param string[] $configFiles
Expand Down Expand Up @@ -58,7 +57,7 @@ public function create(Container $testContainer): Configurator

$parameters = array_intersect_key($params, array_fill_keys($this->copiedParameters, true));

$configurator->addParameters($parameters);
$configurator->addStaticParameters($parameters);
foreach ($this->configFiles as $file) {
$configurator->addConfig($file);
}
Expand Down
38 changes: 22 additions & 16 deletions src/InfrastructureConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,24 @@
namespace Webnazakazku\MangoTester\Infrastructure;

use Closure;
use Nette;
use Nette\DI;
use Nette\DI\Compiler;
use Nette\DI\Config\Loader;
use Nette\DI\Container;
use Nette\DI\ContainerLoader;
use Nette\DI\Extensions\ExtensionsExtension;
use Nette\Schema\Helpers;
use Nette\Utils\FileSystem;
use Tester\Dumper;
use Tester\Environment;

class InfrastructureConfigurator
{

/** @var mixed[] */
protected $parameters;
protected array $parameters;

/** @var array<string|mixed[]> */
protected $configs = [];
protected array $configs = [];

public function __construct(string $path)
{
Expand Down Expand Up @@ -46,7 +51,7 @@ public function setTimeZone(string $timezone): void
*/
public function addParameters(array $params): void
{
$parameters = DI\Config\Helpers::merge($params, $this->parameters);
$parameters = Helpers::merge($params, $this->parameters);
assert(is_array($parameters));

$this->parameters = $parameters;
Expand All @@ -55,17 +60,17 @@ public function addParameters(array $params): void
/**
* @param mixed[]|string $config file or configuration itself
*/
public function addConfig($config): void
public function addConfig(array|string $config): void
{
assert(is_string($config) || is_array($config));
$this->configs[] = $config;
}

public function getContainerFactory(): Closure
{
return function (): DI\Container {
return function (): Container {
$class = $this->loadContainer();
/** @var DI\Container $container */
/** @var Container $container */
$container = new $class([]);
$container->initialize();

Expand All @@ -78,23 +83,22 @@ public function getContainerFactory(): Closure
*/
protected function loadContainer(): string
{
$loader = new DI\ContainerLoader(
$loader = new ContainerLoader(
$this->getCacheDirectory() . '/Mango.Tester.Infrastructure',
$this->parameters['debugMode']
);

return $loader->load(
function (DI\Compiler $compiler) {
return $this->generateContainer($compiler);
},
fn (Compiler $compiler) => $this->generateContainer($compiler),
[$this->parameters, $this->configs, PHP_VERSION_ID - PHP_RELEASE_VERSION]
);
}

protected function generateContainer(DI\Compiler $compiler): string
protected function generateContainer(Compiler $compiler): string
{
$compiler->addConfig(['parameters' => $this->parameters]);

$loader = new DI\Config\Loader();
$loader = new Loader();
$fileInfo = [];
foreach ($this->configs as $config) {
if (is_string($config)) {
Expand All @@ -107,17 +111,19 @@ protected function generateContainer(DI\Compiler $compiler): string

$compiler->addDependencies($loader->getDependencies());

$compiler->addExtension('extensions', new DI\Extensions\ExtensionsExtension());
$compiler->addExtension('extensions', new ExtensionsExtension());
$compiler->addExtension('mango.tester', new MangoTesterExtension());

$classes = $compiler->compile();

return implode("\n", $fileInfo) . "\n\n" . $classes;
}

protected function getCacheDirectory(): string
{
$dir = $this->parameters['tempDir'] . '/cache';
Nette\Utils\FileSystem::createDir($dir);
FileSystem::createDir($dir);

return $dir;
}

Expand Down
Loading

0 comments on commit a8e7630

Please sign in to comment.