SetupTest.php 3.8 KB

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