Skip to content

Commit

Permalink
Make Behat passing with the new directory structure
Browse files Browse the repository at this point in the history
  • Loading branch information
pamil committed Aug 13, 2018
1 parent 2b604c9 commit 14e6221
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 120 deletions.
2 changes: 1 addition & 1 deletion .env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ APP_SECRET=EDITME
# Format described at http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url
# For a sqlite database, use: "sqlite:///%kernel.project_dir%/var/data.db"
# Set "serverVersion" to your server version to avoid edge-case exceptions and extra database calls
DATABASE_URL=mysql:///[email protected]/sylius_${APP_ENV}?serverVersion=5.5
DATABASE_URL=mysql://[email protected]/sylius_${APP_ENV}?serverVersion=5.5
###< doctrine/doctrine-bundle ###

###> symfony/swiftmailer-bundle ###
Expand Down
23 changes: 23 additions & 0 deletions .env.test.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# This file is a "template" of which env vars needs to be defined in your configuration or in an .env file
# Set variables here that may be different on each deployment target of the app, e.g. development, staging, production.
# https://symfony.com/doc/current/best_practices/configuration.html#infrastructure-related-configuration

###> symfony/framework-bundle ###
APP_ENV=test
APP_DEBUG=1
APP_SECRET=EDITME
###< symfony/framework-bundle ###

###> doctrine/doctrine-bundle ###
# Format described at http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url
# For a sqlite database, use: "sqlite:///%kernel.project_dir%/var/data.db"
# Set "serverVersion" to your server version to avoid edge-case exceptions and extra database calls
DATABASE_URL=mysql://[email protected]/sylius_${APP_ENV}?serverVersion=5.5
###< doctrine/doctrine-bundle ###

###> symfony/swiftmailer-bundle ###
# For Gmail as a transport, use: "gmail://username:password@localhost"
# For a generic SMTP server, use: "smtp://localhost:25?encryption=&auth_mode="
# Delivery is disabled by default via "null://localhost"
MAILER_URL=null://localhost
###< symfony/swiftmailer-bundle ###
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ cache:
- $SYLIUS_CACHE_DIR

before_install:
- mv .env.test.dist .env.test
- set -a && source .env.test && set +a

- etc/travis/run-suite before_install "${SYLIUS_SUITE}"

install:
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@
"psr-4": {
"Sylius\\Tests\\": "tests/"
},
"classmap": ["src/Kernel.php", "src/TestKernel.php"]
"classmap": ["src/Kernel.php"]
},
"config": {
"bin-dir": "bin"
Expand Down
4 changes: 2 additions & 2 deletions etc/travis/suites/application/before_script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ run_command "bin/console cache:warmup --env=test_cached --no-debug -vvv" || exit
run_command "bin/console doctrine:migrations:migrate --no-interaction --env=test_cached --no-debug -vvv" || exit $?

print_header "Setting the web assets up" "Sylius"
run_command "bin/console assets:install web --env=test_cached --no-debug -vvv" || exit $?
run_command "bin/console assets:install --env=test_cached --no-debug -vvv" || exit $?
run_command "yarn build" || exit $?

print_header "Making filesystem readonly" "Sylius"
run_command "chmod -R 555 app bin docs features src tests vendor"
run_command "chmod -R 555 app bin config docs features src templates tests vendor"
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ prepare_for_behat_with_js() {
run_command "java -Dwebdriver.chrome.driver=$SYLIUS_CACHE_DIR/chromedriver -jar $SYLIUS_CACHE_DIR/selenium.jar > /dev/null 2>&1 &"

# Run webserver
run_command "bin/console server:run 127.0.0.1:8080 --env=test_cached --docroot=web --router=app/config/router_test_cached.php --no-debug --quiet > /dev/null 2>&1 &"
run_command "bin/console server:run 127.0.0.1:8080 --env=test_cached --no-debug --quiet > /dev/null 2>&1 &"
}

run_behat() {
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<php>
<ini name="error_reporting" value="-1" />

<server name="KERNEL_CLASS" value="TestKernel" />
<server name="KERNEL_CLASS" value="Kernel" />
<server name="APP_ENV" value="test" />
<server name="APP_DEBUG" value="1" />
<server name="APP_SECRET" value="SyLiUsIsAwEsOmE" />
Expand Down
110 changes: 97 additions & 13 deletions src/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@

declare(strict_types=1);

use ProxyManager\Proxy\VirtualProxyInterface;
use PSS\SymfonyMockerContainer\DependencyInjection\MockerContainer;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
use Symfony\Component\Routing\RouteCollectionBuilder;

Expand All @@ -25,44 +28,125 @@ class Kernel extends BaseKernel

private const CONFIG_EXTS = '.{php,xml,yaml,yml}';

private const IGNORED_SERVICES_DURING_CLEANUP = [
'kernel',
'http_kernel',
'liip_imagine.mime_type_guesser',
'liip_imagine.extension_guesser',
];

public function getCacheDir(): string
{
return $this->getProjectDir().'/var/cache/'.$this->environment;
return $this->getProjectDir() . '/var/cache/' . $this->environment;
}

public function getLogDir(): string
{
return $this->getProjectDir().'/var/log';
return $this->getProjectDir() . '/var/log';
}

public function registerBundles(): iterable
{
$contents = require $this->getProjectDir().'/config/bundles.php';
$contents = require $this->getProjectDir() . '/config/bundles.php';
foreach ($contents as $class => $envs) {
if (isset($envs['all']) || isset($envs[$this->environment])) {
yield new $class();
}
}
}

public function shutdown(): void
{
if (!$this->isTestEnvironment()) {
parent::shutdown();

return;
}

if (false === $this->booted) {
return;
}

$container = $this->getContainer();

parent::shutdown();

$this->cleanupContainer($container);
}

protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader): void
{
$container->addResource(new FileResource($this->getProjectDir().'/config/bundles.php'));
$container->addResource(new FileResource($this->getProjectDir() . '/config/bundles.php'));
$container->setParameter('container.dumper.inline_class_loader', true);
$confDir = $this->getProjectDir().'/config';
$confDir = $this->getProjectDir() . '/config';

$loader->load($confDir.'/{packages}/*'.self::CONFIG_EXTS, 'glob');
$loader->load($confDir.'/{packages}/'.$this->environment.'/**/*'.self::CONFIG_EXTS, 'glob');
$loader->load($confDir.'/{services}'.self::CONFIG_EXTS, 'glob');
$loader->load($confDir.'/{services}_'.$this->environment.self::CONFIG_EXTS, 'glob');
$loader->load($confDir . '/{packages}/*' . self::CONFIG_EXTS, 'glob');
$loader->load($confDir . '/{packages}/' . $this->environment . '/**/*' . self::CONFIG_EXTS, 'glob');
$loader->load($confDir . '/{services}' . self::CONFIG_EXTS, 'glob');
$loader->load($confDir . '/{services}_' . $this->environment . self::CONFIG_EXTS, 'glob');
}

protected function configureRoutes(RouteCollectionBuilder $routes): void
{
$confDir = $this->getProjectDir().'/config';
$confDir = $this->getProjectDir() . '/config';

$routes->import($confDir . '/{routes}/*' . self::CONFIG_EXTS, '/', 'glob');
$routes->import($confDir . '/{routes}/' . $this->environment . '/**/*' . self::CONFIG_EXTS, '/', 'glob');
$routes->import($confDir . '/{routes}' . self::CONFIG_EXTS, '/', 'glob');
}

protected function getContainerBaseClass(): string
{
if ($this->isTestEnvironment()) {
return MockerContainer::class;
}

return parent::getContainerBaseClass();
}

private function isTestEnvironment(): bool
{
return 0 === strpos($this->getEnvironment(), 'test');
}

/**
* Remove all container references from all loaded services
*/
private 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, self::IGNORED_SERVICES_DURING_CLEANUP, 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);
}
}

$routes->import($confDir.'/{routes}/*'.self::CONFIG_EXTS, '/', 'glob');
$routes->import($confDir.'/{routes}/'.$this->environment.'/**/*'.self::CONFIG_EXTS, '/', 'glob');
$routes->import($confDir.'/{routes}'.self::CONFIG_EXTS, '/', 'glob');
$containerServicesPropertyReflection->setValue($container, null);
}
}
7 changes: 4 additions & 3 deletions src/Sylius/Behat/Resources/config/profiles/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ default:
show_auto: false

FriendsOfBehat\SymfonyExtension:
env_file: .env.test
kernel:
class: TestAppKernel
path: app/TestAppKernel.php
bootstrap: vendor/autoload.php
class: Kernel
path: src/Kernel.php
bootstrap: ~

FriendsOfBehat\ContextServiceExtension:
imports:
Expand Down
98 changes: 0 additions & 98 deletions src/TestKernel.php

This file was deleted.

0 comments on commit 14e6221

Please sign in to comment.