MemoryInfoTest.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test;
  8. use OC\MemoryInfo;
  9. use PHPUnit\Framework\MockObject\MockObject;
  10. /**
  11. * This class provides tests for the MemoryInfo class.
  12. */
  13. class MemoryInfoTest extends TestCase {
  14. /**
  15. * The "memory_limit" value before tests.
  16. *
  17. * @var string
  18. */
  19. private $iniSettingBeforeTest;
  20. /**
  21. * @beforeClass
  22. */
  23. public function backupMemoryInfoIniSetting() {
  24. $this->iniSettingBeforeTest = ini_get('memory_limit');
  25. }
  26. /**
  27. * @afterClass
  28. */
  29. public function restoreMemoryInfoIniSetting() {
  30. ini_set('memory_limit', $this->iniSettingBeforeTest);
  31. }
  32. /**
  33. * Provides test data.
  34. *
  35. * @return array
  36. */
  37. public function getMemoryLimitTestData(): array {
  38. return [
  39. 'unlimited' => ['-1', -1,],
  40. '524288000 bytes' => ['524288000', 524288000,],
  41. '500M' => ['500M', 524288000,],
  42. '512000K' => ['512000K', 524288000,],
  43. '2G' => ['2G', 2147483648,],
  44. ];
  45. }
  46. /**
  47. * Tests that getMemoryLimit works as expected.
  48. *
  49. * @param string $iniValue The "memory_limit" ini data.
  50. * @param int|float $expected The expected detected memory limit.
  51. * @dataProvider getMemoryLimitTestData
  52. */
  53. public function testMemoryLimit(string $iniValue, int|float $expected) {
  54. ini_set('memory_limit', $iniValue);
  55. $memoryInfo = new MemoryInfo();
  56. self::assertEquals($expected, $memoryInfo->getMemoryLimit());
  57. }
  58. /**
  59. * Provides sufficient memory limit test data.
  60. *
  61. * @return array
  62. */
  63. public function getSufficientMemoryTestData(): array {
  64. return [
  65. 'unlimited' => [-1, true,],
  66. '512M' => [512 * 1024 * 1024, true,],
  67. '1G' => [1024 * 1024 * 1024, true,],
  68. '256M' => [256 * 1024 * 1024, false,],
  69. ];
  70. }
  71. /**
  72. * Tests that isMemoryLimitSufficient returns the correct values.
  73. *
  74. * @param int $memoryLimit The memory limit
  75. * @param bool $expected If the memory limit is sufficient.
  76. * @dataProvider getSufficientMemoryTestData
  77. * @return void
  78. */
  79. public function testIsMemoryLimitSufficient(int $memoryLimit, bool $expected) {
  80. /* @var MemoryInfo|MockObject $memoryInfo */
  81. $memoryInfo = $this->getMockBuilder(MemoryInfo::class)
  82. ->setMethods(['getMemoryLimit',])
  83. ->getMock();
  84. $memoryInfo
  85. ->method('getMemoryLimit')
  86. ->willReturn($memoryLimit);
  87. $isMemoryLimitSufficient = $memoryInfo->isMemoryLimitSufficient();
  88. self::assertEquals($expected, $isMemoryLimitSufficient);
  89. }
  90. }