Completed
Push — master ( a9d748...c3d5b2 )
by Martin
04:21
created

YamlConvertCommandTest::removeFiles()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 3
eloc 5
nc 3
nop 0
1
<?php
2
3
/*
4
 * This is part of the webuni/composer-yaml-plugin package.
5
 *
6
 * (c) Martin Hasoň <[email protected]>
7
 * (c) Webuni s.r.o. <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Webuni\ComposerYamlPlugin\Tests;
14
15
use Symfony\Component\Console\Application;
16
use Symfony\Component\Console\Input\ArrayInput;
17
use Symfony\Component\Console\Output\BufferedOutput;
18
use Webuni\ComposerYamlPlugin\YamlConvertCommand;
19
20
final class YamlConvertCommandTest extends \PHPUnit_Framework_TestCase
21
{
22
    private $app;
23
    private $buffer;
24
    private $cwd;
25
26
    protected function setUp()
27
    {
28
        $this->app = new Application();
29
        $this->app->setAutoExit(false);
30
        $this->app->add(new YamlConvertCommand());
31
        $this->buffer = new BufferedOutput();
32
33
        $this->cwd = getcwd();
34
        chdir(sys_get_temp_dir());
35
        $this->removeFiles();
36
    }
37
38
    protected function tearDown()
39
    {
40
        chdir($this->cwd);
41
        $this->removeFiles();
42
    }
43
44
    public function testDefaultInputFileDoesNotExists()
45
    {
46
        $this->app->run(new ArrayInput(['yaml-convert']), $this->buffer);
47
        $this->assertContains('The input file "composer.yml" does not exist.', $this->buffer->fetch());
48
    }
49
50
    public function testCustomInputFileDoesNotExists()
51
    {
52
        $this->app->run(new ArrayInput(['yaml-convert', 'input' => 'composer.json']), $this->buffer);
53
        $this->assertContains('The input file "composer.json" does not exist.', $this->buffer->fetch());
54
    }
55
56
    public function testInvalidFormat()
57
    {
58
        $this->app->run(new ArrayInput(['yaml-convert', 'input' => 'composer.json']), $this->buffer);
59
        $this->assertContains('The input file "composer.json" does not exist.', $this->buffer->fetch());
60
    }
61
62
    public function testSameFormat()
63
    {
64
        file_put_contents('composer.yml', 'name: package');
65
        $this->app->run(new ArrayInput(['yaml-convert', 'output' => 'composer.yml']), $this->buffer);
66
        $this->assertContains('Input format is same as output format.', $this->buffer->fetch());
67
    }
68
69
    public function testConvertYamlToJson()
70
    {
71
        file_put_contents('composer.yml', 'name: package');
72
        $this->app->run(new ArrayInput(['yaml-convert']), $this->buffer);
73
        $this->assertContains('Converted "composer.yml" to "composer.json"', $this->buffer->fetch());
74
        $this->assertFileExists('composer.json');
75
        $this->assertEquals("{\n    \"name\": \"package\"\n}", file_get_contents('composer.json'));
76
    }
77
78
    public function testConvertJsonToYaml()
79
    {
80
        file_put_contents('composer.json', "{\n    \"name\": \"package\"\n}");
81
        $this->app->run(new ArrayInput(['yaml-convert', 'input' => 'composer.json', 'output' => 'composer.yml']), $this->buffer);
82
        $this->assertContains('Converted "composer.json" to "composer.yml"', $this->buffer->fetch());
83
        $this->assertFileExists('composer.yml');
84
        $this->assertEquals("name: package\n", file_get_contents('composer.yml'));
85
    }
86
87
    private function removeFiles()
88
    {
89
        foreach (['json', 'yml'] as $extension) {
90
            $file = sys_get_temp_dir().'/composer.'.$extension;
91
            if (is_file($file)) {
92
                unlink($file);
93
            }
94
        }
95
    }
96
}
97