Completed
Push — master ( c32bd7...f916e0 )
by Hannes
02:12
created

TestCommand::execute()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 3
Metric Value
c 4
b 0
f 3
dl 0
loc 24
rs 8.6845
cc 4
eloc 15
nc 4
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
               'format',
30
               'f',
31
               InputOption::VALUE_REQUIRED,
32
               'Force input file 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 $example => $returnObj) {
50
                if ($returnObj->isSuccess()) {
51
                    $output->writeln(" <info>Example $example: {$returnObj->getMessage()} </info>");
52
                    continue;
53
                }
54
                $output->writeln(" <error>Example $example: {$returnObj->getMessage()} </error>");
55
                $exitStatus = 1;
56
            }
57
        }
58
59
        return $exitStatus;
60
    }
61
62
    /**
63
     * Create file format object
64
     *
65
     * @param  InputInterface $input
66
     * @param  SplFileObject  $file
67
     * @return Format\FormatInterface
68
     */
69
    private function createFormat(InputInterface $input, SplFileObject $file)
70
    {
71
        $formatIdentifier = $input->getOption('format');
72
73
        if (!$formatIdentifier) {
74
            $formatIdentifier = $file->getExtension();
75
        }
76
77
        $formatFactory = new readmetester\Format\FormatFactory;
78
79
        return $formatFactory->createFormat($formatIdentifier);
80
    }
81
}
82