YamlConvertCommandTest::testInvalidFormat()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
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 CompatibleTestsCase
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.yaml" 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.neon']), $this->buffer);
59
        $this->assertContains('Invalid input format "neon", must be one of: yaml, yml, json.', $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.yaml']), $this->buffer);
66
        $this->assertContains('Input format "yaml" is same as output format.', $this->buffer->fetch());
67
    }
68
69
    public function testConvertYamlToJson()
70
    {
71
        file_put_contents('composer.yaml', 'name: package');
72
        $this->app->run(new ArrayInput(['yaml-convert']), $this->buffer);
73
        $this->assertContains('Converted "composer.yaml" to "composer.json"', $this->buffer->fetch());
74
        $this->assertFileExists('composer.json');
75
        $this->assertEquals("{\n    \"name\": \"package\"\n}\n", file_get_contents('composer.json'));
76
    }
77
78
    public function testConvertYmlToJson()
79
    {
80
        file_put_contents('composer.yml', 'name: package');
81
        $this->app->run(new ArrayInput(['yaml-convert']), $this->buffer);
82
        $this->assertContains('Converted "composer.yml" to "composer.json"', $this->buffer->fetch());
83
        $this->assertFileExists('composer.json');
84
        $this->assertEquals("{\n    \"name\": \"package\"\n}\n", file_get_contents('composer.json'));
85
    }
86
87 View Code Duplication
    public function testConvertJsonToYaml()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
88
    {
89
        file_put_contents('composer.json', "{\n    \"name\": \"package\"\n}");
90
        $this->app->run(new ArrayInput(['yaml-convert', 'input' => 'composer.json']), $this->buffer);
91
        $this->assertContains('Converted "composer.json" to "composer.yaml"', $this->buffer->fetch());
92
        $this->assertFileExists('composer.yaml');
93
        $this->assertEquals("name: package\n", file_get_contents('composer.yaml'));
94
    }
95
96 View Code Duplication
    public function testConvertJsonToYml()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
    {
98
        file_put_contents('composer.json', "{\n    \"name\": \"package\"\n}");
99
        $this->app->run(new ArrayInput(['yaml-convert', 'input' => 'composer.json', 'output' => 'composer.yml']), $this->buffer);
100
        $this->assertContains('Converted "composer.json" to "composer.yml"', $this->buffer->fetch());
101
        $this->assertFileExists('composer.yml');
102
        $this->assertEquals("name: package\n", file_get_contents('composer.yml'));
103
    }
104
105
    private function removeFiles()
106
    {
107
        foreach (['json', 'yaml', 'yml'] as $extension) {
108
            $file = sys_get_temp_dir().'/composer.'.$extension;
109
            if (is_file($file)) {
110
                unlink($file);
111
            }
112
        }
113
    }
114
}
115