Skip to content

Commit f17c779

Browse files
Add info command to display config information
1 parent f81fe0d commit f17c779

File tree

4 files changed

+158
-3
lines changed

4 files changed

+158
-3
lines changed

src/Console/Application.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public function getDefaultCommands(): array
6464
new Cmd\Install($resolver),
6565
new Cmd\Uninstall($resolver),
6666
new Cmd\Configuration($resolver),
67+
new Cmd\Info($resolver),
6768
new Cmd\Add($resolver),
6869
new Cmd\Disable($resolver),
6970
new Cmd\Enable($resolver),

src/Console/Command/Info.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CaptainHook
5+
*
6+
* (c) Sebastian Feldmann <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace CaptainHook\App\Console\Command;
13+
14+
use CaptainHook\App\Console\IOUtil;
15+
use CaptainHook\App\Runner\Config\Reader;
16+
use Symfony\Component\Console\Input\InputArgument;
17+
use Symfony\Component\Console\Input\InputInterface;
18+
use Symfony\Component\Console\Input\InputOption;
19+
use Symfony\Component\Console\Output\OutputInterface;
20+
21+
/**
22+
* Command to display configuration information
23+
*
24+
* @package CaptainHook
25+
* @author Sebastian Feldmann <[email protected]>
26+
* @link https://github.com/captainhookphp/captainhook
27+
* @since Class available since Release 5.24.0
28+
*/
29+
class Info extends RepositoryAware
30+
{
31+
/**
32+
* Configure the command
33+
*
34+
* @return void
35+
*/
36+
protected function configure(): void
37+
{
38+
parent::configure();
39+
$this->setName('config:info')
40+
->setAliases(['info'])
41+
->setDescription('Displays information about the configuration')
42+
->setHelp('Displays information about the configuration')
43+
->addArgument('hook', InputArgument::OPTIONAL, 'Hook you want to investigate')
44+
->addOption(
45+
'list-actions',
46+
'a',
47+
InputOption::VALUE_NONE,
48+
'List all actions'
49+
)
50+
->addOption(
51+
'list-conditions',
52+
'p',
53+
InputOption::VALUE_NONE,
54+
'List all conditions'
55+
)
56+
->addOption(
57+
'list-options',
58+
'o',
59+
InputOption::VALUE_NONE,
60+
'List all options'
61+
)
62+
->addOption(
63+
'list-config',
64+
's',
65+
InputOption::VALUE_NONE,
66+
'List all config settings'
67+
)
68+
->addOption(
69+
'extensive',
70+
'e',
71+
InputOption::VALUE_NONE,
72+
'Show more detailed information'
73+
);
74+
}
75+
76+
/**
77+
* Execute the command
78+
*
79+
* @param \Symfony\Component\Console\Input\InputInterface $input
80+
* @param \Symfony\Component\Console\Output\OutputInterface $output
81+
* @return int
82+
* @throws \CaptainHook\App\Exception\InvalidHookName
83+
* @throws \Exception
84+
*/
85+
protected function execute(InputInterface $input, OutputInterface $output): int
86+
{
87+
$io = $this->getIO($input, $output);
88+
$config = $this->createConfig($input, true, ['git-directory']);
89+
$repo = $this->createRepository(dirname($config->getGitDirectory()));
90+
91+
$editor = new Reader($io, $config, $repo);
92+
$editor->setHook(IOUtil::argToString($input->getArgument('hook')))
93+
->display(Reader::OPT_ACTIONS, $input->getOption('list-actions'))
94+
->display(Reader::OPT_CONDITIONS, $input->getOption('list-conditions'))
95+
->display(Reader::OPT_OPTIONS, $input->getOption('list-options'))
96+
->extensive($input->getOption('extensive'))
97+
->run();
98+
99+
return 0;
100+
}
101+
}

tests/unit/Console/ApplicationTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,9 @@ public function testExecuteList(): void
4949
->disableOriginalConstructor()
5050
->getMock();
5151

52-
$output->expects($this->atLeastOnce())->method('write');
53-
5452
$app = new Application('captainhook');
5553
$app->setAutoExit(false);
56-
$app->run($input, $output);
54+
55+
$this->assertEquals(0, $app->run($input, $output));
5756
}
5857
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CaptainHook
5+
*
6+
* (c) Sebastian Feldmann <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace CaptainHook\App\Console\Command;
13+
14+
use CaptainHook\App\Console\Runtime\Resolver;
15+
use CaptainHook\App\Git\DummyRepo;
16+
use CaptainHook\App\Runner\Config\Reader;
17+
use Exception;
18+
use org\bovigo\vfs\vfsStream;
19+
use PHPUnit\Framework\TestCase;
20+
use Symfony\Component\Console\Input\ArrayInput;
21+
use Symfony\Component\Console\Output\NullOutput;
22+
23+
class InfoTest extends TestCase
24+
{
25+
public function testFailsWithoutConfig(): void
26+
{
27+
$this->expectException(Exception::class);
28+
29+
$resolver = new Resolver();
30+
$output = new NullOutput();
31+
$input = new ArrayInput([
32+
'hook' => 'pre-commit',
33+
'--configuration' => 'foo',
34+
]);
35+
36+
$install = new Info($resolver);
37+
$install->run($input, $output);
38+
}
39+
40+
public function testDisplaySingleHook(): void
41+
{
42+
$dummyRepo = new DummyRepo();
43+
$resolver = new Resolver();
44+
$output = new NullOutput();
45+
$input = new ArrayInput([
46+
'hook' => 'pre-commit',
47+
'-c' => CH_PATH_FILES . '/config/valid.json',
48+
'-g' => $dummyRepo->getRoot() . '/.git',
49+
]);
50+
51+
$add = new Info($resolver);
52+
$this->assertEquals(0, $add->run($input, $output));
53+
}
54+
}

0 commit comments

Comments
 (0)