To avoid port clashes, I am starting a web server in @BeforeSuite event on an available port.

note: To find an available port:

    $socket = stream_socket_server("tcp://$host:0");
    $address = stream_socket_get_name($socket, false);
    fclose($socket);
    list($host, $port) = explode(':', address);

once I have the port # I start php -S $address -t $document_root as an async process (using Symfony\Component\Process\Process) and I want until the socket is open.

This works very well, but then, I don't know how to tell Mink extensions to use http://$host:$port/ as the base_url

Is it possible?

-- edit

my attempts:

    global $app;

    $r = new \ReflectionClass('Symfony\Component\Console\Application');
    $p = $r->getProperty('runningCommand');
    $p->setAccessible(true);
    $runningCommand = $p->getValue($app);

    $r = new \ReflectionObject($runningCommand);
    $p = $r->getProperty('container');
    $p->setAccessible(true);

    $container = $p->getValue($runningCommand);
    $parameterBag = $container->getParameterBag();
    $r = new \ReflectionClass('Symfony\Component\DependencyInjection\ParameterBag\ParameterBag');
    $m = $r->getMethod('set');
    $m->invoke($parameterBag, 'behat.mink.base_url', "http://$address/");

    $minkContextInitializer = $container->getDefinitions()['behat.mink.context.initializer'];
    $arguments = $minkContextInitializer->getArguments();
    $arguments[1]['base_url'] = "http://$address/";
    $minkContextInitializer->setArguments($arguments);

    $minkContextInitializer = $container->getDefinitions()['behat.mink.listener.sessions_listener'];
    $arguments = $minkContextInitializer->getArguments();
    $arguments[1]['base_url'] = "http://$address/";
    $minkContextInitializer->setArguments($arguments);

mink extension (zombie in my case) is still using the base_url from behat.yml. Am i missing something obvious?

有帮助吗?

解决方案

here is a workaround, in app/behat, i put:

#!/usr/bin/env php
<?php

require_once __DIR__.'/../vendor/autoload.php';

use NoPair\Test\Behat\ContextPreparation;

$address = ContextPreparation::startServer('127.0.0.1', realpath(__DIR__.'/../web'));

putenv("BEHAT_PARAMS=extensions[Behat\MinkExtension\Extension][base_url]=http://$address/");

define('BEHAT_PHP_BIN_PATH', getenv('PHP_PEAR_PHP_BIN') ?: '/usr/bin/env php');
define('BEHAT_BIN_PATH',     __FILE__);
define('BEHAT_VERSION',      'DEV');

$app = new Behat\Behat\Console\BehatApplication(BEHAT_VERSION);
$app->run();

this is working, but requires to call a different script. so I'm still looking for a solution which works in the @BeforeSuite hook

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top