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