1
0

CommandTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\Files_External\Tests\Command;
  26. use OCA\Files_External\Lib\StorageConfig;
  27. use OCA\Files_External\NotFoundException;
  28. use Symfony\Component\Console\Command\Command;
  29. use Symfony\Component\Console\Input\ArrayInput;
  30. use Symfony\Component\Console\Input\Input;
  31. use Symfony\Component\Console\Output\BufferedOutput;
  32. use Test\TestCase;
  33. abstract class CommandTest extends TestCase {
  34. /**
  35. * @param StorageConfig[] $mounts
  36. * @return \OCA\Files_External\Service\GlobalStoragesService|\PHPUnit\Framework\MockObject\MockObject
  37. */
  38. protected function getGlobalStorageService(array $mounts = []) {
  39. $mock = $this->getMockBuilder('OCA\Files_External\Service\GlobalStoragesService')
  40. ->disableOriginalConstructor()
  41. ->getMock();
  42. $this->bindMounts($mock, $mounts);
  43. return $mock;
  44. }
  45. /**
  46. * @param \PHPUnit\Framework\MockObject\MockObject $mock
  47. * @param StorageConfig[] $mounts
  48. */
  49. protected function bindMounts(\PHPUnit\Framework\MockObject\MockObject $mock, array $mounts) {
  50. $mock->expects($this->any())
  51. ->method('getStorage')
  52. ->willReturnCallback(function ($id) use ($mounts) {
  53. foreach ($mounts as $mount) {
  54. if ($mount->getId() === $id) {
  55. return $mount;
  56. }
  57. }
  58. throw new NotFoundException();
  59. });
  60. }
  61. /**
  62. * @param $id
  63. * @param $mountPoint
  64. * @param $backendClass
  65. * @param string $applicableIdentifier
  66. * @param array $config
  67. * @param array $options
  68. * @param array $users
  69. * @param array $groups
  70. * @return StorageConfig
  71. */
  72. protected function getMount($id, $mountPoint, $backendClass, $applicableIdentifier = 'password::password', $config = [], $options = [], $users = [], $groups = []) {
  73. $mount = new StorageConfig($id);
  74. $mount->setMountPoint($mountPoint);
  75. $mount->setBackendOptions($config);
  76. $mount->setMountOptions($options);
  77. $mount->setApplicableUsers($users);
  78. $mount->setApplicableGroups($groups);
  79. return $mount;
  80. }
  81. protected function getInput(Command $command, array $arguments = [], array $options = []) {
  82. $input = new ArrayInput([]);
  83. $input->bind($command->getDefinition());
  84. foreach ($arguments as $key => $value) {
  85. $input->setArgument($key, $value);
  86. }
  87. foreach ($options as $key => $value) {
  88. $input->setOption($key, $value);
  89. }
  90. return $input;
  91. }
  92. protected function executeCommand(Command $command, Input $input) {
  93. $output = new BufferedOutput();
  94. $this->invokePrivate($command, 'execute', [$input, $output]);
  95. return $output->fetch();
  96. }
  97. }