Make sure all tests use the TestCase method to overwrite services

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2016-10-20 15:11:01 +02:00
parent b946e3ecfb
commit 122edcd0c1
No known key found for this signature in database
GPG Key ID: E166FD8976B3BAC8
4 changed files with 20 additions and 48 deletions

View File

@ -40,7 +40,7 @@ class AppConfigTest extends TestCase {
$sql->delete('appconfig'); $sql->delete('appconfig');
$sql->execute(); $sql->execute();
$this->registerAppConfig(new \OC\AppConfig($this->connection)); $this->overwriteService('AppConfig', new \OC\AppConfig($this->connection));
$sql = $this->connection->getQueryBuilder(); $sql = $this->connection->getQueryBuilder();
$sql->insert('appconfig') $sql->insert('appconfig')
@ -130,21 +130,10 @@ class AppConfigTest extends TestCase {
$sql->execute(); $sql->execute();
} }
$this->registerAppConfig(new \OC\AppConfig(\OC::$server->getDatabaseConnection())); $this->restoreService('AppConfig');
parent::tearDown(); parent::tearDown();
} }
/**
* Register an app config object for testing purposes.
*
* @param \OCP\IAppConfig $appConfig
*/
protected function registerAppConfig($appConfig) {
\OC::$server->registerService('AppConfig', function () use ($appConfig) {
return $appConfig;
});
}
public function testGetApps() { public function testGetApps() {
$config = new \OC\AppConfig(\OC::$server->getDatabaseConnection()); $config = new \OC\AppConfig(\OC::$server->getDatabaseConnection());

View File

@ -493,24 +493,22 @@ class AppTest extends \Test\TestCase {
* @param IAppConfig $appConfig app config mock * @param IAppConfig $appConfig app config mock
*/ */
private function registerAppConfig(IAppConfig $appConfig) { private function registerAppConfig(IAppConfig $appConfig) {
\OC::$server->registerService('AppConfig', function ($c) use ($appConfig) { $this->overwriteService('AppConfig', $appConfig);
return $appConfig; $this->overwriteService('AppManager', new \OC\App\AppManager(
}); \OC::$server->getUserSession(),
\OC::$server->registerService('AppManager', function (\OC\Server $c) use ($appConfig) { $appConfig,
return new \OC\App\AppManager($c->getUserSession(), $appConfig, $c->getGroupManager(), $c->getMemCacheFactory(), $c->getEventDispatcher()); \OC::$server->getGroupManager(),
}); \OC::$server->getMemCacheFactory(),
\OC::$server->getEventDispatcher()
));
} }
/** /**
* Restore the original app config service. * Restore the original app config service.
*/ */
private function restoreAppConfig() { private function restoreAppConfig() {
\OC::$server->registerService('AppConfig', function (\OC\Server $c) { $this->restoreService('AppConfig');
return new \OC\AppConfig($c->getDatabaseConnection()); $this->restoreService('AppManager');
});
\OC::$server->registerService('AppManager', function (\OC\Server $c) {
return new \OC\App\AppManager($c->getUserSession(), $c->getAppConfig(), $c->getGroupManager(), $c->getMemCacheFactory(), $c->getEventDispatcher());
});
// Remove the cache of the mocked apps list with a forceRefresh // Remove the cache of the mocked apps list with a forceRefresh
\OC_App::getEnabledApps(); \OC_App::getEnabledApps();

View File

@ -119,12 +119,6 @@ class ShareTest extends \Test\TestCase {
parent::tearDown(); parent::tearDown();
} }
protected function setHttpHelper($httpHelper) {
\OC::$server->registerService('HTTPHelper', function () use ($httpHelper) {
return $httpHelper;
});
}
public function testShareInvalidShareType() { public function testShareInvalidShareType() {
$message = 'Share type foobar is not valid for test.txt'; $message = 'Share type foobar is not valid for test.txt';
try { try {
@ -1046,11 +1040,10 @@ class ShareTest extends \Test\TestCase {
* @param string $urlHost * @param string $urlHost
*/ */
public function testRemoteShareUrlCalls($shareWith, $urlHost) { public function testRemoteShareUrlCalls($shareWith, $urlHost) {
$oldHttpHelper = \OC::$server->query('HTTPHelper'); $httpHelperMock = $this->getMockBuilder('OC\HTTPHelper')
$httpHelperMock = $this->getMockBuilder('OC\HttpHelper')
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$this->setHttpHelper($httpHelperMock); $this->overwriteService('HTTPHelper', $httpHelperMock);
$httpHelperMock->expects($this->at(0)) $httpHelperMock->expects($this->at(0))
->method('post') ->method('post')
@ -1075,7 +1068,7 @@ class ShareTest extends \Test\TestCase {
->willReturn(['success' => true, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 100]]])]); ->willReturn(['success' => true, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 100]]])]);
\OCP\Share::unshare('test', 'test.txt', \OCP\Share::SHARE_TYPE_REMOTE, $shareWith); \OCP\Share::unshare('test', 'test.txt', \OCP\Share::SHARE_TYPE_REMOTE, $shareWith);
$this->setHttpHelper($oldHttpHelper); $this->restoreService('HTTPHelper');
} }
/** /**
@ -1473,11 +1466,10 @@ class ShareTest extends \Test\TestCase {
* Make sure that a user cannot have multiple identical shares to remote users * Make sure that a user cannot have multiple identical shares to remote users
*/ */
public function testOnlyOneRemoteShare() { public function testOnlyOneRemoteShare() {
$oldHttpHelper = \OC::$server->query('HTTPHelper'); $httpHelperMock = $this->getMockBuilder('OC\HTTPHelper')
$httpHelperMock = $this->getMockBuilder('OC\HttpHelper')
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$this->setHttpHelper($httpHelperMock); $this->overwriteService('HTTPHelper', $httpHelperMock);
$httpHelperMock->expects($this->at(0)) $httpHelperMock->expects($this->at(0))
->method('post') ->method('post')
@ -1502,7 +1494,7 @@ class ShareTest extends \Test\TestCase {
->willReturn(['success' => true, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 100]]])]); ->willReturn(['success' => true, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 100]]])]);
\OCP\Share::unshare('test', 'test.txt', \OCP\Share::SHARE_TYPE_REMOTE, 'foo@localhost'); \OCP\Share::unshare('test', 'test.txt', \OCP\Share::SHARE_TYPE_REMOTE, 'foo@localhost');
$this->setHttpHelper($oldHttpHelper); $this->restoreService('HTTPHelper');
} }
/** /**

View File

@ -87,24 +87,17 @@ class UtilTest extends \Test\TestCase {
function testFormatDateWithTZFromSession($offset, $expected, $expectedTimeZone) { function testFormatDateWithTZFromSession($offset, $expected, $expectedTimeZone) {
date_default_timezone_set("UTC"); date_default_timezone_set("UTC");
$oldDateTimeFormatter = \OC::$server->query('DateTimeFormatter');
\OC::$server->getSession()->set('timezone', $offset); \OC::$server->getSession()->set('timezone', $offset);
$selectedTimeZone = \OC::$server->getDateTimeZone()->getTimeZone(1350129205); $selectedTimeZone = \OC::$server->getDateTimeZone()->getTimeZone(1350129205);
$this->assertEquals($expectedTimeZone, $selectedTimeZone->getName()); $this->assertEquals($expectedTimeZone, $selectedTimeZone->getName());
$newDateTimeFormatter = new \OC\DateTimeFormatter($selectedTimeZone, new \OC_L10N('lib', 'en')); $newDateTimeFormatter = new \OC\DateTimeFormatter($selectedTimeZone, new \OC_L10N('lib', 'en'));
$this->setDateFormatter($newDateTimeFormatter); $this->overwriteService('DateTimeFormatter', $newDateTimeFormatter);
$result = OC_Util::formatDate(1350129205, false); $result = OC_Util::formatDate(1350129205, false);
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
$this->setDateFormatter($oldDateTimeFormatter); $this->restoreService('DateTimeFormatter');
}
protected function setDateFormatter($formatter) {
\OC::$server->registerService('DateTimeFormatter', function ($c) use ($formatter) {
return $formatter;
});
} }
function testSanitizeHTML() { function testSanitizeHTML() {