diff options
author | Olivier Paroz <github@oparoz.com> | 2015-09-07 23:06:17 +0300 |
---|---|---|
committer | Olivier Paroz <github@oparoz.com> | 2015-09-07 23:06:17 +0300 |
commit | bbdc4d6a5b728bdcff680b7de66b336dc476828f (patch) | |
tree | a9ebbd2c06f2e3a96b77321281978b94ebb05cd1 /tests/unit | |
parent | 812c24e735ede2be70af5d618f5626504e872db7 (diff) |
Unit tests for ConfigParser
Diffstat (limited to 'tests/unit')
-rw-r--r-- | tests/unit/GalleryUnitTest.php | 10 | ||||
-rw-r--r-- | tests/unit/config/ConfigParserTest.php | 211 | ||||
-rw-r--r-- | tests/unit/service/ConfigServiceTest.php | 8 |
3 files changed, 217 insertions, 12 deletions
diff --git a/tests/unit/GalleryUnitTest.php b/tests/unit/GalleryUnitTest.php index 6595a1f4..b506e3f5 100644 --- a/tests/unit/GalleryUnitTest.php +++ b/tests/unit/GalleryUnitTest.php @@ -147,8 +147,6 @@ abstract class GalleryUnitTest extends \Test\TestCase { ->willReturn($filename); $file->method('getMimeType') ->willReturn('image/jpeg'); - - return $file; } private function mockSvgFileMethods($file) { @@ -159,8 +157,6 @@ abstract class GalleryUnitTest extends \Test\TestCase { ->willReturn($filename); $file->method('getMimeType') ->willReturn('image/svg+xml'); - - return $file; } private function mockAnimatedGifFileMethods($file) { @@ -173,9 +169,7 @@ abstract class GalleryUnitTest extends \Test\TestCase { ->willReturn('image/gif'); $file->method('fopen') ->with('rb') - ->willReturn(fopen(__DIR__ . '/../_data/' . $filename, 'rb')); - - return $file; + ->willReturn(fopen(__DIR__ . '/../_data/' . $filename, 'rb'));; } private function mockNoMediaFileMethods($file) { @@ -186,8 +180,6 @@ abstract class GalleryUnitTest extends \Test\TestCase { ->willReturn($filename); $file->method('getMimeType') ->willReturn('image/jpeg'); - - return $file; } protected function mockBadFile() { diff --git a/tests/unit/config/ConfigParserTest.php b/tests/unit/config/ConfigParserTest.php new file mode 100644 index 00000000..4362efba --- /dev/null +++ b/tests/unit/config/ConfigParserTest.php @@ -0,0 +1,211 @@ +<?php +/** + * ownCloud - gallery + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Olivier Paroz <owncloud@interfasys.ch> + * + * @copyright Olivier Paroz 2015 + */ + +namespace OCA\Gallery\Config; + +use Symfony\Component\Yaml\Parser; +use Symfony\Component\Yaml\Dumper; +use Symfony\Component\Yaml\Exception\ParseException; + +/** + * Class ConfigParserTest + * + * @package OCA\Gallery\Config + */ +class ConfigParserTest extends \Test\GalleryUnitTest { + + /** @var string */ + protected $configName = 'gallery.cnf'; + /** @var ConfigParser */ + protected $configParser; + + /** + * Test set up + */ + protected function setUp() { + parent::setUp(); + + $this->configParser = new ConfigParser(); + } + + public function providesGetFeaturesListData() { + $emptyConfig = []; + $noFeatures = [ + 'information' => [ + 'description_link' => 'readme.md' + ] + ]; + $emptyFeatures = [ + 'features' => [] + ]; + $featureList = [ + 'external_shares' => "no", + 'native_svg' => "yes", + ]; + $features = [ + 'features' => $featureList + ]; + + return [ + [$emptyConfig, []], + [$noFeatures, []], + [$emptyFeatures, []], + [$features, $featureList], + ]; + } + + /** + * @dataProvider providesGetFeaturesListData + * + * @param $config + * @param $expectedResult + */ + public function testGetFeaturesList($config, $expectedResult) { + $folder = $this->mockFolderWithConfig($config); + + $response = $this->configParser->getFeaturesList($folder, $this->configName); + + $this->assertEquals($expectedResult, $response); + } + + /** + * @expectedException \OCA\Gallery\Config\ConfigException + */ + public function testGetFeaturesListWithBrokenConfig() { + $folder = $this->mockFolder('home::user', 121212, []); + $folder->method('get') + ->with($this->configName) + ->willThrowException(new \Exception('Computer says no')); + + $this->configParser->getFeaturesList($folder, $this->configName); + } + + public function providesGetFolderConfigData() { + $emptyConfig = []; + $description = 'My cute description'; + $copyright = 'Copyright 2004-2016 interfaSys sàrl'; + $infoList = [ + 'description_link' => $description, + 'copyright_link' => $copyright, + 'inherit' => 'yes' + ]; + $information = [ + 'information' => $infoList + ]; + + $sortingList = [ + 'type' => 'name', + 'order' => 'des', + 'inherit' => 'yes' + ]; + $sorting = [ + 'sorting' => $sortingList + ]; + $standardRootConfig = array_merge($information, $sorting); + $standardLevel = 0; + $rootLevel = 1; + $nothingCompleted = ['information' => false, 'sorting' => false]; + $sortingCompleted = ['information' => false, 'sorting' => true]; + $infoCompleted = ['information' => true, 'sorting' => false]; + $allCompleted = ['information' => true, 'sorting' => true]; + + // One config in the current folder only + $currentConfigOnlyResult = $standardRootConfig; + $currentConfigOnlyResult['information']['level'] = $standardLevel; + $currentConfigOnlyResult['sorting']['level'] = $standardLevel; + + // Sorting with missing type + $brokenSortingConfig = [ + 'sorting' => [ + 'order' => 'des', + 'inherit' => 'no' + ] + ]; + + // Sorting with different type + $dateSortingConfig = [ + 'sorting' => [ + 'type' => 'date', + 'order' => 'des', + ] + ]; + + $dateSortingConfigResult = array_merge($standardRootConfig, $dateSortingConfig); + $dateSortingConfigResult['information']['level'] = $rootLevel; + + $infoConfig = [ + 'information' => [ + 'description_link' => 'Local conf', + 'copyright_link' => '2015 me', + ] + ]; + + // Full information is inherited from root + $infoConfigResult = array_merge($standardRootConfig, $infoConfig); + $infoConfigResult['sorting']['level'] = $rootLevel; + + return [ + [ + $emptyConfig, $nothingCompleted, $standardRootConfig, $standardLevel, + [$currentConfigOnlyResult, $allCompleted] + ], + [ + $emptyConfig, $nothingCompleted, $brokenSortingConfig, + $standardLevel, [$emptyConfig, $nothingCompleted] + ], + [ + $dateSortingConfig, $sortingCompleted, $standardRootConfig, $rootLevel, + [$dateSortingConfigResult, $allCompleted] + ], + [ + $infoConfig, $infoCompleted, $standardRootConfig, $rootLevel, + [$infoConfigResult, $allCompleted] + ], + + ]; + } + + /** + * @dataProvider providesGetFolderConfigData + * + * @param $currentConfig + * @param $configItems + * @param $newConfig + * @param $level + * @param $expectedResult + */ + public function testGetFolderConfig( + $currentConfig, $configItems, $newConfig, $level, $expectedResult + ) { + $folder = $this->mockFolderWithConfig($newConfig); + + $response = $this->configParser->getFolderConfig( + $folder, $this->configName, $currentConfig, $configItems, $level + ); + + $this->assertEquals($expectedResult, $response); + } + + private function mockFolderWithConfig($config) { + $file = $this->mockFile(212121); + $yaml = new Dumper(); + $file->method('getContent') + ->willReturn($yaml->dump($config)); + $folder = $this->mockFolder('home::user', 121212, [$file]); + $folder->method('get') + ->with($this->configName) + ->willReturn($file); + + return $folder; + } + +} diff --git a/tests/unit/service/ConfigServiceTest.php b/tests/unit/service/ConfigServiceTest.php index f4b4d5aa..2b27a2fc 100644 --- a/tests/unit/service/ConfigServiceTest.php +++ b/tests/unit/service/ConfigServiceTest.php @@ -12,6 +12,8 @@ namespace OCA\Gallery\Service; +use OCA\Gallery\Config\ConfigParser; +use OCA\Gallery\Config\ConfigException; /** * Class ConfigServiceTest @@ -31,7 +33,7 @@ class ConfigServiceTest extends \Test\GalleryUnitTest { public function setUp() { parent::setUp(); - $this->configParser = $this->getMockBuilder('\OCA\Gallery\Service\ConfigParser') + $this->configParser = $this->getMockBuilder('\OCA\Gallery\Config\ConfigParser') ->disableOriginalConstructor() ->getMock(); $this->service = new ConfigService ( @@ -51,9 +53,9 @@ class ConfigServiceTest extends \Test\GalleryUnitTest { $configItems = ['information' => false, 'sorting' => false]; // Default in the class $level = 0; $configPath = 'Some/folder'; - $exception = new ServiceException('Boom'); + $exception = new ConfigException('Boom'); $result = - [[['error' => ['message' => 'Boom' . "</br></br>Config location: /$configPath"]]]]; + [[['error' => ['message' => 'Boom' . ". Config location: /$configPath"]]]]; $this->mockGetPathFromVirtualRoot($folder, $configPath); $this->mockGetFolderConfigWithBrokenSetup( |