-
Notifications
You must be signed in to change notification settings - Fork 0
Unit tests added #1
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,6 @@ | ||
| .idea/ | ||
| composer.lock | ||
| .phpcs.cache | ||
| .phpunit.cache | ||
| composer.lock | ||
| tests/coverage | ||
| vendor/ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| # Composer i18n Scripts | ||
|
|
||
| Simplify the internationalization of your WordPress plugin or theme using WP-CLI — powered by Composer. | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| parameters: | ||
| level: 8 | ||
| level: 10 | ||
| paths: | ||
| - src/ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,25 @@ | ||
| <!-- phpunit.xml.dist --> | ||
| <phpunit bootstrap="vendor/autoload.php" | ||
| colors="true" | ||
| stopOnFailure="false"> | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd" | ||
| bootstrap="vendor/autoload.php" | ||
| cacheDirectory=".phpunit.cache" | ||
| executionOrder="depends,defects" | ||
| requireCoverageMetadata="true" | ||
| beStrictAboutCoverageMetadata="true" | ||
| beStrictAboutOutputDuringTests="true" | ||
| displayDetailsOnPhpunitDeprecations="true" | ||
| failOnPhpunitDeprecation="true" | ||
| failOnRisky="true" | ||
| failOnWarning="true"> | ||
| <testsuites> | ||
| <testsuite name="Unit"> | ||
| <testsuite name="default"> | ||
| <directory>tests</directory> | ||
| </testsuite> | ||
| </testsuites> | ||
| </phpunit> | ||
|
|
||
| <source ignoreIndirectDeprecations="true" restrictNotices="true" restrictWarnings="true"> | ||
| <include> | ||
| <directory>src</directory> | ||
| </include> | ||
| </source> | ||
| </phpunit> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -69,7 +69,10 @@ protected function execute( InputInterface $input, OutputInterface $output ): in | |
|
|
||
| $output->writeln( '<info>Running: ' . $command . '</info>' ); | ||
|
|
||
| $this->runner->exec( $command, $output_lines, $exit_code ); | ||
| $output_lines = []; | ||
| $exit_code = 0; | ||
|
Comment on lines
+72
to
+73
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Initializing Consider adding a check after the |
||
|
|
||
| $this->runner->exec( $command, $output_lines, $exit_code ); | ||
|
|
||
| foreach ( $output_lines as $line ) { | ||
| $output->writeln( $line ); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,11 +18,11 @@ class ShellRunner { | |
| * Executes a shell command. | ||
| * | ||
| * @param string $command The command to execute. | ||
| * @param ?string[] $output The output in an array. | ||
| * @param ?int $result_code The result code of the command execution. | ||
| * @param string[] $output The output in an array. | ||
| * @param int $result_code The result code of the command execution. | ||
|
Comment on lines
+21
to
+22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changing the type hints from nullable to non-nullable is a good step towards stricter type checking. However, the Consider adding error handling to check if |
||
| * @return void | ||
| */ | ||
| public function exec( string $command, ?array &$output, ?int &$result_code ): void { | ||
| public function exec( string $command, array &$output, int &$result_code ): void { | ||
| // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.system_calls_exec | ||
| exec( $command, $output, $result_code ); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| <?php | ||
| /** | ||
| * Test of MakePotCommand | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Commands; | ||
|
|
||
| use lloc\ComposerI18nScripts\Commands\CreatePoCommand; | ||
|
|
||
| use lloc\ComposerI18nScripts\ShellRunner; | ||
| use PHPUnit\Framework\Attributes\CoversClass; | ||
| use PHPUnit\Framework\TestCase; | ||
| use Symfony\Component\Console\Application; | ||
|
|
||
| #[CoversClass(CreatePoCommand::class)] | ||
| class CreatePoCommandTest extends TestCase { | ||
|
|
||
| protected Application $application; | ||
|
|
||
| protected function setUp(): void { | ||
| $runner = $this->createMock(ShellRunner::class); | ||
| $runner->method('exec') | ||
| ->willReturnCallback(function ($cmd, &$output, &$exitCode) { | ||
| $output = ['Mocked .po creation']; | ||
| $exitCode = 0; | ||
| }); | ||
|
|
||
| $command = new CreatePoCommand($runner); | ||
|
|
||
| $this->application = new Application(); | ||
| $this->application->setAutoExit(false); // Important for tests | ||
| $this->application->add($command); | ||
| } | ||
|
|
||
| public function testCommandIsRegisteredAndConfigured(): void { | ||
| $command = $this->application->find( 'i18n:create-po' ); | ||
|
|
||
| $this->assertSame( 'i18n:create-po', $command->getName() ); | ||
| $this->assertNotEmpty( $command->getDescription() ); | ||
| $this->assertNotEmpty( $command->getHelp() ); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| <?php | ||
| /** | ||
| * Test of MakeJsonCommand | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace lloc\ComposerI18nScriptsTests\Commands; | ||
|
|
||
| use lloc\ComposerI18nScripts\Commands\MakeJsonCommand; | ||
| use lloc\ComposerI18nScripts\ShellRunner; | ||
| use PHPUnit\Framework\TestCase; | ||
| use PHPUnit\Framework\Attributes\CoversClass; | ||
| use Symfony\Component\Console\Application; | ||
|
|
||
| #[CoversClass(MakeJsonCommand::class)] | ||
| class MakeJsonCommandTest extends TestCase { | ||
|
|
||
| protected Application $application; | ||
|
|
||
| protected function setUp(): void { | ||
| $runner = $this->createMock(ShellRunner::class); | ||
| $runner->method('exec') | ||
| ->willReturnCallback(function ($cmd, &$output, &$exitCode) { | ||
| $output = ['Mocked .json generation']; | ||
| $exitCode = 0; | ||
| }); | ||
|
|
||
| $command = new MakeJsonCommand($runner); | ||
|
|
||
| $this->application = new Application(); | ||
| $this->application->setAutoExit(false); // Important for tests | ||
| $this->application->add($command); | ||
| } | ||
|
|
||
|
|
||
| public function testCommandIsRegisteredAndConfigured(): void { | ||
| $command = $this->application->find( 'i18n:make-json' ); | ||
|
|
||
| $this->assertSame( 'i18n:make-json', $command->getName() ); | ||
| $this->assertNotEmpty( $command->getDescription() ); | ||
| $this->assertNotEmpty( $command->getHelp() ); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| <?php | ||
| /** | ||
| * Test of MakeMoCommand | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Commands; | ||
|
|
||
| use lloc\ComposerI18nScripts\Commands\MakeMoCommand; | ||
| use lloc\ComposerI18nScripts\ShellRunner; | ||
| use PHPUnit\Framework\Attributes\CoversClass; | ||
| use PHPUnit\Framework\TestCase; | ||
| use Symfony\Component\Console\Application; | ||
|
|
||
| #[CoversClass(MakeMoCommand::class)] | ||
| class MakeMoCommandTest extends TestCase { | ||
|
|
||
| protected Application $application; | ||
|
|
||
| protected function setUp(): void { | ||
| $runner = $this->createMock(ShellRunner::class); | ||
| $runner->method('exec') | ||
| ->willReturnCallback(function ($cmd, &$output, &$exitCode) { | ||
| $output = ['Mocked .mo generation']; | ||
| $exitCode = 0; | ||
| }); | ||
|
|
||
| $command = new MakeMoCommand($runner); | ||
|
|
||
| $this->application = new Application(); | ||
| $this->application->setAutoExit(false); // Important for tests | ||
| $this->application->add($command); | ||
| } | ||
|
|
||
| public function testCommandIsRegisteredAndConfigured(): void { | ||
| $command = $this->application->find( 'i18n:make-mo' ); | ||
|
|
||
| $this->assertSame( 'i18n:make-mo', $command->getName() ); | ||
| $this->assertNotEmpty( $command->getDescription() ); | ||
| $this->assertNotEmpty( $command->getHelp() ); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| <?php | ||
| /** | ||
| * Test of MakePotCommand | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Commands; | ||
|
|
||
| use lloc\ComposerI18nScripts\Commands\MakePhpCommand; | ||
|
|
||
| use lloc\ComposerI18nScripts\ShellRunner; | ||
| use PHPUnit\Framework\Attributes\CoversClass; | ||
| use PHPUnit\Framework\TestCase; | ||
| use Symfony\Component\Console\Application; | ||
|
|
||
| #[CoversClass(MakePhpCommand::class)] | ||
| class MakePhpCommandTest extends TestCase { | ||
|
|
||
| protected Application $application; | ||
|
|
||
| protected function setUp(): void { | ||
| $runner = $this->createMock(ShellRunner::class); | ||
| $runner->method('exec') | ||
| ->willReturnCallback(function ($cmd, &$output, &$exitCode) { | ||
| $output = ['Mocked .php generation']; | ||
| $exitCode = 0; | ||
| }); | ||
|
|
||
| $command = new MakePhpCommand($runner); | ||
|
|
||
| $this->application = new Application(); | ||
| $this->application->setAutoExit(false); // Important for tests | ||
| $this->application->add($command); | ||
| } | ||
|
|
||
| public function testCommandIsRegisteredAndConfigured(): void { | ||
| $command = $this->application->find( 'i18n:make-php' ); | ||
|
|
||
| $this->assertSame( 'i18n:make-php', $command->getName() ); | ||
| $this->assertNotEmpty( $command->getDescription() ); | ||
| $this->assertNotEmpty( $command->getHelp() ); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,35 +10,35 @@ | |
| use lloc\ComposerI18nScripts\Commands\MakePotCommand; | ||
|
|
||
| use lloc\ComposerI18nScripts\ShellRunner; | ||
| use PHPUnit\Framework\Attributes\CoversClass; | ||
| use PHPUnit\Framework\TestCase; | ||
| use Symfony\Component\Console\Application; | ||
| use Symfony\Component\Console\Tester\CommandTester; | ||
|
|
||
| #[CoversClass(MakePotCommand::class)] | ||
| class MakePotCommandTest extends TestCase { | ||
|
|
||
| public function testCommandIsRegisteredAndConfigured(): void { | ||
| $runner = new ShellRunner(); | ||
| $application = new Application(); | ||
| $application->add( new MakePotCommand($runner) ); | ||
| protected Application $application; | ||
|
|
||
| $command = $application->find( 'i18n:make-pot' ); | ||
| $this->assertSame( 'i18n:make-pot', $command->getName() ); | ||
| $this->assertNotEmpty( $command->getDescription() ); | ||
| $this->assertNotEmpty( $command->getHelp() ); | ||
| } | ||
| protected function setUp(): void { | ||
| $runner = $this->createMock(ShellRunner::class); | ||
| $runner->method('exec') | ||
| ->willReturnCallback(function ($cmd, &$output, &$exitCode) { | ||
| $output = ['Mocked .pot generation']; | ||
| $exitCode = 0; | ||
| }); | ||
|
|
||
| public function testCommandExecutionReturnsSuccess(): void { | ||
| $runner = new ShellRunner(); | ||
| $application = new Application(); | ||
| $application->add( new MakePotCommand($runner) ); | ||
| $command = new MakePotCommand($runner); | ||
|
|
||
| $command = $application->find( 'i18n:make-pot' ); | ||
| $tester = new CommandTester( $command ); | ||
| $this->application = new Application(); | ||
| $this->application->setAutoExit(false); // Important for tests | ||
| $this->application->add($command); | ||
|
Comment on lines
+30
to
+34
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Consider removing the reassignment of the $runner = $this->createMock(ShellRunner::class);
$runner->method('exec')
->willReturnCallback(function ($cmd, &$output, &$exitCode) {
$output = ['Mocked .pot generation'];
$exitCode = 0;
});
$this->application = new Application();
$this->application->setAutoExit(false); // Important for tests
$this->application->add(new MakePotCommand($runner)); |
||
| } | ||
|
|
||
| // You may need to mock or override shell exec inside the command to make this testable | ||
| $tester->execute( array() ); | ||
| public function testCommandIsRegisteredAndConfigured(): void { | ||
| $command = $this->application->find( 'i18n:make-pot' ); | ||
|
|
||
| // Just test if it runs (for now) | ||
| $this->assertSame( 0, $tester->getStatusCode() ); | ||
| $this->assertSame( 'i18n:make-pot', $command->getName() ); | ||
| $this->assertNotEmpty( $command->getDescription() ); | ||
| $this->assertNotEmpty( $command->getHelp() ); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| <?php | ||
| /** | ||
| * Test of MakePotCommand | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Commands; | ||
|
|
||
| use lloc\ComposerI18nScripts\Commands\UpdatePoCommand; | ||
| use lloc\ComposerI18nScripts\ShellRunner; | ||
| use PHPUnit\Framework\Attributes\CoversClass; | ||
| use PHPUnit\Framework\TestCase; | ||
| use Symfony\Component\Console\Application; | ||
|
|
||
| #[CoversClass(UpdatePoCommand::class)] | ||
| class UpdatePoCommandTest extends TestCase { | ||
|
|
||
| protected Application $application; | ||
|
|
||
| protected function setUp(): void { | ||
| $runner = $this->createMock(ShellRunner::class); | ||
| $runner->method('exec') | ||
| ->willReturnCallback(function ($cmd, &$output, &$exitCode) { | ||
| $output = ['Mocked .po update']; | ||
| $exitCode = 0; | ||
| }); | ||
|
|
||
| $command = new UpdatePoCommand($runner); | ||
|
|
||
| $this->application = new Application(); | ||
| $this->application->setAutoExit(false); // Important for tests | ||
| $this->application->add($command); | ||
| } | ||
|
|
||
| public function testCommandIsRegisteredAndConfigured(): void { | ||
| $command = $this->application->find( 'i18n:update-po' ); | ||
|
|
||
| $this->assertSame( 'i18n:update-po', $command->getName() ); | ||
| $this->assertNotEmpty( $command->getDescription() ); | ||
| $this->assertNotEmpty( $command->getHelp() ); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The condition
empty( $locale ) || ! is_string( $locale )can be simplified by using! is_string( $locale )alone.emptyalready checks fornull,'',false,0,0.0,'0',[], so the additionalemptycheck is redundant and makes the code less readable.Consider removing the
emptycheck.