Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test #269

Merged
merged 3 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions src/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,21 @@ final class Application extends BaseApplication
public const APPLICATION_NAME = 'Twig-CS-Fixer';
public const PACKAGE_NAME = 'vincentlanglet/twig-cs-fixer';

public function __construct()
public function __construct(string $name = self::APPLICATION_NAME, string $package = self::PACKAGE_NAME)
{
parent::__construct(self::APPLICATION_NAME, $this->getPackageVersion());
parent::__construct($name, $this->getPackageVersion($package));
}

private function getPackageVersion(): string
private function getPackageVersion(string $package): string
{
foreach (InstalledVersions::getAllRawData() as $installed) {
if (!isset($installed['versions'][self::PACKAGE_NAME])) {
if (!isset($installed['versions'][$package])) {
continue;
}

$version = $installed['versions'][self::PACKAGE_NAME]['pretty_version']
?? $installed['versions'][self::PACKAGE_NAME]['version']
?? 'dev';

$aliases = $installed['versions'][self::PACKAGE_NAME]['aliases'] ?? [];

$reference = InstalledVersions::getReference(self::PACKAGE_NAME);
$version = $installed['versions'][$package]['pretty_version'] ?? 'dev';
$aliases = $installed['versions'][$package]['aliases'] ?? [];
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@llaville I didn't found a single lib in vendor/composer/installed.php which have an alias. I'm thinking about changing

if (null === $reference) {
                return $aliases[0] ?? $version;
            }

to

if (null === $reference) {
                return $version;
            }

What would I lose ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you have only one branch, or if don't specify a composer branch constraint, you won't see the difference.

For example:

Twig-CS-Fixer dev-main@af956d3

But when you've project with multiple branches, you are able to see immediatly the difference.

For example: (do it on your repo local copy)

git checkout -b 3.0
composer update
bin/twig-cs-fixer --version

And you should see something like

Twig-CS-Fixer 3.0.x-dev@af956d3

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried

git checkout -b 3.0
composer update
bin/twig-cs-fixer --version

and i get

Twig-CS-Fixer 3.0.x-dev@af956d3

even if I removed the alias code.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can try it on this branch if you want

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is Composer that do the magic job. Look into vendor/composer/installed.php file,
and you'll get.

        'vincentlanglet/twig-cs-fixer' => array(
            'pretty_version' => '3.0.x-dev',
            'version' => '3.0.9999999.9999999-dev',
            'reference' => 'af956d3e56a8766f66a3e28f9bba25f35a85a943',
            'type' => 'coding-standard',
            'install_path' => __DIR__ . '/../../',
            'aliases' => array(),
            'dev_requirement' => false,
        ),

As pretty version has a highter priority in our implementation, you think alias do nothing, but it's not the case

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without pretty version

            $version = null//$installed['versions'][self::PACKAGE_NAME]['pretty_version']
                ?? $installed['versions'][self::PACKAGE_NAME]['version']
                ?? 'dev';

you'll get Twig-CS-Fixer 3.0.9999999.9999999-dev@af956d3

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anyway, you'll have the last word.
The most important is to provide a version that allow to identify this application.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll try like this, since I cannot test the alias ; I'll see later if it need to be revert.

$reference = $installed['versions'][$package]['reference'] ?? null;
if (null === $reference) {
return $aliases[0] ?? $version;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Util/StringUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Symfony\Component\String\UnicodeString;

class StringUtil
final class StringUtil
{
/**
* @see UnicodeString::camel()
Expand Down
32 changes: 32 additions & 0 deletions tests/Console/ApplicationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace TwigCsFixer\Tests\Console;

use PHPUnit\Framework\TestCase;
use TwigCsFixer\Console\Application;

final class ApplicationTest extends TestCase
{
public function testVersion(): void
{
$app = new Application();
static::assertSame(Application::APPLICATION_NAME, $app->getName());
static::assertMatchesRegularExpression('/^dev-.+@.{7}$/', $app->getVersion());
}

public function testNotInstalledLib(): void
{
$app = new Application('Foo', 'foo');
static::assertSame('Foo', $app->getName());
static::assertSame('UNKNOWN', $app->getVersion());
}

public function testLibWithoutVersion(): void
{
$app = new Application('Psalm', 'psalm/psalm');
static::assertSame('Psalm', $app->getName());
static::assertSame('dev', $app->getVersion());
}
}
2 changes: 1 addition & 1 deletion tests/Rules/Node/ForbiddenBlock/ForbiddenBlockRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use TwigCsFixer\Rules\Node\ForbiddenBlockRule;
use TwigCsFixer\Tests\Rules\AbstractRuleTestCase;

class ForbiddenBlockRuleTest extends AbstractRuleTestCase
final class ForbiddenBlockRuleTest extends AbstractRuleTestCase
{
public function testConfiguration(): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use TwigCsFixer\Rules\Node\ForbiddenFilterRule;
use TwigCsFixer\Tests\Rules\AbstractRuleTestCase;

class ForbiddenFilterRuleTest extends AbstractRuleTestCase
final class ForbiddenFilterRuleTest extends AbstractRuleTestCase
{
public function testConfiguration(): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use TwigCsFixer\Rules\Node\ForbiddenFunctionRule;
use TwigCsFixer\Tests\Rules\AbstractRuleTestCase;

class ForbiddenFunctionRuleTest extends AbstractRuleTestCase
final class ForbiddenFunctionRuleTest extends AbstractRuleTestCase
{
public function testConfiguration(): void
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Rules/String/HashQuote/HashQuoteRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use TwigCsFixer\Rules\String\HashQuoteRule;
use TwigCsFixer\Tests\Rules\AbstractRuleTestCase;

class HashQuoteRuleTest extends AbstractRuleTestCase
final class HashQuoteRuleTest extends AbstractRuleTestCase
{
public function testConfiguration(): void
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Rules/String/SimpleQuote/SingleQuoteRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use TwigCsFixer\Rules\String\SingleQuoteRule;
use TwigCsFixer\Tests\Rules\AbstractRuleTestCase;

class SingleQuoteRuleTest extends AbstractRuleTestCase
final class SingleQuoteRuleTest extends AbstractRuleTestCase
{
public function testConfiguration(): void
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Util/StringUtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use PHPUnit\Framework\TestCase;
use TwigCsFixer\Util\StringUtil;

class StringUtilTest extends TestCase
final class StringUtilTest extends TestCase
{
/**
* @dataProvider toSnakeCaseDataProvider
Expand Down
Loading