Skip to content

Commit

Permalink
Merge pull request #73 from garygitton/feature/issue-71-class-name-re…
Browse files Browse the repository at this point in the history
…solution-problem

issue-71 - class name resolution problem
  • Loading branch information
weierophinney authored Jul 14, 2021
2 parents e967638 + 9db4ac9 commit 478fa13
Show file tree
Hide file tree
Showing 6 changed files with 369 additions and 182 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@
"BazConf\\": "test/Model/TestAsset/module/BazConf/",
"InputFilter\\": "test/Model/TestAsset/module/InputFilter/",
"Version\\": "test/Model/TestAsset/module/Version/src/Version/",
"LaminasTest\\ApiTools\\Admin\\": "test/"
"LaminasTest\\ApiTools\\Admin\\": "test/",
"LaminasIntegrationTest\\ApiTools\\Admin\\": "test/Integration"
}
},
"bin": [
Expand Down
373 changes: 194 additions & 179 deletions config/module.config.php

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

<!-- Paths to check -->
<file>bin</file>
<file>config</file>
<file>src</file>
<file>test</file>
<file>view</file>
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="./vendor/autoload.php"
bootstrap="test/Bootstrap.php"
colors="true"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd">
<coverage processUncoveredFiles="true">
Expand Down
98 changes: 98 additions & 0 deletions test/Bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

declare(strict_types=1);

namespace LaminasTest\ApiTools\Admin;

use Laminas\ModuleManager\ModuleManager;
use Laminas\Mvc\Service\ServiceManagerConfig;
use Laminas\ServiceManager\ServiceManager;

use function date_default_timezone_set;
use function error_reporting;
use function ini_set;
use function is_array;

use const E_ALL;

class Bootstrap
{
/** @var ServiceManager */
protected static $serviceManager;

/** @var bool */
private static $initialized = false;

public static function init(): void
{
if (static::$initialized) {
return;
}

ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
ini_set('log_errors_max_len', '0');
error_reporting(E_ALL);
date_default_timezone_set('UTC');

include 'vendor/autoload.php';

$config = [
'modules' => [
'Laminas\\Filter',
'Laminas\\Validator',
'Laminas\\InputFilter',
'Laminas\\ApiTools\\Admin',
],
'module_listener_options' => [
'config_glob_paths' => [
'./config/autoload/*.php',
],
'module_paths' => [
'./module',
'./vendor',
],
'config_cache_enabled' => false,
'module_map_cache_enabled' => false,
'check_dependencies' => true,
],
];

$serviceManagerConfig = new ServiceManagerConfig();
$serviceManager = new ServiceManager();
$serviceManagerConfig->configureServiceManager($serviceManager);
$serviceManager->setService('ApplicationConfig', $config);
/** @var ModuleManager $moduleManager */
$moduleManager = $serviceManager->get('ModuleManager');
$moduleManager->loadModules();

static::$serviceManager = $serviceManager;
static::$initialized = true;
}

/**
* @return mixed|object
*/
public static function getService(string $name)
{
$serviceManager = self::getServiceManager();

return $serviceManager->get($name);
}

public static function getConfig(): array
{
$serviceManager = self::getServiceManager();
/** @psalm-suppress MixedAssignment */
$config = $serviceManager->get('config');

return is_array($config) ? $config : [];
}

public static function getServiceManager(): ServiceManager
{
self::init();

return static::$serviceManager;
}
}
74 changes: 74 additions & 0 deletions test/Integration/InputFilter/LaminasTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

namespace LaminasIntegrationTest\ApiTools\Admin\InputFilter;

use Exception;
use Laminas\InputFilter\InputFilter;
use Laminas\InputFilter\InputFilterInterface;
use Laminas\InputFilter\InputFilterPluginManager;
use LaminasTest\ApiTools\Admin\Bootstrap;
use PHPUnit\Framework\TestCase;

use function array_keys;
use function is_array;

class LaminasTest extends TestCase
{
/**
* @test
* @throws Exception
*/
public function inputFilterServiceKeyWillReturnInputFilter(): void
{
$inputFilterConfig = $this->getInputFilterAliases();
$inputFilterKeys = array_keys($inputFilterConfig);

/** @var string $key */
foreach ($inputFilterKeys as $key) {
$inputFilter = $this->getInputFilter($key);
$this->assertInstanceOf(InputFilter::class, $inputFilter);
}
}

private function getInputFilterManager(): InputFilterPluginManager
{
$inputFilterManager = Bootstrap::getService(InputFilterPluginManager::class);

if (! $inputFilterManager instanceof InputFilterPluginManager) {
throw new Exception('Invalid class.');
}

return $inputFilterManager;
}

/**
* @throws Exception
*/
private function getInputFilter(string $name): InputFilterInterface
{
$inputFilterManager = $this->getInputFilterManager();
$inputFilter = $inputFilterManager->get($name);

if (! $inputFilter instanceof InputFilterInterface) {
throw new Exception('Invalid class.');
}

return $inputFilter;
}

private function getInputFilterConfig(): array
{
$config = Bootstrap::getConfig();

return is_array($config['input_filters']) ? $config['input_filters'] : [];
}

private function getInputFilterAliases(): array
{
$config = $this->getInputFilterConfig();

return is_array($config['aliases']) ? $config['aliases'] : [];
}
}

0 comments on commit 478fa13

Please sign in to comment.