Skip to content

Commit

Permalink
deduplicate test methods using data provider
Browse files Browse the repository at this point in the history
  • Loading branch information
petrduda committed Mar 13, 2023
1 parent a5ce432 commit aad4988
Showing 1 changed file with 44 additions and 35 deletions.
79 changes: 44 additions & 35 deletions tests/RobotLoaderClassFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,63 @@

namespace Consistence\ClassFinder\RobotLoader;

use Generator;
use Nette\Loaders\RobotLoader;
use PHPUnit\Framework\Assert;

class RobotLoaderClassFinderTest extends \PHPUnit\Framework\TestCase
{

public function testFindClass(): void
/**
* @return mixed[][]|\Generator
*/
public function findByInterfaceDataProvider(): Generator
{
$classList = array_values($this->getRobotLoaderClassFinder()->findByInterface(ExtendingClass::class));

Assert::assertContains(ExtendingClass::class, $classList);
Assert::assertCount(1, $classList);
}

public function testFindClassAndChildren(): void
{
$classList = array_values($this->getRobotLoaderClassFinder()->findByInterface(BaseClass::class));
sort($classList);

$expected = [
BaseClass::class,
ExtendingClass::class,
yield 'class' => [
'interfaceName' => ExtendingClass::class,
'expectedClassList' => [
ExtendingClass::class,
],
];
yield 'class and children' => [
'interfaceName' => BaseClass::class,
'expectedClassList' => [
BaseClass::class,
ExtendingClass::class,
],
];
yield 'not implemented interface' => [
'interfaceName' => NotImplementedInterface::class,
'expectedClassList' => [
NotImplementedInterface::class,
],
];
yield 'interface and implementations' => [
'interfaceName' => BaseInterface::class,
'expectedClassList' => [
BaseClass::class,
BaseInterface::class,
ExtendingClass::class,
ExtendingInterface::class,
],
];

Assert::assertEquals($expected, $classList);
}

public function testFindInterface(): void
{
$classList = array_values($this->getRobotLoaderClassFinder()->findByInterface(NotImplementedInterface::class));

Assert::assertContains(NotImplementedInterface::class, $classList);
Assert::assertCount(1, $classList);
}

public function testFindInterfaceAndImplementations(): void
/**
* @dataProvider findByInterfaceDataProvider
*
* @param string $interfaceName
* @param string[] $expectedClassList
*/
public function testFindByInterface(
string $interfaceName,
array $expectedClassList
): void
{
$classList = array_values($this->getRobotLoaderClassFinder()->findByInterface(BaseInterface::class));
$classList = array_values($this->getRobotLoaderClassFinder()->findByInterface($interfaceName));
sort($classList);

$expected = [
BaseClass::class,
BaseInterface::class,
ExtendingClass::class,
ExtendingInterface::class,
];

Assert::assertEquals($expected, $classList);
Assert::assertEquals($expectedClassList, $classList);
}

private function getRobotLoaderClassFinder(): RobotLoaderClassFinder
Expand Down

0 comments on commit aad4988

Please sign in to comment.