Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimise plugins registration #324

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Ignore files of IDE
.idea

# Ignore potentially sensitive phpunit file
phpunit.xml
# Ignore PHPUnit cache file
.phpunit.result.cache

# Ignore composer generated files
composer.lock
19 changes: 9 additions & 10 deletions src/EightPointsGuzzleBundle.php
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@

class EightPointsGuzzleBundle extends Bundle
{
/** @var \EightPoints\Bundle\GuzzleBundle\PluginInterface[] */
/** @var array<string,\EightPoints\Bundle\GuzzleBundle\PluginInterface> */
protected $plugins = [];

/**
@@ -50,7 +50,7 @@ public function build(ContainerBuilder $container)
public function getContainerExtension() : ExtensionInterface
{
if ($this->extension === null) {
$this->extension = new EightPointsGuzzleExtension($this->plugins);
$this->extension = new EightPointsGuzzleExtension(array_values($this->plugins));
}

return $this->extension;
@@ -75,16 +75,15 @@ public function boot()
*/
protected function registerPlugin(PluginInterface $plugin) : void
{
$pluginName = $plugin->getPluginName();
// Check plugins name duplication
foreach ($this->plugins as $registeredPlugin) {
if ($registeredPlugin->getPluginName() === $plugin->getPluginName()) {
throw new InvalidConfigurationException(sprintf(
'Trying to connect two plugins with same name: %s',
$plugin->getPluginName()
));
}
if (array_key_exists($pluginName, $this->plugins)) {
throw new InvalidConfigurationException(sprintf(
'Trying to connect two plugins with same name: %s',
$pluginName
));
}

$this->plugins[] = $plugin;
$this->plugins[$pluginName] = $plugin;
}
}
4 changes: 2 additions & 2 deletions tests/DataCollector/HttpDataCollectorTest.php
Original file line number Diff line number Diff line change
@@ -341,8 +341,8 @@ public function testTotalTime()
$collector->addTotalTime(3.14);
$this->assertEquals(3.14, $collector->getTotalTime());

$collector->addTotalTime(2.17);
$this->assertEquals(5.31, $collector->getTotalTime());
$collector->addTotalTime(2.18);
$this->assertEquals(5.32, $collector->getTotalTime());
}

/**
22 changes: 21 additions & 1 deletion tests/EightPointsGuzzleBundleTest.php
Original file line number Diff line number Diff line change
@@ -39,13 +39,33 @@ public function testInitWithPluginsNameDuplication()
->willReturn('wsse');

$secondPlugin = $this->getMockBuilder(PluginInterface::class)->getMock();
$secondPlugin->expects($this->exactly(2))
$secondPlugin->expects($this->once())
->method('getPluginName')
->willReturn('wsse');

new EightPointsGuzzleBundle([$firstPlugin, $secondPlugin]);
}

public function testPluginNameAskedOnceOnInit()
{
$firstPlugin = $this->getMockBuilder(PluginInterface::class)->getMock();
$firstPlugin->expects($this->once())
->method('getPluginName')
->willReturn('a');

$secondPlugin = $this->getMockBuilder(PluginInterface::class)->getMock();
$secondPlugin->expects($this->once())
->method('getPluginName')
->willReturn('b');

$thirdPlugin = $this->getMockBuilder(PluginInterface::class)->getMock();
$thirdPlugin->expects($this->once())
->method('getPluginName')
->willReturn('c');

new EightPointsGuzzleBundle([$firstPlugin, $secondPlugin, $thirdPlugin]);
}

public function testBoot()
{
$plugin = $this->getMockBuilder(PluginInterface::class)->getMock();