Adds tests for the memory checks

Signed-off-by: Michael Weimann <mail@michael-weimann.eu>
This commit is contained in:
Michael Weimann 2018-08-04 21:54:00 +02:00 committed by John Molakvoæ (skjnldsv)
parent c164409ee7
commit 7aed47f776
No known key found for this signature in database
GPG Key ID: 60C25B8C072916CF
3 changed files with 57 additions and 73 deletions

View File

@ -171,7 +171,7 @@ describe('OC.SetupChecks tests', function() {
cronInfo: { cronInfo: {
diffInSeconds: 0 diffInSeconds: 0
}, },
isTheMemoryLimitHighEnough: true isMemoryLimitSufficient: true
}) })
); );
@ -219,7 +219,7 @@ describe('OC.SetupChecks tests', function() {
cronInfo: { cronInfo: {
diffInSeconds: 0 diffInSeconds: 0
}, },
isTheMemoryLimitHighEnough: true isMemoryLimitSufficient: true
}) })
); );
@ -268,7 +268,7 @@ describe('OC.SetupChecks tests', function() {
cronInfo: { cronInfo: {
diffInSeconds: 0 diffInSeconds: 0
}, },
isTheMemoryLimitHighEnough: true isMemoryLimitSufficient: true
}) })
); );
@ -315,7 +315,7 @@ describe('OC.SetupChecks tests', function() {
cronInfo: { cronInfo: {
diffInSeconds: 0 diffInSeconds: 0
}, },
isTheMemoryLimitHighEnough: true isMemoryLimitSufficient: true
}) })
); );
@ -360,7 +360,7 @@ describe('OC.SetupChecks tests', function() {
cronInfo: { cronInfo: {
diffInSeconds: 0 diffInSeconds: 0
}, },
isTheMemoryLimitHighEnough: true isMemoryLimitSufficient: true
}) })
); );
@ -405,7 +405,7 @@ describe('OC.SetupChecks tests', function() {
cronInfo: { cronInfo: {
diffInSeconds: 0 diffInSeconds: 0
}, },
isTheMemoryLimitHighEnough: true isMemoryLimitSufficient: true
}) })
); );
@ -450,7 +450,7 @@ describe('OC.SetupChecks tests', function() {
cronInfo: { cronInfo: {
diffInSeconds: 0 diffInSeconds: 0
}, },
isTheMemoryLimitHighEnough: true isMemoryLimitSufficient: true
}) })
); );
@ -463,7 +463,7 @@ describe('OC.SetupChecks tests', function() {
}); });
}); });
it('should return a warning if the memory limit is too small', function(done) { it('should return a warning if the memory limit is below the recommended value', function(done) {
var async = OC.SetupChecks.checkSetup(); var async = OC.SetupChecks.checkSetup();
suite.server.requests[0].respond( suite.server.requests[0].respond(
@ -495,7 +495,7 @@ describe('OC.SetupChecks tests', function() {
cronInfo: { cronInfo: {
diffInSeconds: 0 diffInSeconds: 0
}, },
isTheMemoryLimitHighEnough: false isMemoryLimitSufficient: false
}) })
); );
@ -561,7 +561,7 @@ describe('OC.SetupChecks tests', function() {
cronInfo: { cronInfo: {
diffInSeconds: 0 diffInSeconds: 0
}, },
isTheMemoryLimitHighEnough: true isMemoryLimitSufficient: true
}) })
); );
@ -607,7 +607,7 @@ describe('OC.SetupChecks tests', function() {
cronInfo: { cronInfo: {
diffInSeconds: 0 diffInSeconds: 0
}, },
isTheMemoryLimitHighEnough: true isMemoryLimitSufficient: true
}) })
); );
@ -653,7 +653,7 @@ describe('OC.SetupChecks tests', function() {
cronInfo: { cronInfo: {
diffInSeconds: 0 diffInSeconds: 0
}, },
isTheMemoryLimitHighEnough: true isMemoryLimitSufficient: true
}) })
); );
@ -699,7 +699,7 @@ describe('OC.SetupChecks tests', function() {
cronInfo: { cronInfo: {
diffInSeconds: 0 diffInSeconds: 0
}, },
isTheMemoryLimitHighEnough: true isMemoryLimitSufficient: true
}) })
); );

View File

@ -78,13 +78,6 @@ class CheckSetupControllerTest extends TestCase {
/** @var MemoryInfo|MockObject */ /** @var MemoryInfo|MockObject */
private $memoryInfo; private $memoryInfo;
/**
* Backup of the "memory_limit" ini value before tests.
*
* @var string
*/
private $memoryLimitIniValueBeforeTest;
public function setUp() { public function setUp() {
parent::setUp(); parent::setUp();
@ -115,7 +108,7 @@ class CheckSetupControllerTest extends TestCase {
$this->lockingProvider = $this->getMockBuilder(ILockingProvider::class)->getMock(); $this->lockingProvider = $this->getMockBuilder(ILockingProvider::class)->getMock();
$this->dateTimeFormatter = $this->getMockBuilder(IDateTimeFormatter::class)->getMock(); $this->dateTimeFormatter = $this->getMockBuilder(IDateTimeFormatter::class)->getMock();
$this->memoryInfo = $this->getMockBuilder(MemoryInfo::class) $this->memoryInfo = $this->getMockBuilder(MemoryInfo::class)
->setMethods(['getMemoryLimit',]) ->setMethods(['isMemoryLimitSufficient',])
->getMock(); ->getMock();
$this->checkSetupController = $this->getMockBuilder('\OC\Settings\Controller\CheckSetupController') $this->checkSetupController = $this->getMockBuilder('\OC\Settings\Controller\CheckSetupController')
->setConstructorArgs([ ->setConstructorArgs([
@ -440,8 +433,8 @@ class CheckSetupControllerTest extends TestCase {
->method('hasPassedCheck') ->method('hasPassedCheck')
->willReturn(true); ->willReturn(true);
$this->memoryInfo $this->memoryInfo
->method('getMemoryLimit') ->method('isMemoryLimitSufficient')
->willReturn(512 * 1024 * 1024); ->willReturn(true);
$expected = new DataResponse( $expected = new DataResponse(
[ [
@ -483,7 +476,7 @@ class CheckSetupControllerTest extends TestCase {
'missingIndexes' => [], 'missingIndexes' => [],
'isPhpMailerUsed' => false, 'isPhpMailerUsed' => false,
'mailSettingsDocumentation' => 'https://server/index.php/settings/admin', 'mailSettingsDocumentation' => 'https://server/index.php/settings/admin',
'isTheMemoryLimitHighEnough' => true, 'isMemoryLimitSufficient' => true,
] ]
); );
$this->assertEquals($expected, $this->checkSetupController->check()); $this->assertEquals($expected, $this->checkSetupController->check());
@ -1180,37 +1173,4 @@ Array
); );
$this->assertEquals($expected, $this->checkSetupController->getFailedIntegrityCheckFiles()); $this->assertEquals($expected, $this->checkSetupController->getFailedIntegrityCheckFiles());
} }
/**
* This function returns test values for the memory limit check.
* 1. the memory limit
* 2. the expected check value
*
* @return array
*/
public function getMemoryLimitTestData(): array {
return [
'unlimited' => [-1, true,],
'512M' => [512 * 1024 * 1024, true,],
'1G' => [1024 * 1024 * 1024, true,],
'64M' => [64 * 1024 * 1024, false,],
];
}
/**
* Tests that if the memory limit is high enough, there is no message.
*
* @param string $memoryLimit The memory limit reported by MemoryInfo.
* @param bool $expected The expected memory check return value.
* @dataProvider getMemoryLimitTestData
*/
public function testMemoryLimit(string $memoryLimit, bool $expected) {
$this->memoryInfo
->method('getMemoryLimit')
->willReturn($memoryLimit);
$this->assertSame(
$expected,
$this->invokePrivate($this->checkSetupController, 'isTheMemoryLimitHighEnough')
);
}
} }

View File

@ -3,16 +3,12 @@
namespace Test; namespace Test;
use OC\MemoryInfo; use OC\MemoryInfo;
use PHPUnit\Framework\MockObject\MockObject;
/** /**
* This class provides tests for the MemoryInfo class. * This class provides tests for the MemoryInfo class.
*/ */
class MemoryInfoTest extends TestCase { class MemoryInfoTest extends TestCase {
/**
* @var MemoryInfo
*/
private $memoryInfo;
/** /**
* The "memory_limit" value before tests. * The "memory_limit" value before tests.
* *
@ -34,15 +30,6 @@ class MemoryInfoTest extends TestCase {
ini_set('memory_limit', $this->iniSettingBeforeTest); ini_set('memory_limit', $this->iniSettingBeforeTest);
} }
/**
* Setups a MemoryInfo instance for tests.
*
* @before
*/
public function setupMemoryInfo() {
$this->memoryInfo = new MemoryInfo();
}
/** /**
* Provides test data. * Provides test data.
* *
@ -66,8 +53,45 @@ class MemoryInfoTest extends TestCase {
* @param int $expected The expected detected memory limit. * @param int $expected The expected detected memory limit.
* @dataProvider getMemoryLimitTestData * @dataProvider getMemoryLimitTestData
*/ */
public function testMemoryLimit($iniValue, $expected) { public function testMemoryLimit($iniValue, int $expected) {
ini_set('memory_limit', $iniValue); ini_set('memory_limit', $iniValue);
self::assertEquals($expected, $this->memoryInfo->getMemoryLimit()); $memoryInfo = new MemoryInfo();
self::assertEquals($expected, $memoryInfo->getMemoryLimit());
}
/**
* Provides sufficient memory limit test data.
*
* @return array
*/
public function getSufficientMemoryTestData(): array {
return [
'unlimited' => [-1, true,],
'512M' => [512 * 1024 * 1024, true,],
'1G' => [1024 * 1024 * 1024, true,],
'256M' => [256 * 1024 * 1024, false,],
];
}
/**
* Tests that isMemoryLimitSufficient returns the correct values.
*
* @param int $memoryLimit The memory limit
* @param bool $expected If the memory limit is sufficient.
* @dataProvider getSufficientMemoryTestData
* @return void
*/
public function testIsMemoryLimitSufficient(int $memoryLimit, bool $expected) {
/* @var MemoryInfo|MockObject $memoryInfo */
$memoryInfo = $this->getMockBuilder(MemoryInfo::class)
->setMethods(['getMemoryLimit',])
->getMock();
$memoryInfo
->method('getMemoryLimit')
->willReturn($memoryLimit);
$isMemoryLimitSufficient = $memoryInfo->isMemoryLimitSufficient();
self::assertEquals($expected, $isMemoryLimitSufficient);
} }
} }