diff options
Diffstat (limited to 'tests/lib/App/CodeChecker')
-rw-r--r-- | tests/lib/App/CodeChecker/CodeCheckerTest.php | 64 | ||||
-rw-r--r-- | tests/lib/App/CodeChecker/DeprecationCheckTest.php | 70 | ||||
-rw-r--r-- | tests/lib/App/CodeChecker/InfoCheckerTest.php | 74 | ||||
-rw-r--r-- | tests/lib/App/CodeChecker/Mock/TestList.php | 92 | ||||
-rw-r--r-- | tests/lib/App/CodeChecker/NodeVisitorTest.php | 73 | ||||
-rw-r--r-- | tests/lib/App/CodeChecker/StrongComparisonCheckTest.php | 70 |
6 files changed, 443 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..cdbb7c17da5 --- /dev/null +++ b/tests/lib/App/CodeChecker/CodeCheckerTest.php @@ -0,0 +1,64 @@ +<?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\App\CodeChecker\CodeChecker; +use OC\App\CodeChecker\EmptyCheck; +use OC\App\CodeChecker\PrivateCheck; +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 CodeChecker( + new PrivateCheck(new EmptyCheck()) + ); + $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 CodeChecker( + new PrivateCheck(new EmptyCheck()) + ); + $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/DeprecationCheckTest.php b/tests/lib/App/CodeChecker/DeprecationCheckTest.php new file mode 100644 index 00000000000..ee69f075a61 --- /dev/null +++ b/tests/lib/App/CodeChecker/DeprecationCheckTest.php @@ -0,0 +1,70 @@ +<?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\CodeChecker; + +use OC\App\CodeChecker\CodeChecker; +use OC\App\CodeChecker\DeprecationCheck; +use OC\App\CodeChecker\EmptyCheck; +use Test\TestCase; + +class DeprecationCheckTest extends TestCase { + + /** + * @dataProvider providesFilesToCheck + * @param string $expectedErrorToken + * @param int $expectedErrorCode + * @param string $fileToVerify + */ + public function testFindInvalidUsage($expectedErrorToken, $expectedErrorCode, $fileToVerify) { + $checker = new CodeChecker( + new DeprecationCheck(new EmptyCheck()) + ); + $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 [ + ['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'], + ['OC_API::ADMIN_AUTH', 1003, 'test-const.php'], + ]; + } + + /** + * @dataProvider validFilesData + * @param string $fileToVerify + */ + public function testPassValidUsage($fileToVerify) { + $checker = new CodeChecker( + new DeprecationCheck(new EmptyCheck()) + ); + $errors = $checker->analyseFile(\OC::$SERVERROOT . "/tests/data/app/code-checker/$fileToVerify"); + + $this->assertEquals(0, count($errors)); + } + + public function validFilesData() { + return [ + ['test-equal.php'], + ['test-not-equal.php'], + ['test-extends.php'], + ['test-implements.php'], + ['test-static-call.php'], + ['test-new.php'], + ['test-use.php'], + ['test-identical-operator.php'], + ]; + } +} diff --git a/tests/lib/App/CodeChecker/InfoCheckerTest.php b/tests/lib/App/CodeChecker/InfoCheckerTest.php new file mode 100644 index 00000000000..1032e800be1 --- /dev/null +++ b/tests/lib/App/CodeChecker/InfoCheckerTest.php @@ -0,0 +1,74 @@ +<?php +/** + * @author Morris Jobke <hey@morrisjobke.de> + * + * @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; + +use OC\App\CodeChecker\InfoChecker; +use OC\App\InfoParser; +use Test\TestCase; + +class InfoCheckerTest extends TestCase { + /** @var InfoChecker */ + protected $infoChecker; + + public static function setUpBeforeClass() { + \OC::$APPSROOTS[] = [ + 'path' => \OC::$SERVERROOT . '/tests/apps', + 'url' => '/apps-test', + 'writable' => false, + ]; + } + + public static function tearDownAfterClass() { + // remove last element + array_pop(\OC::$APPSROOTS); + } + + protected function setUp() { + parent::setUp(); + $infoParser = new InfoParser(\OC::$server->getURLGenerator()); + + $this->infoChecker = new InfoChecker($infoParser); + } + + public function appInfoData() { + return [ + ['testapp-infoxml', []], + ['testapp-version', []], + ['testapp-infoxml-version', []], + ['testapp-infoxml-version-different', [['type' => 'differentVersions', 'message' => 'appinfo/version: 1.2.4 - appinfo/info.xml: 1.2.3']]], + ['testapp-version-missing', []], + ['testapp-name-missing', [['type' => 'mandatoryFieldMissing', 'field' => 'name']]], + ]; + } + + /** + * @dataProvider appInfoData + * + * @param $appId + * @param $expectedErrors + */ + public function testApps($appId, $expectedErrors) { + $errors = $this->infoChecker->analyse($appId); + + $this->assertEquals($expectedErrors, $errors); + } +} diff --git a/tests/lib/App/CodeChecker/Mock/TestList.php b/tests/lib/App/CodeChecker/Mock/TestList.php new file mode 100644 index 00000000000..1fe83293acf --- /dev/null +++ b/tests/lib/App/CodeChecker/Mock/TestList.php @@ -0,0 +1,92 @@ +<?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\ICheck; + +class TestList implements ICheck { + /** @var ICheck */ + protected $check; + + /** + * @param ICheck $check + */ + public function __construct(ICheck $check) { + $this->check = $check; + } + + /** + * @param int $errorCode + * @param string $errorObject + * @return string + */ + public function getDescription($errorCode, $errorObject) { + return 'testing'; + } + + /** + * @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..ca0b6e9ccc1 --- /dev/null +++ b/tests/lib/App/CodeChecker/NodeVisitorTest.php @@ -0,0 +1,73 @@ +<?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\CodeChecker; + +use OC\App\CodeChecker\CodeChecker; +use OC\App\CodeChecker\EmptyCheck; +use Test\App\CodeChecker\Mock\TestList; +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 CodeChecker( + new TestList(new EmptyCheck()) + ); + $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']); + } + } +} diff --git a/tests/lib/App/CodeChecker/StrongComparisonCheckTest.php b/tests/lib/App/CodeChecker/StrongComparisonCheckTest.php new file mode 100644 index 00000000000..c73eae286ab --- /dev/null +++ b/tests/lib/App/CodeChecker/StrongComparisonCheckTest.php @@ -0,0 +1,70 @@ +<?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\CodeChecker; + +use OC\App\CodeChecker\CodeChecker; +use OC\App\CodeChecker\EmptyCheck; +use OC\App\CodeChecker\StrongComparisonCheck; +use Test\TestCase; + +class StrongComparisonCheckTest extends TestCase { + + /** + * @dataProvider providesFilesToCheck + * @param string $expectedErrorToken + * @param int $expectedErrorCode + * @param string $fileToVerify + */ + public function testFindInvalidUsage($expectedErrorToken, $expectedErrorCode, $fileToVerify) { + $checker = new CodeChecker( + new StrongComparisonCheck(new EmptyCheck()) + ); + $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'], + ]; + } + + /** + * @dataProvider validFilesData + * @param string $fileToVerify + */ + public function testPassValidUsage($fileToVerify) { + $checker = new CodeChecker( + new StrongComparisonCheck(new EmptyCheck()) + ); + $errors = $checker->analyseFile(\OC::$SERVERROOT . "/tests/data/app/code-checker/$fileToVerify"); + + $this->assertEquals(0, count($errors)); + } + + public function validFilesData() { + return [ + ['test-deprecated-use.php'], + ['test-deprecated-use-alias.php'], + ['test-deprecated-use-sub.php'], + ['test-deprecated-use-sub-alias.php'], + ['test-extends.php'], + ['test-implements.php'], + ['test-static-call.php'], + ['test-const.php'], + ['test-new.php'], + ['test-use.php'], + ['test-identical-operator.php'], + ]; + } +} |