2017-04-04 14:11:34 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2017, Daniel Calviño Sánchez (danxuliu@gmail.com)
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
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".
|
|
|
|
*
|
2017-04-10 18:56:47 +00:00
|
|
|
* The Nextcloud server is provided by an instance of NextcloudTestServerHelper;
|
|
|
|
* its class must be specified when this context is created. By default,
|
2017-04-19 06:05:40 +00:00
|
|
|
* "NextcloudTestServerLocalHelper" is used, although that can be customized
|
2017-04-10 18:56:47 +00:00
|
|
|
* 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
|
2017-04-19 06:05:40 +00:00
|
|
|
* as in the constructor of the helper class (by default, [ ]).
|
2017-04-10 18:56:47 +00:00
|
|
|
*
|
|
|
|
* Example of custom parameters in "behat.yml":
|
|
|
|
* default:
|
|
|
|
* suites:
|
|
|
|
* default:
|
|
|
|
* contexts:
|
|
|
|
* - NextcloudTestServerContext:
|
2017-04-19 06:05:40 +00:00
|
|
|
* nextcloudTestServerHelper: NextcloudTestServerCustomHelper
|
2017-04-10 18:56:47 +00:00
|
|
|
* nextcloudTestServerHelperParameters:
|
2017-04-19 06:05:40 +00:00
|
|
|
* - first-parameter-value
|
|
|
|
* - second-parameter-value
|
2017-04-04 14:11:34 +00:00
|
|
|
*/
|
|
|
|
class NextcloudTestServerContext implements Context {
|
|
|
|
|
|
|
|
/**
|
2017-04-10 18:56:47 +00:00
|
|
|
* @var NextcloudTestServerHelper
|
2017-04-04 14:11:34 +00:00
|
|
|
*/
|
2017-04-10 18:56:47 +00:00
|
|
|
private $nextcloudTestServerHelper;
|
2017-04-04 14:11:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new NextcloudTestServerContext.
|
|
|
|
*
|
2017-04-10 18:56:47 +00:00
|
|
|
* @param string $nextcloudTestServerHelper the name of the
|
|
|
|
* NextcloudTestServerHelper implementing class to use.
|
|
|
|
* @param array $nextcloudTestServerHelperParameters the parameters for the
|
|
|
|
* constructor of the $nextcloudTestServerHelper class.
|
2017-04-04 14:11:34 +00:00
|
|
|
*/
|
2017-04-19 06:05:40 +00:00
|
|
|
public function __construct($nextcloudTestServerHelper = "NextcloudTestServerLocalHelper",
|
|
|
|
$nextcloudTestServerHelperParameters = [ ]) {
|
2017-04-10 18:56:47 +00:00
|
|
|
$nextcloudTestServerHelperClass = new ReflectionClass($nextcloudTestServerHelper);
|
|
|
|
|
|
|
|
if ($nextcloudTestServerHelperParameters === null) {
|
|
|
|
$nextcloudTestServerHelperParameters = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->nextcloudTestServerHelper = $nextcloudTestServerHelperClass->newInstanceArgs($nextcloudTestServerHelperParameters);
|
2017-04-04 14:11:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @BeforeScenario
|
|
|
|
*
|
|
|
|
* Sets up the Nextcloud test server before each scenario.
|
|
|
|
*
|
2017-04-10 18:56:47 +00:00
|
|
|
* 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.
|
2017-04-04 14:11:34 +00:00
|
|
|
*
|
|
|
|
* @param \Behat\Behat\Hook\Scope\BeforeScenarioScope $scope the
|
|
|
|
* BeforeScenario hook scope.
|
2017-04-10 18:56:47 +00:00
|
|
|
* @throws \Exception if the Nextcloud test server can not be set up or its
|
|
|
|
* base URL got.
|
2017-04-04 14:11:34 +00:00
|
|
|
*/
|
2017-04-10 10:26:52 +00:00
|
|
|
public function setUpNextcloudTestServer(BeforeScenarioScope $scope) {
|
2017-04-10 18:56:47 +00:00
|
|
|
$this->nextcloudTestServerHelper->setUp();
|
2017-04-04 14:11:34 +00:00
|
|
|
|
2017-04-10 18:56:47 +00:00
|
|
|
$this->setBaseUrlInSiblingRawMinkContexts($scope, $this->nextcloudTestServerHelper->getBaseUrl());
|
2017-04-04 14:11:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @AfterScenario
|
|
|
|
*
|
|
|
|
* Cleans up the Nextcloud test server after each scenario.
|
|
|
|
*
|
2017-04-10 18:56:47 +00:00
|
|
|
* @throws \Exception if the Nextcloud test server can not be cleaned up.
|
2017-04-04 14:11:34 +00:00
|
|
|
*/
|
2017-04-10 10:26:52 +00:00
|
|
|
public function cleanUpNextcloudTestServer() {
|
2017-04-10 18:56:47 +00:00
|
|
|
$this->nextcloudTestServerHelper->cleanUp();
|
2017-04-04 14:11:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|