MemoryInfoTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2018, Michael Weimann (<mail@michael-weimann.eu>)
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace Test;
  23. use OC\MemoryInfo;
  24. use PHPUnit\Framework\MockObject\MockObject;
  25. /**
  26. * This class provides tests for the MemoryInfo class.
  27. */
  28. class MemoryInfoTest extends TestCase {
  29. /**
  30. * The "memory_limit" value before tests.
  31. *
  32. * @var string
  33. */
  34. private $iniSettingBeforeTest;
  35. /**
  36. * @beforeClass
  37. */
  38. public function backupMemoryInfoIniSetting() {
  39. $this->iniSettingBeforeTest = ini_get('memory_limit');
  40. }
  41. /**
  42. * @afterClass
  43. */
  44. public function restoreMemoryInfoIniSetting() {
  45. ini_set('memory_limit', $this->iniSettingBeforeTest);
  46. }
  47. /**
  48. * Provides test data.
  49. *
  50. * @return array
  51. */
  52. public function getMemoryLimitTestData(): array {
  53. return [
  54. 'unlimited' => ['-1', -1,],
  55. '524288000 bytes' => ['524288000', 524288000,],
  56. '500M' => ['500M', 524288000,],
  57. '512000K' => ['512000K', 524288000,],
  58. '2G' => ['2G', 2147483648,],
  59. ];
  60. }
  61. /**
  62. * Tests that getMemoryLimit works as expected.
  63. *
  64. * @param string $iniValue The "memory_limit" ini data.
  65. * @param int $expected The expected detected memory limit.
  66. * @dataProvider getMemoryLimitTestData
  67. */
  68. public function testMemoryLimit($iniValue, int $expected) {
  69. ini_set('memory_limit', $iniValue);
  70. $memoryInfo = new MemoryInfo();
  71. self::assertEquals($expected, $memoryInfo->getMemoryLimit());
  72. }
  73. /**
  74. * Provides sufficient memory limit test data.
  75. *
  76. * @return array
  77. */
  78. public function getSufficientMemoryTestData(): array {
  79. return [
  80. 'unlimited' => [-1, true,],
  81. '512M' => [512 * 1024 * 1024, true,],
  82. '1G' => [1024 * 1024 * 1024, true,],
  83. '256M' => [256 * 1024 * 1024, false,],
  84. ];
  85. }
  86. /**
  87. * Tests that isMemoryLimitSufficient returns the correct values.
  88. *
  89. * @param int $memoryLimit The memory limit
  90. * @param bool $expected If the memory limit is sufficient.
  91. * @dataProvider getSufficientMemoryTestData
  92. * @return void
  93. */
  94. public function testIsMemoryLimitSufficient(int $memoryLimit, bool $expected) {
  95. /* @var MemoryInfo|MockObject $memoryInfo */
  96. $memoryInfo = $this->getMockBuilder(MemoryInfo::class)
  97. ->setMethods(['getMemoryLimit',])
  98. ->getMock();
  99. $memoryInfo
  100. ->method('getMemoryLimit')
  101. ->willReturn($memoryLimit);
  102. $isMemoryLimitSufficient = $memoryInfo->isMemoryLimitSufficient();
  103. self::assertEquals($expected, $isMemoryLimitSufficient);
  104. }
  105. }