SetupTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * Copyright (c) 2014 Lukas Reschke <lukas@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace Test;
  9. use bantu\IniGetWrapper\IniGetWrapper;
  10. use OC\Installer;
  11. use OC\SystemConfig;
  12. use OCP\Defaults;
  13. use OCP\IL10N;
  14. use OCP\ILogger;
  15. use OCP\Security\ISecureRandom;
  16. class SetupTest extends \Test\TestCase {
  17. /** @var SystemConfig|\PHPUnit_Framework_MockObject_MockObject */
  18. protected $config;
  19. /** @var \bantu\IniGetWrapper\IniGetWrapper|\PHPUnit_Framework_MockObject_MockObject */
  20. private $iniWrapper;
  21. /** @var \OCP\IL10N|\PHPUnit_Framework_MockObject_MockObject */
  22. private $l10n;
  23. /** @var Defaults|\PHPUnit_Framework_MockObject_MockObject */
  24. private $defaults;
  25. /** @var \OC\Setup|\PHPUnit_Framework_MockObject_MockObject */
  26. protected $setupClass;
  27. /** @var \OCP\ILogger|\PHPUnit_Framework_MockObject_MockObject */
  28. protected $logger;
  29. /** @var \OCP\Security\ISecureRandom|\PHPUnit_Framework_MockObject_MockObject */
  30. protected $random;
  31. /** @var Installer|\PHPUnit_Framework_MockObject_MockObject */
  32. protected $installer;
  33. protected function setUp() {
  34. parent::setUp();
  35. $this->config = $this->createMock(SystemConfig::class);
  36. $this->iniWrapper = $this->createMock(IniGetWrapper::class);
  37. $this->l10n = $this->createMock(IL10N::class);
  38. $this->defaults = $this->createMock(Defaults::class);
  39. $this->logger = $this->createMock(ILogger::class);
  40. $this->random = $this->createMock(ISecureRandom::class);
  41. $this->installer = $this->createMock(Installer::class);
  42. $this->setupClass = $this->getMockBuilder('\OC\Setup')
  43. ->setMethods(['class_exists', 'is_callable', 'getAvailableDbDriversForPdo'])
  44. ->setConstructorArgs([$this->config, $this->iniWrapper, $this->l10n, $this->defaults, $this->logger, $this->random, $this->installer])
  45. ->getMock();
  46. }
  47. public function testGetSupportedDatabasesWithOneWorking() {
  48. $this->config
  49. ->expects($this->once())
  50. ->method('getValue')
  51. ->will($this->returnValue(
  52. array('sqlite', 'mysql', 'oci')
  53. ));
  54. $this->setupClass
  55. ->expects($this->once())
  56. ->method('is_callable')
  57. ->will($this->returnValue(false));
  58. $this->setupClass
  59. ->expects($this->any())
  60. ->method('getAvailableDbDriversForPdo')
  61. ->will($this->returnValue(['sqlite']));
  62. $result = $this->setupClass->getSupportedDatabases();
  63. $expectedResult = array(
  64. 'sqlite' => 'SQLite'
  65. );
  66. $this->assertSame($expectedResult, $result);
  67. }
  68. public function testGetSupportedDatabasesWithNoWorking() {
  69. $this->config
  70. ->expects($this->once())
  71. ->method('getValue')
  72. ->will($this->returnValue(
  73. array('sqlite', 'mysql', 'oci', 'pgsql')
  74. ));
  75. $this->setupClass
  76. ->expects($this->any())
  77. ->method('is_callable')
  78. ->will($this->returnValue(false));
  79. $this->setupClass
  80. ->expects($this->any())
  81. ->method('getAvailableDbDriversForPdo')
  82. ->will($this->returnValue([]));
  83. $result = $this->setupClass->getSupportedDatabases();
  84. $this->assertSame(array(), $result);
  85. }
  86. public function testGetSupportedDatabasesWithAllWorking() {
  87. $this->config
  88. ->expects($this->once())
  89. ->method('getValue')
  90. ->will($this->returnValue(
  91. array('sqlite', 'mysql', 'pgsql', 'oci')
  92. ));
  93. $this->setupClass
  94. ->expects($this->any())
  95. ->method('is_callable')
  96. ->will($this->returnValue(true));
  97. $this->setupClass
  98. ->expects($this->any())
  99. ->method('getAvailableDbDriversForPdo')
  100. ->will($this->returnValue(['sqlite', 'mysql', 'pgsql']));
  101. $result = $this->setupClass->getSupportedDatabases();
  102. $expectedResult = array(
  103. 'sqlite' => 'SQLite',
  104. 'mysql' => 'MySQL/MariaDB',
  105. 'pgsql' => 'PostgreSQL',
  106. 'oci' => 'Oracle'
  107. );
  108. $this->assertSame($expectedResult, $result);
  109. }
  110. /**
  111. * @expectedException \Exception
  112. * @expectedExceptionMessage Supported databases are not properly configured.
  113. */
  114. public function testGetSupportedDatabaseException() {
  115. $this->config
  116. ->expects($this->once())
  117. ->method('getValue')
  118. ->will($this->returnValue('NotAnArray'));
  119. $this->setupClass->getSupportedDatabases();
  120. }
  121. }