1
0

CommandTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_External\Tests\Command;
  8. use OCA\Files_External\Lib\StorageConfig;
  9. use OCA\Files_External\NotFoundException;
  10. use OCA\Files_External\Service\GlobalStoragesService;
  11. use Symfony\Component\Console\Command\Command;
  12. use Symfony\Component\Console\Input\ArrayInput;
  13. use Symfony\Component\Console\Input\Input;
  14. use Symfony\Component\Console\Output\BufferedOutput;
  15. use Test\TestCase;
  16. abstract class CommandTest extends TestCase {
  17. /**
  18. * @param StorageConfig[] $mounts
  19. * @return GlobalStoragesService|\PHPUnit\Framework\MockObject\MockObject
  20. */
  21. protected function getGlobalStorageService(array $mounts = []) {
  22. $mock = $this->getMockBuilder('OCA\Files_External\Service\GlobalStoragesService')
  23. ->disableOriginalConstructor()
  24. ->getMock();
  25. $this->bindMounts($mock, $mounts);
  26. return $mock;
  27. }
  28. /**
  29. * @param \PHPUnit\Framework\MockObject\MockObject $mock
  30. * @param StorageConfig[] $mounts
  31. */
  32. protected function bindMounts(\PHPUnit\Framework\MockObject\MockObject $mock, array $mounts) {
  33. $mock->expects($this->any())
  34. ->method('getStorage')
  35. ->willReturnCallback(function ($id) use ($mounts) {
  36. foreach ($mounts as $mount) {
  37. if ($mount->getId() === $id) {
  38. return $mount;
  39. }
  40. }
  41. throw new NotFoundException();
  42. });
  43. }
  44. /**
  45. * @param $id
  46. * @param $mountPoint
  47. * @param $backendClass
  48. * @param string $applicableIdentifier
  49. * @param array $config
  50. * @param array $options
  51. * @param array $users
  52. * @param array $groups
  53. * @return StorageConfig
  54. */
  55. protected function getMount($id, $mountPoint, $backendClass, $applicableIdentifier = 'password::password', $config = [], $options = [], $users = [], $groups = []) {
  56. $mount = new StorageConfig($id);
  57. $mount->setMountPoint($mountPoint);
  58. $mount->setBackendOptions($config);
  59. $mount->setMountOptions($options);
  60. $mount->setApplicableUsers($users);
  61. $mount->setApplicableGroups($groups);
  62. return $mount;
  63. }
  64. protected function getInput(Command $command, array $arguments = [], array $options = []) {
  65. $input = new ArrayInput([]);
  66. $input->bind($command->getDefinition());
  67. foreach ($arguments as $key => $value) {
  68. $input->setArgument($key, $value);
  69. }
  70. foreach ($options as $key => $value) {
  71. $input->setOption($key, $value);
  72. }
  73. return $input;
  74. }
  75. protected function executeCommand(Command $command, Input $input) {
  76. $output = new BufferedOutput();
  77. $this->invokePrivate($command, 'execute', [$input, $output]);
  78. return $output->fetch();
  79. }
  80. }