Skip to content

Commit

Permalink
chore: add missing tests for non-documentation classes (#7848)
Browse files Browse the repository at this point in the history
  • Loading branch information
kubawerlos committed Feb 26, 2024
1 parent 0c28cca commit e1b66dd
Show file tree
Hide file tree
Showing 10 changed files with 327 additions and 302 deletions.
23 changes: 17 additions & 6 deletions src/Console/SelfUpdate/GithubClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
*/
final class GithubClient implements GithubClientInterface
{
private string $url = 'https://api.github.com/repos/PHP-CS-Fixer/PHP-CS-Fixer/tags';

public function getTags(): array
{
$url = 'https://api.github.com/repos/PHP-CS-Fixer/PHP-CS-Fixer/tags';

$result = @file_get_contents(
$url,
$this->url,
false,
stream_context_create([
'http' => [
Expand All @@ -34,18 +34,29 @@ public function getTags(): array
);

if (false === $result) {
throw new \RuntimeException(sprintf('Failed to load tags at "%s".', $url));
throw new \RuntimeException(sprintf('Failed to load tags at "%s".', $this->url));
}

/**
* @var list<array{
* name: string,
* zipball_url: string,
* tarball_url: string,
* commit: array{sha: string, url: string},
* }>
*/
$result = json_decode($result, true);
if (JSON_ERROR_NONE !== json_last_error()) {
throw new \RuntimeException(sprintf(
'Failed to read response from "%s" as JSON: %s.',
$url,
$this->url,
json_last_error_msg()
));
}

return $result;
return array_map(
static fn (array $tagData): string => $tagData['name'],
$result
);
}
}
7 changes: 1 addition & 6 deletions src/Console/SelfUpdate/GithubClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,7 @@
interface GithubClientInterface
{
/**
* @return list<array{
* name: string,
* zipball_url: string,
* tarball_url: string,
* commit: array{sha: string, url: string},
* }>
* @return list<string>
*/
public function getTags(): array;
}
4 changes: 1 addition & 3 deletions src/Console/SelfUpdate/NewVersionChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,7 @@ private function retrieveAvailableVersions(): void
return;
}

foreach ($this->githubClient->getTags() as $tag) {
$version = $tag['name'];

foreach ($this->githubClient->getTags() as $version) {
try {
$this->versionParser->normalize($version);

Expand Down
8 changes: 0 additions & 8 deletions tests/AutoReview/ProjectCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,16 @@
use PhpCsFixer\AbstractProxyFixer;
use PhpCsFixer\Console\Command\DocumentationCommand;
use PhpCsFixer\Console\Command\FixCommand;
use PhpCsFixer\Console\SelfUpdate\GithubClient;
use PhpCsFixer\DocBlock\Annotation;
use PhpCsFixer\DocBlock\DocBlock;
use PhpCsFixer\Doctrine\Annotation\DocLexer;
use PhpCsFixer\Documentation\DocumentationLocator;
use PhpCsFixer\Documentation\FixerDocumentGenerator;
use PhpCsFixer\Documentation\RstUtils;
use PhpCsFixer\Documentation\RuleSetDocumentationGenerator;
use PhpCsFixer\ExecutorWithoutErrorHandlerException;
use PhpCsFixer\Fixer\AbstractPhpUnitFixer;
use PhpCsFixer\Fixer\PhpUnit\PhpUnitNamespacedFixer;
use PhpCsFixer\FixerFactory;
use PhpCsFixer\Preg;
use PhpCsFixer\Runner\FileCachingLintingIterator;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
use PhpCsFixer\Tests\Test\AbstractIntegrationTestCase;
use PhpCsFixer\Tests\TestCase;
Expand Down Expand Up @@ -77,13 +73,9 @@ final class ProjectCodeTest extends TestCase
* @var string[]
*/
private static $classesWithoutTests = [
DocLexer::class,
DocumentationCommand::class,
DocumentationLocator::class,
ExecutorWithoutErrorHandlerException::class,
FileCachingLintingIterator::class,
FixerDocumentGenerator::class,
GithubClient::class,
RstUtils::class,
RuleSetDocumentationGenerator::class,
];
Expand Down
44 changes: 44 additions & 0 deletions tests/Console/SelfUpdate/GithubClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?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\SelfUpdate;

use PhpCsFixer\Console\SelfUpdate\GithubClient;
use PhpCsFixer\Tests\TestCase;

/**
* @internal
*
* @covers \PhpCsFixer\Console\SelfUpdate\GithubClient
*/
final class GithubClientTest extends TestCase
{
public function testGettingTags(): void
{
$githubClient = new GithubClient();

\Closure::bind(static function (GithubClient $githubClient): void {
$githubClient->url = __DIR__.'/../../Fixtures/api_github_com_tags.json';
}, null, $githubClient)($githubClient);

self::assertSame(
[
'v3.48.0',
'v3.47.1',
'v3.47.0',
],
$githubClient->getTags()
);
}
}

0 comments on commit e1b66dd

Please sign in to comment.