diff options
Diffstat (limited to 'tests/lib/App/AppStore/Fetcher/FetcherBase.php')
-rw-r--r-- | tests/lib/App/AppStore/Fetcher/FetcherBase.php | 326 |
1 files changed, 136 insertions, 190 deletions
diff --git a/tests/lib/App/AppStore/Fetcher/FetcherBase.php b/tests/lib/App/AppStore/Fetcher/FetcherBase.php index 87a09cb617d..a5a9cc73974 100644 --- a/tests/lib/App/AppStore/Fetcher/FetcherBase.php +++ b/tests/lib/App/AppStore/Fetcher/FetcherBase.php @@ -1,22 +1,8 @@ <?php + /** - * @copyright Copyright (c) 2016 Lukas Reschke <lukas@statuscode.ch> - * - * @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: 2016 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace Test\App\AppStore\Fetcher; @@ -74,24 +60,17 @@ abstract class FetcherBase extends TestCase { $this->registry = $this->createMock(IRegistry::class); } - public function testGetWithAlreadyExistingFileAndUpToDateTimestampAndVersion() { - $this->config - ->expects($this->at(0)) - ->method('getSystemValueBool') - ->with('appstoreenabled', true) - ->willReturn(true); + public function testGetWithAlreadyExistingFileAndUpToDateTimestampAndVersion(): void { $this->config - ->expects($this->at(1)) - ->method('getSystemValue') - ->with('has_internet_connection', true) - ->willReturn(true); - $this->config - ->expects($this->at(2)) - ->method('getSystemValue') - ->with( - $this->equalTo('version'), - $this->anything() - )->willReturn('11.0.0.2'); + ->method('getSystemValueString') + ->willReturnCallback(function ($var, $default) { + if ($var === 'version') { + return '11.0.0.2'; + } + return $default; + }); + $this->config->method('getSystemValueBool') + ->willReturnArgument(1); $folder = $this->createMock(ISimpleFolder::class); $file = $this->createMock(ISimpleFile::class); @@ -122,23 +101,19 @@ abstract class FetcherBase extends TestCase { $this->assertSame($expected, $this->fetcher->get()); } - public function testGetWithNotExistingFileAndUpToDateTimestampAndVersion() { + public function testGetWithNotExistingFileAndUpToDateTimestampAndVersion(): void { $this->config - ->method('getSystemValue') + ->method('getSystemValueString') ->willReturnCallback(function ($var, $default) { - if ($var === 'has_internet_connection') { - return true; - } elseif ($var === 'appstoreurl') { + if ($var === 'appstoreurl') { return 'https://apps.nextcloud.com/api/v1'; } elseif ($var === 'version') { return '11.0.0.2'; } return $default; }); - $this->config - ->method('getSystemValueBool') - ->with('appstoreenabled', $this->anything()) - ->willReturn(true); + $this->config->method('getSystemValueBool') + ->willReturnArgument(1); $folder = $this->createMock(ISimpleFolder::class); $file = $this->createMock(ISimpleFile::class); @@ -148,12 +123,12 @@ abstract class FetcherBase extends TestCase { ->with('/') ->willReturn($folder); $folder - ->expects($this->at(0)) + ->expects($this->once()) ->method('getFile') ->with($this->fileName) ->willThrowException(new NotFoundException()); $folder - ->expects($this->at(1)) + ->expects($this->once()) ->method('newFile') ->with($this->fileName) ->willReturn($file); @@ -177,15 +152,15 @@ abstract class FetcherBase extends TestCase { ->willReturn('"myETag"'); $fileData = '{"data":[{"id":"MyNewApp","foo":"foo"},{"id":"bar"}],"timestamp":1502,"ncversion":"11.0.0.2","ETag":"\"myETag\""}'; $file - ->expects($this->at(0)) + ->expects($this->once()) ->method('putContent') ->with($fileData); $file - ->expects($this->at(1)) + ->expects($this->once()) ->method('getContent') ->willReturn($fileData); $this->timeFactory - ->expects($this->at(0)) + ->expects($this->once()) ->method('getTime') ->willReturn(1502); @@ -201,8 +176,8 @@ abstract class FetcherBase extends TestCase { $this->assertSame($expected, $this->fetcher->get()); } - public function testGetWithAlreadyExistingFileAndOutdatedTimestamp() { - $this->config->method('getSystemValue') + public function testGetWithAlreadyExistingFileAndOutdatedTimestamp(): void { + $this->config->method('getSystemValueString') ->willReturnCallback(function ($key, $default) { if ($key === 'version') { return '11.0.0.2'; @@ -210,10 +185,8 @@ abstract class FetcherBase extends TestCase { return $default; } }); - $this->config - ->method('getSystemValueBool') - ->with('appstoreenabled', true) - ->willReturn(true); + $this->config->method('getSystemValueBool') + ->willReturnArgument(1); $folder = $this->createMock(ISimpleFolder::class); $file = $this->createMock(ISimpleFile::class); @@ -227,14 +200,25 @@ abstract class FetcherBase extends TestCase { ->method('getFile') ->with($this->fileName) ->willReturn($file); + $fileData = '{"data":[{"id":"MyNewApp","foo":"foo"},{"id":"bar"}],"timestamp":1502,"ncversion":"11.0.0.2","ETag":"\"myETag\""}'; $file - ->expects($this->at(0)) + ->expects($this->once()) + ->method('putContent') + ->with($fileData); + $file + ->expects($this->exactly(2)) ->method('getContent') - ->willReturn('{"timestamp":1200,"data":{"MyApp":{"id":"MyApp"}},"ncversion":"11.0.0.2"}'); + ->willReturnOnConsecutiveCalls( + '{"timestamp":1200,"data":{"MyApp":{"id":"MyApp"}},"ncversion":"11.0.0.2"}', + $fileData + ); $this->timeFactory - ->expects($this->at(0)) + ->expects($this->exactly(2)) ->method('getTime') - ->willReturn(4801); + ->willReturnOnConsecutiveCalls( + 4801, + 1502 + ); $client = $this->createMock(IClient::class); $this->clientService ->expects($this->once()) @@ -253,19 +237,6 @@ abstract class FetcherBase extends TestCase { $response->method('getHeader') ->with($this->equalTo('ETag')) ->willReturn('"myETag"'); - $fileData = '{"data":[{"id":"MyNewApp","foo":"foo"},{"id":"bar"}],"timestamp":1502,"ncversion":"11.0.0.2","ETag":"\"myETag\""}'; - $file - ->expects($this->at(1)) - ->method('putContent') - ->with($fileData); - $file - ->expects($this->at(2)) - ->method('getContent') - ->willReturn($fileData); - $this->timeFactory - ->expects($this->at(1)) - ->method('getTime') - ->willReturn(1502); $expected = [ [ @@ -279,23 +250,19 @@ abstract class FetcherBase extends TestCase { $this->assertSame($expected, $this->fetcher->get()); } - public function testGetWithAlreadyExistingFileAndNoVersion() { + public function testGetWithAlreadyExistingFileAndNoVersion(): void { $this->config - ->method('getSystemValue') + ->method('getSystemValueString') ->willReturnCallback(function ($var, $default) { - if ($var === 'has_internet_connection') { - return true; - } elseif ($var === 'appstoreurl') { + if ($var === 'appstoreurl') { return 'https://apps.nextcloud.com/api/v1'; } elseif ($var === 'version') { return '11.0.0.2'; } return $default; }); - $this->config - ->method('getSystemValueBool') - ->with('appstoreenabled', true) - ->willReturn(true); + $this->config->method('getSystemValueBool') + ->willReturnArgument(1); $folder = $this->createMock(ISimpleFolder::class); $file = $this->createMock(ISimpleFile::class); @@ -309,12 +276,20 @@ abstract class FetcherBase extends TestCase { ->method('getFile') ->with($this->fileName) ->willReturn($file); + $fileData = '{"data":[{"id":"MyNewApp","foo":"foo"},{"id":"bar"}],"timestamp":1201,"ncversion":"11.0.0.2","ETag":"\"myETag\""}'; + $file + ->expects($this->once()) + ->method('putContent') + ->with($fileData); $file - ->expects($this->at(0)) + ->expects($this->exactly(2)) ->method('getContent') - ->willReturn('{"timestamp":1200,"data":{"MyApp":{"id":"MyApp"}}'); + ->willReturnOnConsecutiveCalls( + '{"timestamp":1200,"data":{"MyApp":{"id":"MyApp"}}', + $fileData + ); $this->timeFactory - ->expects($this->at(0)) + ->expects($this->once()) ->method('getTime') ->willReturn(1201); $client = $this->createMock(IClient::class); @@ -335,15 +310,6 @@ abstract class FetcherBase extends TestCase { $response->method('getHeader') ->with($this->equalTo('ETag')) ->willReturn('"myETag"'); - $fileData = '{"data":[{"id":"MyNewApp","foo":"foo"},{"id":"bar"}],"timestamp":1201,"ncversion":"11.0.0.2","ETag":"\"myETag\""}'; - $file - ->expects($this->at(1)) - ->method('putContent') - ->with($fileData); - $file - ->expects($this->at(2)) - ->method('getContent') - ->willReturn($fileData); $expected = [ [ @@ -357,23 +323,19 @@ abstract class FetcherBase extends TestCase { $this->assertSame($expected, $this->fetcher->get()); } - public function testGetWithAlreadyExistingFileAndOutdatedVersion() { + public function testGetWithAlreadyExistingFileAndOutdatedVersion(): void { $this->config - ->method('getSystemValue') + ->method('getSystemValueString') ->willReturnCallback(function ($var, $default) { - if ($var === 'has_internet_connection') { - return true; - } elseif ($var === 'appstoreurl') { + if ($var === 'appstoreurl') { return 'https://apps.nextcloud.com/api/v1'; } elseif ($var === 'version') { return '11.0.0.2'; } return $default; }); - $this->config - ->method('getSystemValueBool') - ->with('appstoreenabled', true) - ->willReturn(true); + $this->config->method('getSystemValueBool') + ->willReturnArgument(1); $folder = $this->createMock(ISimpleFolder::class); $file = $this->createMock(ISimpleFile::class); @@ -387,10 +349,18 @@ abstract class FetcherBase extends TestCase { ->method('getFile') ->with($this->fileName) ->willReturn($file); + $fileData = '{"data":[{"id":"MyNewApp","foo":"foo"},{"id":"bar"}],"timestamp":1201,"ncversion":"11.0.0.2","ETag":"\"myETag\""}'; + $file + ->expects($this->once()) + ->method('putContent') + ->with($fileData); $file - ->expects($this->at(0)) + ->expects($this->exactly(2)) ->method('getContent') - ->willReturn('{"timestamp":1200,"data":{"MyApp":{"id":"MyApp"}},"ncversion":"11.0.0.1"'); + ->willReturnOnConsecutiveCalls( + '{"timestamp":1200,"data":{"MyApp":{"id":"MyApp"}},"ncversion":"11.0.0.1"', + $fileData + ); $this->timeFactory ->method('getTime') ->willReturn(1201); @@ -412,15 +382,6 @@ abstract class FetcherBase extends TestCase { $response->method('getHeader') ->with($this->equalTo('ETag')) ->willReturn('"myETag"'); - $fileData = '{"data":[{"id":"MyNewApp","foo":"foo"},{"id":"bar"}],"timestamp":1201,"ncversion":"11.0.0.2","ETag":"\"myETag\""}'; - $file - ->expects($this->at(1)) - ->method('putContent') - ->with($fileData); - $file - ->expects($this->at(2)) - ->method('getContent') - ->willReturn($fileData); $expected = [ [ @@ -434,15 +395,11 @@ abstract class FetcherBase extends TestCase { $this->assertSame($expected, $this->fetcher->get()); } - public function testGetWithExceptionInClient() { - $this->config->method('getSystemValue') - ->willReturnCallback(function ($key, $default) { - return $default; - }); - $this->config - ->method('getSystemValueBool') - ->with('appstoreenabled', true) - ->willReturn(true); + public function testGetWithExceptionInClient(): void { + $this->config->method('getSystemValueString') + ->willReturnArgument(1); + $this->config->method('getSystemValueBool') + ->willReturnArgument(1); $folder = $this->createMock(ISimpleFolder::class); $file = $this->createMock(ISimpleFile::class); @@ -457,7 +414,7 @@ abstract class FetcherBase extends TestCase { ->with($this->fileName) ->willReturn($file); $file - ->expects($this->at(0)) + ->expects($this->once()) ->method('getContent') ->willReturn('{"timestamp":1200,"data":{"MyApp":{"id":"MyApp"}}}'); $client = $this->createMock(IClient::class); @@ -474,8 +431,8 @@ abstract class FetcherBase extends TestCase { $this->assertSame([], $this->fetcher->get()); } - public function testGetMatchingETag() { - $this->config->method('getSystemValue') + public function testGetMatchingETag(): void { + $this->config->method('getSystemValueString') ->willReturnCallback(function ($key, $default) { if ($key === 'version') { return '11.0.0.2'; @@ -483,10 +440,8 @@ abstract class FetcherBase extends TestCase { return $default; } }); - $this->config - ->method('getSystemValueBool') - ->with('appstoreenabled', true) - ->willReturn(true); + $this->config->method('getSystemValueBool') + ->willReturnArgument(1); $folder = $this->createMock(ISimpleFolder::class); $file = $this->createMock(ISimpleFile::class); @@ -501,18 +456,26 @@ abstract class FetcherBase extends TestCase { ->with($this->fileName) ->willReturn($file); $origData = '{"data":[{"id":"MyNewApp","foo":"foo"},{"id":"bar"}],"timestamp":1200,"ncversion":"11.0.0.2","ETag":"\"myETag\""}'; + + $newData = '{"data":[{"id":"MyNewApp","foo":"foo"},{"id":"bar"}],"timestamp":4802,"ncversion":"11.0.0.2","ETag":"\"myETag\""}'; $file - ->expects($this->at(0)) + ->expects($this->once()) + ->method('putContent') + ->with($newData); + $file + ->expects($this->exactly(2)) ->method('getContent') - ->willReturn($origData); - $this->timeFactory - ->expects($this->at(0)) - ->method('getTime') - ->willReturn(4801); + ->willReturnOnConsecutiveCalls( + $origData, + $newData, + ); $this->timeFactory - ->expects($this->at(1)) + ->expects($this->exactly(2)) ->method('getTime') - ->willReturn(4802); + ->willReturnOnConsecutiveCalls( + 4801, + 4802 + ); $client = $this->createMock(IClient::class); $this->clientService ->expects($this->once()) @@ -534,16 +497,6 @@ abstract class FetcherBase extends TestCase { $response->method('getStatusCode') ->willReturn(304); - $newData = '{"data":[{"id":"MyNewApp","foo":"foo"},{"id":"bar"}],"timestamp":4802,"ncversion":"11.0.0.2","ETag":"\"myETag\""}'; - $file - ->expects($this->at(1)) - ->method('putContent') - ->with($newData); - $file - ->expects($this->at(2)) - ->method('getContent') - ->willReturn($newData); - $expected = [ [ 'id' => 'MyNewApp', @@ -557,8 +510,8 @@ abstract class FetcherBase extends TestCase { $this->assertSame($expected, $this->fetcher->get()); } - public function testGetNoMatchingETag() { - $this->config->method('getSystemValue') + public function testGetNoMatchingETag(): void { + $this->config->method('getSystemValueString') ->willReturnCallback(function ($key, $default) { if ($key === 'version') { return '11.0.0.2'; @@ -566,10 +519,8 @@ abstract class FetcherBase extends TestCase { return $default; } }); - $this->config - ->method('getSystemValueBool') - ->with('appstoreenabled', true) - ->willReturn(true); + $this->config->method('getSystemValueBool') + ->willReturnArgument(1); $folder = $this->createMock(ISimpleFolder::class); $file = $this->createMock(ISimpleFile::class); @@ -579,14 +530,29 @@ abstract class FetcherBase extends TestCase { ->with('/') ->willReturn($folder); $folder - ->expects($this->at(0)) + ->expects($this->once()) ->method('getFile') ->with($this->fileName) ->willReturn($file); + $fileData = '{"data":[{"id":"MyNewApp","foo":"foo"},{"id":"bar"}],"timestamp":4802,"ncversion":"11.0.0.2","ETag":"\"newETag\""}'; + $file + ->expects($this->once()) + ->method('putContent') + ->with($fileData); $file - ->expects($this->at(0)) + ->expects($this->exactly(2)) ->method('getContent') - ->willReturn('{"data":[{"id":"MyOldApp","abc":"def"}],"timestamp":1200,"ncversion":"11.0.0.2","ETag":"\"myETag\""}'); + ->willReturnOnConsecutiveCalls( + '{"data":[{"id":"MyOldApp","abc":"def"}],"timestamp":1200,"ncversion":"11.0.0.2","ETag":"\"myETag\""}', + $fileData, + ); + $this->timeFactory + ->expects($this->exactly(2)) + ->method('getTime') + ->willReturnOnConsecutiveCalls( + 4801, + 4802, + ); $client = $this->createMock(IClient::class); $this->clientService ->expects($this->once()) @@ -615,23 +581,6 @@ abstract class FetcherBase extends TestCase { $response->method('getHeader') ->with($this->equalTo('ETag')) ->willReturn('"newETag"'); - $fileData = '{"data":[{"id":"MyNewApp","foo":"foo"},{"id":"bar"}],"timestamp":4802,"ncversion":"11.0.0.2","ETag":"\"newETag\""}'; - $file - ->expects($this->at(1)) - ->method('putContent') - ->with($fileData); - $file - ->expects($this->at(2)) - ->method('getContent') - ->willReturn($fileData); - $this->timeFactory - ->expects($this->at(0)) - ->method('getTime') - ->willReturn(4801); - $this->timeFactory - ->expects($this->at(1)) - ->method('getTime') - ->willReturn(4802); $expected = [ [ @@ -646,8 +595,8 @@ abstract class FetcherBase extends TestCase { } - public function testFetchAfterUpgradeNoETag() { - $this->config->method('getSystemValue') + public function testFetchAfterUpgradeNoETag(): void { + $this->config->method('getSystemValueString') ->willReturnCallback(function ($key, $default) { if ($key === 'version') { return '11.0.0.3'; @@ -655,10 +604,8 @@ abstract class FetcherBase extends TestCase { return $default; } }); - $this->config - ->method('getSystemValueBool') - ->with('appstoreenabled', true) - ->willReturn(true); + $this->config->method('getSystemValueBool') + ->willReturnArgument(1); $folder = $this->createMock(ISimpleFolder::class); $file = $this->createMock(ISimpleFile::class); @@ -668,14 +615,22 @@ abstract class FetcherBase extends TestCase { ->with('/') ->willReturn($folder); $folder - ->expects($this->at(0)) + ->expects($this->once()) ->method('getFile') ->with($this->fileName) ->willReturn($file); + $fileData = '{"data":[{"id":"MyNewApp","foo":"foo"},{"id":"bar"}],"timestamp":1501,"ncversion":"11.0.0.3","ETag":"\"newETag\""}'; + $file + ->expects($this->once()) + ->method('putContent') + ->with($fileData); $file - ->expects($this->at(0)) + ->expects($this->exactly(2)) ->method('getContent') - ->willReturn('{"data":[{"id":"MyOldApp","abc":"def"}],"timestamp":1200,"ncversion":"11.0.0.2","ETag":"\"myETag\""}'); + ->willReturnOnConsecutiveCalls( + '{"data":[{"id":"MyOldApp","abc":"def"}],"timestamp":1200,"ncversion":"11.0.0.2","ETag":"\"myETag\""}', + $fileData + ); $client = $this->createMock(IClient::class); $this->clientService ->expects($this->once()) @@ -701,15 +656,6 @@ abstract class FetcherBase extends TestCase { $response->method('getHeader') ->with($this->equalTo('ETag')) ->willReturn('"newETag"'); - $fileData = '{"data":[{"id":"MyNewApp","foo":"foo"},{"id":"bar"}],"timestamp":1501,"ncversion":"11.0.0.3","ETag":"\"newETag\""}'; - $file - ->expects($this->at(1)) - ->method('putContent') - ->with($fileData); - $file - ->expects($this->at(2)) - ->method('getContent') - ->willReturn($fileData); $this->timeFactory ->expects($this->once()) ->method('getTime') |