Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Härtl <jus@bitgrid.net>
  • Loading branch information
juliushaertl committed Sep 24, 2020
1 parent aa95b3d commit 0daf097
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 90 deletions.
109 changes: 26 additions & 83 deletions apps/theming/tests/Controller/ThemingControllerTest.php
Expand Up @@ -47,34 +47,32 @@
use OCP\Files\IAppData;
use OCP\Files\NotFoundException;
use OCP\Files\SimpleFS\ISimpleFile;
use OCP\Files\SimpleFS\ISimpleFolder;
use OCP\IConfig;
use OCP\IL10N;
use OCP\IRequest;
use OCP\ITempManager;
use OCP\IURLGenerator;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;

class ThemingControllerTest extends TestCase {
/** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
/** @var IRequest|MockObject */
private $request;
/** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
/** @var IConfig|MockObject */
private $config;
/** @var ThemingDefaults|\PHPUnit\Framework\MockObject\MockObject */
/** @var ThemingDefaults|MockObject */
private $themingDefaults;
/** @var \OCP\AppFramework\Utility\ITimeFactory */
private $timeFactory;
/** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */
/** @var IL10N|MockObject */
private $l10n;
/** @var ThemingController */
private $themingController;
/** @var ITempManager */
private $tempManager;
/** @var IAppManager|\PHPUnit\Framework\MockObject\MockObject */
/** @var IAppManager|MockObject */
private $appManager;
/** @var IAppData|\PHPUnit\Framework\MockObject\MockObject */
/** @var IAppData|MockObject */
private $appData;
/** @var ImageManager|\PHPUnit\Framework\MockObject\MockObject */
/** @var ImageManager|MockObject */
private $imageManager;
/** @var SCSSCacher */
private $scssCacher;
Expand All @@ -93,12 +91,12 @@ protected function setUp(): void {
$this->urlGenerator = $this->createMock(IURLGenerator::class);
$this->imageManager = $this->createMock(ImageManager::class);

$this->timeFactory = $this->createMock(ITimeFactory::class);
$this->timeFactory->expects($this->any())
$timeFactory = $this->createMock(ITimeFactory::class);
$timeFactory->expects($this->any())
->method('getTime')
->willReturn(123);

$this->overwriteService(ITimeFactory::class, $this->timeFactory);
$this->overwriteService(ITimeFactory::class, $timeFactory);

$this->themingController = new ThemingController(
'theming',
Expand Down Expand Up @@ -287,12 +285,9 @@ public function testUploadSVGFaviconWithoutImagemagick() {
return $str;
});

$folder = $this->createMock(ISimpleFolder::class);
$this->appData
->expects($this->once())
->method('getFolder')
->with('images')
->willReturn($folder);
$this->imageManager->expects($this->once())
->method('updateImage')
->willThrowException(new \Exception('Unsupported image type'));

$expected = new DataResponse(
[
Expand Down Expand Up @@ -331,12 +326,9 @@ public function testUpdateLogoInvalidMimeType() {
return $str;
});

$folder = $this->createMock(ISimpleFolder::class);
$this->appData
->expects($this->once())
->method('getFolder')
->with('images')
->willReturn($folder);
$this->imageManager->expects($this->once())
->method('updateImage')
->willThrowException(new \Exception('Unsupported image type'));

$expected = new DataResponse(
[
Expand Down Expand Up @@ -392,38 +384,17 @@ public function testUpdateLogoNormalLogoUpload($mimeType, $folderExists=true) {
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())
->method('linkTo')
->willReturn('serverCss');
$this->imageManager->expects($this->once())
->method('getImageUrl')
->with('logo')
->willReturn('imageUrl');

$this->imageManager->expects($this->once())
->method('updateImage');

$expected = new DataResponse(
[
'data' =>
Expand Down Expand Up @@ -468,30 +439,8 @@ public function testUpdateLogoLoginScreenUpload($folderExists) {
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('background')
->willReturn($file);
$this->imageManager->expects($this->once())
->method('updateImage');

$this->urlGenerator->expects($this->once())
->method('linkTo')
Expand Down Expand Up @@ -542,12 +491,9 @@ public function testUpdateLogoLoginScreenUploadWithInvalidImage() {
return $str;
});

$folder = $this->createMock(ISimpleFolder::class);
$this->appData
->expects($this->once())
->method('getFolder')
->with('images')
->willReturn($folder);
$this->imageManager->expects($this->once())
->method('updateImage')
->willThrowException(new \Exception('Unsupported image type'));

$expected = new DataResponse(
[
Expand Down Expand Up @@ -717,9 +663,6 @@ public function testUndoDelete($value, $filename) {
->method('linkTo')
->with('', '/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(
[
Expand Down
72 changes: 65 additions & 7 deletions apps/theming/tests/ImageManagerTest.php
Expand Up @@ -36,23 +36,27 @@
use OCP\ICacheFactory;
use OCP\IConfig;
use OCP\ILogger;
use OCP\ITempManager;
use OCP\IURLGenerator;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;

class ImageManagerTest extends TestCase {

/** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
/** @var IConfig|MockObject */
protected $config;
/** @var IAppData|\PHPUnit\Framework\MockObject\MockObject */
/** @var IAppData|MockObject */
protected $appData;
/** @var ImageManager */
protected $imageManager;
/** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
/** @var IURLGenerator|MockObject */
private $urlGenerator;
/** @var ICacheFactory|\PHPUnit\Framework\MockObject\MockObject */
/** @var ICacheFactory|MockObject */
private $cacheFactory;
/** @var ILogger|\PHPUnit\Framework\MockObject\MockObject */
/** @var ILogger|MockObject */
private $logger;
/** @var ITempManager|MockObject */
private $tempManager;

protected function setUp(): void {
parent::setUp();
Expand All @@ -61,12 +65,14 @@ protected function setUp(): void {
$this->urlGenerator = $this->createMock(IURLGenerator::class);
$this->cacheFactory = $this->createMock(ICacheFactory::class);
$this->logger = $this->createMock(ILogger::class);
$this->tempManager = $this->createMock(ITempManager::class);
$this->imageManager = new ImageManager(
$this->config,
$this->appData,
$this->urlGenerator,
$this->cacheFactory,
$this->logger
$this->logger,
$this->tempManager
);
}

Expand All @@ -84,7 +90,7 @@ private function checkImagick() {
}

public function mockGetImage($key, $file) {
/** @var \PHPUnit\Framework\MockObject\MockObject $folder */
/** @var MockObject $folder */
$folder = $this->createMock(ISimpleFolder::class);
if ($file === null) {
$folder->expects($this->once())
Expand Down Expand Up @@ -327,4 +333,56 @@ public function testCleanup() {
->willReturn($folders[2]);
$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);
}
}

0 comments on commit 0daf097

Please sign in to comment.