config = $this->createMock(IConfig::class); $this->secureRandom = $this->createMock(ISecureRandom::class); $this->command = new Add($this->config, $this->secureRandom); $this->input = $this->createMock(InputInterface::class); $this->output = $this->createMock(OutputInterface::class); } public function testServerEmptyString(): void { $this->input->method('getArgument') ->willReturnCallback(function ($arg) { if ($arg === 'schemes') { return 'turn,turns'; } elseif ($arg === 'server') { return ''; } elseif ($arg === 'protocols') { return 'udp,tcp'; } throw new \Exception(); }); $this->input->method('getOption') ->willReturnCallback(function ($arg) { if ($arg === 'secret') { return 'my-test-secret'; } elseif ($arg === 'generate-secret') { return false; } throw new \Exception(); }); $this->output->expects($this->once()) ->method('writeln') ->with($this->equalTo('Server cannot be empty.')); $this->config->expects($this->never()) ->method('setAppValue'); self::invokePrivate($this->command, 'execute', [$this->input, $this->output]); } public function testSecretEmpty(): void { $this->input->method('getArgument') ->willReturnCallback(function ($arg) { if ($arg === 'schemes') { return 'turn,turns'; } elseif ($arg === 'server') { return 'turn.test.com'; } elseif ($arg === 'protocols') { return 'udp,tcp'; } throw new \Exception(); }); $this->input->method('getOption') ->willReturnCallback(function ($arg) { if ($arg === 'secret') { return ''; } elseif ($arg === 'generate-secret') { return false; } throw new \Exception(); }); $this->output->expects($this->once()) ->method('writeln') ->with($this->equalTo('Secret cannot be empty.')); $this->config->expects($this->never()) ->method('setAppValue'); self::invokePrivate($this->command, 'execute', [$this->input, $this->output]); } public function testGenerateSecret(): void { $this->input->method('getArgument') ->willReturnCallback(function ($arg) { if ($arg === 'schemes') { return 'turn,turns'; } elseif ($arg === 'server') { return 'turn.test.com'; } elseif ($arg === 'protocols') { return 'udp,tcp'; } throw new \Exception(); }); $this->input->method('getOption') ->willReturnCallback(function ($arg) { if ($arg === 'secret') { return null; } elseif ($arg === 'generate-secret') { return true; } throw new \Exception(); }); $this->secureRandom->expects($this->once()) ->method('generate') ->willReturn('O2vWVFk5QRdJ/9clK4XIHbkxYXvVe6ySggANw4TG/B/HCtFzpi7v4GVNB/6wUZvA13v2EN4WgDk+gjATk9zhhc7B6FzxLNWOlQFBADg2aYHb+Ozse2BABDk3VUHCR+W9'); $this->config->method('getAppValue') ->with('spreed', 'turn_servers') ->willReturn(json_encode([])); $this->config->expects($this->once()) ->method('setAppValue') ->with( $this->equalTo('spreed'), $this->equalTo('turn_servers'), $this->equalTo(json_encode([ [ 'schemes' => 'turn,turns', 'server' => 'turn.test.com', 'secret' => 'O2vWVFk5QRdJ/9clK4XIHbkxYXvVe6ySggANw4TG/B/HCtFzpi7v4GVNB/6wUZvA13v2EN4WgDk+gjATk9zhhc7B6FzxLNWOlQFBADg2aYHb+Ozse2BABDk3VUHCR+W9', 'protocols' => 'udp,tcp' ] ])) ); $this->output->expects($this->once()) ->method('writeln') ->with($this->equalTo('Added turn.test.com.')); self::invokePrivate($this->command, 'execute', [$this->input, $this->output]); } public function testSecretAndGenerateSecretOptions(): void { $this->input->method('getArgument') ->willReturnCallback(function ($arg) { if ($arg === 'schemes') { return 'turn,turns'; } elseif ($arg === 'server') { return 'turn.test.com'; } elseif ($arg === 'protocols') { return 'udp,tcp'; } throw new \Exception(); }); $this->input->method('getOption') ->willReturnCallback(function ($arg) { if ($arg === 'secret') { return 'my-test-secret'; } elseif ($arg === 'generate-secret') { return true; } throw new \Exception(); }); $this->output->expects($this->once()) ->method('writeln') ->with($this->equalTo('You must provide --secret or --generate-secret.')); $this->config->expects($this->never()) ->method('setAppValue'); self::invokePrivate($this->command, 'execute', [$this->input, $this->output]); } public function testInvalidSchemesString(): void { $this->input->method('getArgument') ->willReturnCallback(function ($arg) { if ($arg === 'schemes') { return 'invalid-scheme'; } elseif ($arg === 'server') { return 'turn.test.com'; } elseif ($arg === 'protocols') { return 'udp,tcp'; } throw new \Exception(); }); $this->input->method('getOption') ->willReturnCallback(function ($arg) { if ($arg === 'secret') { return 'my-test-secret'; } elseif ($arg === 'generate-secret') { return false; } throw new \Exception(); }); $this->output->expects($this->once()) ->method('writeln') ->with($this->equalTo('Not allowed schemes, must be turn or turns or turn,turns.')); $this->config->expects($this->never()) ->method('setAppValue'); self::invokePrivate($this->command, 'execute', [$this->input, $this->output]); } public function testInvalidProtocolsString(): void { $this->input->method('getArgument') ->willReturnCallback(function ($arg) { if ($arg === 'schemes') { return 'turn,turns'; } elseif ($arg === 'server') { return 'turn.test.com'; } elseif ($arg === 'protocols') { return 'invalid-protocol'; } throw new \Exception(); }); $this->input->method('getOption') ->willReturnCallback(function ($arg) { if ($arg === 'secret') { return 'my-test-secret'; } elseif ($arg === 'generate-secret') { return false; } throw new \Exception(); }); $this->output->expects($this->once()) ->method('writeln') ->with($this->equalTo('Not allowed protocols, must be udp or tcp or udp,tcp.')); $this->config->expects($this->never()) ->method('setAppValue'); self::invokePrivate($this->command, 'execute', [$this->input, $this->output]); } public function testAddServerToEmptyList(): void { $this->input->method('getArgument') ->willReturnCallback(function ($arg) { if ($arg === 'schemes') { return 'turn,turns'; } elseif ($arg === 'server') { return 'turn.test.com'; } elseif ($arg === 'protocols') { return 'udp,tcp'; } throw new \Exception(); }); $this->input->method('getOption') ->willReturnCallback(function ($arg) { if ($arg === 'secret') { return 'my-test-secret'; } elseif ($arg === 'generate-secret') { return false; } throw new \Exception(); }); $this->config->method('getAppValue') ->with('spreed', 'turn_servers') ->willReturn(json_encode([])); $this->config->expects($this->once()) ->method('setAppValue') ->with( $this->equalTo('spreed'), $this->equalTo('turn_servers'), $this->equalTo(json_encode([ [ 'schemes' => 'turn,turns', 'server' => 'turn.test.com', 'secret' => 'my-test-secret', 'protocols' => 'udp,tcp' ] ])) ); $this->output->expects($this->once()) ->method('writeln') ->with($this->equalTo('Added turn.test.com.')); self::invokePrivate($this->command, 'execute', [$this->input, $this->output]); } public function testAddServerToNonEmptyList(): void { $this->input->method('getArgument') ->willReturnCallback(function ($arg) { if ($arg === 'schemes') { return 'turn,turns'; } elseif ($arg === 'server') { return 'turn2.test.com'; } elseif ($arg === 'protocols') { return 'udp,tcp'; } throw new \Exception(); }); $this->input->method('getOption') ->willReturnCallback(function ($arg) { if ($arg === 'secret') { return 'my-test-secret-2'; } elseif ($arg === 'generate-secret') { return false; } throw new \Exception(); }); $this->config->method('getAppValue') ->with('spreed', 'turn_servers') ->willReturn(json_encode([ [ 'schemes' => 'turn', 'server' => 'turn1.test.com', 'secret' => 'my-test-secret-1', 'protocols' => 'udp,tcp' ] ])); $this->config->expects($this->once()) ->method('setAppValue') ->with( $this->equalTo('spreed'), $this->equalTo('turn_servers'), $this->equalTo(json_encode([ [ 'schemes' => 'turn', 'server' => 'turn1.test.com', 'secret' => 'my-test-secret-1', 'protocols' => 'udp,tcp' ], [ 'schemes' => 'turn,turns', 'server' => 'turn2.test.com', 'secret' => 'my-test-secret-2', 'protocols' => 'udp,tcp' ] ])) ); $this->output->expects($this->once()) ->method('writeln') ->with($this->equalTo('Added turn2.test.com.')); self::invokePrivate($this->command, 'execute', [$this->input, $this->output]); } public function testServerSanitization(): void { $this->input->method('getArgument') ->willReturnCallback(function ($arg) { if ($arg === 'schemes') { return 'turn,turns'; } elseif ($arg === 'server') { return 'https://turn.test.com'; } elseif ($arg === 'protocols') { return 'udp,tcp'; } throw new \Exception(); }); $this->input->method('getOption') ->willReturnCallback(function ($arg) { if ($arg === 'secret') { return 'my-test-secret'; } elseif ($arg === 'generate-secret') { return false; } throw new \Exception(); }); $this->config->method('getAppValue') ->with('spreed', 'turn_servers') ->willReturn(json_encode([])); $this->config->expects($this->once()) ->method('setAppValue') ->with( $this->equalTo('spreed'), $this->equalTo('turn_servers'), $this->equalTo(json_encode([ [ 'schemes' => 'turn,turns', 'server' => 'turn.test.com', 'secret' => 'my-test-secret', 'protocols' => 'udp,tcp' ] ])) ); self::invokePrivate($this->command, 'execute', [$this->input, $this->output]); } public function testAddDuplicateServer(): void { $this->input->method('getArgument') ->willReturnCallback(function ($arg) { if ($arg === 'schemes') { return 'turn,turns'; } elseif ($arg === 'server') { return 'turn.test.com'; } elseif ($arg === 'protocols') { return 'udp,tcp'; } throw new \Exception(); }); $this->input->method('getOption') ->willReturnCallback(function ($arg) { if ($arg === 'secret') { return 'my-test-secret'; } elseif ($arg === 'generate-secret') { return false; } throw new \Exception(); }); $this->config->method('getAppValue') ->with('spreed', 'turn_servers') ->willReturn(json_encode([[ 'schemes' => 'turn,turns', 'server' => 'turn.test.com', 'secret' => 'my-test-secret', 'protocols' => 'udp,tcp' ]])); $this->config->expects($this->never()) ->method('setAppValue'); $this->output->expects($this->once()) ->method('writeln') ->with($this->equalTo('Server already exists with the same configuration.')); $this->assertSame(1, self::invokePrivate($this->command, 'execute', [$this->input, $this->output])); } }