Merge pull request #13321 from nupplaphil/bug/redis

Make InstanceManager strategy naming convention case insensitive.
This commit is contained in:
Hypolite Petovan 2023-07-29 16:04:45 +02:00 committed by GitHub
commit 91effb3576
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 4 deletions

View file

@ -49,11 +49,11 @@ class DiceInstanceManager implements ICanCreateInstances, ICanRegisterStrategies
/** {@inheritDoc} */ /** {@inheritDoc} */
public function registerStrategy(string $interface, string $class, ?string $name = null): ICanRegisterStrategies public function registerStrategy(string $interface, string $class, ?string $name = null): ICanRegisterStrategies
{ {
if (!empty($this->instance[$interface][$name])) { if (!empty($this->instance[$interface][strtolower($name)])) {
throw new HookRegisterArgumentException(sprintf('A class with the name %s is already set for the interface %s', $name, $interface)); throw new HookRegisterArgumentException(sprintf('A class with the name %s is already set for the interface %s', $name, $interface));
} }
$this->instance[$interface][$name] = $class; $this->instance[$interface][strtolower($name)] = $class;
return $this; return $this;
} }
@ -61,10 +61,10 @@ class DiceInstanceManager implements ICanCreateInstances, ICanRegisterStrategies
/** {@inheritDoc} */ /** {@inheritDoc} */
public function create(string $class, string $strategy, array $arguments = []): object public function create(string $class, string $strategy, array $arguments = []): object
{ {
if (empty($this->instance[$class][$strategy])) { if (empty($this->instance[$class][strtolower($strategy)])) {
throw new HookInstanceException(sprintf('The class with the name %s isn\'t registered for the class or interface %s', $strategy, $class)); throw new HookInstanceException(sprintf('The class with the name %s isn\'t registered for the class or interface %s', $strategy, $class));
} }
return $this->dice->create($this->instance[$class][$strategy], $arguments); return $this->dice->create($this->instance[$class][strtolower($strategy)], $arguments);
} }
} }

View file

@ -235,4 +235,24 @@ class InstanceManagerTest extends MockedTest
self::assertEquals($cBool, $getInstanceA->getCBool()); self::assertEquals($cBool, $getInstanceA->getCBool());
self::assertEquals($cBool, $getInstanceB->getCBool()); self::assertEquals($cBool, $getInstanceB->getCBool());
} }
/**
* @see https://github.com/friendica/friendica/issues/13318
*/
public function testCaseInsensitiveNames()
{
$instance = new DiceInstanceManager(new Dice(), $this->hookFileManager);
$instance->registerStrategy(IAmADecoratedInterface::class, FakeInstance::class, 'fake');
// CamelCase
self::assertInstanceOf(FakeInstance::class, $instance->create(IAmADecoratedInterface::class, 'Fake'));
// UPPER CASE
self::assertInstanceOf(FakeInstance::class, $instance->create(IAmADecoratedInterface::class, 'FAKE'));
// lower case
self::assertInstanceOf(FakeInstance::class, $instance->create(IAmADecoratedInterface::class, 'fake'));
// UnKnOwN
self::assertInstanceOf(FakeInstance::class, $instance->create(IAmADecoratedInterface::class, 'fAkE'));
}
} }