diff options
author | Joas Schilling <nickvergessen@owncloud.com> | 2015-07-07 12:08:12 +0200 |
---|---|---|
committer | Joas Schilling <nickvergessen@owncloud.com> | 2015-07-17 12:34:56 +0200 |
commit | bba87a2a3b46d20a552efa4a1d00d3253aedf27b (patch) | |
tree | b243d9e54018e114123b53021d0d93c327fbefcc /tests/lib/app/codechecker | |
parent | 9e469046faee66171be15fa6d0bb6eef2289fa0f (diff) | |
download | nextcloud-server-bba87a2a3b46d20a552efa4a1d00d3253aedf27b.tar.gz nextcloud-server-bba87a2a3b46d20a552efa4a1d00d3253aedf27b.zip |
Restructor the code into different classes instead of extending
Diffstat (limited to 'tests/lib/app/codechecker')
-rw-r--r-- | tests/lib/app/codechecker/codecheckertest.php | 62 | ||||
-rw-r--r-- | tests/lib/app/codechecker/deprecationlisttest.php | 68 | ||||
-rw-r--r-- | tests/lib/app/codechecker/mock/testlist.php | 81 | ||||
-rw-r--r-- | tests/lib/app/codechecker/nodevisitortest.php | 71 |
4 files changed, 282 insertions, 0 deletions
diff --git a/tests/lib/app/codechecker/codecheckertest.php b/tests/lib/app/codechecker/codecheckertest.php new file mode 100644 index 00000000000..93bf0b32c58 --- /dev/null +++ b/tests/lib/app/codechecker/codecheckertest.php @@ -0,0 +1,62 @@ +<?php +/** + * Copyright (c) 2015 Thomas Müller <deepdiver@owncloud.com> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace Test\App\CodeChecker; + +use OC; +use Test\TestCase; + +class CodeCheckerTest extends TestCase { + + /** + * @dataProvider providesFilesToCheck + * @param string $expectedErrorToken + * @param int $expectedErrorCode + * @param string $fileToVerify + */ + public function testFindInvalidUsage($expectedErrorToken, $expectedErrorCode, $fileToVerify) { + $checker = new OC\App\CodeChecker\CodeChecker( + new OC\App\CodeChecker\PrivateList() + ); + $errors = $checker->analyseFile(OC::$SERVERROOT . "/tests/data/app/code-checker/$fileToVerify"); + + $this->assertEquals(1, count($errors)); + $this->assertEquals($expectedErrorCode, $errors[0]['errorCode']); + $this->assertEquals($expectedErrorToken, $errors[0]['disallowedToken']); + } + + public function providesFilesToCheck() { + return [ + ['OC_Hook', 1000, 'test-extends.php'], + ['oC_Avatar', 1001, 'test-implements.php'], + ['OC_App', 1002, 'test-static-call.php'], + ['OC_API', 1003, 'test-const.php'], + ['OC_AppConfig', 1004, 'test-new.php'], + ['OC_AppConfig', 1006, 'test-use.php'], + ]; + } + + /** + * @dataProvider validFilesData + * @param string $fileToVerify + */ + public function testPassValidUsage($fileToVerify) { + $checker = new OC\App\CodeChecker\CodeChecker( + new OC\App\CodeChecker\PrivateList() + ); + $errors = $checker->analyseFile(OC::$SERVERROOT . "/tests/data/app/code-checker/$fileToVerify"); + + $this->assertEquals(0, count($errors)); + } + + public function validFilesData() { + return [ + ['test-identical-operator.php'], + ]; + } +} diff --git a/tests/lib/app/codechecker/deprecationlisttest.php b/tests/lib/app/codechecker/deprecationlisttest.php new file mode 100644 index 00000000000..2293d31a1f6 --- /dev/null +++ b/tests/lib/app/codechecker/deprecationlisttest.php @@ -0,0 +1,68 @@ +<?php +/** + * Copyright (c) 2015 Joas Schilling <nickvergessen@owncloud.com> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace Test\App; + +use OC; +use Test\TestCase; + +class DeprecationListTest extends TestCase { + + /** + * @dataProvider providesFilesToCheck + * @param string $expectedErrorToken + * @param int $expectedErrorCode + * @param string $fileToVerify + */ + public function testFindInvalidUsage($expectedErrorToken, $expectedErrorCode, $fileToVerify) { + $checker = new OC\App\CodeChecker\CodeChecker( + new OC\App\CodeChecker\DeprecationList() + ); + $errors = $checker->analyseFile(OC::$SERVERROOT . "/tests/data/app/code-checker/$fileToVerify"); + + $this->assertEquals(1, count($errors)); + $this->assertEquals($expectedErrorCode, $errors[0]['errorCode']); + $this->assertEquals($expectedErrorToken, $errors[0]['disallowedToken']); + } + + public function providesFilesToCheck() { + return [ + ['==', 1005, 'test-equal.php'], + ['!=', 1005, 'test-not-equal.php'], + ['OCP\AppFramework\IApi', 1006, 'test-deprecated-use.php'], + ['OCP\AppFramework\IApi', 1006, 'test-deprecated-use-alias.php'], + ['AppFramework\IApi', 1001, 'test-deprecated-use-sub.php'], + ['OAF\IApi', 1001, 'test-deprecated-use-sub-alias.php'], + ]; + } + + /** + * @dataProvider validFilesData + * @param string $fileToVerify + */ + public function testPassValidUsage($fileToVerify) { + $checker = new OC\App\CodeChecker\CodeChecker( + new OC\App\CodeChecker\DeprecationList() + ); + $errors = $checker->analyseFile(OC::$SERVERROOT . "/tests/data/app/code-checker/$fileToVerify"); + + $this->assertEquals(0, count($errors)); + } + + public function validFilesData() { + return [ + ['test-extends.php'], + ['test-implements.php'], + ['test-static-call.php'], + ['test-const.php'], + ['test-new.php'], + ['test-use.php'], + ['test-identical-operator.php'], + ]; + } +} diff --git a/tests/lib/app/codechecker/mock/testlist.php b/tests/lib/app/codechecker/mock/testlist.php new file mode 100644 index 00000000000..c4985e8e625 --- /dev/null +++ b/tests/lib/app/codechecker/mock/testlist.php @@ -0,0 +1,81 @@ +<?php +/** + * @author Joas Schilling <nickvergessen@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 Test\App\CodeChecker\Mock; + +use OC\App\CodeChecker\ICheckList; + +class TestList implements ICheckList { + + /** + * @return string + */ + public function getDescription() { + return 'deprecated'; + } + + /** + * @return array E.g.: `'ClassName' => 'oc version',` + */ + public function getClasses() { + return [ + // Deprecated classes + 'OCP\AppFramework\IApi' => '8.0.0', + ]; + } + + /** + * @return array E.g.: `'ClassName::CONSTANT_NAME' => 'oc version',` + */ + public function getConstants() { + return [ + // Deprecated constants + 'OCP\NamespaceName\ClassName::CONSTANT_NAME' => '8.0.0', + ]; + } + + /** + * @return array E.g.: `'functionName' => 'oc version',` + */ + public function getFunctions() { + return [ + // Deprecated functions + 'OCP\NamespaceName\ClassName::functionName' => '8.0.0', + ]; + } + + /** + * @return array E.g.: `'ClassName::methodName' => 'oc version',` + */ + public function getMethods() { + return [ + // Deprecated methods + 'OCP\NamespaceName\ClassName::methodName' => '8.0.0', + ]; + } + + /** + * @return bool + */ + public function checkStrongComparisons() { + return true; + } +} diff --git a/tests/lib/app/codechecker/nodevisitortest.php b/tests/lib/app/codechecker/nodevisitortest.php new file mode 100644 index 00000000000..1207ca6a043 --- /dev/null +++ b/tests/lib/app/codechecker/nodevisitortest.php @@ -0,0 +1,71 @@ +<?php +/** + * Copyright (c) 2015 Joas Schilling <nickvergessen@owncloud.com> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace Test\AppCodeChecker; + +use OC; +use Test\TestCase; + +class NodeVisitorTest extends TestCase { + + public function providesFilesToCheck() { + return [ + [[['OCP\AppFramework\IApi', 1006]], 'test-deprecated-use.php'], + [[['OCP\AppFramework\IApi', 1006]], 'test-deprecated-use-alias.php'], + [[['AppFramework\IApi', 1001]], 'test-deprecated-use-sub.php'], + [[['OAF\IApi', 1001]], 'test-deprecated-use-sub-alias.php'], + + [[['OCP\NamespaceName\ClassName::CONSTANT_NAME', 1003]], 'test-deprecated-constant.php'], + [[['Alias::CONSTANT_NAME', 1003]], 'test-deprecated-constant-alias.php'], + [[['NamespaceName\ClassName::CONSTANT_NAME', 1003]], 'test-deprecated-constant-sub.php'], + [[['SubAlias\ClassName::CONSTANT_NAME', 1003]], 'test-deprecated-constant-sub-alias.php'], + + [[ + ['OCP\NamespaceName\ClassName::functionName', 1002], + ['OCP\NamespaceName\ClassName::methodName', 1007], + ], 'test-deprecated-function.php'], + [[ + ['Alias::functionName', 1002], + ['Alias::methodName', 1007], + ], 'test-deprecated-function-alias.php'], + [[ + ['NamespaceName\ClassName::functionName', 1002], + ['NamespaceName\ClassName::methodName', 1007], + ], 'test-deprecated-function-sub.php'], + [[ + ['SubAlias\ClassName::functionName', 1002], + ['SubAlias\ClassName::methodName', 1007], + ], 'test-deprecated-function-sub-alias.php'], + + // TODO Failing to resolve variables to classes +// [[['OCP\NamespaceName\ClassName::methodName', 1007]], 'test-deprecated-method.php'], +// [[['Alias::methodName', 1002]], 'test-deprecated-method-alias.php'], +// [[['NamespaceName\ClassName::methodName', 1002]], 'test-deprecated-method-sub.php'], +// [[['SubAlias\ClassName::methodName', 1002]], 'test-deprecated-method-sub-alias.php'], + ]; + } + + /** + * @dataProvider providesFilesToCheck + * @param array $expectedErrors + * @param string $fileToVerify + */ + public function testMethodsToCheck($expectedErrors, $fileToVerify) { + $checker = new OC\App\CodeChecker\CodeChecker( + new \Test\App\CodeChecker\Mock\TestList() + ); + $errors = $checker->analyseFile(OC::$SERVERROOT . "/tests/data/app/code-checker/$fileToVerify"); + + $this->assertCount(sizeof($expectedErrors), $errors); + + foreach ($expectedErrors as $int => $expectedError) { + $this->assertEquals($expectedError[0], $errors[$int]['disallowedToken']); + $this->assertEquals($expectedError[1], $errors[$int]['errorCode']); + } + } +} |