1
0
mirror of https://github.com/danog/class-finder.git synced 2025-01-22 22:01:19 +01:00

Add failing tests

This commit is contained in:
Hayden Pierce 2019-02-25 18:24:44 -06:00
parent ee813f453d
commit c6fe61af45
7 changed files with 230 additions and 0 deletions

View File

@ -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"

4
ci/php72-no-autoload.sh Normal file
View File

@ -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

View File

@ -25,5 +25,9 @@
<file>test/app1/src/FilesTest.php</file>
<directory>test/unit/Files</directory>
</testsuite>
<testsuite name="noAutoload">
<file>test/app2/PSR4NoAutoloadTest.php</file>
<file>test/app2/ClassmapNoAutoloadTest.php</file>
</testsuite>
</testsuites>
</phpunit>

View File

@ -0,0 +1,95 @@
<?php
namespace TestApp1;
require_once __DIR__ . '/vendor/autoload.php';
use HaydenPierce\ClassFinder\ClassFinder;
class ClassmapTest extends \PHPUnit_Framework_TestCase
{
public function setup()
{
// Reset ClassFinder back to normal.
ClassFinder::setAppRoot(null);
}
/**
* @dataProvider classFinderDataProvider
*/
public function testClassFinder($namespace, $expected, $message)
{
try {
$classes = ClassFinder::getClassesInNamespace($namespace);
} catch (Exception $e) {
$this->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.'
)
);
}
}

View File

@ -0,0 +1,104 @@
<?php
namespace TestApp1;
require_once __DIR__ . '/vendor/autoload.php';
use HaydenPierce\ClassFinder\ClassFinder;
class PSR4NoAutoloadTest extends \PHPUnit_Framework_TestCase
{
public function setup()
{
// Reset ClassFinder back to normal.
ClassFinder::setAppRoot(null);
}
/**
* @dataProvider getClassesInNamespaceDataProvider
*/
public function testGetClassesInNamespace($namespace, $expected, $message)
{
try {
$classes = ClassFinder::getClassesInNamespace($namespace);
} catch (\Exception $e) {
$this->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);
}
}

3
test/app2/README.md Normal file
View File

@ -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.

16
test/app2/composer.json Normal file
View File

@ -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"
}
}