Completed
Push — master ( 577058...958f25 )
by Hannes
02:13
created

TestCommand::createFormat()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
rs 9.4286
cc 2
eloc 4
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;
11
use SplFileObject;
12
13
/**
14
 * CLI command to run test
15
 */
16
class TestCommand extends Command
17
{
18
    protected function configure()
19
    {
20
        $this->setName('test')
21
            ->setDescription('Test examples in readme files')
22
            ->addArgument(
23
                'filename',
24
                InputArgument::OPTIONAL|InputArgument::IS_ARRAY,
25
                'Name of file to test',
26
                ['README.md']
27
            )
28
            ->addOption(
29
               'markdown',
30
               null,
31
               InputOption::VALUE_NONE,
32
               'Assume markdown format'
33
            )
34
        ;
35
    }
36
37
    protected function execute(InputInterface $input, OutputInterface $output)
38
    {
39
        $tester = new readmetester\ReadmeTester;
40
        $exitStatus = 0;
41
42
        foreach ($input->getArgument('filename') as $filename) {
43
            $file = new SplFileObject($filename);
44
            $output->writeln("Testing examples in <comment>{$file->getRealPath()}</comment>");
45
46
            $format = $this->createFormat($input, $file);
47
            $output->writeln("Using format <comment>{$format->getName()}</comment>");
48
49
            foreach ($tester->test($file, $format) as $line) {
50
                $output->writeln(" <error>$line</error>");
51
                $exitStatus = 1;
52
            }
53
        }
54
55
        return $exitStatus;
56
    }
57
58
    /**
59
     * Create file format object
60
     *
61
     * @param  InputInterface $input
62
     * @param  SplFileObject  $file
63
     * @return Format\FormatInterface
64
     */
65
    private function createFormat(InputInterface $input, SplFileObject $file)
66
    {
67
        if ($input->getOption('markdown')) {
68
            return new readmetester\Format\Markdown;
69
        }
70
        return (new readmetester\Format\Factory)->createFormat($file);
71
    }
72
}
73