You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CommandTest.php 3.2KB

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