. * */ use Behat\Behat\Context\Context; use Behat\Behat\Hook\Scope\BeforeScenarioScope; /** * Behat context to run each scenario against a clean Nextcloud server. * * Before each scenario is run, this context sets up a fresh Nextcloud server * with predefined data and configuration. Thanks to this every scenario is * independent from the others and they all know the initial state of the * server. * * This context is expected to be used along with RawMinkContext contexts (or * subclasses). As the server address can be different for each scenario, this * context automatically sets the "base_url" parameter of all its sibling * RawMinkContexts; just add NextcloudTestServerContext to the context list of a * suite in "behat.yml". * * The Nextcloud server is provided by an instance of NextcloudTestServerHelper; * its class must be specified when this context is created. By default, * "NextcloudTestServerDockerHelper" is used, although that can be customized * using the "nextcloudTestServerHelper" parameter in "behat.yml". In the same * way, the parameters to be passed to the helper when it is created can be * customized using the "nextcloudTestServerHelperParameters" parameter, which * is an array (without keys) with the value of the parameters in the same order * as in the constructor of the helper class (by default, * [ "nextcloud-local-test-acceptance", "15000-16000" ]). * * Example of custom parameters in "behat.yml": * default: * suites: * default: * contexts: * - NextcloudTestServerContext: * nextcloudTestServerHelper: NextcloudTestServerDockerHelper * nextcloudTestServerHelperParameters: * - nextcloud-local-test-acceptance-custom-image * - 23000-42000 * * Note that using Docker containers as a regular user requires giving access to * the Docker daemon to that user. Unfortunately, that makes possible for that * user to get root privileges for the system. Please see the * NextcloudTestServerDockerHelper documentation for further information on this * issue. */ class NextcloudTestServerContext implements Context { /** * @var NextcloudTestServerHelper */ private $nextcloudTestServerHelper; /** * Creates a new NextcloudTestServerContext. * * @param string $nextcloudTestServerHelper the name of the * NextcloudTestServerHelper implementing class to use. * @param array $nextcloudTestServerHelperParameters the parameters for the * constructor of the $nextcloudTestServerHelper class. */ public function __construct($nextcloudTestServerHelper = "NextcloudTestServerDockerHelper", $nextcloudTestServerHelperParameters = [ "nextcloud-local-test-acceptance", "15000-16000" ]) { $nextcloudTestServerHelperClass = new ReflectionClass($nextcloudTestServerHelper); if ($nextcloudTestServerHelperParameters === null) { $nextcloudTestServerHelperParameters = array(); } $this->nextcloudTestServerHelper = $nextcloudTestServerHelperClass->newInstanceArgs($nextcloudTestServerHelperParameters); } /** * @BeforeScenario * * Sets up the Nextcloud test server before each scenario. * * Once the Nextcloud test server is set up, the "base_url" parameter of the * sibling RawMinkContexts is set to the base URL of the Nextcloud test * server. * * @param \Behat\Behat\Hook\Scope\BeforeScenarioScope $scope the * BeforeScenario hook scope. * @throws \Exception if the Nextcloud test server can not be set up or its * base URL got. */ public function setUpNextcloudTestServer(BeforeScenarioScope $scope) { $this->nextcloudTestServerHelper->setUp(); $this->setBaseUrlInSiblingRawMinkContexts($scope, $this->nextcloudTestServerHelper->getBaseUrl()); } /** * @AfterScenario * * Cleans up the Nextcloud test server after each scenario. * * @throws \Exception if the Nextcloud test server can not be cleaned up. */ public function cleanUpNextcloudTestServer() { $this->nextcloudTestServerHelper->cleanUp(); } private function setBaseUrlInSiblingRawMinkContexts(BeforeScenarioScope $scope, $baseUrl) { $environment = $scope->getEnvironment(); foreach ($environment->getContexts() as $context) { if ($context instanceof Behat\MinkExtension\Context\RawMinkContext) { $context->setMinkParameter("base_url", $baseUrl); } } } }