-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unit tests for BackendService, BackendConfig and BackendParameter
Two potential bugs pre-empted and fixed
- Loading branch information
Robin McCorkell
committed
Jul 23, 2015
1 parent
4a8fb4c
commit 7455ba1
Showing
7 changed files
with
431 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
<?php | ||
/** | ||
* @author Robin McCorkell <rmccorkell@owncloud.com> | ||
* | ||
* @copyright Copyright (c) 2015, ownCloud, Inc. | ||
* @license AGPL-3.0 | ||
* | ||
* This code is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License, version 3, | ||
* as published by the Free Software Foundation. | ||
* | ||
* 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, version 3, | ||
* along with this program. If not, see <http://www.gnu.org/licenses/> | ||
* | ||
*/ | ||
|
||
namespace OCA\Files_External\Tests; | ||
|
||
use \OCA\Files_External\Lib\BackendConfig; | ||
use \OCA\Files_External\Lib\BackendDependency; | ||
|
||
class BackendConfigTest extends \Test\TestCase { | ||
|
||
public function testJsonSerialization() { | ||
$param = $this->getMockBuilder('\OCA\Files_External\Lib\BackendParameter') | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
$param->method('getName')->willReturn('foo'); | ||
|
||
$backendConfig = new BackendConfig('\OC\Files\Storage\SMB', 'smb', [$param]); | ||
$backendConfig->setPriority(123); | ||
$backendConfig->setCustomJs('foo/bar.js'); | ||
|
||
$json = $backendConfig->jsonSerialize(); | ||
|
||
$this->assertEquals('smb', $json['backend']); | ||
$this->assertEquals(123, $json['priority']); | ||
$this->assertEquals('foo/bar.js', $json['custom']); | ||
|
||
$configuration = $json['configuration']; | ||
$this->assertArrayHasKey('foo', $configuration); | ||
} | ||
|
||
public function validateStorageProvider() { | ||
return [ | ||
[true, ['foo' => true, 'bar' => true, 'baz' => true]], | ||
[false, ['foo' => true, 'bar' => false]] | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider validateStorageProvider | ||
*/ | ||
public function testValidateStorage($expectedSuccess, $params) { | ||
$backendParams = []; | ||
foreach ($params as $name => $valid) { | ||
$param = $this->getMockBuilder('\OCA\Files_External\Lib\BackendParameter') | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
$param->method('getName') | ||
->willReturn($name); | ||
$param->expects($this->once()) | ||
->method('validateValue') | ||
->willReturn($valid); | ||
$backendParams[] = $param; | ||
} | ||
|
||
$storageConfig = $this->getMockBuilder('\OCA\Files_External\Lib\StorageConfig') | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
$storageConfig->expects($this->once()) | ||
->method('getBackendOptions') | ||
->willReturn([]); | ||
|
||
$backendConfig = new BackendConfig('\OC\Files\Storage\SMB', 'smb', $backendParams); | ||
$this->assertEquals($expectedSuccess, $backendConfig->validateStorage($storageConfig)); | ||
} | ||
|
||
/** | ||
* Static method for testCheckDependencies() | ||
* Do not stare at for too long, keep all limbs well away from the function | ||
* | ||
* @return array | ||
*/ | ||
public static function checkDependencies() { | ||
return ['dependency' => 'missing dependency']; | ||
} | ||
|
||
public function testCheckDependencies() { | ||
$backend = new BackendConfig('\OCA\Files_External\Tests\BackendConfigTest', 'test', []); | ||
$backend->setHasDependencies(true); | ||
|
||
$dependencies = $backend->checkDependencies(); | ||
$this->assertCount(1, $dependencies); | ||
$this->assertEquals('dependency', $dependencies[0]->getDependency()); | ||
$this->assertEquals('missing dependency', $dependencies[0]->getMessage()); | ||
$this->assertEquals($backend, $dependencies[0]->getBackend()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
/** | ||
* @author Robin McCorkell <rmccorkell@owncloud.com> | ||
* | ||
* @copyright Copyright (c) 2015, ownCloud, Inc. | ||
* @license AGPL-3.0 | ||
* | ||
* This code is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License, version 3, | ||
* as published by the Free Software Foundation. | ||
* | ||
* 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, version 3, | ||
* along with this program. If not, see <http://www.gnu.org/licenses/> | ||
* | ||
*/ | ||
|
||
namespace OCA\Files_External\Tests; | ||
|
||
use \OCA\Files_External\Lib\BackendParameter as Param; | ||
|
||
class BackendParameterTest extends \Test\TestCase { | ||
|
||
public function testJsonSerialization() { | ||
$param = new Param('foo', 'bar'); | ||
$this->assertEquals('bar', $param->jsonSerialize()); | ||
|
||
$param->setType(Param::VALUE_BOOLEAN); | ||
$this->assertEquals('!bar', $param->jsonSerialize()); | ||
|
||
$param->setType(Param::VALUE_PASSWORD); | ||
$param->setFlag(Param::FLAG_OPTIONAL); | ||
$this->assertEquals('&*bar', $param->jsonSerialize()); | ||
|
||
$param->setType(Param::VALUE_HIDDEN); | ||
$param->setFlags(Param::FLAG_NONE); | ||
$this->assertEquals('#bar', $param->jsonSerialize()); | ||
} | ||
|
||
public function validateValueProvider() { | ||
return [ | ||
[Param::VALUE_TEXT, Param::FLAG_NONE, 'abc', true], | ||
[Param::VALUE_TEXT, Param::FLAG_NONE, '', false], | ||
[Param::VALUE_TEXT, Param::FLAG_OPTIONAL, '', true], | ||
|
||
[Param::VALUE_BOOLEAN, Param::FLAG_NONE, false, true], | ||
[Param::VALUE_BOOLEAN, Param::FLAG_NONE, 123, false], | ||
|
||
[Param::VALUE_PASSWORD, Param::FLAG_NONE, 'foobar', true], | ||
[Param::VALUE_PASSWORD, Param::FLAG_NONE, '', false], | ||
|
||
[Param::VALUE_HIDDEN, Param::FLAG_NONE, '', false] | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider validateValueProvider | ||
*/ | ||
public function testValidateValue($type, $flags, $value, $success) { | ||
$param = new Param('foo', 'bar'); | ||
$param->setType($type); | ||
$param->setFlags($flags); | ||
|
||
$this->assertEquals($success, $param->validateValue($value)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.