additional binding for tests

This commit is contained in:
Yurii
2026-01-29 09:55:53 +02:00
parent ca1020403d
commit 9ac836dc3e
5 changed files with 50 additions and 13 deletions
@@ -29,12 +29,19 @@
namespace Espo\Core\Application;
use Espo\Core\Binding\BindingProcessor;
/**
* @since 9.2.0
*/
readonly class ApplicationParams
{
/**
* @param bool $noErrorHandler Disable error handling for tests.
* @param ?BindingProcessor $binding Additional DI binding for integration tests. Since v9.3.
*/
public function __construct(
public bool $noErrorHandler = false,
public ?BindingProcessor $binding = null,
) {}
}
@@ -37,8 +37,10 @@ class EspoBindingLoader implements BindingLoader
/** @var string[] */
private array $moduleNameList;
public function __construct(Module $module)
{
public function __construct(
Module $module,
private ?BindingProcessor $binding = null,
) {
$this->moduleNameList = $module->getOrderedList();
}
@@ -55,6 +57,8 @@ class EspoBindingLoader implements BindingLoader
$this->loadCustom($binder);
$this->binding?->process($binder);
return $data;
}
@@ -53,7 +53,7 @@ use Espo\Core\Loaders\Metadata as MetadataLoader;
*/
class ContainerBuilder
{
/** @var class-string<ContainerInterface> */
/** @var class-string<ContainerInterface & Container> */
private string $containerClassName = Container::class;
/** @var class-string<Configuration> */
private string $containerConfigurationClassName = ContainerConfiguration::class;
@@ -117,7 +117,7 @@ class ContainerBuilder
}
/**
* @param class-string<ContainerInterface> $containerClassName
* @param class-string<ContainerInterface & Container> $containerClassName
*/
public function withContainerClassName(string $containerClassName): self
{
@@ -207,7 +207,10 @@ class ContainerBuilder
$this->services['systemConfig'] = $systemConfig;
$bindingLoader = $this->bindingLoader ?? (
new EspoBindingLoader($module)
new EspoBindingLoader(
module: $module,
binding: $this->params?->binding,
)
);
$bindingContainer = new BindingContainer($bindingLoader->load());
+13 -3
View File
@@ -32,6 +32,7 @@ namespace tests\integration\Core;
use Espo\Core\Api\RequestWrapper;
use Espo\Core\Api\ResponseWrapper;
use Espo\Core\Application;
use Espo\Core\Binding\BindingProcessor;
use Espo\Core\Container;
use Espo\Core\DataManager;
use Espo\Core\InjectableFactory;
@@ -75,9 +76,18 @@ abstract class BaseTestCase extends TestCase
*/
protected $initData = null;
protected function createApplication(bool $clearCache = true, ?string $portalId = null): Application
{
return $this->espoTester->getApplication(true, $clearCache, $portalId);
protected function createApplication(
bool $clearCache = true,
?string $portalId = null,
?BindingProcessor $binding = null,
): Application {
return $this->espoTester->getApplication(
reload: true,
clearCache: $clearCache,
portalId: $portalId,
binding: $binding,
);
}
protected function setApplication(Application $application): void
+18 -5
View File
@@ -34,6 +34,9 @@ use Espo\Core\Authentication\Authentication;
use Espo\Core\Authentication\AuthenticationData;
use Espo\Core\Application;
use Espo\Core\Binding\BindingProcessor;
use Espo\Core\Exceptions\Forbidden;
use Espo\Core\Exceptions\NotFound;
use Espo\Core\InjectableFactory;
use Espo\Core\ORM\DatabaseParamsFactory;
use Espo\Core\Portal\Application as PortalApplication;
@@ -190,7 +193,8 @@ class Tester
public function getApplication(
bool $reload = false,
bool $clearCache = true,
?string $portalId = null
?string $portalId = null,
?BindingProcessor $binding = null,
): Application {
$portalId = $portalId ?? $this->portalId ?? null;
@@ -200,11 +204,20 @@ class Tester
$this->clearCache();
}
$applicationParams = new Application\ApplicationParams(noErrorHandler: true);
$applicationParams = new Application\ApplicationParams(
noErrorHandler: true,
binding: $binding,
);
$this->application = !$portalId ?
new Application($applicationParams) :
new PortalApplication($portalId, $applicationParams);
if ($portalId) {
try {
$this->application = new PortalApplication($portalId, $applicationParams);
} catch (Forbidden|NotFound $e) {
throw new RuntimeException(previous: $e);
}
} else {
$this->application = new Application($applicationParams);
}
$auth = $this->application
->getContainer()