From f574a9d44f28b9d2279adbc63ae4a3a98ec19b10 Mon Sep 17 00:00:00 2001
From: Joas Schilling <coding@schilljs.com>
Date: Mon, 13 Jun 2016 16:16:16 +0200
Subject: [PATCH] Make sure the exception is catched

---
 tests/Core/Command/User/SettingTest.php | 36 +++++++++++++++++++++----
 1 file changed, 31 insertions(+), 5 deletions(-)

diff --git a/tests/Core/Command/User/SettingTest.php b/tests/Core/Command/User/SettingTest.php
index 1ed275d6673..a40aeb748e8 100644
--- a/tests/Core/Command/User/SettingTest.php
+++ b/tests/Core/Command/User/SettingTest.php
@@ -37,9 +37,6 @@ class SettingTest extends TestCase {
 	/** @var \Symfony\Component\Console\Output\OutputInterface|\PHPUnit_Framework_MockObject_MockObject */
 	protected $consoleOutput;
 
-	/** @var \Symfony\Component\Console\Command\Command */
-	protected $command;
-
 	protected function setUp() {
 		parent::setUp();
 
@@ -58,8 +55,23 @@ class SettingTest extends TestCase {
 		$this->consoleOutput = $this->getMockBuilder('Symfony\Component\Console\Output\OutputInterface')
 			->disableOriginalConstructor()
 			->getMock();
+	}
+
+	public function getCommand(array $methods = []) {
+		if (empty($methods)) {
+			return new Setting($this->userManager, $this->config, $this->connection);
+		} else {
+			$mock = $this->getMockBuilder('OC\Core\Command\User\Setting')
+				->setConstructorArgs([
+					$this->userManager,
+					$this->config,
+					$this->connection,
+				])
+				->setMethods($methods)
+				->getMock();
+			return $mock;
+		}
 
-		$this->command = new Setting($this->userManager, $this->config, $this->connection);
 	}
 
 	public function dataCheckInput() {
@@ -201,11 +213,25 @@ class SettingTest extends TestCase {
 				->willReturn($user);
 		}
 
+		$command = $this->getCommand();
 		try {
-			$this->invokePrivate($this->command, 'checkInput', [$this->consoleInput]);
+			$this->invokePrivate($command, 'checkInput', [$this->consoleInput]);
 			$this->assertFalse($expectedException);
 		} catch (\InvalidArgumentException $e) {
 			$this->assertEquals($expectedException, $e->getMessage());
 		}
 	}
+
+	public function testCheckInputExceptionCatch() {
+		$command = $this->getCommand(['checkInput']);
+		$command->expects($this->once())
+			->method('checkInput')
+			->willThrowException(new \InvalidArgumentException('test'));
+
+		$this->consoleOutput->expects($this->once())
+			->method('writeln')
+			->with('<error>test</error>');
+
+		$this->assertEquals(1, $this->invokePrivate($command, 'execute', [$this->consoleInput, $this->consoleOutput]));
+	}
 }
-- 
GitLab