Fix tests

Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
Julius Härtl 2020-09-24 08:20:03 +02:00
parent c5792f698a
commit 0dd6819b5f
No known key found for this signature in database
GPG Key ID: 4C614C6ED2CDE6DF
2 changed files with 78 additions and 75 deletions

View File

@ -49,12 +49,12 @@ use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Files\IAppData; use OCP\Files\IAppData;
use OCP\Files\NotFoundException; use OCP\Files\NotFoundException;
use OCP\Files\SimpleFS\ISimpleFile; use OCP\Files\SimpleFS\ISimpleFile;
use OCP\Files\SimpleFS\ISimpleFolder;
use OCP\IConfig; use OCP\IConfig;
use OCP\IL10N; use OCP\IL10N;
use OCP\IRequest; use OCP\IRequest;
use OCP\ITempManager; use OCP\ITempManager;
use OCP\IURLGenerator; use OCP\IURLGenerator;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase; use Test\TestCase;
class ThemingControllerTest extends TestCase { class ThemingControllerTest extends TestCase {
@ -98,12 +98,12 @@ class ThemingControllerTest extends TestCase {
$this->urlGenerator = $this->createMock(IURLGenerator::class); $this->urlGenerator = $this->createMock(IURLGenerator::class);
$this->imageManager = $this->createMock(ImageManager::class); $this->imageManager = $this->createMock(ImageManager::class);
$this->timeFactory = $this->createMock(ITimeFactory::class); $timeFactory = $this->createMock(ITimeFactory::class);
$this->timeFactory->expects($this->any()) $timeFactory->expects($this->any())
->method('getTime') ->method('getTime')
->willReturn(123); ->willReturn(123);
$this->overwriteService(ITimeFactory::class, $this->timeFactory); $this->overwriteService(ITimeFactory::class, $timeFactory);
$this->themingController = new ThemingController( $this->themingController = new ThemingController(
'theming', 'theming',
@ -293,12 +293,9 @@ class ThemingControllerTest extends TestCase {
return $str; return $str;
}); });
$folder = $this->createMock(ISimpleFolder::class); $this->imageManager->expects($this->once())
$this->appData ->method('updateImage')
->expects($this->once()) ->willThrowException(new \Exception('Unsupported image type'));
->method('getFolder')
->with('images')
->willReturn($folder);
$expected = new DataResponse( $expected = new DataResponse(
[ [
@ -337,12 +334,9 @@ class ThemingControllerTest extends TestCase {
return $str; return $str;
}); });
$folder = $this->createMock(ISimpleFolder::class); $this->imageManager->expects($this->once())
$this->appData ->method('updateImage')
->expects($this->once()) ->willThrowException(new \Exception('Unsupported image type'));
->method('getFolder')
->with('images')
->willReturn($folder);
$expected = new DataResponse( $expected = new DataResponse(
[ [
@ -398,31 +392,6 @@ class ThemingControllerTest extends TestCase {
return $str; return $str;
}); });
$file = $this->createMock(ISimpleFile::class);
$folder = $this->createMock(ISimpleFolder::class);
if ($folderExists) {
$this->appData
->expects($this->once())
->method('getFolder')
->with('images')
->willReturn($folder);
} else {
$this->appData
->expects($this->at(0))
->method('getFolder')
->with('images')
->willThrowException(new NotFoundException());
$this->appData
->expects($this->at(1))
->method('newFolder')
->with('images')
->willReturn($folder);
}
$folder->expects($this->once())
->method('newFile')
->with('logo')
->willReturn($file);
$this->urlGenerator->expects($this->once()) $this->urlGenerator->expects($this->once())
->method('linkTo') ->method('linkTo')
->willReturn('serverCss'); ->willReturn('serverCss');
@ -430,6 +399,10 @@ class ThemingControllerTest extends TestCase {
->method('getImageUrl') ->method('getImageUrl')
->with('logo') ->with('logo')
->willReturn('imageUrl'); ->willReturn('imageUrl');
$this->imageManager->expects($this->once())
->method('updateImage');
$expected = new DataResponse( $expected = new DataResponse(
[ [
'data' => 'data' =>
@ -474,30 +447,8 @@ class ThemingControllerTest extends TestCase {
return $str; return $str;
}); });
$file = $this->createMock(ISimpleFile::class); $this->imageManager->expects($this->once())
$folder = $this->createMock(ISimpleFolder::class); ->method('updateImage');
if ($folderExists) {
$this->appData
->expects($this->once())
->method('getFolder')
->with('images')
->willReturn($folder);
} else {
$this->appData
->expects($this->at(0))
->method('getFolder')
->with('images')
->willThrowException(new NotFoundException());
$this->appData
->expects($this->at(1))
->method('newFolder')
->with('images')
->willReturn($folder);
}
$folder->expects($this->once())
->method('newFile')
->with('background')
->willReturn($file);
$this->urlGenerator->expects($this->once()) $this->urlGenerator->expects($this->once())
->method('linkTo') ->method('linkTo')
@ -548,12 +499,9 @@ class ThemingControllerTest extends TestCase {
return $str; return $str;
}); });
$folder = $this->createMock(ISimpleFolder::class); $this->imageManager->expects($this->once())
$this->appData ->method('updateImage')
->expects($this->once()) ->willThrowException(new \Exception('Unsupported image type'));
->method('getFolder')
->with('images')
->willReturn($folder);
$expected = new DataResponse( $expected = new DataResponse(
[ [
@ -723,9 +671,6 @@ class ThemingControllerTest extends TestCase {
->method('linkTo') ->method('linkTo')
->with('', '/core/css/someHash-css-variables.scss') ->with('', '/core/css/someHash-css-variables.scss')
->willReturn('/nextcloudWebroot/core/css/someHash-css-variables.scss'); ->willReturn('/nextcloudWebroot/core/css/someHash-css-variables.scss');
$this->imageManager->expects($this->once())
->method('delete')
->with($filename);
$expected = new DataResponse( $expected = new DataResponse(
[ [

View File

@ -35,7 +35,9 @@ use OCP\Files\SimpleFS\ISimpleFolder;
use OCP\ICacheFactory; use OCP\ICacheFactory;
use OCP\IConfig; use OCP\IConfig;
use OCP\ILogger; use OCP\ILogger;
use OCP\ITempManager;
use OCP\IURLGenerator; use OCP\IURLGenerator;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase; use Test\TestCase;
class ImageManagerTest extends TestCase { class ImageManagerTest extends TestCase {
@ -52,6 +54,8 @@ class ImageManagerTest extends TestCase {
private $cacheFactory; private $cacheFactory;
/** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */ /** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
private $logger; private $logger;
/** @var ITempManager|MockObject */
private $tempManager;
protected function setUp(): void { protected function setUp(): void {
parent::setUp(); parent::setUp();
@ -60,12 +64,14 @@ class ImageManagerTest extends TestCase {
$this->urlGenerator = $this->createMock(IURLGenerator::class); $this->urlGenerator = $this->createMock(IURLGenerator::class);
$this->cacheFactory = $this->createMock(ICacheFactory::class); $this->cacheFactory = $this->createMock(ICacheFactory::class);
$this->logger = $this->createMock(ILogger::class); $this->logger = $this->createMock(ILogger::class);
$this->tempManager = $this->createMock(ITempManager::class);
$this->imageManager = new ImageManager( $this->imageManager = new ImageManager(
$this->config, $this->config,
$this->appData, $this->appData,
$this->urlGenerator, $this->urlGenerator,
$this->cacheFactory, $this->cacheFactory,
$this->logger $this->logger,
$this->tempManager
); );
} }
@ -326,4 +332,56 @@ class ImageManagerTest extends TestCase {
->willReturn($folders[2]); ->willReturn($folders[2]);
$this->imageManager->cleanup(); $this->imageManager->cleanup();
} }
public function dataUpdateImage() {
return [
['background', __DIR__ . '/../../../tests/data/testimage.png', true, true],
['background', __DIR__ . '/../../../tests/data/testimage.png', false, true],
['background', __DIR__ . '/../../../tests/data/testimage.jpg', true, true],
['logo', __DIR__ . '/../../../tests/data/testimagelarge.svg', true, false],
];
}
/**
* @dataProvider dataUpdateImage
*/
public function testUpdateImage($key, $tmpFile, $folderExists, $shouldConvert) {
$file = $this->createMock(ISimpleFile::class);
$folder = $this->createMock(ISimpleFolder::class);
$oldFile = $this->createMock(ISimpleFile::class);
$folder->expects($this->any())
->method('getFile')
->willReturn($oldFile);
if ($folderExists) {
$this->appData
->expects($this->any())
->method('getFolder')
->with('images')
->willReturn($folder);
} else {
$this->appData
->expects($this->any())
->method('getFolder')
->with('images')
->willThrowException(new NotFoundException());
$this->appData
->expects($this->any())
->method('newFolder')
->with('images')
->willReturn($folder);
}
$folder->expects($this->once())
->method('newFile')
->with($key)
->willReturn($file);
if ($shouldConvert) {
$this->tempManager->expects($this->once())
->method('getTemporaryFile')
->willReturn('/tmp/randomtempfile-theming');
}
$this->imageManager->updateImage($key, $tmpFile);
}
} }