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

Commit

Permalink
Symfony 4 directory structure in test app
Browse files Browse the repository at this point in the history
  • Loading branch information
bartoszpietrzak1994 committed Nov 8, 2018
1 parent c5d0a2b commit 745b3bb
Show file tree
Hide file tree
Showing 76 changed files with 879 additions and 263 deletions.
22 changes: 13 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ before_install:
- echo "memory_limit=4096M" >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini
- mkdir -p "${SYLIUS_CACHE_DIR}"

- mv .env.test.dist .env.test
- set -a && source .env.test && set +a
- cp tests/Application/.env.test.dist tests/Application/.env.test
- set -a && source tests/Application/.env.test && set +a

install:
- composer require symfony/symfony:${SYMFONY_VERSION} --no-interaction --no-update --no-scripts --update-with-all-dependencies --prefer-dist
Expand All @@ -37,8 +37,12 @@ install:
before_script:
- (cd tests/Application && bin/console doctrine:database:create --env=test -vvv)
- (cd tests/Application && bin/console doctrine:schema:create --env=test -vvv)
- (cd tests/Application && bin/console assets:install web --env=test -vvv)
- (cd tests/Application && yarn run gulp)
- (cd tests/Application && bin/console assets:install --env=test -vvv)
- (cd tests/Application && bin/console cache:warmup --env=test -vvv)
- (cd tests/Application && yarn build)

# Running fixtures to be sure they're not failing
- (cd tests/Application && bin/console sylius:fixtures:load -n --env test)

# Configure display
- /sbin/start-stop-daemon --start --quiet --pidfile /tmp/xvfb_99.pid --make-pidfile --background --exec /usr/bin/Xvfb -- :99 -ac -screen 0 1680x1050x16
Expand Down Expand Up @@ -67,15 +71,15 @@ before_script:
- java -Dwebdriver.chrome.driver=$SYLIUS_CACHE_DIR/chromedriver -jar $SYLIUS_CACHE_DIR/selenium.jar > /dev/null 2>&1 &

# Run webserver
- (cd tests/Application && bin/console server:run 127.0.0.1:8080 -d web --env=test --quiet > /dev/null 2>&1 &)
- (cd tests/Application && bin/console server:run 127.0.0.1:8080 --env=test --quiet > /dev/null 2>&1 &)

script:
- composer validate --strict
- bin/phpstan.phar analyse -c phpstan.neon -l max src/
- composer analyse

- bin/phpunit tests/
- bin/phpspec run
- bin/behat --strict -vvv --no-interaction || bin/behat --strict -vvv --no-interaction --rerun
- vendor/bin/phpunit tests/
- vendor/bin/phpspec run
- vendor/bin/behat --strict -vvv --no-interaction || vendor/bin/behat --strict -vvv --no-interaction --rerun

after_failure:
- vendor/lakion/mink-debug-extension/travis/tools/upload-textfiles "${SYLIUS_BUILD_DIR}/*.log"
9 changes: 6 additions & 3 deletions behat.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ default:
- tests/Behat/Resources/services.xml

FriendsOfBehat\SymfonyExtension:
env_file: tests/Application/.env.test
kernel:
class: AppKernel
path: tests/Application/app/AppKernel.php
bootstrap: vendor/autoload.php
env: test
debug: true
class: Tests\Sylius\CustomerReorderPlugin\Application\Kernel
path: tests/Application/src/Kernel.php
bootstrap: ~

Lakion\Behat\MinkDebugExtension:
directory: etc/build
Expand Down
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@
"Tests\\Sylius\\CustomerReorderPlugin\\": "tests/"
}
},
"autoload-dev": {
"classmap": ["tests/Application/app/AppKernel.php"]
"scripts": {
"analyse": [
"vendor/bin/phpstan.phar analyse -c phpstan.neon -l max src/",
"vendor/bin/ecs check src/ spec/"
]
},
"extra": {
"branch-alias": {
"dev-master": "1.1-dev"
}
},
"config": {
"bin-dir": "bin"
}
}
23 changes: 23 additions & 0 deletions tests/Application/.env.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=dev
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://root@127.0.0.1/sylius_customer_reorder_plugin_%kernel.environment%?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=smtp://localhost
###< symfony/swiftmailer-bundle ###
23 changes: 23 additions & 0 deletions tests/Application/.env.prod.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=prod
APP_DEBUG=0
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://root@127.0.0.1/sylius_customer_reorder_plugin_%kernel.environment%?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=smtp://localhost
###< symfony/swiftmailer-bundle ###
23 changes: 23 additions & 0 deletions tests/Application/.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://root@127.0.0.1/sylius_customer_reorder_plugin_%kernel.environment%?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 ###
22 changes: 15 additions & 7 deletions tests/Application/.gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
/node_modules/

/var/*
!/var/.gitkeep
!/var/.gitignore

/public/assets
/public/bundles
/public/css
/public/js
/public/media

/vendor
/node_modules

/web/*
!/web/app.php
!/web/app_dev.php
!/web/app_test.php
/.env
/.env.prod
/.env.staging
/.env.test
/.env.test_cached
144 changes: 144 additions & 0 deletions tests/Application/Kernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<?php

declare(strict_types=1);

namespace Tests\Sylius\CustomerReorderPlugin\Application;

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;

final class Kernel extends BaseKernel
{
use MicroKernelTrait;

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;
}

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

public function registerBundles(): iterable
{
$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->setParameter('container.dumper.inline_class_loader', true);
$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');
}

protected function configureRoutes(RouteCollectionBuilder $routes): void
{
$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);
}
}

$containerServicesPropertyReflection->setValue($container, null);
}
}
33 changes: 0 additions & 33 deletions tests/Application/app/AppKernel.php

This file was deleted.

Loading

0 comments on commit 745b3bb

Please sign in to comment.