Skip to content

Commit

Permalink
uses symfony/console for bin script
Browse files Browse the repository at this point in the history
  • Loading branch information
ju1ius committed Dec 27, 2023
1 parent 604176c commit 6ca358c
Show file tree
Hide file tree
Showing 20 changed files with 234 additions and 46 deletions.
8 changes: 8 additions & 0 deletions .idea/chicot.iml

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

12 changes: 12 additions & 0 deletions .idea/codeception.xml

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

13 changes: 13 additions & 0 deletions .idea/php.xml

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

13 changes: 13 additions & 0 deletions .idea/phpspec.xml

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

10 changes: 10 additions & 0 deletions .idea/phpunit.xml

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

10 changes: 7 additions & 3 deletions bin/chicot.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

require $_composer_autoload_path ?? __DIR__ . '/../vendor/autoload.php';

use Souplette\Chicot\Cli;
use Souplette\Chicot\Command\GenerateStubsCommand;
use Souplette\Chicot\Command\ListExtensionsCommand;
use Symfony\Component\Console\Application;

$app = new Cli();
exit($app->run($argv));
$app = new Application('chicot', '0.1.0');
$app->add(new ListExtensionsCommand('list-extensions'));
$app->add(new GenerateStubsCommand('stub'));
$app->run();
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"require": {
"php": ">=8.3",
"composer-runtime-api": "^2.2",
"nikic/php-parser": "^4.18"
"nikic/php-parser": "^4.18",
"symfony/console": "^7.0"
}
}
28 changes: 0 additions & 28 deletions src/Cli.php

This file was deleted.

37 changes: 37 additions & 0 deletions src/Command/GenerateStubsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php declare(strict_types=1);

namespace Souplette\Chicot\Command;

use ReflectionExtension;
use Souplette\Chicot\StubsGenerator;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

final class GenerateStubsCommand extends Command
{
#[\Override]
protected function configure(): void
{
$this
->setDescription('Generates IDE stubs for an extension.')
->addArgument('extension', InputArgument::REQUIRED, 'The name of the extension')
->addArgument('output-file', InputArgument::OPTIONAL, 'The output file path')
;
}

#[\Override]
protected function execute(InputInterface $input, OutputInterface $output): int
{
$ext = new ReflectionExtension($input->getArgument('extension'));
$code = StubsGenerator::generate($ext);
if ($outputPath = $input->getArgument('output-file')) {
$file = new \SplFileObject($outputPath, 'w');
$file->fwrite($code);
} else {
$output->writeln($code);
}
return Command::SUCCESS;
}
}
27 changes: 27 additions & 0 deletions src/Command/ListExtensionsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php declare(strict_types=1);

namespace Souplette\Chicot\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

final class ListExtensionsCommand extends Command
{
#[\Override]
protected function configure(): void
{
$this
->setDescription('Lists installed extensions.')
;
}

#[\Override]
protected function execute(InputInterface $input, OutputInterface $output): int
{
foreach (get_loaded_extensions() as $name) {
$output->writeln($name);
}
return Command::SUCCESS;
}
}
11 changes: 8 additions & 3 deletions src/Internal/AstBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ private function buildNamespace(ReflectionNamespace $namespace): Stmt\Namespace_
default => $name,
});
foreach ($namespace->getConstants() as $name => $value) {
$builder->addStmt(new Stmt\Const_([
new Node\Const_($name, $this->builderFactory->val($value)),
]));
$builder->addStmt($this->buildConstant($name, $value));
}
foreach ($namespace->getFunctions() as $function) {
$builder->addStmt($this->buildFunction($function));
Expand All @@ -77,6 +75,13 @@ private function buildNamespace(ReflectionNamespace $namespace): Stmt\Namespace_
return $builder->getNode();
}

private function buildConstant(string $name, mixed $value): Stmt\Const_
{
return new Stmt\Const_([
new Node\Const_($name, $this->builderFactory->val($value)),
]);
}

private function buildFunction(ReflectionFunction $function): Stmt\Function_
{
$builder = $this->builderFactory->function($function->getShortName());
Expand Down
39 changes: 39 additions & 0 deletions tests/Command/GenerateStubsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php declare(strict_types=1);

namespace Souplette\Chicot\Tests\Command;

use PHPUnit\Framework\Assert;
use PHPUnit\Framework\TestCase;
use Souplette\Chicot\Command\GenerateStubsCommand;
use Souplette\Chicot\Tests\TemporaryFileGuard;
use Symfony\Component\Console\Tester\CommandTester;

final class GenerateStubsTest extends TestCase
{
public function testItPrintStubsToStdout(): void
{
$cmd = new GenerateStubsCommand('stub');
$tester = new CommandTester($cmd);
// json extension is a PHPUnit dependency, so should always be present in this environment.
$tester->execute(['extension' => 'json']);
$tester->assertCommandIsSuccessful();
$output = $tester->getDisplay();
Assert::assertStringContainsString('function json_encode(', $output);
}

public function testItWritesStubsToFile(): void
{
$guard = new TemporaryFileGuard();
$cmd = new GenerateStubsCommand('stub');
$tester = new CommandTester($cmd);
// json extension is a PHPUnit dependency, so should always be present in this environment.
$tester->execute([
'extension' => 'json',
'output-file' => $guard->path,
]);
$tester->assertCommandIsSuccessful();

Assert::assertFileExists($guard->path);
Assert::assertStringContainsString('function json_encode(', $guard->getContents());
}
}
18 changes: 18 additions & 0 deletions tests/Command/ListExtensionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php declare(strict_types=1);

namespace Souplette\Chicot\Tests\Command;

use PHPUnit\Framework\TestCase;
use Souplette\Chicot\Command\ListExtensionsCommand;
use Symfony\Component\Console\Tester\CommandTester;

final class ListExtensionsTest extends TestCase
{
public function testListExtensions(): void
{
$cmd = new ListExtensionsCommand();
$tester = new CommandTester($cmd);
$tester->execute([]);
$tester->assertCommandIsSuccessful();
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<?php declare(strict_types=1);

namespace Souplette\Chicot\Tests;
namespace Souplette\Chicot\Tests\Generator;

use PHPUnit\Framework\Attributes\DataProvider;
use ReflectionClass;
use Souplette\Chicot\Mocks\AllTargetsAttr;
use Souplette\Chicot\Mocks\ClassAndMethodAttr;
use Souplette\Chicot\Tests\GeneratorTestCase;

final class ExtensionAttributesTest extends GeneratorTestCase
final class AttributesTest extends GeneratorTestCase
{
#[DataProvider('attributesProvider')]
public function testAttributes(string $name, string $expected): void
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php declare(strict_types=1);

namespace Souplette\Chicot\Tests;
namespace Souplette\Chicot\Tests\Generator;

use PHPUnit\Framework\Attributes\DataProvider;
use ReflectionClass;
Expand All @@ -9,8 +9,9 @@
use Souplette\Chicot\Mocks\GrandChild;
use Souplette\Chicot\Mocks\ReadOnlyMock;
use Souplette\Chicot\Mocks\Root;
use Souplette\Chicot\Tests\GeneratorTestCase;

final class ExtensionClassTest extends GeneratorTestCase
final class ClassesTest extends GeneratorTestCase
{
#[DataProvider('generateClassProvider')]
public function testGenerateClass(string $name, string $expected): void
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php declare(strict_types=1);

namespace Souplette\Chicot\Tests;
namespace Souplette\Chicot\Tests\Generator;

use PHPUnit\Framework\Attributes\DataProvider;
use Souplette\Chicot\Tests\GeneratorTestCase;

final class ExtensionConstantsTest extends GeneratorTestCase
final class ConstantsTest extends GeneratorTestCase
{
#[DataProvider('generateConstantsProvider')]
public function testGenerateConstants(array $constants, string $expected): void
Expand Down
5 changes: 3 additions & 2 deletions tests/ExtensionEnumTest.php → tests/Generator/EnumsTest.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
<?php declare(strict_types=1);

namespace Souplette\Chicot\Tests;
namespace Souplette\Chicot\Tests\Generator;

use PHPUnit\Framework\Attributes\DataProvider;
use ReflectionEnum;
use Souplette\Chicot\Mocks\TheNumbers;
use Souplette\Chicot\Mocks\TheStrings;
use Souplette\Chicot\Mocks\TheUnits;
use Souplette\Chicot\Tests\GeneratorTestCase;

final class ExtensionEnumTest extends GeneratorTestCase
final class EnumsTest extends GeneratorTestCase
{
#[DataProvider('generateEnumProvider')]
public function testGenerateEnum(string $name, string $expected): void
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?php declare(strict_types=1);

namespace Souplette\Chicot\Tests;
namespace Souplette\Chicot\Tests\Generator;

use PHPUnit\Framework\Attributes\DataProvider;
use ReflectionClass;
use Souplette\Chicot\Mocks\ChildInterface;
use Souplette\Chicot\Mocks\RootInterface;
use Souplette\Chicot\Tests\GeneratorTestCase;

final class ExtensionInterfaceTest extends GeneratorTestCase
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php declare(strict_types=1);

namespace Souplette\Chicot\Tests;
namespace Souplette\Chicot\Tests\Generator;

use PHPUnit\Framework\Attributes\DataProvider;
use ReflectionFunction;
use Souplette\Chicot\Tests\GeneratorTestCase;
use function Souplette\Chicot\Mocks\by_ref_arg;
use function Souplette\Chicot\Mocks\default_arg;
use function Souplette\Chicot\Mocks\dnf_arg;
Expand All @@ -13,7 +14,7 @@
use function Souplette\Chicot\Mocks\variadic_arg;
use function Souplette\Chicot\Mocks\with_doc_comment;

final class ExtensionFunctionsTest extends GeneratorTestCase
final class FunctionsTest extends GeneratorTestCase
{
#[DataProvider('functionsProvider')]
public function testFunctions(callable $fn, string $expected): void
Expand Down
Loading

0 comments on commit 6ca358c

Please sign in to comment.