TestCommand::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
crap 1
1
<?php
2
3
namespace SyncFS\Command;
4
5
use Symfony\Component\Console\Input\InputInterface;
6
use Symfony\Component\Console\Input\InputOption;
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Symfony\Component\Yaml\Yaml;
9
use SyncFS\Test\Functional\Helper\FileSystemHelper;
10
11
/**
12
 * Class TestCommand
13
 *
14
 * @package SyncFS\Command
15
 * @author  Matej Velikonja <[email protected]>
16
 */
17
class TestCommand extends Command
18
{
19
    const COMMAND_NAME = 'test';
20
21
    /**
22
     * Configure command.
23
     */
24 5
    protected function configure()
25
    {
26 5
        $this
27 5
            ->setName(self::COMMAND_NAME)
28 5
            ->setDescription('Execute simple test sync. It creates temporary test folders.')
29 5
            ->addOption('fast', 'f', InputOption::VALUE_NONE, 'Does not create big files. So it executes a bit faster');
30 5
    }
31
32
    /**
33
     * @param InputInterface  $input
34
     * @param OutputInterface $output
35
     *
36
     * @throws \RuntimeException
37
     *
38
     * @return int|null|void
39
     */
40 1
    protected function execute(InputInterface $input, OutputInterface $output)
41
    {
42 1
        $this->input  = $input;
43 1
        $this->output = $output;
44
45 1
        $fast       = $this->input->getOption('fast');
46 1
        $testDir    = sys_get_temp_dir() . '/' . uniqid('SyncFS_tests');
47 1
        $configPath = $testDir . '/config.yml';
48 1
        $srcDir     = $testDir . '/sync-from-here';
49 1
        $dstDir     = $testDir . '/sync-to-here';
50 1
        $fsHelper   = new FileSystemHelper($srcDir);
51
52 1
        $output->writeln(sprintf('<info>Creating temporary files in dir `%s`.</info>', $testDir));
53 1
        $fsHelper->create($srcDir, ! $fast);
54
55 1
        $this->createConfig(
56 1
            $configPath,
57
            array(
58
                'sync-fs' => array(
59
                    'maps' => array(
60
                        'random-folders-sync' => array(
61 1
                            'src' => $srcDir,
62 1
                            'dst' => $dstDir,
63 1
                        ),
64 1
                    ),
65 1
                    'timeout'        => 30,
66
                    'default_client' => 'rsync'
67 1
                )
68 1
            )
69 1
        );
70
71 1
        $this->runCommand(
72 1
            ShowCommand::COMMAND_NAME,
73
            array(
74 1
                self::ARG_CONFIG_PATH => $configPath,
75
            )
76 1
        );
77
78 1
        $this->runCommand(
79 1
            SyncCommand::COMMAND_NAME,
80
            array(
81 1
                self::ARG_CONFIG_PATH => $configPath,
82
            )
83 1
        );
84
85 1
        $output->writeln(sprintf('<info>Cleaning up...</info>'));
86 1
        $fsHelper->cleanUp($testDir);
87 1
    }
88
89
    /**
90
     * @param string $configPath
91
     * @param array  $data
92
     *
93
     * @return int
94
     */
95 1
    private function createConfig($configPath, array $data)
96
    {
97 1
        $content = Yaml::dump($data);
98
99 1
        return file_put_contents($configPath, $content);
100
    }
101
102
103
    /**
104
     * @param string $name
105
     * @param array  $args
106
     *
107
     * @return int
108
     * @throws \LogicException
109
     */
110 1
    protected function runCommand($name, array $args)
111
    {
112 1
        $this->writeSeparator('command: ' . $name);
113
114 1
        parent::runCommand($name, $args);
115
116 1
        $this->writeSeparator('end ' . $name);
117 1
    }
118
119
    /**
120
     * @param string $title
121
     */
122 1
    private function writeSeparator($title)
123
    {
124 1
        $output = strtoupper("*************** - $title - ***************");
125 1
        $this->output->writeln("<comment>$output</comment>");
126 1
        $this->output->writeln('');
127 1
    }
128
}
129