Skip to content
Snippets Groups Projects
Commit c1158994 authored by Alexey Korepov's avatar Alexey Korepov
Browse files

Issue #3464490 by Murz: Remove calling the parent constructor for LoggerChannelFactoryStub

parent 09549a97
No related branches found
Tags 7.x-1.0-beta1
No related merge requests found
......@@ -62,10 +62,25 @@ class EntityTypeManagerStub extends EntityTypeManager implements EntityTypeManag
'current_user' => NULL,
'entity_bundle.listener' => NULL,
'entity.repository' => NULL,
'entity_type.bundle.info' => NULL,
]);
$entityTypeBundleInfo = TestHelpers::service('entity_type.bundle.info');
$entityTypeRepositoryParams = [
$this,
];
if (version_compare(\Drupal::VERSION, '10.2', '>=')) {
$entityTypeRepositoryParams[] = $entityTypeBundleInfo;
}
TestHelpers::service('typed_data_manager');
TestHelpers::setServices([
'entity_type.repository' => new EntityTypeRepository($this, TestHelpers::service('entity_type.bundle.info')),
// PHPStan shows an error:
// ```
// Class Drupal\Core\Entity\EntityTypeRepository constructor invoked
// with 1 parameter, 2 required.
// ```
// But here we have a workaround for this to keep the module compatible
// with different Drupal versions together.
// @phpstan-ignore-next-line
'entity_type.repository' => new EntityTypeRepository(...$entityTypeRepositoryParams),
'entity.memory_cache' => NULL,
'language_manager' => NULL,
'entity.query.sql' => new EntityQueryServiceStub(),
......@@ -81,16 +96,46 @@ class EntityTypeManagerStub extends EntityTypeManager implements EntityTypeManag
]);
// Calling original constructor with mocked services.
parent::__construct(
$namespaces,
$module_handler,
$cache,
$string_translation,
$class_resolver,
$entity_last_installed_schema_repository,
$container
);
if (version_compare(\Drupal::VERSION, '10.3.0', '<')) {
// PHPStan shows an error:
// ```
// Method Drupal\Core\Entity\EntityTypeManager::__construct() invoked
// with 6 parameters, 7 required.
// ```
// But here we have a workaround for this to keep the module compatible
// with different Drupal versions together.
// @phpstan-ignore-next-line
parent::__construct(
$namespaces,
$module_handler,
$cache,
$string_translation,
$class_resolver,
$entity_last_installed_schema_repository,
);
if (method_exists($this, 'setContainer')) {
// PHPStan shows an error:
// ```
// Call to deprecated method setContainer()
// ```
// But here we have a workaround for this to keep the module compatible
// with different Drupal versions together.
// @phpstan-ignore-next-line
$this->setContainer($container);
}
}
else {
parent::__construct(
$namespaces,
$module_handler,
$cache,
$string_translation,
$class_resolver,
$entity_last_installed_schema_repository,
$container,
);
}
}
/**
......
......@@ -29,11 +29,10 @@ class LoggerChannelFactoryStub extends LoggerChannelFactory {
?RequestStack $requestStack = NULL,
?AccountInterface $currentUser = NULL,
) {
$requestStack ??= TestHelpers::service('request_stack');
$currentUser ??= TestHelpers::service('current_user');
$this->requestStack ??= TestHelpers::service('request_stack');
$this->currentUser ??= TestHelpers::service('current_user');
$this->staticLogger = new StaticLogger();
$this->addLogger($this->staticLogger);
parent::__construct($requestStack, $currentUser);
}
/**
......
......@@ -11,11 +11,6 @@ use PHPUnit\Framework\MockObject\MockObject;
*/
class UnitTestCaseWrapper extends UnitTestCase {
public function __construct(string $name = NULL) {
$name ??= 'UnitTestCaseWrapper';
parent::__construct($name);
}
/**
* {@inheritdoc}
*/
......@@ -137,7 +132,7 @@ class UnitTestCaseWrapper extends UnitTestCase {
public static function getInstance() {
if (!self::$instance) {
$c = get_called_class();
self::$instance = new $c(...func_get_args());
self::$instance = new $c('UnitTestCaseWrapper');
}
return self::$instance;
......
......@@ -9,7 +9,7 @@ use Drupal\test_helpers\TestHelpers;
use Drupal\Tests\UnitTestCase;
/**
* Tests ConfigFactoryStub class.
* Tests LoggerChannelFactoryStub class.
*
* @coversDefaultClass \Drupal\test_helpers\Stub\LoggerChannelFactoryStub
* @group test_helpers
......@@ -42,43 +42,87 @@ class LoggerChannelFactoryStubTest extends UnitTestCase {
$logs = $factory->stubGetLogs();
$this->assertTrue(TestHelpers::isNestedArraySubsetOf($logs[0], [
'uid' => 0,
'type' => 'my_channel1',
'message' => 'My message',
'severity' => 4,
'link' => '',
'location' => TestHelpers::REQUEST_STUB_DEFAULT_URI,
'referer' => '',
'hostname' => '127.0.0.1',
'_context' => [
// Seems the logger returns different results for Drupal 10.3 and earlier.
$log0Expected = version_compare(\Drupal::VERSION, '10.3', '>=')
? [
'uid' => 0,
'uid_custom' => '42',
'channel' => 'my_channel1',
'type' => 'my_channel1',
'message' => 'My message',
'severity' => 4,
'link' => '',
'request_uri' => TestHelpers::REQUEST_STUB_DEFAULT_URI,
'location' => TestHelpers::REQUEST_STUB_DEFAULT_URI,
'referer' => '',
'ip' => '127.0.0.1',
],
]));
$this->assertTrue(TestHelpers::isNestedArraySubsetOf($logs[1], [
'uid' => 2,
'type' => 'my_channel2',
'message' => 'My error',
'severity' => 3,
'link' => '',
'location' => TestHelpers::REQUEST_STUB_DEFAULT_URI,
'referer' => '',
'hostname' => '127.0.0.1',
'_context' => [
'hostname' => '127.0.0.1',
'_context' => [
'uid' => 0,
'uid_custom' => '42',
'channel' => 'my_channel1',
'link' => '',
'request_uri' => TestHelpers::REQUEST_STUB_DEFAULT_URI,
'referer' => '',
'ip' => '127.0.0.1',
],
]
: [
'uid' => '41',
'type' => 'my_channel1',
'message' => 'My message',
'severity' => 4,
'link' => '',
'location' => '',
'referer' => '',
'hostname' => '',
'_context' => [
'uid' => '41',
'uid_custom' => '42',
'channel' => 'my_channel1',
'link' => '',
'request_uri' => '',
'referer' => '',
'ip' => '',
],
];
// Seems the logger returns different results for Drupal 10.3 and earlier.
$this->assertTrue(TestHelpers::isNestedArraySubsetOf($logs[0], $log0Expected));
$log1Expected = version_compare(\Drupal::VERSION, '10.3', '>=')
? [
'uid' => 2,
'channel' => 'my_channel2',
'type' => 'my_channel2',
'message' => 'My error',
'severity' => 3,
'link' => '',
'location' => TestHelpers::REQUEST_STUB_DEFAULT_URI,
'referer' => '',
'hostname' => '127.0.0.1',
'_context' => [
'uid' => 2,
'channel' => 'my_channel2',
'link' => '',
'request_uri' => TestHelpers::REQUEST_STUB_DEFAULT_URI,
'referer' => '',
'ip' => '127.0.0.1',
],
]
: [
'uid' => '53',
'type' => 'my_channel2',
'message' => 'My error',
'severity' => 3,
'link' => '',
'request_uri' => TestHelpers::REQUEST_STUB_DEFAULT_URI,
'location' => '',
'referer' => '',
'ip' => '127.0.0.1',
],
]));
'hostname' => '',
'_context' => [
'uid' => '53',
'channel' => 'my_channel2',
'link' => '',
'request_uri' => '',
'referer' => '',
'ip' => '',
],
];
$this->assertTrue(TestHelpers::isNestedArraySubsetOf($logs[1], $log1Expected));
$this->assertIsNumeric($logs[0]["timestamp"]);
$this->assertIsNumeric($logs[1]["_context"]["timestamp"]);
$this->assertGreaterThan($logs[0]["_microtime"], $logs[1]["_microtime"]);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment