TalkContext.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. use Behat\Behat\Context\Context;
  7. class TalkContext implements Context {
  8. /**
  9. * @BeforeFeature @Talk
  10. * @BeforeScenario @Talk
  11. */
  12. public static function skipTestsIfTalkIsNotInstalled() {
  13. if (!TalkContext::isTalkInstalled()) {
  14. throw new Exception('Talk needs to be installed to run features or scenarios tagged with @Talk');
  15. }
  16. }
  17. /**
  18. * @AfterScenario @Talk
  19. */
  20. public static function disableTalk() {
  21. TalkContext::runOcc(['app:disable', 'spreed']);
  22. }
  23. private static function isTalkInstalled(): bool {
  24. $appList = TalkContext::runOcc(['app:list']);
  25. return strpos($appList, 'spreed') !== false;
  26. }
  27. private static function runOcc(array $args): string {
  28. // Based on "runOcc" from CommandLine trait (which can not be used due
  29. // to not being static and being already used in other sibling
  30. // contexts).
  31. $args = array_map(function ($arg) {
  32. return escapeshellarg($arg);
  33. }, $args);
  34. $args[] = '--no-ansi --no-warnings';
  35. $args = implode(' ', $args);
  36. $descriptor = [
  37. 0 => ['pipe', 'r'],
  38. 1 => ['pipe', 'w'],
  39. 2 => ['pipe', 'w'],
  40. ];
  41. $process = proc_open('php console.php ' . $args, $descriptor, $pipes, $ocPath = '../..');
  42. $lastStdOut = stream_get_contents($pipes[1]);
  43. proc_close($process);
  44. return $lastStdOut;
  45. }
  46. }