Skip to content

Commit

Permalink
Removed custom env code in favour of Behat feature (#277)
Browse files Browse the repository at this point in the history
* Removed parts of custom env handling code

* Fixed failing test
  • Loading branch information
mnocon authored Aug 17, 2022
1 parent b93437c commit 3329636
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 90 deletions.
11 changes: 4 additions & 7 deletions behat_ibexa_oss.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ default:
extensions:
Behat\MinkExtension:
files_path: '%paths.base%/vendor/ezsystems/ezplatform-admin-ui/src/lib/Behat/TestFiles/'
base_url: 'http://localhost'
base_url: '%env(string:WEB_HOST)%'
goutte: ~
javascript_session: selenium
javascript_session: 'selenium'
sessions:
selenium:
selenium2:
browser: chrome
wd_host: 'http://localhost:4444/wd/hub'
wd_host: '%env(string:SELENIUM_HOST)%'
capabilities:
extra_capabilities:
chromeOptions:
Expand All @@ -37,7 +37,7 @@ default:
- "--disable-features=site-per-process"
chrome:
chrome:
api_url: "http://localhost:9222"
api_url: '%env(string:CHROMIUM_HOST)%'

DMore\ChromeExtension\Behat\ServiceContainer\ChromeExtension: ~

Expand All @@ -46,10 +46,7 @@ default:

EzSystems\BehatBundle\BehatExtension:
mink:
base_url: '%env(string:WEB_HOST)%'
default_javascript_session: '%env(string:MINK_DEFAULT_SESSION)%'
selenium_webdriver_host: '%env(string:SELENIUM_HOST)%'
chrome_api_url: '%env(string:CHROMIUM_HOST)%'

Liuggio\Fastest\Behat\ListFeaturesExtension\Extension: ~

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"require": {
"php": "^7.3 || ^8.0",
"ext-json": "*",
"behat/behat": "^3.8",
"behat/behat": "^3.11",
"behat/mink-goutte-driver": "^1.2",
"behat/mink-selenium2-driver": "^1.4",
"bex/behat-screenshot": "^2.1",
Expand Down
2 changes: 1 addition & 1 deletion features/examples/configuration.feature
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ Feature: Example scenarios showing how to set configuration
"""
And I append configuration to "default" siteaccess under "http_cache" key
"""
purge_servers: ['http://my_purge_server']
purge_servers: ['http://web']
"""
81 changes: 0 additions & 81 deletions src/bundle/BehatExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,55 +9,30 @@
namespace EzSystems\BehatBundle;

use Behat\Behat\EventDispatcher\ServiceContainer\EventDispatcherExtension;
use Behat\Mink\Driver\Selenium2Driver;
use Behat\Testwork\ServiceContainer\Extension;
use Behat\Testwork\ServiceContainer\ExtensionManager;
use DMore\ChromeDriver\ChromeDriver;
use EzSystems\BehatBundle\Initializer\BehatSiteAccessInitializer;
use FriendsOfBehat\SymfonyExtension\ServiceContainer\SymfonyExtension;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\DependencyInjection\Reference;

class BehatExtension implements Extension
{
private const MINK_BASE_URL_PARAMETER = 'ibexa.platform.behat.mink.base_url';

private const MINK_DEFAULT_JAVASCRIPT_SESSION_PARAMETER = 'ibexa.platform.behat.mink.default_javascript_session';

private const MINK_SELENIUM_WEBDRIVER_HOST = 'ibexa.platform.behat.mink.selenium.webdriver_host';

private const MINK_CHROME_API_URL = 'ibexa.platform.behat.mink.chrome.api_url';

public function getConfigKey()
{
return 'ezbehatextension';
}

public function process(ContainerBuilder $container)
{
if ($container->hasParameter(self::MINK_BASE_URL_PARAMETER)) {
$baseUrl = $container->getParameter(self::MINK_BASE_URL_PARAMETER);
$this->setBaseUrl($container, $baseUrl);
}

if ($container->hasParameter(self::MINK_DEFAULT_JAVASCRIPT_SESSION_PARAMETER)) {
$defaultJavascriptSession = $container->getParameter(self::MINK_DEFAULT_JAVASCRIPT_SESSION_PARAMETER);
$this->setDefaultJavascriptSession($container, $defaultJavascriptSession);
}

if ($container->hasParameter(self::MINK_SELENIUM_WEBDRIVER_HOST)) {
$seleniumWebdriverHost = $container->getParameter(self::MINK_SELENIUM_WEBDRIVER_HOST);
$this->setSeleniumWebdriverHost($container, $seleniumWebdriverHost);
}

if ($container->hasParameter(self::MINK_CHROME_API_URL)) {
$chromeApiUrl = $container->getParameter(self::MINK_CHROME_API_URL);
$this->setChromeApiUrl($container, $chromeApiUrl);
}
}

public function initialize(ExtensionManager $extensionManager)
Expand All @@ -70,22 +45,14 @@ public function configure(ArrayNodeDefinition $builder)
->children()
->arrayNode('mink')
->children()
->scalarNode('base_url')->defaultNull()->end()
->scalarNode('default_javascript_session')->defaultNull()->end()
->scalarNode('selenium_webdriver_host')->defaultNull()->end()
->scalarNode('chrome_api_url')->defaultNull()->end()
->end()
->end()
->end();
}

public function load(ContainerBuilder $container, array $config)
{
$loader = new YamlFileLoader(
$container,
new FileLocator(__DIR__ . '/Resources/config')
);

$this->loadSiteAccessInitializer($container);
$this->setMinkParameters($container, $config);
}
Expand All @@ -107,10 +74,7 @@ private function setMinkParameters(ContainerBuilder $container, array $config):
}

$keyParameterMap = [
'base_url' => self::MINK_BASE_URL_PARAMETER,
'default_javascript_session' => self::MINK_DEFAULT_JAVASCRIPT_SESSION_PARAMETER,
'selenium_webdriver_host' => self::MINK_SELENIUM_WEBDRIVER_HOST,
'chrome_api_url' => self::MINK_CHROME_API_URL,
];

foreach ($keyParameterMap as $key => $parameter) {
Expand All @@ -121,53 +85,8 @@ private function setMinkParameters(ContainerBuilder $container, array $config):
}
}

private function setBaseUrl(ContainerBuilder $container, string $baseUrl): void
{
$container->setParameter('mink.base_url', $baseUrl);
$parameters = $container->getParameter('mink.parameters');
$parameters['base_url'] = $baseUrl;
$container->setParameter('mink.parameters', $parameters);
}

private function setDefaultJavascriptSession(ContainerBuilder $container, string $defaultJavascriptSession): void
{
$container->setParameter('mink.javascript_session', $defaultJavascriptSession);
}

private function setSeleniumWebdriverHost(ContainerBuilder $container, string $seleniumWebdriverHost): void
{
foreach ($this->getDriverDefinitions($container) as $driverDefinition) {
if (is_a($driverDefinition->getClass(), Selenium2Driver::class, true)) {
$driverDefinition->setArgument(2, $seleniumWebdriverHost);
}
}
}

private function setChromeApiUrl(ContainerBuilder $container, string $chromeApiUrl): void
{
foreach ($this->getDriverDefinitions($container) as $driverDefinition) {
if (is_a($driverDefinition->getClass(), ChromeDriver::class, true)) {
$driverDefinition->setArgument(0, $chromeApiUrl);
}
}
}

/**
* @return \Symfony\Component\DependencyInjection\Definition[]
*/
private function getDriverDefinitions(ContainerBuilder $container): array
{
$minkDefinition = $container->getDefinition('mink');

$registerSessionCalls = array_filter($minkDefinition->getMethodCalls(), static function ($methodCall) {
return $methodCall[0] === 'registerSession';
});

return array_map(static function (array $registerSessionCall) {
/** @var \Symfony\Component\DependencyInjection\Definition $sessionDefinition */
$sessionDefinition = $registerSessionCall[1][1];

return $sessionDefinition->getArgument(0);
}, $registerSessionCalls);
}
}

0 comments on commit 3329636

Please sign in to comment.