diff options
Diffstat (limited to 'tests/lib/Template/JSCombinerTest.php')
-rw-r--r-- | tests/lib/Template/JSCombinerTest.php | 170 |
1 files changed, 73 insertions, 97 deletions
diff --git a/tests/lib/Template/JSCombinerTest.php b/tests/lib/Template/JSCombinerTest.php index 5c678840c63..bc286695bc7 100644 --- a/tests/lib/Template/JSCombinerTest.php +++ b/tests/lib/Template/JSCombinerTest.php @@ -1,24 +1,8 @@ <?php + /** - * @copyright 2017, Roeland Jago Douma <roeland@famdouma.nl> - * - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace Test\Template; @@ -32,7 +16,9 @@ use OCP\Files\SimpleFS\ISimpleFile; use OCP\Files\SimpleFS\ISimpleFolder; use OCP\ICache; use OCP\ICacheFactory; +use OCP\ITempManager; use OCP\IURLGenerator; +use OCP\Server; use Psr\Log\LoggerInterface; class JSCombinerTest extends \Test\TestCase { @@ -59,7 +45,7 @@ class JSCombinerTest extends \Test\TestCase { $this->config = $this->createMock(SystemConfig::class); $this->cacheFactory = $this->createMock(ICacheFactory::class); $this->depsCache = $this->createMock(ICache::class); - $this->cacheFactory->expects($this->at(0)) + $this->cacheFactory->expects($this->atLeastOnce()) ->method('createDistributed') ->willReturn($this->depsCache); $this->logger = $this->createMock(LoggerInterface::class); @@ -72,7 +58,7 @@ class JSCombinerTest extends \Test\TestCase { ); } - public function testProcessDebugMode() { + public function testProcessDebugMode(): void { $this->config ->expects($this->once()) ->method('getValue') @@ -83,33 +69,27 @@ class JSCombinerTest extends \Test\TestCase { $this->assertFalse($actual); } - public function testProcessNotInstalled() { - $this->config - ->expects($this->at(0)) - ->method('getValue') - ->with('debug') - ->willReturn(false); + public function testProcessNotInstalled(): void { $this->config - ->expects($this->at(1)) + ->expects($this->exactly(2)) ->method('getValue') - ->with('installed') - ->willReturn(false); + ->willReturnMap([ + ['debug', false], + ['installed', false] + ]); $actual = $this->jsCombiner->process(__DIR__, '/data/combine.json', 'awesomeapp'); $this->assertFalse($actual); } - public function testProcessUncachedFileNoAppDataFolder() { - $this->config - ->expects($this->at(0)) - ->method('getValue') - ->with('debug') - ->willReturn(false); + public function testProcessUncachedFileNoAppDataFolder(): void { $this->config - ->expects($this->at(1)) + ->expects($this->exactly(2)) ->method('getValue') - ->with('installed') - ->willReturn(true); + ->willReturnMap([ + ['debug', '', false], + ['installed', '', true], + ]); $folder = $this->createMock(ISimpleFolder::class); $this->appData->expects($this->once())->method('getFolder')->with('awesomeapp')->willThrowException(new NotFoundException()); $this->appData->expects($this->once())->method('newFolder')->with('awesomeapp')->willReturn($folder); @@ -138,17 +118,14 @@ class JSCombinerTest extends \Test\TestCase { $this->assertTrue($actual); } - public function testProcessUncachedFile() { - $this->config - ->expects($this->at(0)) - ->method('getValue') - ->with('debug') - ->willReturn(false); + public function testProcessUncachedFile(): void { $this->config - ->expects($this->at(1)) + ->expects($this->exactly(2)) ->method('getValue') - ->with('installed') - ->willReturn(true); + ->willReturnMap([ + ['debug', '', false], + ['installed', '', true], + ]); $folder = $this->createMock(ISimpleFolder::class); $this->appData->expects($this->once())->method('getFolder')->with('awesomeapp')->willReturn($folder); $file = $this->createMock(ISimpleFile::class); @@ -175,17 +152,14 @@ class JSCombinerTest extends \Test\TestCase { $this->assertTrue($actual); } - public function testProcessCachedFile() { - $this->config - ->expects($this->at(0)) - ->method('getValue') - ->with('debug') - ->willReturn(false); + public function testProcessCachedFile(): void { $this->config - ->expects($this->at(1)) + ->expects($this->exactly(2)) ->method('getValue') - ->with('installed') - ->willReturn(true); + ->willReturnMap([ + ['debug', '', false], + ['installed', '', true], + ]); $folder = $this->createMock(ISimpleFolder::class); $this->appData->expects($this->once())->method('getFolder')->with('awesomeapp')->willReturn($folder); $file = $this->createMock(ISimpleFile::class); @@ -215,17 +189,14 @@ class JSCombinerTest extends \Test\TestCase { $this->assertTrue($actual); } - public function testProcessCachedFileMemcache() { + public function testProcessCachedFileMemcache(): void { $this->config - ->expects($this->at(0)) + ->expects($this->exactly(2)) ->method('getValue') - ->with('debug') - ->willReturn(false); - $this->config - ->expects($this->at(1)) - ->method('getValue') - ->with('installed') - ->willReturn(true); + ->willReturnMap([ + ['debug', '', false], + ['installed', '', true], + ]); $folder = $this->createMock(ISimpleFolder::class); $this->appData->expects($this->once()) ->method('getFolder') @@ -255,7 +226,7 @@ class JSCombinerTest extends \Test\TestCase { $this->assertTrue($actual); } - public function testIsCachedNoDepsFile() { + public function testIsCachedNoDepsFile(): void { $fileName = 'combine.json'; $folder = $this->createMock(ISimpleFolder::class); $file = $this->createMock(ISimpleFile::class); @@ -275,7 +246,7 @@ class JSCombinerTest extends \Test\TestCase { $this->assertFalse($actual); } - public function testIsCachedWithNotExistingFile() { + public function testIsCachedWithNotExistingFile(): void { $fileName = 'combine.json'; $folder = $this->createMock(ISimpleFolder::class); $folder->method('fileExists') @@ -293,7 +264,7 @@ class JSCombinerTest extends \Test\TestCase { $this->assertFalse($actual); } - public function testIsCachedWithOlderMtime() { + public function testIsCachedWithOlderMtime(): void { $fileName = 'combine.json'; $folder = $this->createMock(ISimpleFolder::class); $folder->method('fileExists') @@ -311,7 +282,7 @@ class JSCombinerTest extends \Test\TestCase { $this->assertFalse($actual); } - public function testIsCachedWithoutContent() { + public function testIsCachedWithoutContent(): void { $fileName = 'combine.json'; $folder = $this->createMock(ISimpleFolder::class); $folder->method('fileExists') @@ -331,7 +302,7 @@ class JSCombinerTest extends \Test\TestCase { $this->assertFalse($actual); } - public function testCacheNoFile() { + public function testCacheNoFile(): void { $fileName = 'combine.js'; $folder = $this->createMock(ISimpleFolder::class); @@ -364,7 +335,7 @@ class JSCombinerTest extends \Test\TestCase { $this->assertTrue($actual); } - public function testCache() { + public function testCache(): void { $fileName = 'combine.js'; $folder = $this->createMock(ISimpleFolder::class); @@ -395,19 +366,25 @@ class JSCombinerTest extends \Test\TestCase { $this->assertTrue($actual); } - public function testCacheNotPermittedException() { + public function testCacheNotPermittedException(): void { $fileName = 'combine.js'; $folder = $this->createMock(ISimpleFolder::class); $file = $this->createMock(ISimpleFile::class); $depsFile = $this->createMock(ISimpleFile::class); + $gzFile = $this->createMock(ISimpleFile::class); $path = __DIR__ . '/data/'; - $folder->expects($this->at(0))->method('getFile')->with($fileName)->willReturn($file); - $folder->expects($this->at(1))->method('getFile')->with($fileName . '.deps')->willReturn($depsFile); + $folder->expects($this->exactly(3)) + ->method('getFile') + ->willReturnMap([ + [$fileName, $file], + [$fileName . '.deps', $depsFile], + [$fileName . '.gzip', $gzFile] + ]); - $file->expects($this->at(0)) + $file->expects($this->once()) ->method('putContent') ->with('var a = \'hello\'; @@ -417,21 +394,21 @@ var b = \'world\'; '); $depsFile - ->expects($this->at(0)) + ->expects($this->once()) ->method('putContent') ->with($this->callback( - function ($content) { - $deps = json_decode($content, true); - return array_key_exists(__DIR__ . '/data//1.js', $deps) - && array_key_exists(__DIR__ . '/data//2.js', $deps); - })) + function ($content) { + $deps = json_decode($content, true); + return array_key_exists(__DIR__ . '/data//1.js', $deps) + && array_key_exists(__DIR__ . '/data//2.js', $deps); + })) ->willThrowException(new NotPermittedException()); $actual = self::invokePrivate($this->jsCombiner, 'cache', [$path, 'combine.json', $folder]); $this->assertFalse($actual); } - public function testCacheSuccess() { + public function testCacheSuccess(): void { $fileName = 'combine.js'; $folder = $this->createMock(ISimpleFolder::class); @@ -455,7 +432,7 @@ var b = \'world\'; } ); - $file->expects($this->at(0)) + $file->expects($this->once()) ->method('putContent') ->with('var a = \'hello\'; @@ -464,13 +441,13 @@ var b = \'world\'; '); - $depsFile->expects($this->at(0))->method('putContent')->with($this->callback( + $depsFile->expects($this->once())->method('putContent')->with($this->callback( function ($content) { $deps = json_decode($content, true); return array_key_exists(__DIR__ . '/data//1.js', $deps) && array_key_exists(__DIR__ . '/data//2.js', $deps); })); - $gzFile->expects($this->at(0))->method('putContent')->with($this->callback( + $gzFile->expects($this->once())->method('putContent')->with($this->callback( function ($content) { return gzdecode($content) === 'var a = \'hello\'; @@ -486,7 +463,7 @@ var b = \'world\'; $this->assertTrue($actual); } - public function dataGetCachedSCSS() { + public static function dataGetCachedSCSS(): array { return [ ['awesomeapp', 'core/js/foo.json', '/js/core/foo.js'], ['files', 'apps/files/js/foo.json', '/js/files/foo.js'] @@ -497,9 +474,9 @@ var b = \'world\'; * @param $appName * @param $fileName * @param $result - * @dataProvider dataGetCachedSCSS */ - public function testGetCachedSCSS($appName, $fileName, $result) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataGetCachedSCSS')] + public function testGetCachedSCSS($appName, $fileName, $result): void { $this->urlGenerator->expects($this->once()) ->method('linkToRoute') ->with('core.Js.getJs', [ @@ -512,9 +489,9 @@ var b = \'world\'; $this->assertEquals(substr($result, 1), $actual); } - public function testGetContent() { + public function testGetContent(): void { // Create temporary file with some content - $tmpFile = \OC::$server->getTempManager()->getTemporaryFile('JSCombinerTest'); + $tmpFile = Server::get(ITempManager::class)->getTemporaryFile('JSCombinerTest'); $pathInfo = pathinfo($tmpFile); file_put_contents($tmpFile, json_encode(['/foo/bar/test', $pathInfo['dirname'] . '/js/mytest.js'])); $tmpFilePathArray = explode('/', $pathInfo['basename']); @@ -527,16 +504,16 @@ var b = \'world\'; $this->assertEquals($expected, $this->jsCombiner->getContent($pathInfo['dirname'], $pathInfo['basename'])); } - public function testGetContentInvalidJson() { + public function testGetContentInvalidJson(): void { // Create temporary file with some content - $tmpFile = \OC::$server->getTempManager()->getTemporaryFile('JSCombinerTest'); + $tmpFile = Server::get(ITempManager::class)->getTemporaryFile('JSCombinerTest'); $pathInfo = pathinfo($tmpFile); file_put_contents($tmpFile, 'CertainlyNotJson'); $expected = []; $this->assertEquals($expected, $this->jsCombiner->getContent($pathInfo['dirname'], $pathInfo['basename'])); } - public function testResetCache() { + public function testResetCache(): void { $file = $this->createMock(ISimpleFile::class); $file->expects($this->once()) ->method('delete'); @@ -550,9 +527,8 @@ var b = \'world\'; $this->cacheFactory->expects($this->once()) ->method('createDistributed') ->willReturn($cache); - $cache->expects($this->once()) - ->method('clear') - ->with(''); + $cache->expects($this->never()) + ->method('clear'); $this->appData->expects($this->once()) ->method('getDirectoryListing') ->willReturn([$folder]); |