CommandTest.php 2.6 KB

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