-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #215 from pvgnd/services-reset
Add ServicesResetterProcessor (closes #193)
- Loading branch information
Showing
6 changed files
with
101 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# ServicesResetter Processor | ||
|
||
ServicesResetterProcessor is a [swarrot](https://github.com/swarrot/swarrot) processor. | ||
|
||
It resets services from Symfony Container after the processor ran to avoid memory leaks (clear Monolog logger for example). |
43 changes: 43 additions & 0 deletions
43
src/Swarrot/Processor/ServicesResetter/ServicesResetterProcessor.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Swarrot\Processor\ServicesResetter; | ||
|
||
use Swarrot\Broker\Message; | ||
use Swarrot\Processor\ProcessorInterface; | ||
use Symfony\Contracts\Service\ResetInterface; | ||
|
||
/** | ||
* @author Pierrick Vignand <pierrick.vignand@gmail.com> | ||
*/ | ||
class ServicesResetterProcessor implements ProcessorInterface | ||
{ | ||
/** | ||
* @var ProcessorInterface | ||
*/ | ||
private $processor; | ||
|
||
/** | ||
* @var ResetInterface | ||
*/ | ||
private $servicesResetter; | ||
|
||
public function __construct(ProcessorInterface $processor, ResetInterface $servicesResetter) | ||
{ | ||
$this->processor = $processor; | ||
$this->servicesResetter = $servicesResetter; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function process(Message $message, array $options) | ||
{ | ||
try { | ||
return $this->processor->process($message, $options); | ||
} finally { | ||
$this->servicesResetter->reset(); | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
tests/Swarrot/Processor/ServicesResetter/ServicesResetterProcessorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace Swarrot\Tests\Processor\ServicesResetter; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Swarrot\Broker\Message; | ||
use Swarrot\Processor\ProcessorInterface; | ||
use Swarrot\Processor\ServicesResetter\ServicesResetterProcessor; | ||
use Symfony\Contracts\Service\ResetInterface; | ||
|
||
class ServicesResetterProcessorTest extends TestCase | ||
{ | ||
public function test() | ||
{ | ||
$message = new Message(); | ||
$options = []; | ||
|
||
$innerProcessorProphecy = $this->prophesize(ProcessorInterface::class); | ||
$innerProcessorProphecy->process($message, $options)->willReturn(true); | ||
|
||
$serviceResetterProphecy = $this->prophesize(ResetInterface::class); | ||
$serviceResetterProphecy->reset()->shouldBeCalled(); | ||
|
||
$processor = new ServicesResetterProcessor($innerProcessorProphecy->reveal(), $serviceResetterProphecy->reveal()); | ||
|
||
$this->assertEquals($processor->process($message, $options), true); | ||
} | ||
|
||
public function testWithException() | ||
{ | ||
$message = new Message(); | ||
$options = []; | ||
|
||
$innerProcessorProphecy = $this->prophesize(ProcessorInterface::class); | ||
$innerProcessorProphecy->process($message, $options)->willThrow(new \Exception('my_fake_message')); | ||
|
||
$serviceResetterProphecy = $this->prophesize(ResetInterface::class); | ||
$serviceResetterProphecy->reset()->shouldBeCalled(); | ||
|
||
$processor = new ServicesResetterProcessor($innerProcessorProphecy->reveal(), $serviceResetterProphecy->reveal()); | ||
|
||
$this->expectException(\Exception::class); | ||
$this->expectExceptionMessage('my_fake_message'); | ||
|
||
$processor->process($message, $options); | ||
} | ||
} |