ImportTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. * @author Joas Schilling <nickvergessen@owncloud.com>
  4. *
  5. * @copyright Copyright (c) 2015, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace Tests\Core\Command\Config;
  22. use OC\Core\Command\Config\Import;
  23. use OCP\IConfig;
  24. use Symfony\Component\Console\Input\InputInterface;
  25. use Symfony\Component\Console\Output\OutputInterface;
  26. use Test\TestCase;
  27. class ImportTest extends TestCase {
  28. /** @var \PHPUnit_Framework_MockObject_MockObject */
  29. protected $config;
  30. /** @var \PHPUnit_Framework_MockObject_MockObject */
  31. protected $consoleInput;
  32. /** @var \PHPUnit_Framework_MockObject_MockObject */
  33. protected $consoleOutput;
  34. /** @var \Symfony\Component\Console\Command\Command */
  35. protected $command;
  36. protected function setUp() {
  37. parent::setUp();
  38. $config = $this->config = $this->getMockBuilder(IConfig::class)
  39. ->disableOriginalConstructor()
  40. ->getMock();
  41. $this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock();
  42. $this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock();
  43. /** @var \OCP\IConfig $config */
  44. $this->command = new Import($config);
  45. }
  46. public function validateAppsArrayData() {
  47. return [
  48. [0],
  49. [1],
  50. [null],
  51. ['new \Exception()'],
  52. [json_encode([])],
  53. ];
  54. }
  55. /**
  56. * @dataProvider validateAppsArrayData
  57. *
  58. * @param mixed $configValue
  59. */
  60. public function testValidateAppsArray($configValue) {
  61. $this->invokePrivate($this->command, 'validateAppsArray', [['app' => ['name' => $configValue]]]);
  62. $this->assertTrue(true, 'Asserting that no exception is thrown');
  63. }
  64. public function validateAppsArrayThrowsData() {
  65. return [
  66. [false],
  67. [true],
  68. [[]],
  69. [new \Exception()],
  70. ];
  71. }
  72. /**
  73. * @dataProvider validateAppsArrayThrowsData
  74. *
  75. * @param mixed $configValue
  76. */
  77. public function testValidateAppsArrayThrows($configValue) {
  78. try {
  79. $this->invokePrivate($this->command, 'validateAppsArray', [['app' => ['name' => $configValue]]]);
  80. $this->fail('Did not throw expected UnexpectedValueException');
  81. } catch (\UnexpectedValueException $e) {
  82. $this->assertStringStartsWith('Invalid app config value for "app":"name".', $e->getMessage());
  83. }
  84. }
  85. public function checkTypeRecursivelyData() {
  86. return [
  87. [0],
  88. [1],
  89. [null],
  90. ['new \Exception()'],
  91. [json_encode([])],
  92. [false],
  93. [true],
  94. [[]],
  95. [['string']],
  96. [['test' => 'string']],
  97. [['test' => ['sub' => 'string']]],
  98. ];
  99. }
  100. /**
  101. * @dataProvider checkTypeRecursivelyData
  102. *
  103. * @param mixed $configValue
  104. */
  105. public function testCheckTypeRecursively($configValue) {
  106. $this->invokePrivate($this->command, 'checkTypeRecursively', [$configValue, 'name']);
  107. $this->assertTrue(true, 'Asserting that no exception is thrown');
  108. }
  109. public function checkTypeRecursivelyThrowsData() {
  110. return [
  111. [new \Exception()],
  112. [[new \Exception()]],
  113. [['test' => new \Exception()]],
  114. [['test' => ['sub' => new \Exception()]]],
  115. ];
  116. }
  117. /**
  118. * @dataProvider checkTypeRecursivelyThrowsData
  119. *
  120. * @param mixed $configValue
  121. */
  122. public function testCheckTypeRecursivelyThrows($configValue) {
  123. try {
  124. $this->invokePrivate($this->command, 'checkTypeRecursively', [$configValue, 'name']);
  125. $this->fail('Did not throw expected UnexpectedValueException');
  126. } catch (\UnexpectedValueException $e) {
  127. $this->assertStringStartsWith('Invalid system config value for "name"', $e->getMessage());
  128. }
  129. }
  130. public function validateArrayData() {
  131. return [
  132. [['system' => []]],
  133. [['apps' => []]],
  134. [['system' => [], 'apps' => []]],
  135. ];
  136. }
  137. /**
  138. * @dataProvider validateArrayData
  139. *
  140. * @param array $configArray
  141. */
  142. public function testValidateArray($configArray) {
  143. $this->invokePrivate($this->command, 'validateArray', [$configArray]);
  144. $this->assertTrue(true, 'Asserting that no exception is thrown');
  145. }
  146. public function validateArrayThrowsData() {
  147. return [
  148. [[], 'At least one key of the following is expected:'],
  149. [[0 => []], 'Found invalid entries in root'],
  150. [['string' => []], 'Found invalid entries in root'],
  151. ];
  152. }
  153. /**
  154. * @dataProvider validateArrayThrowsData
  155. *
  156. * @param mixed $configArray
  157. * @param string $expectedException
  158. */
  159. public function testValidateArrayThrows($configArray, $expectedException) {
  160. try {
  161. $this->invokePrivate($this->command, 'validateArray', [$configArray]);
  162. $this->fail('Did not throw expected UnexpectedValueException');
  163. } catch (\UnexpectedValueException $e) {
  164. $this->assertStringStartsWith($expectedException, $e->getMessage());
  165. }
  166. }
  167. }