Skip to content

Commit

Permalink
fix(tests): restructure tests, add functional test for issue 13
Browse files Browse the repository at this point in the history
  • Loading branch information
alekitto committed Mar 20, 2024
1 parent 22c2c16 commit a32f5c5
Show file tree
Hide file tree
Showing 25 changed files with 186 additions and 144 deletions.
9 changes: 7 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,15 @@ jobs:
extensions: :opcache, pcov

- name: Install Composer dependencies
uses: ramsey/composer-install@v2
uses: ramsey/composer-install@v3
with:
composer-options: ${{ matrix.composer_flags }}
custom-cache-suffix: $(date -u "+%Y-%m")

- name: Install Composer dependencies (tests)
uses: ramsey/composer-install@v3
with:
composer-options: ${{ matrix.composer_flags }}
working-directory: tests/issue-13

- run: vendor/bin/phpunit
if: ${{ matrix.php_version != '8.2' }}
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/vendor/
vendor/
.phpunit.result.cache
.php_cs.cache
tests/**/composer.lock
10 changes: 5 additions & 5 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 8 additions & 4 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" backupGlobals="false" colors="true" bootstrap="vendor/autoload.php">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" backupGlobals="false" colors="true" processIsolation="false">
<php>
<ini name="error_reporting" value="-1"/>
</php>
<testsuites>
<testsuite name="Project Test Suite">
<directory>tests/</directory>
<directory suffix=".phpt">tests/</directory>
<testsuite name="Unit tests">
<directory suffix="Test.php">tests/unit/</directory>
</testsuite>
<testsuite name="Functional tests">
<directory suffix=".phpt">tests/functional/</directory>
<file>tests/issue-13/test.phpt</file>
<file>tests/issue-13/test-path-callback.phpt</file>
</testsuite>
</testsuites>
<source>
Expand Down
21 changes: 0 additions & 21 deletions tests/FunctionalTest.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ ErrorHandler - Test User Error does throw Error

use Kcs\ClassFinder\Util\ErrorHandler;

require __DIR__ . '/../../vendor/autoload.php';
require __DIR__ . '/../../../vendor/autoload.php';

ErrorHandler::register();
trigger_error('This is an error', E_USER_ERROR);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ ErrorHandler - Test User Notice does not throw Error

use Kcs\ClassFinder\Util\ErrorHandler;

require __DIR__ . '/../../vendor/autoload.php';
require __DIR__ . '/../../../vendor/autoload.php';

ErrorHandler::register();
trigger_error('This is a notice', E_USER_NOTICE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ ErrorHandler - Test User Warning does not throw Error

use Kcs\ClassFinder\Util\ErrorHandler;

require __DIR__ . '/../../vendor/autoload.php';
require __DIR__ . '/../../../vendor/autoload.php';

ErrorHandler::register();
trigger_error('This is a warning', E_USER_WARNING);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ ErrorHandler - Test User Notice does not panic if previous handler returns void

use Kcs\ClassFinder\Util\ErrorHandler;

require __DIR__ . '/../../vendor/autoload.php';
require __DIR__ . '/../../../vendor/autoload.php';

set_error_handler(static function (): void {});

Expand Down
13 changes: 13 additions & 0 deletions tests/functional/lists-all-classes-in-project.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
--TEST--
ComposerFinder - should list all classes in a project w/o autoloading
--FILE--
<?php
require __DIR__ . '/../../vendor/autoload.php';

$finder = (new Kcs\ClassFinder\Finder\ComposerFinder())->useAutoloading(false);
$classes = iterator_to_array($finder);

printf('> found %d class(es)' . PHP_EOL, count($classes));
?>
--EXPECTF--
> found %d class(es)
12 changes: 12 additions & 0 deletions tests/issue-13/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"repositories": [
{
"type": "vcs",
"url": "../.."
}
],
"require": {
"symfony/cache": "^6.4 || ^7.0",
"kcs/class-finder": "^1.0@dev"
}
}
17 changes: 17 additions & 0 deletions tests/issue-13/test-path-callback.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
ComposerFinder - compatibility with symfony/cache: exclude path (#13)
--FILE--
<?php
require __DIR__ . '/vendor/autoload.php';

$finder = (new Kcs\ClassFinder\Finder\ComposerFinder())
->pathFilter(static fn (string $path): bool => !preg_match('#symfony/cache/Traits/Redis(?:Cluster)?\dProxy\.php$#', $path));

$count = 0;
foreach ($finder as $className => $reflector) {
++$count;
}
printf('> found %d class(es)' . PHP_EOL, $count);
?>
--EXPECTF--
> found %d class(es)
17 changes: 17 additions & 0 deletions tests/issue-13/test.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
ComposerFinder - compatibility with symfony/cache: exclude namespace (#13)
--FILE--
<?php
require __DIR__ . '/vendor/autoload.php';

$finder = (new Kcs\ClassFinder\Finder\ComposerFinder())
->notInNamespace('Symfony\Component\Cache\Traits');

$count = 0;
foreach ($finder as $className => $reflector) {
++$count;
}
printf('> found %d class(es)' . PHP_EOL, $count);
?>
--EXPECTF--
> found %d class(es)
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Kcs\ClassFinder\Tests\Finder;
namespace Kcs\ClassFinder\Tests\unit\Finder;

use Kcs\ClassFinder\Finder\ComposerFinder;
use Kcs\ClassFinder\Fixtures\Psr0;
Expand All @@ -12,7 +12,6 @@
use ReflectionClass;
use Symfony\Component\ErrorHandler\DebugClassLoader as ErrorHandlerClassLoader;
use Traversable;

use function iterator_to_array;

class ComposerFinderTest extends TestCase
Expand All @@ -37,7 +36,7 @@ public function testShouldNotThrowWhenSymfonyErrorHandlerClassLoaderIsEnabled():

public function testFinderShouldFilterByNamespace(): void
{
$finder = new ComposerFinder();
$finder = (new ComposerFinder())->useAutoloading(false);
$finder->inNamespace(['Kcs\ClassFinder\Fixtures\Psr4']);

self::assertEquals([
Expand Down Expand Up @@ -67,7 +66,7 @@ public function testFinderShouldFilterByExcludedNamespace(): void
public function testFinderShouldFilterByDirectory(): void
{
$finder = new ComposerFinder();
$finder->in([__DIR__ . '/../../data/Composer/Psr0']);
$finder->in([__DIR__ . '/../../../data/Composer/Psr0']);

self::assertEquals([
Psr0\BarBar::class => new ReflectionClass(Psr0\BarBar::class),
Expand All @@ -79,7 +78,7 @@ public function testFinderShouldFilterByDirectory(): void
public function testFinderShouldFilterBySubDirectory(): void
{
$finder = (new ComposerFinder())->useAutoloading(false);
$finder->in([__DIR__ . '/../../data/Composer/Psr4/SubNs']);
$finder->in([__DIR__ . '/../../../data/Composer/Psr4/SubNs']);

self::assertEquals([
Psr4\SubNs\FooBaz::class => new ReflectionClass(Psr4\SubNs\FooBaz::class),
Expand All @@ -89,7 +88,7 @@ public function testFinderShouldFilterBySubDirectory(): void
public function testFinderShouldFilterByInterfaceImplementation(): void
{
$finder = (new ComposerFinder())->useAutoloading(false);
$finder->in([__DIR__ . '/../../data']);
$finder->in([__DIR__ . '/../../../data']);
$finder->implementationOf(Psr4\FooInterface::class);

self::assertEquals([
Expand All @@ -101,7 +100,7 @@ public function testFinderShouldFilterByInterfaceImplementation(): void
public function testFinderShouldFilterBySuperClass(): void
{
$finder = (new ComposerFinder())->useAutoloading(false);
$finder->in([__DIR__ . '/../../data']);
$finder->in([__DIR__ . '/../../../data']);
$finder->subclassOf(Psr4\AbstractClass::class);

self::assertEquals([
Expand All @@ -113,7 +112,7 @@ public function testFinderShouldFilterBySuperClass(): void
public function testFinderShouldFilterByAnnotation(): void
{
$finder = (new ComposerFinder())->useAutoloading(false);
$finder->in([__DIR__ . '/../../data']);
$finder->in([__DIR__ . '/../../../data']);
$finder->annotatedBy(Psr4\SubNs\FooBaz::class);

self::assertEquals([
Expand All @@ -125,7 +124,7 @@ public function testFinderShouldFilterByAnnotation(): void
public function testFinderShouldFilterByAttribute(): void
{
$finder = (new ComposerFinder())->useAutoloading(false);
$finder->in([__DIR__ . '/../../data']);
$finder->in([__DIR__ . '/../../../data']);
$finder->withAttribute(Psr4\SubNs\FooBaz::class);

self::assertEquals([
Expand All @@ -137,7 +136,7 @@ public function testFinderShouldFilterByAttribute(): void
public function testFinderShouldFilterByCallback(): void
{
$finder = (new ComposerFinder())->useAutoloading(false);
$finder->in([__DIR__ . '/../../data']);
$finder->in([__DIR__ . '/../../../data']);
$finder->filter(static function (ReflectionClass $class) {
return $class->getName() === Psr4\AbstractClass::class;
});
Expand All @@ -150,7 +149,7 @@ public function testFinderShouldFilterByCallback(): void
public function testFinderShouldFilterByPath(): void
{
$finder = (new ComposerFinder())->useAutoloading(false);
$finder->in([__DIR__ . '/../../data']);
$finder->in([__DIR__ . '/../../../data']);
$finder->path('SubNs');

self::assertEquals([
Expand All @@ -162,7 +161,7 @@ public function testFinderShouldFilterByPath(): void
public function testFinderShouldFilterByPathRegex(): void
{
$finder = (new ComposerFinder())->useAutoloading(false);
$finder->in([__DIR__ . '/../../data']);
$finder->in([__DIR__ . '/../../../data']);
$finder->path('/subns/i');

self::assertEquals([
Expand All @@ -174,7 +173,7 @@ public function testFinderShouldFilterByPathRegex(): void
public function testFinderShouldFilterByNotPath(): void
{
$finder = (new ComposerFinder())->useAutoloading(false);
$finder->in([__DIR__ . '/../../data']);
$finder->in([__DIR__ . '/../../../data']);
$finder->notPath('SubNs');

self::assertEquals([
Expand All @@ -192,7 +191,7 @@ public function testFinderShouldFilterByNotPath(): void
public function testFinderShouldFilterByNotPathRegex(): void
{
$finder = (new ComposerFinder())->useAutoloading(false);
$finder->in([__DIR__ . '/../../data']);
$finder->in([__DIR__ . '/../../../data']);
$finder->notPath('/subns/i');

self::assertEquals([
Expand All @@ -210,7 +209,7 @@ public function testFinderShouldFilterByNotPathRegex(): void
public function testFinderShouldFilterByPathCallback(): void
{
$finder = (new ComposerFinder())->useAutoloading(false);
$finder->in([__DIR__ . '/../../data']);
$finder->in([__DIR__ . '/../../../data']);
$finder->pathFilter(static fn (string $path): bool => !str_ends_with($path, 'BarBar.php'));

self::assertEquals([
Expand Down
Loading

0 comments on commit a32f5c5

Please sign in to comment.