Skip to content

Commit

Permalink
refactor #315 Remove container cleanup in test environment (1.2) (Zal…
Browse files Browse the repository at this point in the history
…es0123)

This PR was merged into the 1.2 branch.

Discussion
----------

changes incorporated from Sylius/Sylius#10067

Commits
-------

48bf83c Remove container cleanup in test environment
  • Loading branch information
pamil authored Jan 11, 2019
2 parents 058e3ed + 48bf83c commit 22dbc5b
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 76 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ env:
- SYLIUS_BUILD_DIR=etc/build
matrix:
- SYMFONY_VERSION="3.4.*"
- SYMFONY_VERSION="4.1.*"
- SYMFONY_VERSION="4.2.*"

services:
- memcached
Expand Down
75 changes: 0 additions & 75 deletions app/TestAppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,87 +13,12 @@

require_once __DIR__.'/AppKernel.php';

use ProxyManager\Proxy\VirtualProxyInterface;
use PSS\SymfonyMockerContainer\DependencyInjection\MockerContainer;
use Symfony\Component\DependencyInjection\ContainerInterface;

class TestAppKernel extends AppKernel
{
/**
* {@inheritdoc}
*/
public function shutdown(): void
{
if (false === $this->booted) {
return;
}

if (!in_array($this->getEnvironment(), ['test', 'test_cached'], true)) {
parent::shutdown();

return;
}

$container = $this->getContainer();
parent::shutdown();
$this->cleanupContainer($container);
}

/**
* Remove all container references from all loaded services.
*
* @param ContainerInterface $container
*/
protected function cleanupContainer(ContainerInterface $container): void
{
$containerReflection = new \ReflectionObject($container);
$containerServicesPropertyReflection = $containerReflection->getProperty('services');
$containerServicesPropertyReflection->setAccessible(true);

$services = $containerServicesPropertyReflection->getValue($container) ?: [];
foreach ($services as $serviceId => $service) {
if (null === $service) {
continue;
}

if (in_array($serviceId, $this->getServicesToIgnoreDuringContainerCleanup(), true)) {
continue;
}

$serviceReflection = new \ReflectionObject($service);

if ($serviceReflection->implementsInterface(VirtualProxyInterface::class)) {
continue;
}

$servicePropertiesReflections = $serviceReflection->getProperties();
$servicePropertiesDefaultValues = $serviceReflection->getDefaultProperties();
foreach ($servicePropertiesReflections as $servicePropertyReflection) {
$defaultPropertyValue = null;
if (isset($servicePropertiesDefaultValues[$servicePropertyReflection->getName()])) {
$defaultPropertyValue = $servicePropertiesDefaultValues[$servicePropertyReflection->getName()];
}

$servicePropertyReflection->setAccessible(true);
$servicePropertyReflection->setValue($service, $defaultPropertyValue);
}
}

$containerServicesPropertyReflection->setValue($container, null);
}

protected function getContainerBaseClass(): string
{
return MockerContainer::class;
}

protected function getServicesToIgnoreDuringContainerCleanup(): array
{
return [
'kernel',
'http_kernel',
'liip_imagine.mime_type_guesser',
'liip_imagine.extension_guesser',
];
}
}

0 comments on commit 22dbc5b

Please sign in to comment.