Skip to content

Commit

Permalink
test: check that incorrect classes don't trigger autoloading errors
Browse files Browse the repository at this point in the history
  • Loading branch information
fogrye committed Mar 14, 2024
1 parent 5414813 commit 19054eb
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
8 changes: 8 additions & 0 deletions tests/Fixtures/BadNamespace/BadlyNamespacedClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace TheCodingMachine\GraphQLite\Fixtures\BadNamespace\None;

class BadlyNamespacedClass
{

}
5 changes: 5 additions & 0 deletions tests/Fixtures/BadNamespace/ClassWithoutNamespace.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
class ClassWithoutNamespace
{

}
56 changes: 56 additions & 0 deletions tests/Utils/NsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace TheCodingMachine\GraphQLite\Utils;

use Kcs\ClassFinder\Finder\ComposerFinder;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Cache\Psr16Cache;
use TheCodingMachine\GraphQLite\Utils\Namespaces\NS;
use TheCodingMachine\GraphQLite\Fixtures\Types\AbstractFooType;
use TheCodingMachine\GraphQLite\Fixtures\Types\FooExtendType;
use TheCodingMachine\GraphQLite\Fixtures\Types\FooType;
use TheCodingMachine\GraphQLite\Fixtures\Types\GetterSetterType;
use TheCodingMachine\GraphQLite\Fixtures\Types\MagicGetterSetterType;
use TheCodingMachine\GraphQLite\Fixtures\Types\NoTypeAnnotation;
use TheCodingMachine\GraphQLite\Fixtures\Types\TestFactory;

class NsTest extends TestCase
{
/**
* @dataProvider loadsClassListProvider
*/
public function testLoadsClassList(array $expectedClasses, string $namespace): void
{
$ns = new NS(
namespace: $namespace,
cache: new Psr16Cache(new ArrayAdapter()),
finder: new ComposerFinder(),
globTTL: null
);

self::assertEqualsCanonicalizing($expectedClasses, array_keys($ns->getClassList()));
}

public static function loadsClassListProvider(): iterable
{
yield 'autoload' => [
[
TestFactory::class,
GetterSetterType::class,
FooType::class,
MagicGetterSetterType::class,
FooExtendType::class,
NoTypeAnnotation::class,
AbstractFooType::class,
],
'TheCodingMachine\GraphQLite\Fixtures\Types',
];

// The class should be ignored.
yield 'incorrect namespace class without autoload' => [
[],
'TheCodingMachine\GraphQLite\Fixtures\BadNamespace',
];
}
}

0 comments on commit 19054eb

Please sign in to comment.