Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
nexana committed Jan 7, 2022
2 parents 3c465f6 + dc4b0e4 commit a168e96
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 3 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@ lint-phpcs: ## Check phpcs.

lint-phpcs-fix: ## Check phpcs and try to automatically fix issues.
@bin/php-cs-fixer fix --config=./config/.php-cs --diff --using-cache=no --allow-risky=yes --ansi $(options) $(files)

args?="tests"
test: ## Run tests.
@bin/phpunit $(args)
11 changes: 8 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@
"symfony/filesystem": "^5.3|^4.4",
"symfony/finder": "^5.3|^4.4"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"symfony/test-pack": "^1.0"
},
"autoload": {
"psr-4": {
"Clearfacts\\Bundle\\CodestyleBundle\\": "src/"
"Clearfacts\\Bundle\\CodestyleBundle\\": "src/",
"Clearfacts\\Bundle\\CodestyleBundle\\Tests\\": "tests/"
}
},
"bin": ["bin/cf-codestyle"],
Expand All @@ -21,8 +26,8 @@
"./bin/cf-codestyle clearfacts:codestyle:hooks-setup"
],
"copy-phpcs-config": "bin/cf-codestyle clearfacts:codestyle:copy-cs-config",
"lint-phpcs": "bin/php-cs-fixer fix --config=config/.php-cs --dry-run --diff --using-cache=no --allow-risky=yes --ansi src/",
"lint-phpcs-fix": "bin/php-cs-fixer fix --config=config/.php-cs --diff --using-cache=no --allow-risky=yes --ansi src/"
"lint-phpcs": "bin/php-cs-fixer fix --config=config/.php-cs --dry-run --diff --using-cache=no --allow-risky=yes --ansi src/ tests/",
"lint-phpcs-fix": "bin/php-cs-fixer fix --config=config/.php-cs --diff --using-cache=no --allow-risky=yes --ansi src/ tests/"
},
"config": {
"bin-dir": "bin",
Expand Down
42 changes: 42 additions & 0 deletions tests/Command/CopyCsConfigCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Clearfacts\Bundle\CodestyleBundle\Tests\Command;

use Clearfacts\Bundle\CodestyleBundle\Command\CopyCsConfigCommand;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;

class CopyCsConfigCommandTest extends TestCase
{
public function testExecute()
{
// Given
$phpcsConfigPath = __DIR__ . '/.php-cs';
@unlink($phpcsConfigPath);

$application = new Application();
$application->add(new CopyCsConfigCommand());

$command = $application->find('clearfacts:codestyle:copy-cs-config');
$commandTester = new CommandTester($command);

// When
$commandTester->execute([
'--root' => __DIR__,
'--config-dir' => '.'
]);

// Then
$commandTester->assertCommandIsSuccessful();
$output = $commandTester->getDisplay();
$this->assertStringContainsString('Preparing to copy cs config', $output);
$this->assertStringContainsString('Cs config copied!', $output);
$this->assertTrue(file_exists($phpcsConfigPath));
$this->assertStringContainsString('PhpCsFixer\Config', file_get_contents($phpcsConfigPath));

@unlink($phpcsConfigPath);
}
}
Empty file.
57 changes: 57 additions & 0 deletions tests/Command/SetupGitHooksCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace Clearfacts\Bundle\CodestyleBundle\Tests\Command;

use Clearfacts\Bundle\CodestyleBundle\Command\SetupGitHooksCommand;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;

class SetupGitHooksCommandTest extends TestCase
{
public function testExecute()
{
// Given
$gitHooksPath = __DIR__ . '/.git/hooks/';
$this->removeAllFiles($gitHooksPath);

$application = new Application();
$application->add(new SetupGitHooksCommand());

$command = $application->find('clearfacts:codestyle:hooks-setup');
$commandTester = new CommandTester($command);

// When
$commandTester->execute([
'--root' => __DIR__,
'--custom-hooks-dir' => '/Resources/',
]);

// Then
$commandTester->assertCommandIsSuccessful();
$output = $commandTester->getDisplay();
$this->assertStringContainsString('Preparing git-hooks', $output);
$this->assertStringContainsString('Git hooks copied!', $output);
$this->assertTrue(file_exists($gitHooksPath . 'pre-commit'));
$this->assertTrue(file_exists($gitHooksPath . 'pre-commit-phpcs'));
$this->assertTrue(file_exists($gitHooksPath . 'test-hook'));
$this->assertStringContainsString('test-hook', file_get_contents($gitHooksPath . '/pre-commit'));

$this->removeAllFiles($gitHooksPath);
}

private function removeAllFiles(string $dir)
{
foreach (scandir($dir) as $file) {
if ('.' === $file || '..' === $file) {
continue;
}

unlink($dir . $file);
}
@rmdir(__DIR__ . '/.git/hooks');
@rmdir(__DIR__ . '/.git');
}
}

0 comments on commit a168e96

Please sign in to comment.