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.

242 lines
6.7 KiB

  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2018, Denis Mosolov <denismosolov@gmail.com>
  5. *
  6. * @author Denis Mosolov <denismosolov@gmail.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Afferoq General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  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
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Talk\Tests\php\Command\Turn;
  25. use OCA\Talk\Command\Turn\Delete;
  26. use OCP\IConfig;
  27. use PHPUnit\Framework\MockObject\MockObject;
  28. use Symfony\Component\Console\Input\InputInterface;
  29. use Symfony\Component\Console\Output\OutputInterface;
  30. use Test\TestCase;
  31. class DeleteTest extends TestCase {
  32. protected IConfig&MockObject $config;
  33. protected InputInterface&MockObject $input;
  34. protected OutputInterface&MockObject $output;
  35. protected Delete $command;
  36. public function setUp(): void {
  37. parent::setUp();
  38. $this->config = $this->createMock(IConfig::class);
  39. $this->command = new Delete($this->config);
  40. $this->input = $this->createMock(InputInterface::class);
  41. $this->output = $this->createMock(OutputInterface::class);
  42. }
  43. public function testDeleteIfEmpty(): void {
  44. $this->input->method('getArgument')
  45. ->willReturnCallback(function ($arg) {
  46. if ($arg === 'schemes') {
  47. return 'turn,turns';
  48. } elseif ($arg === 'server') {
  49. return 'turn.example.com';
  50. } elseif ($arg === 'protocols') {
  51. return 'udp,tcp';
  52. }
  53. throw new \Exception();
  54. });
  55. $this->config->expects($this->once())
  56. ->method('getAppValue')
  57. ->with('spreed', 'turn_servers')
  58. ->willReturn('');
  59. $this->config->expects($this->once())
  60. ->method('setAppValue')
  61. ->with(
  62. $this->equalTo('spreed'),
  63. $this->equalTo('turn_servers'),
  64. $this->equalTo(json_encode([]))
  65. );
  66. $this->output->expects($this->once())
  67. ->method('writeln')
  68. ->with($this->equalTo('<info>There is nothing to delete.</info>'));
  69. self::invokePrivate($this->command, 'execute', [$this->input, $this->output]);
  70. }
  71. public function testDelete(): void {
  72. $this->input->method('getArgument')
  73. ->willReturnCallback(function ($arg) {
  74. if ($arg === 'schemes') {
  75. return 'turn,turns';
  76. } elseif ($arg === 'server') {
  77. return 'turn2.example.com';
  78. } elseif ($arg === 'protocols') {
  79. return 'udp,tcp';
  80. }
  81. throw new \Exception();
  82. });
  83. $this->config->expects($this->once())
  84. ->method('getAppValue')
  85. ->with('spreed', 'turn_servers')
  86. ->willReturn(json_encode([
  87. [
  88. 'schemes' => 'turn,turns',
  89. 'server' => 'turn1.example.com',
  90. 'secret' => 'my-test-secret-1',
  91. 'protocols' => 'udp,tcp'
  92. ]
  93. ]));
  94. $this->config->expects($this->once())
  95. ->method('setAppValue')
  96. ->with(
  97. $this->equalTo('spreed'),
  98. $this->equalTo('turn_servers'),
  99. $this->equalTo(json_encode([
  100. [
  101. 'schemes' => 'turn,turns',
  102. 'server' => 'turn1.example.com',
  103. 'secret' => 'my-test-secret-1',
  104. 'protocols' => 'udp,tcp'
  105. ]
  106. ]))
  107. );
  108. $this->output->expects($this->once())
  109. ->method('writeln')
  110. ->with($this->equalTo('<info>There is nothing to delete.</info>'));
  111. self::invokePrivate($this->command, 'execute', [$this->input, $this->output]);
  112. }
  113. public function testNothingToDelete(): void {
  114. $this->input->method('getArgument')
  115. ->willReturnCallback(function ($arg) {
  116. if ($arg === 'schemes') {
  117. return 'turn,turns';
  118. } elseif ($arg === 'server') {
  119. return 'turn4.example.com';
  120. } elseif ($arg === 'protocols') {
  121. return 'udp,tcp';
  122. }
  123. throw new \Exception();
  124. });
  125. $this->config->expects($this->once())
  126. ->method('getAppValue')
  127. ->with('spreed', 'turn_servers')
  128. ->willReturn(json_encode([
  129. [
  130. 'schemes' => 'turn,turns',
  131. 'server' => 'turn1.example.com',
  132. 'secret' => 'my-test-secret-1',
  133. 'protocols' => 'udp,tcp'
  134. ],
  135. [
  136. 'schemes' => 'turn,turns',
  137. 'server' => 'turn2.example.com',
  138. 'secret' => 'my-test-secret-2',
  139. 'protocols' => 'udp,tcp'
  140. ],
  141. [
  142. 'schemes' => 'turn,turns',
  143. 'server' => 'turn3.example.com',
  144. 'secret' => 'my-test-secret-3',
  145. 'protocols' => 'udp,tcp'
  146. ],
  147. ]));
  148. $this->config->expects($this->once())
  149. ->method('setAppValue')
  150. ->with(
  151. $this->equalTo('spreed'),
  152. $this->equalTo('turn_servers'),
  153. $this->equalTo(json_encode([
  154. [
  155. 'schemes' => 'turn,turns',
  156. 'server' => 'turn1.example.com',
  157. 'secret' => 'my-test-secret-1',
  158. 'protocols' => 'udp,tcp'
  159. ],
  160. [
  161. 'schemes' => 'turn,turns',
  162. 'server' => 'turn2.example.com',
  163. 'secret' => 'my-test-secret-2',
  164. 'protocols' => 'udp,tcp'
  165. ],
  166. [
  167. 'schemes' => 'turn,turns',
  168. 'server' => 'turn3.example.com',
  169. 'secret' => 'my-test-secret-3',
  170. 'protocols' => 'udp,tcp'
  171. ],
  172. ]))
  173. );
  174. $this->output->expects($this->once())
  175. ->method('writeln')
  176. ->with($this->equalTo('<info>There is nothing to delete.</info>'));
  177. self::invokePrivate($this->command, 'execute', [$this->input, $this->output]);
  178. }
  179. public function testDeleteMatchingSchemes(): void {
  180. $this->input->method('getArgument')
  181. ->willReturnCallback(function ($arg) {
  182. if ($arg === 'schemes') {
  183. return 'turn,turns';
  184. } elseif ($arg === 'server') {
  185. return 'turn.example.com';
  186. } elseif ($arg === 'protocols') {
  187. return 'udp,tcp';
  188. }
  189. throw new \Exception();
  190. });
  191. $this->config->expects($this->once())
  192. ->method('getAppValue')
  193. ->with('spreed', 'turn_servers')
  194. ->willReturn(json_encode([
  195. [
  196. 'schemes' => 'turn,turns',
  197. 'server' => 'turn.example.com',
  198. 'secret' => 'my-test-secret-1',
  199. 'protocols' => 'udp,tcp'
  200. ],
  201. [
  202. 'schemes' => 'turn',
  203. 'server' => 'turn.example.com',
  204. 'secret' => 'my-test-secret-1',
  205. 'protocols' => 'udp,tcp'
  206. ]
  207. ]));
  208. $this->config->expects($this->once())
  209. ->method('setAppValue')
  210. ->with(
  211. $this->equalTo('spreed'),
  212. $this->equalTo('turn_servers'),
  213. $this->equalTo(json_encode([
  214. [
  215. 'schemes' => 'turn',
  216. 'server' => 'turn.example.com',
  217. 'secret' => 'my-test-secret-1',
  218. 'protocols' => 'udp,tcp'
  219. ]
  220. ]))
  221. );
  222. $this->output->expects($this->once())
  223. ->method('writeln')
  224. ->with($this->equalTo('<info>Deleted turn.example.com.</info>'));
  225. self::invokePrivate($this->command, 'execute', [$this->input, $this->output]);
  226. }
  227. }