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. '0' => ['0', 0,],
  56. '134217728 bytes' => ['134217728', 134217728,],
  57. '128M' => ['128M', 134217728,],
  58. '131072K' => ['131072K', 134217728,],
  59. '2G' => ['2G', 2147483648,],
  60. ];
  61. }
  62. /**
  63. * Tests that getMemoryLimit works as expected.
  64. *
  65. * @param string $iniValue The "memory_limit" ini data.
  66. * @param int $expected The expected detected memory limit.
  67. * @dataProvider getMemoryLimitTestData
  68. */
  69. public function testMemoryLimit($iniValue, int $expected) {
  70. ini_set('memory_limit', $iniValue);
  71. $memoryInfo = new MemoryInfo();
  72. self::assertEquals($expected, $memoryInfo->getMemoryLimit());
  73. }
  74. /**
  75. * Provides sufficient memory limit test data.
  76. *
  77. * @return array
  78. */
  79. public function getSufficientMemoryTestData(): array {
  80. return [
  81. 'unlimited' => [-1, true,],
  82. '512M' => [512 * 1024 * 1024, true,],
  83. '1G' => [1024 * 1024 * 1024, true,],
  84. '256M' => [256 * 1024 * 1024, false,],
  85. ];
  86. }
  87. /**
  88. * Tests that isMemoryLimitSufficient returns the correct values.
  89. *
  90. * @param int $memoryLimit The memory limit
  91. * @param bool $expected If the memory limit is sufficient.
  92. * @dataProvider getSufficientMemoryTestData
  93. * @return void
  94. */
  95. public function testIsMemoryLimitSufficient(int $memoryLimit, bool $expected) {
  96. /* @var MemoryInfo|MockObject $memoryInfo */
  97. $memoryInfo = $this->getMockBuilder(MemoryInfo::class)
  98. ->setMethods(['getMemoryLimit',])
  99. ->getMock();
  100. $memoryInfo
  101. ->method('getMemoryLimit')
  102. ->willReturn($memoryLimit);
  103. $isMemoryLimitSufficient = $memoryInfo->isMemoryLimitSufficient();
  104. self::assertEquals($expected, $isMemoryLimitSufficient);
  105. }
  106. }