Skip to content

Commit

Permalink
test: add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
vuongxuongminh committed Mar 27, 2024
1 parent ef6c45b commit ef8bdd7
Show file tree
Hide file tree
Showing 11 changed files with 372 additions and 5 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/unit_tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Unit Tests

on:
push:
branches:
- main
pull_request: ~

jobs:
unit_tests:
strategy:
matrix:
php:
- '8.2'
- '8.3'

runs-on: ubuntu-latest

name: "PHP ${{ matrix.php }} tests"

steps:
- uses: actions/checkout@v4

- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
coverage: pcov

- uses: "ramsey/composer-install@v2"

- run: ./vendor/bin/phpunit

- uses: codecov/codecov-action@v2
with:
name: "phpunit-php${{ matrix.php }}"
flags: phpunit
fail_ci_if_error: true
continue-on-error: true
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
coverage/
vendor/
.idea/
tests/generated/
composer.lock
clover.xml
.phpunit.result.cache
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"type": "library",
"require-dev": {
"phpunit/phpunit": "^11.0",
"symfony/filesystem": "^6.4 || ^7.0",
"symplify/easy-coding-standard": "^12.1"
},
"bin": [
Expand Down
10 changes: 5 additions & 5 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private function parseSource(): DocumentNode

if (!is_dir($dir) || !is_readable($dir)) {
throw new RuntimeException(
sprintf('Directory `%s` does not exists or not have permission to read files', $dir)
sprintf('Directory `%s` does not exists or not have read permission', $dir)
);
}

Expand All @@ -79,7 +79,7 @@ private function parseSource(): DocumentNode
);

foreach ($dirIterator as $matches) {
$src = file_get_contents($matches[0]) . PHP_EOL;
$src .= file_get_contents($matches[0]) . PHP_EOL;
}

if ('' === $src) {
Expand All @@ -103,7 +103,7 @@ private function generateOperationTrait(OperationDefinitionNode $operation, arra
$hasVariables = 0 < $operation->variableDefinitions->count();
$file = $this->generatePhpFile();

$namespaceName = sprintf('%s\\%s', $this->namespace, $this->queryClassName);
$namespaceName = ltrim(sprintf('%s\\%s', $this->namespace, $this->queryClassName), '\\');
$namespace = $file->addNamespace($namespaceName);

$namespace->addUse(Promise::class);
Expand Down Expand Up @@ -234,9 +234,9 @@ private function writePhpFile(PhpFile $file): void
$destPath = rtrim($this->destinationPath, DIRECTORY_SEPARATOR);

if (!is_dir($destPath) || !is_writable($destPath)) {
throw new \RuntimeException(
throw new RuntimeException(
sprintf(
'Please make sure destination path: `%s` exists and have write permission before generate code',
'Please make sure destination path: `%s` exists and have write permission',
$destPath
)
);
Expand Down
43 changes: 43 additions & 0 deletions tests/Console/CodegenCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace XGraphQL\Codegen\Test\Console;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Tester\CommandTester;
use XGraphQL\Codegen\Console\CodegenCommand;
use XGraphQL\Codegen\Generator;

class CodegenCommandTest extends TestCase
{
public function testRunCommand()
{
$command = new CodegenCommand();
$command->setGenerators(
[
'test' => new Generator(
'',
__DIR__ . '/../fixtures/source_dir',
__DIR__ . '/../generated',
)
]
);
$tester = new CommandTester($command);
$tester->execute([]);

$tester->assertCommandIsSuccessful();
$this->assertMatchesRegularExpression('~Generated PHP code for `test` successful~', $tester->getDisplay());
}

public function testRunCommandWithEmptyGenerators()
{
$command = new CodegenCommand();
$command->setGenerators([]);
$tester = new CommandTester($command);
$tester->execute([]);

$tester->assertCommandIsSuccessful();
$this->assertEmpty($tester->getDisplay());
}
}
55 changes: 55 additions & 0 deletions tests/Console/InitConfigCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace XGraphQL\Codegen\Test\Console;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Tester\CommandTester;
use XGraphQL\Codegen\Console\InitConfigCommand;

class InitConfigCommandTest extends TestCase
{
private const CONFIG_FILE = __DIR__ . '/../generated/config.php';

protected function setUp(): void
{
parent::setUp();

@mkdir(__DIR__ . '../generated');
@unlink(self::CONFIG_FILE);
}

public function testInitConfig(): void
{
$this->assertFileDoesNotExist(self::CONFIG_FILE);

$command = new InitConfigCommand();
$command->setConfigFile(self::CONFIG_FILE);
$tester = new CommandTester($command);
$tester->execute([]);

$tester->assertCommandIsSuccessful();
$this->assertFileExists(self::CONFIG_FILE);
$this->assertFileEquals(__DIR__ . '/../../resources/config.template.php', self::CONFIG_FILE);
}

public function testInitConfigExisted(): void
{
touch(self::CONFIG_FILE);
$this->assertFileExists(self::CONFIG_FILE);

$command = new InitConfigCommand();
$command->setConfigFile(self::CONFIG_FILE);
$tester = new CommandTester($command);
$tester->execute([]);

$tester->assertCommandIsSuccessful();
$this->assertEmpty(file_get_contents(self::CONFIG_FILE));

$tester->setInputs(['yes']);
$tester->execute([]);

$this->assertFileEquals(__DIR__ . '/../../resources/config.template.php', self::CONFIG_FILE);
}
}
71 changes: 71 additions & 0 deletions tests/GeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare(strict_types=1);

namespace XGraphQL\Codegen\Test;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Filesystem\Filesystem;
use XGraphQL\Codegen\Exception\RuntimeException;
use XGraphQL\Codegen\Generator;

class GeneratorTest extends TestCase
{
private const GENERATED_PATH = __DIR__ . '/generated';

protected function setUp(): void
{
$fileSystem = new Filesystem();

$fileSystem->remove(self::GENERATED_PATH);
$fileSystem->mkdir(self::GENERATED_PATH);

parent::setUp();
}

public function testUseInvalidSourcePathWillThrowException(): void
{
$generator = new Generator('', '', '');

$this->expectException(RuntimeException::class);
$this->expectExceptionMessageMatches('~does not exists or not have read permission$~');

$generator->generate();
}

public function testEmptyQueryWillThrowException(): void
{
$generator = new Generator('', __DIR__ . '/fixtures/empty_source_dir', '');

$this->expectException(RuntimeException::class);
$this->expectExceptionMessageMatches('~^Not found any query~');

$generator->generate();
}

public function testInvalidDestinationPathWillThrowException(): void
{
$generator = new Generator('', __DIR__ . '/fixtures/source_dir', '');

$this->expectException(RuntimeException::class);
$this->expectExceptionMessageMatches('~exists and have write permission$~');

$generator->generate();
}

public function testCanGenerate(): void
{
$queryName = uniqid('Q');
$generator = new Generator(
'',
__DIR__ . '/fixtures/source_dir',
self::GENERATED_PATH,
$queryName,
);
$generator->generate();

$this->assertFileExists(sprintf('%s/%s.php', self::GENERATED_PATH, $queryName));
$this->assertFileExists(sprintf('%s/%s/GetUsersTrait.php', self::GENERATED_PATH, $queryName));
$this->assertFileExists(sprintf('%s/%s/GetCountryTrait.php', self::GENERATED_PATH, $queryName));
}
}
Loading

0 comments on commit ef8bdd7

Please sign in to comment.