From c6fe61af45f307d5177e749dc27bf5e4b57a2340 Mon Sep 17 00:00:00 2001 From: Hayden Pierce Date: Mon, 25 Feb 2019 18:24:44 -0600 Subject: [PATCH] Add failing tests --- .gitlab-ci.yml | 4 ++ ci/php72-no-autoload.sh | 4 ++ phpunit.xml | 4 ++ test/app2/ClassmapNoAutoloadTest.php | 95 ++++++++++++++++++++++++ test/app2/PSR4NoAutoloadTest.php | 104 +++++++++++++++++++++++++++ test/app2/README.md | 3 + test/app2/composer.json | 16 +++++ 7 files changed, 230 insertions(+) create mode 100644 ci/php72-no-autoload.sh create mode 100644 test/app2/ClassmapNoAutoloadTest.php create mode 100644 test/app2/PSR4NoAutoloadTest.php create mode 100644 test/app2/README.md create mode 100644 test/app2/composer.json diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 749e6d0..711d813 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -4,4 +4,8 @@ PHP 5.3 - PHPUnit: PHP 7.2 - PHPUnit: script: "ci/php72.sh $CI_PROJECT_DIR" + image: "registry.gitlab.com/hpierce1102/classfinder/php72" + +PHP 7.2 - No Autoloader Property: + script: "ci/php72-no-autoload.sh $CI_PROJECT_DIR" image: "registry.gitlab.com/hpierce1102/classfinder/php72" \ No newline at end of file diff --git a/ci/php72-no-autoload.sh b/ci/php72-no-autoload.sh new file mode 100644 index 0000000..4cf7f55 --- /dev/null +++ b/ci/php72-no-autoload.sh @@ -0,0 +1,4 @@ +composer install --working-dir=$1/test/app2 --quiet || exit 1 +composer install --working-dir=$1 --quiet || exit 1 +php --version +php $1/vendor/bin/phpunit --testsuite noAutoload \ No newline at end of file diff --git a/phpunit.xml b/phpunit.xml index 7765f0f..f518ded 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -25,5 +25,9 @@ test/app1/src/FilesTest.php test/unit/Files + + test/app2/PSR4NoAutoloadTest.php + test/app2/ClassmapNoAutoloadTest.php + \ No newline at end of file diff --git a/test/app2/ClassmapNoAutoloadTest.php b/test/app2/ClassmapNoAutoloadTest.php new file mode 100644 index 0000000..3dfeca8 --- /dev/null +++ b/test/app2/ClassmapNoAutoloadTest.php @@ -0,0 +1,95 @@ +assertFalse(true, 'An exception occurred: ' . $e->getMessage()); + $classes = array(); + } + + $this->assertEquals($expected, $classes, $message); + } + + public function classFinderDataProvider() + { + return array( + array( + 'HaydenPierce\Classmap', + array( + 'HaydenPierce\Classmap\Classmap2ClassmapINC', + 'HaydenPierce\Classmap\Classmap2ClassmapPHP', + 'HaydenPierce\Classmap\Classmap3ClassesPHP', + 'HaydenPierce\Classmap\ClassmapClassmap2PHP' + ), + 'Classfinder should be able to load classes based on a classmap from 3rd party packages.' + ), + array( + 'HaydenPierce\Classmap2', + array( + 'HaydenPierce\Classmap2\Classmap2ClassmapINC', + 'HaydenPierce\Classmap2\Classmap2ClassmapPHP', + 'HaydenPierce\Classmap2\Classmap3ClassesPHP', + 'HaydenPierce\Classmap2\ClassmapClassmap2PHP' + ), + 'Classfinder should be able to handle multiple namespaces in a single file for a classmap.' + ) + ); + } + + /** + * @dataProvider classesInNamespaceRecursivelyDataProvider + */ + public function testClassesInNamespaceRecursively($namespace, $expected, $message) + { + ClassFinder::disablePSR4Support(); + + try { + $classes = ClassFinder::getClassesInNamespace($namespace, ClassFinder::RECURSIVE_MODE); + } catch (Exception $e) { + $this->assertFalse(true, 'An exception occurred: ' . $e->getMessage()); + $classes = array(); + } + + ClassFinder::enablePSR4Support(); + + $this->assertEquals($expected, $classes, $message); + } + + public function classesInNamespaceRecursivelyDataProvider() + { + return array( + array( + 'HaydenPierce', + array( + 'HaydenPierce\Classmap2\Classmap2ClassmapINC', + 'HaydenPierce\Classmap2\Classmap2ClassmapPHP', + 'HaydenPierce\Classmap2\Classmap3ClassesPHP', + 'HaydenPierce\Classmap2\ClassmapClassmap2PHP', + 'HaydenPierce\Classmap\Classmap2ClassmapINC', + 'HaydenPierce\Classmap\Classmap2ClassmapPHP', + 'HaydenPierce\Classmap\Classmap3ClassesPHP', + 'HaydenPierce\Classmap\ClassmapClassmap2PHP', + ), + 'Classfinder should be able to load third party classes recursively based on a classmap.' + ) + ); + } +} \ No newline at end of file diff --git a/test/app2/PSR4NoAutoloadTest.php b/test/app2/PSR4NoAutoloadTest.php new file mode 100644 index 0000000..bcfee0c --- /dev/null +++ b/test/app2/PSR4NoAutoloadTest.php @@ -0,0 +1,104 @@ +assertFalse(true, 'An exception occurred: ' . $e->getMessage()); + $classes = array(); + } + + $this->assertEquals($expected, $classes, $message); + } + + public function getClassesInNamespaceDataProvider() + { + return array( + array( + 'HaydenPierce\SandboxApp', + array( + 'HaydenPierce\SandboxApp\Foy' + ), + 'ClassFinder should be able to find 3rd party classes' + ), + array( + 'HaydenPierce\SandboxApp\Foo\Bar', + array( + 'HaydenPierce\SandboxApp\Foo\Bar\Barc', + 'HaydenPierce\SandboxApp\Foo\Bar\Barp' + ), + 'ClassFinder should be able to find 3rd party classes multiple namespaces deep.' + ), + array( + 'HaydenPierce\SandboxAppMulti', + array( + 'HaydenPierce\SandboxAppMulti\Zip', + 'HaydenPierce\SandboxAppMulti\Zop', + 'HaydenPierce\SandboxAppMulti\Zap', + 'HaydenPierce\SandboxAppMulti\Zit' + ), + 'ClassFinder should be able to find 3rd party classes when a provided namespace root maps to multiple directories (Example: "HaydenPierce\\SandboxAppMulti\\": ["multi/Bop", "multi/Bot"] )' + ), + array( + 'TestApp1\Foo\Empty', + array(), + 'ClassFinder should return an empty array if the namespace is known, but contains no classes.' + ) + ); + } + + public function testGetClassesInNamespaceRecursively() + { + $namespace = 'HaydenPierce'; + $expected = array( + 'HaydenPierce\SandboxApp\Foy', + 'HaydenPierce\SandboxApp\Fob\Soz', + 'HaydenPierce\SandboxApp\Foo\Larc', + 'HaydenPierce\SandboxApp\Foo\Bar\Barc', + 'HaydenPierce\SandboxApp\Foo\Bar\Barp', + 'HaydenPierce\SandboxAppMulti\Zip', + 'HaydenPierce\SandboxAppMulti\Zop', + 'HaydenPierce\SandboxAppMulti\Zap', + 'HaydenPierce\SandboxAppMulti\Zit' + ); + $message = 'ClassFinder should be able to find 3rd party classes'; + + ClassFinder::disableClassmapSupport(); + + try { + $classes = ClassFinder::getClassesInNamespace($namespace, ClassFinder::RECURSIVE_MODE); + } catch (\Exception $e) { + $this->assertFalse(true, 'An exception occurred: ' . $e->getMessage()); + $classes = array(); + } + + // ClassFinder has the ability to find itself. This ability, while intended, is incontinent for tests + // because of the 'HaydenPierce' test case. Whenever ClassFinder would be updated, we would need to update the + // test. To prevent the flakiness, we just remove ClassFinder's classes. + $classes = array_filter($classes, function($class) { + return strpos($class, 'HaydenPierce\ClassFinder') !== 0; + }); + + ClassFinder::enableClassmapSupport(); + + $this->assertEquals($expected, $classes, $message); + } +} \ No newline at end of file diff --git a/test/app2/README.md b/test/app2/README.md new file mode 100644 index 0000000..4e9cf03 --- /dev/null +++ b/test/app2/README.md @@ -0,0 +1,3 @@ +This test app tests that ClassFinder is working in applications that do't specify their own autoload configs in composer.json. + +This can occur when ClassFinder is used only to retrieve 3rd party dependencies. \ No newline at end of file diff --git a/test/app2/composer.json b/test/app2/composer.json new file mode 100644 index 0000000..5425005 --- /dev/null +++ b/test/app2/composer.json @@ -0,0 +1,16 @@ +{ + "name": "HaydenPierce/testApp", + "type": "project", + "license": "MIT", + "authors": [ + { + "name": "Hayden Pierce", + "email": "hayden@haydenpierce.com" + } + ], + "require-dev": { + "phpunit/phpunit": "4.8.36", + "haydenpierce/class-finder": "0.0.1", + "haydenpierce/sandbox-app":"0.3.0" + } +}