Completed
Push — master ( 0aa69f...1f2f47 )
by Hannes
04:03
created

TestCommand::createFormat()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 2
1
<?php
2
3
namespace hanneskod\readmetester\Command;
4
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputOption;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use hanneskod\readmetester\ReadmeTester;
11
12
/**
13
 * CLI command to run test
14
 */
15
class TestCommand extends Command
16
{
17
    protected function configure()
18
    {
19
        $this->setName('test')
20
            ->setDescription('Test examples in readme files')
21
            ->addArgument(
22
                'filename',
23
                InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
24
                'Name of file to test',
25
                ['README.md']
26
            )
27
        ;
28
    }
29
30
    protected function execute(InputInterface $input, OutputInterface $output)
31
    {
32
        $tester = new ReadmeTester;
33
        $exitStatus = 0;
34
35
        foreach ($input->getArgument('filename') as $filename) {
36
            if (!is_file($filename) || !is_readable($filename)) {
37
                throw new \Exception("Not able to read $filename");
38
            }
39
40
            $output->writeln("Testing examples in <comment>$filename</comment>");
41
42
            foreach ($tester->test(file_get_contents($filename)) as $example => $returnObj) {
43
                if ($returnObj->isSuccess()) {
44
                    $output->writeln(" <info>Example $example: {$returnObj->getMessage()}</info>");
45
                    continue;
46
                }
47
                $output->writeln(" <error>Example $example: {$returnObj->getMessage()}</error>");
48
                $exitStatus = 1;
49
            }
50
        }
51
52
        return $exitStatus;
53
    }
54
}
55