NextcloudTestServerContext.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. *
  4. * @copyright Copyright (c) 2017, Daniel Calviño Sánchez (danxuliu@gmail.com)
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. use Behat\Behat\Context\Context;
  23. use Behat\Behat\Hook\Scope\BeforeScenarioScope;
  24. /**
  25. * Behat context to run each scenario against a clean Nextcloud server.
  26. *
  27. * Before each scenario is run, this context sets up a fresh Nextcloud server
  28. * with predefined data and configuration. Thanks to this every scenario is
  29. * independent from the others and they all know the initial state of the
  30. * server.
  31. *
  32. * This context is expected to be used along with RawMinkContext contexts (or
  33. * subclasses). As the server address can be different for each scenario, this
  34. * context automatically sets the "base_url" parameter of all its sibling
  35. * RawMinkContexts; just add NextcloudTestServerContext to the context list of a
  36. * suite in "behat.yml".
  37. *
  38. * The Nextcloud server is provided by an instance of NextcloudTestServerHelper;
  39. * its class must be specified when this context is created. By default,
  40. * "NextcloudTestServerLocalBuiltInHelper" is used, although that can be
  41. * customized using the "nextcloudTestServerHelper" parameter in "behat.yml". In
  42. * the same way, the parameters to be passed to the helper when it is created
  43. * can be customized using the "nextcloudTestServerHelperParameters" parameter,
  44. * which is an array (without keys) with the value of the parameters in the same
  45. * order as in the constructor of the helper class (by default, [ ]).
  46. *
  47. * Example of custom parameters in "behat.yml":
  48. * default:
  49. * suites:
  50. * default:
  51. * contexts:
  52. * - NextcloudTestServerContext:
  53. * nextcloudTestServerHelper: NextcloudTestServerCustomHelper
  54. * nextcloudTestServerHelperParameters:
  55. * - first-parameter-value
  56. * - second-parameter-value
  57. */
  58. class NextcloudTestServerContext implements Context {
  59. /**
  60. * @var NextcloudTestServerHelper
  61. */
  62. private $nextcloudTestServerHelper;
  63. /**
  64. * Creates a new NextcloudTestServerContext.
  65. *
  66. * @param string $nextcloudTestServerHelper the name of the
  67. * NextcloudTestServerHelper implementing class to use.
  68. * @param array $nextcloudTestServerHelperParameters the parameters for the
  69. * constructor of the $nextcloudTestServerHelper class.
  70. */
  71. public function __construct($nextcloudTestServerHelper = "NextcloudTestServerLocalBuiltInHelper",
  72. $nextcloudTestServerHelperParameters = [ ]) {
  73. $nextcloudTestServerHelperClass = new ReflectionClass($nextcloudTestServerHelper);
  74. if ($nextcloudTestServerHelperParameters === null) {
  75. $nextcloudTestServerHelperParameters = [];
  76. }
  77. $this->nextcloudTestServerHelper = $nextcloudTestServerHelperClass->newInstanceArgs($nextcloudTestServerHelperParameters);
  78. }
  79. /**
  80. * @BeforeScenario
  81. *
  82. * Sets up the Nextcloud test server before each scenario.
  83. *
  84. * Once the Nextcloud test server is set up, the "base_url" parameter of the
  85. * sibling RawMinkContexts is set to the base URL of the Nextcloud test
  86. * server.
  87. *
  88. * @param \Behat\Behat\Hook\Scope\BeforeScenarioScope $scope the
  89. * BeforeScenario hook scope.
  90. * @throws \Exception if the Nextcloud test server can not be set up or its
  91. * base URL got.
  92. */
  93. public function setUpNextcloudTestServer(BeforeScenarioScope $scope) {
  94. $this->nextcloudTestServerHelper->setUp();
  95. $this->setBaseUrlInSiblingRawMinkContexts($scope, $this->nextcloudTestServerHelper->getBaseUrl());
  96. }
  97. /**
  98. * @AfterScenario
  99. *
  100. * Cleans up the Nextcloud test server after each scenario.
  101. *
  102. * @throws \Exception if the Nextcloud test server can not be cleaned up.
  103. */
  104. public function cleanUpNextcloudTestServer() {
  105. $this->nextcloudTestServerHelper->cleanUp();
  106. }
  107. private function setBaseUrlInSiblingRawMinkContexts(BeforeScenarioScope $scope, $baseUrl) {
  108. $environment = $scope->getEnvironment();
  109. foreach ($environment->getContexts() as $context) {
  110. if ($context instanceof Behat\MinkExtension\Context\RawMinkContext) {
  111. $context->setMinkParameter("base_url", $baseUrl);
  112. }
  113. }
  114. }
  115. }