Completed
Push — master ( 247d13...46aee1 )
by Zac
13:30 queued 12s
created

TestsRunCommand::getSummary()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 3
Bugs 2 Features 0
Metric Value
c 3
b 2
f 0
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 9.4286
cc 2
eloc 8
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Overwatch\TestBundle\Command;
4
5
use Overwatch\ResultBundle\Enum\ResultStatus;
6
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\DependencyInjection\ContainerInterface;
11
12
/**
13
 * TestsRunCommand
14
 * The overwatch:tests:run command, the heart of Overwatch
15
 */
16
class TestsRunCommand extends ContainerAwareCommand
17
{
18
    /**
19
     * @var Overwatch\ExpectationBundle\Helper\ExpectationManger 
20
     */
21
    private $expectations;
22
    
23
    private $_em;
24
    private $results = [];
25
    private $colours = [];
26
    
27 7
    protected function configure()
28
    {
29 7
        $this
30 7
            ->setName('overwatch:tests:run')
31 7
            ->setDescription('Run a set of overwatch tests')
32 7
            ->addOption('test', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Only run tests that are named this value or are in groups named this value')
33 7
            ->addOption('discard-results', null, InputOption::VALUE_NONE, 'Do not save the results to the database')
34
        ;
35
        
36
        //Initalize results array
37 7
        $this->results = [
38 7
            ResultStatus::ERROR          => 0,
39 7
            ResultStatus::FAILED         => 0,
40 7
            ResultStatus::PASSED         => 0,
41 7
            ResultStatus::UNSATISFACTORY => 0
42 7
        ];
43
        
44
        //Initalize colours array
45 7
        $this->colours = [
46 7
            ResultStatus::ERROR          => 'error',
47 7
            ResultStatus::FAILED         => 'error',
48 7
            ResultStatus::PASSED         => 'info',
49 7
            ResultStatus::UNSATISFACTORY => 'comment'
50 7
        ];
51 7
    }
52
53 7
    public function setContainer(ContainerInterface $container = null)
54
    {
55 7
        parent::setContainer($container);
56
        
57
        //Set up some shortcuts to services
58 7
        if ($container !== null) {
59 7
            $this->expectations = $container->get('overwatch_expectation.expectation_manager');
60 7
            $this->_em = $container->get('doctrine.orm.entity_manager');
61 7
        }
62 7
    }
63
64 7
    protected function execute(InputInterface $input, OutputInterface $output)
65
    {
66 7
        $start = new \DateTime;
67
68 7
        $tests = $this->_em->getRepository('OverwatchTestBundle:Test')->findTests($input->getOption('test'));
69 7
        if (empty($tests)) {
70 1
            throw new \InvalidArgumentException('Could not find any tests to run.');
71
        }
72
        
73 6
        $output->writeln($this->getApplication()->getLongVersion() . ', running <info>' . count($tests) . '</info> tests');
74
        
75 6
        foreach ($tests as $test) {
76 6
            $result = $this->expectations->run($test);
77 6
            $this->results[$result->getStatus()]++;
78
            
79
            //Don't output if we're not running verbosely and the test passes.
80 6
            if ($result->getStatus() !== ResultStatus::PASSED || $output->isVerbose()) {
81 4
                $output->writeln(
82 4
                    ' > ' . $test->getName() . ' : ' .
83 4
                    $this->getColouredStatus($result->getStatus()) .
84 4
                    ' - ' . $result->getInfo()
85 4
                );
86 4
            }
87 6
            $this->_em->persist($result);
88 6
        }
89
90 6
        if ($input->getOption('discard-results')) {
91
            $output->writeln(' > <comment>Ran with --discard-results, results NOT saved.</comment>');
92
        } else {
93 6
            $this->_em->flush();
94
        }
95
96 6
        $output->writeln($this->getSummary($start));
97
98
        //Exit with status code equal to the number of tests that didn't pass
99 6
        return (count($tests) - $this->results[ResultStatus::PASSED]);
100
    }
101
    
102 6
    private function getColouredStatus($status, $value = null)
103
    {
104 6
        if ($value === null) {
105 4
            $value = $status;
106 4
        } else {
107 6
            $value .= ' ' . $status;
108
        }
109
        
110 6
        ResultStatus::isValid($status);
111 6
        return '<' . $this->colours[$status] . '>' . $value . '</' . $this->colours[$status] . '>';
112
    }
113
    
114
    /**
115
     * @param \DateTime $start
116
     */
117 6
    private function getSummary(\DateTime $start)
118
    {
119 6
        $end = new \DateTime;
120 6
        $runTime = $end->diff($start, true);
121 6
        $summary = '';
122
        
123 6
        foreach (ResultStatus::getAll() as $status) {
124 6
            $summary .= $this->getColouredStatus($status, $this->results[$status]) . ', ';
125 6
        }
126
        
127 6
        $summary .= 'in ' . $runTime->i . ' minutes and ' . $runTime->s . ' seconds';
128 6
        return $summary;
129
    }
130
}
131