SetupTest.php 3.8 KB

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