Skip to content

Commit

Permalink
chore: tests documentation classes
Browse files Browse the repository at this point in the history
  • Loading branch information
kubawerlos committed Feb 26, 2024
1 parent e1b66dd commit bd4325a
Show file tree
Hide file tree
Showing 9 changed files with 375 additions and 48 deletions.
3 changes: 2 additions & 1 deletion dev-tools/doc.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@

use PhpCsFixer\Console\Command\DocumentationCommand;
use Symfony\Component\Console\Application;
use Symfony\Component\Filesystem\Filesystem;

$command = new DocumentationCommand();
$command = new DocumentationCommand(new Filesystem());

$application = new Application();
$application->add($command);
Expand Down
21 changes: 14 additions & 7 deletions src/Console/Command/DocumentationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ final class DocumentationCommand extends Command
/** @var string */
protected static $defaultName = 'documentation';

private Filesystem $filesystem;

public function __construct(Filesystem $filesystem)
{
parent::__construct();
$this->filesystem = $filesystem;
}

protected function configure(): void
{
$this
Expand All @@ -46,7 +54,6 @@ protected function configure(): void

protected function execute(InputInterface $input, OutputInterface $output): int
{
$filesystem = new Filesystem();
$locator = new DocumentationLocator();

$fixerFactory = new FixerFactory();
Expand All @@ -66,7 +73,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

foreach ($fixers as $fixer) {
$docForFixerRelativePaths[] = $locator->getFixerDocumentationFileRelativePath($fixer);
$filesystem->dumpFile(
$this->filesystem->dumpFile(
$locator->getFixerDocumentationFilePath($fixer),
$fixerDocumentGenerator->generateFixerDocumentation($fixer)
);
Expand All @@ -78,12 +85,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
->in($locator->getFixersDocumentationDirectoryPath())
->notPath($docForFixerRelativePaths) as $file
) {
$filesystem->remove($file->getPathname());
$this->filesystem->remove($file->getPathname());
}

// Fixer doc. index

$filesystem->dumpFile(
$this->filesystem->dumpFile(
$locator->getFixersDocumentationIndexFilePath(),
$fixerDocumentGenerator->generateFixersDocumentationIndex($fixers)
);
Expand All @@ -92,20 +99,20 @@ protected function execute(InputInterface $input, OutputInterface $output): int

/** @var SplFileInfo $file */
foreach ((new Finder())->files()->in($locator->getRuleSetsDocumentationDirectoryPath()) as $file) {
$filesystem->remove($file->getPathname());
$this->filesystem->remove($file->getPathname());
}

$paths = [];

foreach ($setDefinitions as $name => $definition) {
$path = $locator->getRuleSetsDocumentationFilePath($name);
$paths[$path] = $definition;
$filesystem->dumpFile($path, $ruleSetDocumentationGenerator->generateRuleSetsDocumentation($definition, $fixers));
$this->filesystem->dumpFile($path, $ruleSetDocumentationGenerator->generateRuleSetsDocumentation($definition, $fixers));
}

// RuleSet doc. index

$filesystem->dumpFile(
$this->filesystem->dumpFile(
$locator->getRuleSetsDocumentationIndexFilePath(),
$ruleSetDocumentationGenerator->generateRuleSetsDocumentationIndex($paths)
);
Expand Down
5 changes: 0 additions & 5 deletions src/Documentation/DocumentationLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,6 @@ public function getRuleSetsDocumentationFilePath(string $name): string
return $this->getRuleSetsDocumentationDirectoryPath().'/'.str_replace(':risky', 'Risky', ucfirst(substr($name, 1))).'.rst';
}

public function getListingFilePath(): string
{
return $this->path.'/list.rst';
}

public function getUsageFilePath(): string
{
return $this->path.'/usage.rst';
Expand Down
35 changes: 0 additions & 35 deletions tests/AutoReview/ProjectCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,9 @@
use PhpCsFixer\AbstractFixer;
use PhpCsFixer\AbstractPhpdocTypesFixer;
use PhpCsFixer\AbstractProxyFixer;
use PhpCsFixer\Console\Command\DocumentationCommand;
use PhpCsFixer\Console\Command\FixCommand;
use PhpCsFixer\DocBlock\Annotation;
use PhpCsFixer\DocBlock\DocBlock;
use PhpCsFixer\Documentation\DocumentationLocator;
use PhpCsFixer\Documentation\FixerDocumentGenerator;
use PhpCsFixer\Documentation\RstUtils;
use PhpCsFixer\Documentation\RuleSetDocumentationGenerator;
use PhpCsFixer\Fixer\AbstractPhpUnitFixer;
use PhpCsFixer\Fixer\PhpUnit\PhpUnitNamespacedFixer;
use PhpCsFixer\FixerFactory;
Expand Down Expand Up @@ -65,50 +60,20 @@ final class ProjectCodeTest extends TestCase
*/
private static array $tokensCache = [];

/**
* This structure contains older classes that are not yet covered by tests.
*
* It may only shrink, never add anything to it.
*
* @var string[]
*/
private static $classesWithoutTests = [
DocumentationCommand::class,
DocumentationLocator::class,
FixerDocumentGenerator::class,
RstUtils::class,
RuleSetDocumentationGenerator::class,
];

public static function tearDownAfterClass(): void
{
self::$srcClassCases = null;
self::$testClassCases = null;
self::$tokensCache = [];
}

public function testThatClassesWithoutTestsVarIsProper(): void
{
$unknownClasses = array_filter(
self::$classesWithoutTests,
static fn (string $class): bool => !class_exists($class) && !trait_exists($class),
);

self::assertSame([], $unknownClasses);
}

/**
* @dataProvider provideThatSrcClassHaveTestClassCases
*/
public function testThatSrcClassHaveTestClass(string $className): void
{
$testClassName = 'PhpCsFixer\\Tests'.substr($className, 10).'Test';

if (\in_array($className, self::$classesWithoutTests, true)) {
self::assertFalse(class_exists($testClassName), sprintf('Class "%s" already has tests, so it should be removed from "%s::$classesWithoutTests".', $className, self::class));
self::markTestIncomplete(sprintf('Class "%s" has no tests yet, please help and add it.', $className));
}

self::assertTrue(class_exists($testClassName), sprintf('Expected test class "%s" for "%s" not found.', $testClassName, $className));
}

Expand Down
67 changes: 67 additions & 0 deletions tests/Console/Command/DocumentationCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);

/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace PhpCsFixer\Tests\Console\Command;

use PhpCsFixer\Console\Application;
use PhpCsFixer\Console\Command\DocumentationCommand;
use PhpCsFixer\Tests\TestCase;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\Filesystem\Filesystem;

/**
* @internal
*
* @covers \PhpCsFixer\Console\Command\DocumentationCommand
*/
final class DocumentationCommandTest extends TestCase
{
public function testGeneratingDocumentation(): void
{
$filesystem = $this->createFilesystemDouble();

$application = new Application();
$application->add(new DocumentationCommand($filesystem));

$command = $application->find('documentation');
$commandTester = new CommandTester($command);

$commandTester->execute(
[
'command' => $command->getName(),
],
[
'interactive' => false,
'decorated' => false,
'verbosity' => OutputInterface::VERBOSITY_DEBUG,
],
);

self::assertStringContainsString(
'Docs updated.',
$commandTester->getDisplay(),
);
}

private function createFilesystemDouble(): Filesystem
{
return new class() extends Filesystem {
public function dumpFile(string $filename, $content): void {}

/** @phpstan-ignore-next-line */
public function remove($files): void {}
};
}
}
91 changes: 91 additions & 0 deletions tests/Documentation/DocumentationLocatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

declare(strict_types=1);

/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace PhpCsFixer\Tests\Documentation;

use PhpCsFixer\Documentation\DocumentationLocator;
use PhpCsFixer\Fixer\Casing\ConstantCaseFixer;
use PhpCsFixer\Tests\TestCase;

/**
* @internal
*
* @covers \PhpCsFixer\Documentation\DocumentationLocator
*/
final class DocumentationLocatorTest extends TestCase
{
public function testFixersDocumentationDirectoryPath(): void
{
self::assertSame(
realpath(__DIR__.'/../..').'/doc/rules',
(new DocumentationLocator())->getFixersDocumentationDirectoryPath()
);
}

public function testFixersDocumentationIndexFilePath(): void
{
self::assertSame(
realpath(__DIR__.'/../..').'/doc/rules/index.rst',
(new DocumentationLocator())->getFixersDocumentationIndexFilePath()
);
}

public function testFixerDocumentationFilePath(): void
{
self::assertSame(
realpath(__DIR__.'/../..').'/doc/rules/casing/constant_case.rst',
(new DocumentationLocator())->getFixerDocumentationFilePath(new ConstantCaseFixer())
);
}

public function testFixerDocumentationFileRelativePath(): void
{
self::assertSame(
'casing/constant_case.rst',
(new DocumentationLocator())->getFixerDocumentationFileRelativePath(new ConstantCaseFixer())
);
}

public function testRuleSetsDocumentationDirectoryPath(): void
{
self::assertSame(
realpath(__DIR__.'/../..').'/doc/ruleSets',
(new DocumentationLocator())->getRuleSetsDocumentationDirectoryPath()
);
}

public function testRuleSetsDocumentationIndexFilePath(): void
{
self::assertSame(
realpath(__DIR__.'/../..').'/doc/ruleSets/index.rst',
(new DocumentationLocator())->getRuleSetsDocumentationIndexFilePath()
);
}

public function testRuleSetsDocumentationFilePath(): void
{
self::assertSame(
realpath(__DIR__.'/../..').'/doc/ruleSets/PhpCsFixerRisky.rst',
(new DocumentationLocator())->getRuleSetsDocumentationFilePath('@PhpCsFixer:risky')
);
}

public function testUsageFilePath(): void
{
self::assertSame(
realpath(__DIR__.'/../..').'/doc/usage.rst',
(new DocumentationLocator())->getUsageFilePath()
);
}
}
79 changes: 79 additions & 0 deletions tests/Documentation/FixerDocumentGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

declare(strict_types=1);

/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace PhpCsFixer\Tests\Documentation;

use PhpCsFixer\Documentation\DocumentationLocator;
use PhpCsFixer\Documentation\FixerDocumentGenerator;
use PhpCsFixer\Fixer\Basic\BracesFixer;
use PhpCsFixer\Fixer\ClassNotation\VisibilityRequiredFixer;
use PhpCsFixer\Fixer\Comment\HeaderCommentFixer;
use PhpCsFixer\Fixer\FixerInterface;
use PhpCsFixer\Fixer\LanguageConstruct\ClassKeywordFixer;
use PhpCsFixer\Fixer\Strict\StrictParamFixer;
use PhpCsFixer\FixerFactory;
use PhpCsFixer\Tests\TestCase;

/**
* @internal
*
* @covers \PhpCsFixer\Documentation\FixerDocumentGenerator
*/
final class FixerDocumentGeneratorTest extends TestCase
{
/**
* @dataProvider provideGenerateRuleSetsDocumentationCases
*/
public function testGenerateRuleSetsDocumentation(FixerInterface $fixer): void
{
$locator = new DocumentationLocator();
$generator = new FixerDocumentGenerator($locator);

self::assertSame(
file_get_contents($locator->getFixerDocumentationFilePath($fixer)),
$generator->generateFixerDocumentation($fixer),
);
}

/**
* @return iterable<array{FixerInterface}>
*/
public static function provideGenerateRuleSetsDocumentationCases(): iterable
{
yield [new BracesFixer()];

yield [new ClassKeywordFixer()];

yield [new HeaderCommentFixer()];

yield [new StrictParamFixer()];

yield [new VisibilityRequiredFixer()];
}

public function testGenerateFixersDocumentationIndex(): void
{
$locator = new DocumentationLocator();
$generator = new FixerDocumentGenerator($locator);

$fixerFactory = new FixerFactory();
$fixerFactory->registerBuiltInFixers();
$fixers = $fixerFactory->getFixers();

self::assertSame(
file_get_contents($locator->getFixersDocumentationIndexFilePath()),
$generator->generateFixersDocumentationIndex($fixers),
);
}
}

0 comments on commit bd4325a

Please sign in to comment.