Skip to content
This repository has been archived by the owner on Aug 20, 2021. It is now read-only.

Commit

Permalink
Merge pull request #123 from lahmann/config_block_chaining_fix
Browse files Browse the repository at this point in the history
Config block chaining fix
  • Loading branch information
acelaya committed Jan 27, 2016
2 parents 1050409 + be6ab4c commit f3c1897
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 14 deletions.
31 changes: 17 additions & 14 deletions src/Options/Factory/MailOptionsAbstractFactory.php
Expand Up @@ -32,21 +32,24 @@ public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $
$specificConfig = [];
}

// Get extends
$extendsConfigKey = isset($specificConfig['extends']) && is_string($specificConfig['extends'])
? trim($specificConfig['extends'])
: null;
// Always unset the extends, in case it had a value null, to prevent the MailOptions object to throw an
// exception
unset($specificConfig['extends']);
do {
// Get extends
$extendsConfigKey = isset($specificConfig['extends']) && is_string($specificConfig['extends'])
? trim($specificConfig['extends'])
: null;

// Try to extend from another configuration if defined and exists
if (! is_null($extendsConfigKey)
&& array_key_exists($extendsConfigKey, $config)
&& is_array($config[$extendsConfigKey])
) {
$specificConfig = ArrayUtils::merge($config[$extendsConfigKey], $specificConfig);
}
// Always unset the extends, in case it had a value null, to prevent the MailOptions object to throw an
// exception
unset($specificConfig['extends']);

// Try to extend from another configuration if defined and exists
if (! is_null($extendsConfigKey)
&& array_key_exists($extendsConfigKey, $config)
&& is_array($config[$extendsConfigKey])
) {
$specificConfig = ArrayUtils::merge($config[$extendsConfigKey], $specificConfig);
}
} while ($extendsConfigKey != null);

return new MailOptions($specificConfig);
}
Expand Down
89 changes: 89 additions & 0 deletions test/Options/MailOptionsAbstractFactoryTest.php
Expand Up @@ -165,6 +165,95 @@ public function testExtendWithValueNullIsIgnored()
$this->assertInstanceOf('AcMailer\Options\MailOptions', $mailOptions);
}

public function testExtendsSingleChaining()
{
$this->serviceLocator = new ServiceManagerMock([
'Config' => [
'acmailer_options' => [
'default' => [
'extends' => null,
'message_options' => [
'to' => 'foo@bar.com'
]
],
'foo' => [
'extends' => 'default',
'message_options' => [
'from' => 'foo@bar.com'
]
]
]
]
]);

/** @var MailOptions $mailOptions */
$mailOptions = $this->mailOptionsFactory->createServiceWithName(
$this->serviceLocator,
'acmailer.mailoptions.foo',
''
);
$this->assertInstanceOf('AcMailer\Options\MailOptions', $mailOptions);
$this->assertEquals(
[
'to' => [['foo@bar.com']],
'from' => 'foo@bar.com',
],
[
'to' => [$mailOptions->getMessageOptions()->getTo()],
'from' => $mailOptions->getMessageOptions()->getFrom(),
]
);
}

public function testExtendsDoubleChaining()
{
$this->serviceLocator = new ServiceManagerMock([
'Config' => [
'acmailer_options' => [
'default' => [
'extends' => null,
'message_options' => [
'to' => 'foo@bar.com',
]
],
'foo' => [
'extends' => 'default',
'message_options' => [
'from' => 'foo@bar.com'
]
],
'bar' => [
'extends' => 'foo',
'message_options' => [
'to' => 'noone@here.com',
'subject' => 'Foobar subject'
]
]
]
]
]);

/** @var MailOptions $mailOptions */
$mailOptions = $this->mailOptionsFactory->createServiceWithName(
$this->serviceLocator,
'acmailer.mailoptions.bar',
''
);
$this->assertInstanceOf('AcMailer\Options\MailOptions', $mailOptions);
$this->assertEquals(
[
'to' => [['noone@here.com']],
'from' => 'foo@bar.com',
'subject' => 'Foobar subject'
],
[
'to' => [$mailOptions->getMessageOptions()->getTo()],
'from' => $mailOptions->getMessageOptions()->getFrom(),
'subject' => $mailOptions->getMessageOptions()->getSubject()
]
);
}

protected function initServiceManager($mailConfigKey = 'acmailer_options', $serviceName = 'default')
{
$services = [
Expand Down

0 comments on commit f3c1897

Please sign in to comment.